-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.ps1
166 lines (134 loc) · 6.69 KB
/
update.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
param (
[string]$SettingsFilePath,
[string]$ProxyServer,
[string]$ProxyUser,
[string]$proxyPW,
[string]$WindowsServer,
[string]$WindowsUser,
[string]$WindowsPW,
[string]$GiteaServerURL,
[string]$GiteaInstallPath,
[string]$GiteaWindowsServiceName,
[int]$UseProxy
)
# .\update.ps1 -SettingsFilePath ".\settings.json"
# Load settings from the provided settings file
$settings = Get-Content $SettingsFilePath | ConvertFrom-Json
#$settings = Get-Content .\settings.json | ConvertFrom-Json
# Override settings with provided parameters if they are not null or empty
if ($ProxyServer) { $settings.ProxyServer = $ProxyServer }
if ($ProxyUser) { $settings.ProxyUser = $ProxyUser }
if ($proxyPW) { $settings.ProxyPW = $proxyPW }
if ($WindowsServer) { $settings.WindowsServer = $WindowsServer }
if ($WindowsUser) { $settings.WindowsUser = $WindowsUser }
if ($WindowsPW) { $settings.WindowsPW = $WindowsPW }
if ($GiteaServerURL) { $settings.GiteaServerURL = $GiteaServerURL }
if ($GiteaInstallPath) { $settings.GiteaInstallPath = $GiteaInstallPath }
if ($GiteaWindowsServiceName) { $settings.GiteaWindowsServiceName = $GiteaWindowsServiceName }
if ($UseProxy -eq $null) { $settings.UseProxy = $UseProxy }
# Convert proxy password to secure string
$secStringPasswordProxy = ConvertTo-SecureString $settings.ProxyPW -AsPlainText -Force
$proxycreds = New-Object System.Management.Automation.PSCredential ($settings.proxyUser, $secStringPasswordProxy)
# Convert Windows password to secure string
$secStringPasswordWindows = ConvertTo-SecureString $settings.WindowsPW -AsPlainText -Force
$WindowsCreds = New-Object System.Management.Automation.PSCredential ($settings.WindowsUser, $secStringPasswordWindows)
$headers = @{
"Accept" = "application/vnd.github+json"
}
try {
if ($settings.UseProxy -eq 1) {
$currentrelease = Invoke-RestMethod -Method Get -Uri "https://api.github.com/repos/go-gitea/gitea/releases/latest" -Headers $headers -Proxy $settings.ProxyServer -ProxyCredential $proxycreds
}
else {
$currentrelease = Invoke-RestMethod -Method Get -Uri "https://api.github.com/repos/go-gitea/gitea/releases/latest" -Headers $headers
}
$currentreleaseversion = $currentrelease.tag_name.Substring(1)
$currentversion = (Invoke-RestMethod -Method Get -Uri ($settings.GiteaServerURL + "/api/v1/version")).version
}
catch {
Write-Host "Failed to retrieve current version(s)"
}
if (($currentversion.Replace(".","")) -lt ($currentreleaseversion.Replace(".",""))) {
Write-Host -ForegroundColor red "Currently installed version ("$currentversion") older than available version ("$currentreleaseversion")"
Write-Host -foregroundcolor green "Downloading newer version now"
$downloadurl = ($currentrelease.assets | Where-Object { $_.name -eq "gitea-" + $currentreleaseversion + "-windows-4.0-amd64.exe" }).browser_download_url
$downloadurlchecksum = ($currentrelease.assets | Where-Object { $_.name -eq "gitea-" + $currentreleaseversion + "-windows-4.0-amd64.exe.sha256" }).browser_download_url
try {
New-PSDrive -Root $settings.GiteaInstallPath -Name "Gitfiles" -PSProvider FileSystem -Credential $WindowsCreds
New-Item Gitfiles:\Versionarchive\$currentreleaseversion -ItemType Directory
$ArchiveFile = "GitFiles:\versionarchive\" + $currentreleaseversion + "\gitea-" + $currentreleaseversion + "-gogit-windows-4.0-amd64.exe"
if ($settings.UseProxy -eq 1) {
Invoke-WebRequest -Uri $downloadurl -OutFile $ArchiveFile -Proxy $settings.ProxyServer -ProxyCredential $proxycreds
Invoke-WebRequest -Uri $downloadurlchecksum -OutFile ($ArchiveFile + ".sha256") -Proxy $settings.ProxyServer -ProxyCredential $proxycreds
}
else {
Invoke-WebRequest -Uri $downloadurl -OutFile $ArchiveFile
Invoke-WebRequest -Uri $downloadurlchecksum -OutFile ($ArchiveFile + ".sha256")
}
if ((Get-Content -Path ($ArchiveFile + ".sha256")).substring(0, 64) -ne ((Get-FileHash -Path $ArchiveFile).hash)) {
Write-Host -foregroundcolor red "SHA Checksum mismatch"
}
else {
Write-Host -ForegroundColor Green "SHA Checksum match"
$confirmation = Read-Host red "SHA Checksum mismatch, are you Sure You Want To Proceed: (y/n)"
if ($confirmation -eq "y") {
Write-Host -ForegroundColor Green "continue running script..."
}elseif($confirmation -eq "n"){
exit
}
}
}
catch {
Write-Host -foregroundcolor red "Error downloading new version and storing in versionarchive"
}
$global:CheckServiceStatus = {
param($Service)
Get-Service -Name "$Service"
}
$global:ScriptBlock = {
param($Service)
$servicestatus = Get-Service -Name "$Service"
if ($servicestatus.Status -eq "Running") {
Stop-Service -Name "$Service"
}
if ($servicestatus.Status -ne "Running") {
Start-Service -Name "$Service"
}
}
try {
Invoke-Command -ComputerName $settings.WindowsServer -Credential $WindowsCreds -ScriptBlock $ScriptBlock -ArgumentList $settings.GiteaWindowsServiceName
}
catch {
Write-Host -foregroundcolor red "Error stopping gitea service (1)"
}
try {
$GiteaStatus = (Invoke-Command -ComputerName $settings.WindowsServer -Credential $WindowsCreds -ScriptBlock $CheckServiceStatus -ArgumentList $settings.GiteaWindowsServiceName).Status
do {
$GiteaStatus = (Invoke-Command -ComputerName $settings.WindowsServer -Credential $WindowsCreds -ScriptBlock $CheckServiceStatus -ArgumentList $settings.GiteaWindowsServiceName).Status
Write-Host -ForegroundColor Yellow $GiteaStatus
Start-Sleep -Milliseconds 250
} until ($GiteaStatus -eq "stopped")
}
catch {
Write-Host -foregroundcolor red "Unable to retrieve status of GITEA Service"
}
try {
copy-item $ArchiveFile -destination Gitfiles:\"gitea.exe" -Force
}
catch {
Write-Host -foregroundcolor red "Error deleting and replacing gitea.exe file"
}
try {
Invoke-Command -ComputerName $settings.WindowsServer -Credential $WindowsCreds -ScriptBlock $ScriptBlock -ArgumentList $settings.GiteaWindowsServiceName
}
catch {
Write-Host -foregroundcolor red "Error starting gitea service (2)"
}
Write-Host -ForegroundColor green "Update completed"
}
elseif ($currentreleaseversion -eq $currentversion) {
Write-Host -ForegroundColor green "Currently installed version is the same as available version"
}
else {
Write-Host -ForegroundColor green "Currently installed version newer than available version"
}