/
UniversalInstaller.cs
396 lines (340 loc) · 14.7 KB
/
UniversalInstaller.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* FOG Service : A computer management client for the FOG Project
* Copyright (C) 2014-2016 FOG Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Zazzles;
using Mono.Options;
namespace FOG
{
class UniversalInstaller
{
private const string LogName = "Installer";
public static string LogPath;
private static ConsoleColor _infoColor = ConsoleColor.Yellow;
[STAThread]
static void Main(string[] args)
{
Log.Output = Log.Mode.Quiet;
LogPath = Path.Combine(Settings.Location, "SmartInstaller.log");
Log.FilePath = LogPath;
var upgrade = false;
var uninstall = false;
var tray = false;
var start = false;
var rootLog = false;
var https = false;
var server = "";
var webRoot = "";
var logFile = "";
var p = new OptionSet() {
{ "server=|SERVER=", "the FOG server address, defaults to fogserver",
v => server = v },
{ "webroot=|WEBROOT=", "the FOG server webroot, defaults to /fog",
v => webRoot = v },
{ "h|https|HTTPS", "use HTTPS for communication",
v => https = v != null },
{ "r|rootlog|ROOTLOG", "store fog.log in the root of the filesystem",
v => rootLog = v != null },
{ "s|start", "start the service when complete",
v => start = v != null },
{ "t|tray|TRAY", "enable the FOG Tray and notifications",
v => tray = v != null },
{ "u|uninstall", "uninstall an existing installation",
v => uninstall = v != null },
{ "upgrade", "upgrade an existing installation",
v => upgrade = v != null },
{ "l|log", "the log file to use",
v => logFile = v },
};
p.Parse(args);
if (!string.IsNullOrWhiteSpace(logFile))
{
LogPath = logFile;
Log.FilePath = LogPath;
}
if (args.Length == 1)
{
if (args[0].Equals("upgrade"))
upgrade = true;
else if (args[0].Equals("uninstall"))
uninstall = true;
}
if (uninstall)
PerformCLIUninstall();
else if (upgrade)
PerformUpgrade();
else if (args.Length == 0)
InteractiveMode();
else
Install(https ? "1" : "0", tray ? "1" : "0", server, webRoot, "FOG", rootLog ? "1" : "0");
}
private static void PerformUpgrade()
{
Log.Output = Log.Mode.File;
var settingsFile = Path.Combine(Settings.Location, "settings.json");
Settings.SetPath(settingsFile);
Settings.Set("Version", Helper.ClientVersion);
Install(Settings.Get("HTTPS"), Settings.Get("Tray"), Settings.Get("Server"),
Settings.Get("WebRoot"), Settings.Get("Company"), Settings.Get("RootLog"),
true);
File.Copy(settingsFile,
Path.Combine(Helper.Instance.GetLocation(), "settings.json"), true);
File.Copy(Path.Combine(Settings.Location, "token.dat"),
Path.Combine(Helper.Instance.GetLocation(), "token.dat"), true);
}
private static void PrintBanner()
{
var bannerColor = ConsoleColor.White;
var paddingSize = (Log.HeaderLength - 48)/2;
var builder = new StringBuilder();
for (var i = 0; i < paddingSize; i++)
{
builder.Append(" ");
}
var padding = builder.ToString();
Log.NewLine();
Log.WriteLine(padding + " ..#######:. ..,#,.. .::##::. ", bannerColor);
Log.WriteLine(padding + " .:###### .:;####:......;#;.. ", bannerColor);
Log.WriteLine(padding + " ...##... ...##;,;##::::.##... ", bannerColor);
Log.WriteLine(padding + " ,# ...##.....##:::## ..:: ", bannerColor);
Log.WriteLine(padding + " ## .::###,,##. . ##.::#.:######::. ", bannerColor);
Log.WriteLine(padding + " ...##:::###::....#. .. .#...#. #...#:::. ", bannerColor);
Log.WriteLine(padding + " ..:####:.. ..##......##::## .. # ", bannerColor);
Log.WriteLine(padding + " # . ...##:,;##;:::#: ... ##.. ", bannerColor);
Log.WriteLine(padding + " .# . .:;####;::::.##:::;#:.. ", bannerColor);
Log.WriteLine(padding + " # ..:;###.. ", bannerColor);
Log.WriteLine(padding + " ", bannerColor);
Log.WriteLine(padding + " ###########################################", bannerColor);
Log.WriteLine(padding + " # FOG #", bannerColor);
Log.WriteLine(padding + " # Free Computer Imaging Solution #", bannerColor);
Log.WriteLine(padding + " # #", bannerColor);
Log.WriteLine(padding + " # https://www.fogproject.org/ #", bannerColor);
Log.WriteLine(padding + " # #", bannerColor);
Log.WriteLine(padding + " # Credits: #", bannerColor);
Log.WriteLine(padding + " # https://fogproject.org/Credits #", bannerColor);
Log.WriteLine(padding + " # GNU GPL Version 3 #", bannerColor);
Log.WriteLine(padding + " ###########################################", bannerColor);
Log.WriteLine(padding + " # FOG Service Installer #", bannerColor);
Log.NewLine();
}
private static void InteractiveMode()
{
try
{
ShowGUI();
}
catch (Exception ex)
{
if (Settings.OS == Settings.OSType.Windows)
{
Helper.Instance.PrepareFiles();
Helper.Instance.Install();
Helper.Instance.Configure();
}
else
{
PerformCLIInstall();
}
}
}
private static void ShowGUI()
{
throw new NotImplementedException();
Log.Output = Log.Mode.File;
Log.FilePath = Path.Combine(Settings.Location, "FOGService-install.log");
Application.EnableVisualStyles();
var gui = new GUI();
Application.Run(gui);
}
private static void PerformCLIUninstall(bool noInfo = false)
{
if (!noInfo)
{
Log.FilePath = LogPath;
Log.Output = Log.Mode.Console;
PrintBanner();
PrintLicense();
PrintInfo();
}
Log.Header("Uninstall");
Log.NewLine();
DoAction("Uninstalling", Helper.Instance.Uninstall);
Log.NewLine();
}
private static void PerformCLIInstall(bool noInfo = false)
{
if (!noInfo)
{
Log.FilePath = LogPath;
Log.Output = Log.Mode.Console;
PrintBanner();
PrintLicense();
PrintInfo();
}
Log.Header("Configure");
Log.NewLine();
var https = "0";
var tray = "1";
var company = "FOG";
var rootLog = "0";
Log.Write("FOG Server address [default: fogserver]: ");
Console.ForegroundColor = _infoColor;
var server = Console.ReadLine();
Console.ResetColor();
if (string.IsNullOrWhiteSpace(server))
server = "fogserver";
Log.Write("Webroot [default: /fog]: ");
Console.ForegroundColor = _infoColor;
var webRoot = Console.ReadLine();
Console.ResetColor();
if (string.IsNullOrWhiteSpace(webRoot))
webRoot = "/fog";
Log.Write("Enable tray icon? [Y/n]: ");
Console.ForegroundColor = _infoColor;
var rawTray = Console.ReadLine();
Console.ResetColor();
if (rawTray.Trim().Equals("n", StringComparison.OrdinalIgnoreCase))
tray = "0";
// Check hostname length (OSX appends .local by default)
var hostname = Environment.MachineName;
if (hostname.Length > 15)
{
Log.NewLine();
Log.WriteLine("Your hostname is " + hostname, _infoColor);
Log.WriteLine("This exceeds the 15 character limit.", _infoColor);
Log.WriteLine("Auto registration will not run until this is fixed.", _infoColor);
Log.WriteLine("Press ENTER to proceed with installation.", _infoColor);
Console.ReadLine();
Log.NewLine();
}
var start = Settings.OS == Settings.OSType.Linux;
if (start)
{
Log.Write("Start FOG Service when done? [Y/n]: ");
Console.ForegroundColor = _infoColor;
var rawStart = Console.ReadLine();
Console.ResetColor();
if (rawStart.Trim().Equals("n", StringComparison.OrdinalIgnoreCase))
start = false;
}
if (!Install(https, tray, server, webRoot, company, rootLog))
{
Log.NewLine();
Log.WriteLine("Installation failed, cleaning system", _infoColor);
Log.NewLine();
PerformCLIUninstall(true);
}
else
{
if (start)
Start();
}
Log.Header("Finished");
Log.NewLine();
Log.WriteLine($"See {LogPath} for more information.", _infoColor);
Log.NewLine();
}
private static void PrintLicense()
{
Log.Header("License");
Log.NewLine();
Log.WriteLine("FOG Service Copyright (C) 2014-2016 FOG Project", _infoColor);
Log.WriteLine("This program comes with ABSOLUTELY NO WARRANTY.", _infoColor);
Log.WriteLine("This is free software, and you are welcome to redistribute it under certain", _infoColor);
Log.WriteLine("conditions. See your FOG server under 'FOG Configuration' -> 'License' for", _infoColor);
Log.WriteLine("further information.", _infoColor);
Log.NewLine();
}
private static void PrintInfo()
{
Log.Header("Information");
Log.NewLine();
PrintInfo("Version", Helper.ClientVersion);
PrintInfo("OS", Settings.OS.ToString());
PrintInfo("Current Path", Settings.Location);
PrintInfo("Install Location", Helper.Instance.GetLocation());
Helper.Instance.PrintInfo();
Log.NewLine();
}
private static bool Install(string https, string tray, string server,
string webRoot, string company, string rootLog, bool skipSave= false)
{
Log.NewLine();
Log.Header("Installing");
Log.NewLine();
if (!DoAction("Getting things ready", Helper.Instance.PrepareFiles))
return false;
if (!DoAction("Installing files",() => Helper.Instance.Install(https, tray, server, webRoot, company, rootLog)))
return false;
if (Settings.OS == Settings.OSType.Windows)
return true;
if(!skipSave)
if (!DoAction("Saving Configuration",() => Helper.SaveSettings(https, tray, server, webRoot, company, rootLog)))
return false;
if (!DoAction("Applying Configuration", Helper.Instance.Configure))
return false;
if (!DoAction("Pinning FOG Project", Helper.PinFOGCert))
return false;
if (!DoAction("Pinning Server", () => Helper.PinServerCert(Helper.Instance.GetLocation(), skipSave)))
return false;
Log.NewLine();
return true;
}
private static bool DoAction(string action, Func<bool> method )
{
Log.Action(action);
Log.Output = Log.Mode.File;
var success = false;
try
{
success = method();
}
catch (Exception ex)
{
Log.Error(LogName, "Method: " + method.Method.Name);
Log.Error(LogName, ex);
}
Log.Output = Log.Mode.Console;
Log.ActionResult(success);
return success;
}
private static void Start()
{
Log.Action("Starting FOG Service");
Log.Output = Log.Mode.File;
var controlPath = Path.Combine(Helper.Instance.GetLocation(), "control.sh");
var returnCode = ProcessHandler.Run("/bin/bash", controlPath + " start");
Log.Output = Log.Mode.Console;
Log.ActionResult(returnCode == 0);
Log.NewLine();
}
public static void PrintInfo(string setting, string value)
{
var builder = new StringBuilder(setting);
for (var i = setting.Length; i < Log.HeaderLength - value.Length; i++)
{
builder.Append(".");
}
Log.Write(builder.ToString());
Log.WriteLine(value, _infoColor);
}
}
}