-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTAUpdate.cs
231 lines (193 loc) · 7.96 KB
/
OTAUpdate.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace OTAUpdate
{
public static class OTAUpdateHandler
{
public static string upgradeScriptPath = GetCrossPlatformScriptPath(AppDomain.CurrentDomain.BaseDirectory);
public static string WindowsScriptTemplate = @"
param (
[string]$ServiceName = ""${SERVICE_NAME}""
[string]$UpgradeFileName = ""${UPGRADE_FILENAME}""
[string]$ExistingServiceFileName = ""${UPGRADE_FILENAME}""
)
if (-not $ServiceName) {
Write-Host 'Error: No service name provided.'
Write-Host 'Usage: .\restart_service.ps1 <service-name>'
exit 1
}
if (-not $UpgradeFileName) {
Write-Host 'Error: No upgrade file name provided.'
Write-Host 'Usage: .\restart_service.ps1 <service-name> <upgrade-file-name>'
exit 1
}
if (-not $ExistingServiceFileName) {
Write-Host 'Error: No existing service file name provided.'
Write-Host 'Usage: .\restart_service.ps1 <service-name> <upgrade-file-name> <existing-service-file-name>'
exit 1
}
try {
# Stop the service
Write-Host 'Stopping Windows service: $ServiceName'
Stop-Service -Name $ServiceName -Force
# Wait for 10 seconds
Write-Host 'Waiting 10 seconds before copying the new service...'
Start-Sleep -Seconds 10
# Replace the service binary with the new version
Write-Host 'Replacing the service binary with the new version...'
Copy-Item -Path $UpgradeFileName -Destination $ExistingServiceFileName -Force
# wait for 10 seconds
Write-Host 'Waiting 10 seconds before starting the service...'
# Start the service
Write-Host 'Starting Windows service: $ServiceName'
Start-Service -Name $ServiceName
Write-Host 'Service $ServiceName restarted successfully.'
} catch {
Write-Host 'Error: $_'
exit 1
}
";
public static string UnixScriptTemplate = @"
#!/bin/bash
# Definitions
SERVICE_NAME=\""${SERVICE_NAME}\""
UPGRADE_FILENAME=\""${UPGRADE_FILENAME}\"" # Must be in the same folder as this script
EXISTING_SERVICE_FILENAME=\""${EXISTING_SERVICE_FILENAME}\"" # Must be in the same folder as this script
# Determine the operating system
if [[ ""$OSTYPE"" == ""darwin""* ]]; then
# macOS
SERVICE_PLIST=""/Library/LaunchDaemons/${SERVICE_NAME}.plist""
if [ -f ""$SERVICE_PLIST"" ]; then
echo ""Stopping macOS service: $SERVICE_NAME""
sudo launchctl unload ""$SERVICE_PLIST""
echo ""Waiting 10 seconds before copying the new service...""
sleep 10
echo ""Replacing the service binary with the new version...""
cp ""$UPGRADE_FILENAME"" ""$EXISTING_SERVICE_FILENAME""
echo ""Waiting 10 seconds before starting the service...""
sleep 10
echo ""Starting macOS service: $SERVICE_NAME""
sudo launchctl load ""$SERVICE_PLIST""
else
echo ""Error: Service plist file not found: $SERVICE_PLIST""
exit 1
fi
elif [[ ""$OSTYPE"" == ""linux-gnu""* ]]; then
# Linux
echo ""Stopping Linux service: $SERVICE_NAME""
sudo systemctl stop ""$SERVICE_NAME""
echo ""Waiting 10 seconds before copying the service...""
sleep 10
echo ""Replacing the service binary with the new version...""
cp ""$UPGRADE_FILENAME"" ""$EXISTING_SERVICE_FILENAME""
echo ""Waiting 10 seconds before starting the service...""
sleep 10
echo ""Starting Linux service: $SERVICE_NAME""
sudo systemctl start ""$SERVICE_NAME""
else
echo ""Error: Unsupported operating system: $OSTYPE""
exit 1
fi
echo ""Service $SERVICE_NAME restarted successfully.""
";
public static string GetCrossPlatformScriptPath(string basedir) {
return Path.Combine(basedir, "upgrade" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".ps1" : ".sh"));
}
public static void CreateUpgrader( string upgradeFileName, string existingServiceFileName, string serviceName)
{
string scriptContent ;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
scriptContent = WindowsScriptTemplate.Replace("${SERVICE_NAME}", serviceName);
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
scriptContent = UnixScriptTemplate.Replace("${SERVICE_NAME}", serviceName);
}
scriptContent = UnixScriptTemplate.Replace("${SERVICE_NAME}", serviceName);
scriptContent = scriptContent.Replace("${UPGRADE_FILENAME}", upgradeFileName);
scriptContent = scriptContent.Replace("${EXISTING_SERVICE_FILENAME}", existingServiceFileName);
CreateScript(upgradeScriptPath, scriptContent);
}
public static void CreateScript(string scriptPath, string scriptContent)
{
if (File.Exists(scriptPath))
{
File.Delete(scriptPath);
}
// Write the script to the file
File.WriteAllText(scriptPath, scriptContent);
// Ensure the script is executable (if running on Unix-like systems)
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
var chmodProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "chmod",
Arguments = $"+x \"{scriptPath}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
chmodProcess.Start();
chmodProcess.WaitForExit();
}
}
public static void PerformUpgrade() {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-File \"{upgradeScriptPath}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (error == null)
{
Console.WriteLine("Script executed successfully.");
}
else
{
throw new Exception("Error executing the script. " + output);
}
}
} else {
// Run the update script
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"\"{upgradeScriptPath}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
// Optional: Read the script output
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (error == null)
{
Console.WriteLine("Script executed successfully.");
}
else
{
throw new Exception("Error executing the script. " + output);
}
}
}
}
}
}