-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathProgram.cs
More file actions
361 lines (302 loc) · 16.6 KB
/
Program.cs
File metadata and controls
361 lines (302 loc) · 16.6 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Remotely.Shared.Enums;
using Remotely.Shared.Services;
using Remotely.Shared.Utilities;
using Server.Installer.Models;
using Server.Installer.Services;
using System;
using System.IO;
using System.Threading.Tasks;
namespace Server.Installer
{
public class Program
{
public static IServiceProvider Services { get; set; }
public static async Task Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(AppConstants.RemotelyAscii);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("(https://remotely.one)");
Console.WriteLine();
Console.WriteLine();
if (!ParseCliParams(args, out var cliParams))
{
ShowHelpText();
return;
}
if (EnvironmentHelper.Platform != Platform.Windows &&
EnvironmentHelper.Platform != Platform.Linux)
{
ConsoleHelper.WriteError("Remotely Server can only be installed on Linux or Windows.");
return;
}
BuildServices();
var elevationDetector = Services.GetRequiredService<IElevationDetector>();
var serverInstaller = Services.GetRequiredService<IServerInstaller>();
var githubApi = Services.GetRequiredService<IGitHubApi>();
if (!elevationDetector.IsElevated())
{
ConsoleHelper.WriteError("The installer process must be elevated. On Linux, run with sudo. " +
"On Windows, run from a command line that was opened with \"Run as admin\".");
return;
}
ConsoleHelper.WriteLine("Thank you for trying Remotely! This installer will guide you " +
"through deploying the Remotely server onto this machine.");
ConsoleHelper.WriteLine("There are two ways to create the server files. You can download the pre-built package " +
"from the latest public release, or you can use your GitHub credentials to build a customized package through " +
"an integration with GitHub Actions. The pre-built packages will not have your server's URL embedded in the " +
"desktop clients, and end users will need to type it in manually.");
ConsoleHelper.WriteLine("If using GitHub Actions, you will need to enter a GitHub Personal Access Token, " +
"which will allow this app to access your fork of the Remotely repo. You can generate a PAT at " +
"https://github.com/settings/tokens. You need to give it the \"repo\" scope.");
ConsoleHelper.WriteLine("Be sure to retain your GitHub Personal Access Token if you want to re-use it " +
"for upgrading in the future. The installer does not save it locally.");
ConsoleHelper.WriteLine("If you haven't already, please go to the Actions tab in your Remotely repo " +
"and enable them. If not, this process will fail.");
while (cliParams.UsePrebuiltPackage is null)
{
var usePrebuiltPackage = ConsoleHelper.ReadLine("Download pre-built package (yes/no)?",
subprompt: "If no, a customized server package will be created through GitHub Actions.");
if (ConsoleHelper.TryParseBoolLike(usePrebuiltPackage, out var result))
{
cliParams.UsePrebuiltPackage = result;
}
}
while (string.IsNullOrWhiteSpace(cliParams.InstallDirectory))
{
cliParams.InstallDirectory = ConsoleHelper.ReadLine("Which directory should the server files be extracted to (e.g. /var/www/remotely/)?").Trim();
}
while (cliParams.ServerUrl is null)
{
var url = ConsoleHelper.ReadLine("What is your server's public URL (e.g. https://app.remotely.one)?").Trim();
if (Uri.TryCreate(url, UriKind.Absolute, out var serverUrl))
{
cliParams.ServerUrl = serverUrl;
}
}
while (cliParams.WebServer is null)
{
var webServerType = ConsoleHelper.GetSelection("Which web server will be used?",
"Caddy on Ubuntu",
"Nginx on Ubuntu",
"Caddy on CentOS",
"Nginx on CentOS",
"Nginx on RHEL8"
"IIS on Windows Server 2016+");
if (Enum.TryParse<WebServerType>(webServerType, out var result))
{
cliParams.WebServer = result;
}
}
if (cliParams.UsePrebuiltPackage == false)
{
while (string.IsNullOrWhiteSpace(cliParams.GitHubUsername))
{
cliParams.GitHubUsername = ConsoleHelper.ReadLine("What is your GitHub username?").Trim();
}
while (string.IsNullOrWhiteSpace(cliParams.GitHubPat))
{
cliParams.GitHubPat = ConsoleHelper.ReadLine("What GitHub Personal Access Token should be used?").Trim();
}
while (cliParams.CreateNew is null)
{
var createNew = ConsoleHelper.ReadLine("Create new build (yes/no)?",
subprompt: "If no, the latest existing build artifact on GitHub will be used.");
if (ConsoleHelper.TryParseBoolLike(createNew, out var result))
{
cliParams.CreateNew = result;
}
}
if (cliParams.CreateNew == true)
{
if (cliParams.Reference?.Contains("latest", StringComparison.OrdinalIgnoreCase) == true)
{
cliParams.Reference = await githubApi.GetLatestReleaseTag();
}
while (string.IsNullOrWhiteSpace(cliParams.Reference))
{
var selection = ConsoleHelper.GetSelection("Which version would you like to build?",
"Latest official release",
"Preview changes (i.e. master branch)",
"Specific release");
if (int.TryParse(selection, out var result))
{
switch (result)
{
case 0:
cliParams.Reference = await githubApi.GetLatestReleaseTag();
break;
case 1:
cliParams.Reference = "master";
break;
case 2:
ConsoleHelper.WriteLine("Enter the GitHub branch or tag name from which to build " +
"(e.g. \"master\" or \"v2021.04.25.0953\").");
cliParams.Reference = ConsoleHelper.ReadLine("Input Reference").Trim();
break;
default:
break;
}
}
}
}
ConsoleHelper.WriteLine($"Performing server install. GitHub User: {cliParams.GitHubUsername}. " +
$"Server URL: {cliParams.ServerUrl}. Installation Directory: {cliParams.InstallDirectory}. " +
$"Web Server: {cliParams.WebServer}. Create New Build: {cliParams.CreateNew}. " +
$"Git Reference: {cliParams.Reference}");
}
else
{
ConsoleHelper.WriteLine($"Server URL: {cliParams.ServerUrl}. " +
$"Installation Directory: {cliParams.InstallDirectory}. Web Server: {cliParams.WebServer}.");
}
await serverInstaller.PerformInstall(cliParams);
ConsoleHelper.WriteLine("Installation completed.");
}
private static void BuildServices()
{
var services = new ServiceCollection();
services.AddSingleton<IGitHubApi, GitHubApi>();
services.AddSingleton<IServerInstaller, ServerInstaller>();
if (EnvironmentHelper.IsWindows)
{
services.AddSingleton<IElevationDetector, ElevationDetectorWin>();
}
else if (EnvironmentHelper.IsLinux)
{
services.AddSingleton<IElevationDetector, ElevationDetectorLinux>();
}
else
{
throw new NotSupportedException("Operating system not supported.");
}
Services = services.BuildServiceProvider();
}
private static bool ParseCliParams(string[] args, out CliParams cliParams)
{
cliParams = new CliParams();
for (var i = 0; i < args.Length; i += 2)
{
try
{
var key = args[i].Trim();
if (i == args.Length - 1)
{
ConsoleHelper.WriteError("An argument is missing a value.");
return false;
}
var value = args[i + 1].Trim();
switch (key)
{
case "--help":
case "-h":
case "/h":
ShowHelpText();
Environment.Exit(0);
break;
case "--use-prebuilt":
case "-b":
{
if (bool.TryParse(value, out var result))
{
cliParams.UsePrebuiltPackage = result;
continue;
}
ConsoleHelper.WriteError("--use-prebuilt parameter is invalid. Must be true/yes or false/no.");
return false;
}
case "--web-server":
case "-w":
{
if (int.TryParse(value, out var webServerResult))
{
cliParams.WebServer = (WebServerType)webServerResult;
continue;
}
ConsoleHelper.WriteError($"--web-server parameter is invalid. Must be a " +
$"number (0 - {Enum.GetValues<WebServerType>().Length}).");
return false;
}
case "--server-url":
case "-s":
{
if (Uri.TryCreate(value, UriKind.Absolute, out var result))
{
cliParams.ServerUrl = result;
continue;
}
ConsoleHelper.WriteError("--server-url parameter is invalid. Must be a valid URL (e.g. https://app.remotely.one).");
return false;
}
case "--install-directory":
case "-i":
cliParams.InstallDirectory = value;
continue;
case "--github-username":
case "-u":
cliParams.GitHubUsername = value;
continue;
case "--github-pat":
case "-p":
cliParams.GitHubPat = value;
continue;
case "--reference":
case "-r":
cliParams.Reference = value;
continue;
case "--create-new":
case "-c":
{
if (bool.TryParse(value, out var result))
{
cliParams.CreateNew = result;
continue;
}
ConsoleHelper.WriteError("--create-new parameter is invalid. Must be true/yes or false/no.");
return false;
}
default:
return false;
}
}
catch (Exception ex)
{
ConsoleHelper.WriteError($"Error while parsing command line arguments: {ex.Message}");
return false;
}
}
return true;
}
private static void ShowHelpText()
{
ConsoleHelper.WriteLine("Remotely Server Installer", 0, ConsoleColor.Cyan);
ConsoleHelper.WriteLine("Builds a customized Remotely server using GitHub actions " +
"and installs the server on the local machine.", 1);
ConsoleHelper.WriteLine("Usage:");
ConsoleHelper.WriteLine("\tNo Parameters - Run the installer interactively.", 2);
ConsoleHelper.WriteLine("\t--use-prebuilt, -b (true/false or yes/no) Whether to use the pre-built server package from the " +
"latest public release, or to create a customized package through GitHub Actions. The pre-built package " +
"will not contain your server's URL in the desktop clients, and end users will need to type it in manually.", 1);
ConsoleHelper.WriteLine("\t--github-username, -u Your GitHub username, where the forked Remotely repo exists.", 1);
ConsoleHelper.WriteLine("\t--github-pat, -p The GitHub Personal Access Token to use for authentication. " +
"Create one at ttps://github.com/settings/tokens.", 1);
ConsoleHelper.WriteLine("\t--server-url, -s The public URL where your Remotely server will be accessed (e.g. https://app.remotely.one).", 1);
ConsoleHelper.WriteLine("\t--install-directory, -i The directory path where the server files will be installed (e.g. /var/www/remotely/).", 1);
ConsoleHelper.WriteLine("Enter the GitHub branch or tag name from which to build. For example, you can enter " +
" \"master\" to build the latest changes from the default branch. Or you can enter a release tag like \"v2021.04.13.1604\".", 1);
ConsoleHelper.WriteLine("\t--reference, -r Use \"latest\" to build from the latest full release. Otherwise, you can enter the " +
"name of a branch or tag from which to build. For example, you can enter \"master\" to build the most recent preview changes " +
"from the default branch. Or you can enter a specific release tag like \"v2021.04.13.1604\".", 1);
ConsoleHelper.WriteLine("\t--create-new, -c (true/false or yes/no) Whether to run a new build. If false, the latest existing build artifact will be used.", 1);
ConsoleHelper.WriteLine("\t--web-server, -w Number. The web server that will be used as a reverse proxy to forward " +
"requests to the Remotely server. Select the appropriate option for your operating system and web server. " +
"0 = Caddy on Ubuntu. 1 = Nginx on Ubuntu. 2 = Caddy on CentOS. 3 = Nginx on CentOS. 4 = IIS on Windows Server 2016+.", 1);
ConsoleHelper.WriteLine("Example (build latest release):");
ConsoleHelper.WriteLine("sudo ./Remotely_Server_Installer -b false -u lucent-sea -p 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 -s https://app.remotely.one -i /var/www/remotely/ -r latest -c true -w 0", 1);
ConsoleHelper.WriteLine("Example (use pre-built package):");
ConsoleHelper.WriteLine("sudo ./Remotely_Server_Installer -b true -s https://app.remotely.one -i /var/www/remotely/ -w 0");
}
}
}