|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.IO.Compression; |
| 6 | +using System.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.ServiceProcess; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Newtonsoft.Json.Linq; |
| 13 | +using SharpCompress.Archive.SevenZip; |
| 14 | +using SharpCompress.Archive.Zip; |
| 15 | +using SharpCompress.Common; |
| 16 | +using SharpCompress.Reader; |
| 17 | + |
| 18 | +namespace SDInstaller |
| 19 | +{ |
| 20 | + class Program |
| 21 | + { |
| 22 | + private static string TempPath; |
| 23 | + private static string installLocation; |
| 24 | + private static string PluginManifestLocation; |
| 25 | + static void Main(string[] args) |
| 26 | + { |
| 27 | + if (args.Length < 3 || string.IsNullOrWhiteSpace(args[0])) |
| 28 | + { |
| 29 | + Console.WriteLine("Usage: SDInstaller (SD API Key) (Tether web Location) (Tether install location) (OPTIONAL: Plugin Manifest Location)"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + installLocation = args[2]; |
| 34 | + TempPath = Path.Combine(installLocation, "_temp"); |
| 35 | + |
| 36 | + if (!string.IsNullOrWhiteSpace(args[3])) |
| 37 | + { |
| 38 | + PluginManifestLocation = args[3]; |
| 39 | + } |
| 40 | + |
| 41 | + if (!Directory.Exists(installLocation)) |
| 42 | + { |
| 43 | + Directory.CreateDirectory(installLocation); |
| 44 | + } |
| 45 | + |
| 46 | + if (!Directory.Exists(TempPath)) |
| 47 | + { |
| 48 | + Directory.CreateDirectory(TempPath); |
| 49 | + } |
| 50 | + |
| 51 | + WebClient client = new WebClient(); |
| 52 | + Console.WriteLine("Downloading file"); |
| 53 | + var localZip = Path.Combine(TempPath, "Tether.zip"); |
| 54 | + client.DownloadFile(args[1], localZip); |
| 55 | + Console.WriteLine("File Downloaded, executing"); |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + ServiceController ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ThreeOneThree.Tether"); |
| 60 | + |
| 61 | + if (ctl == null) |
| 62 | + { |
| 63 | + Console.WriteLine("Performing Fresh install"); |
| 64 | + PerformFreshInstall(args, localZip, client); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + Console.WriteLine("Performing Upgrade"); |
| 69 | + PerformUpgradeInstall(args, localZip, client, ctl); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + Console.WriteLine("Finished"); |
| 74 | + } |
| 75 | + |
| 76 | + private static void PerformUpgradeInstall(string[] args, string localZip, WebClient client, ServiceController ctl) |
| 77 | + { |
| 78 | + StopService(ctl); |
| 79 | + StopService(ctl); |
| 80 | + StopService(ctl); // Just to be sure it's not running! |
| 81 | + |
| 82 | + var settingsFile = Path.Combine(installLocation, "settings.json"); |
| 83 | + var tempSettings = Path.Combine(TempPath, "settings.json"); |
| 84 | + var copySettingsBack = false; |
| 85 | + |
| 86 | + if (File.Exists(settingsFile)) |
| 87 | + { |
| 88 | + File.Copy(settingsFile, tempSettings, true); |
| 89 | + copySettingsBack = true; |
| 90 | + } |
| 91 | + |
| 92 | + ExtractFilesToLocation(localZip, installLocation); |
| 93 | + |
| 94 | + File.Delete(Path.Combine(TempPath, "Tether.zip")); |
| 95 | + |
| 96 | + File.WriteAllText( Path.Combine(installLocation, "tether.exe.config") ,File.ReadAllText(Path.Combine(installLocation, "tether.exe.config")).Replace("Trace", "Error")); |
| 97 | + |
| 98 | + if (copySettingsBack) |
| 99 | + { |
| 100 | + File.Move(tempSettings, settingsFile); |
| 101 | + } |
| 102 | + |
| 103 | + Thread.Sleep(TimeSpan.FromSeconds(15)); |
| 104 | + |
| 105 | + ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "ThreeOneThree.Tether"); |
| 106 | + ctl.Start(); |
| 107 | + ctl.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(15)); |
| 108 | + if (ctl.Status != ServiceControllerStatus.Running) |
| 109 | + { |
| 110 | + Console.WriteLine("Not running!"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static void StopService(ServiceController ctl) |
| 115 | + { |
| 116 | + if (ctl.Status != ServiceControllerStatus.Stopped) |
| 117 | + { |
| 118 | + ctl.Stop(); |
| 119 | + ctl.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void PerformFreshInstall(string[] args, string localZip, WebClient client) |
| 124 | + { |
| 125 | + ExtractFilesToLocation(localZip, installLocation); |
| 126 | + |
| 127 | + File.Delete(Path.Combine(TempPath, "Tether.zip")); |
| 128 | + |
| 129 | + string SDKey = args[0]; |
| 130 | + |
| 131 | + var downloadString = client.DownloadString(@"https://api.serverdensity.io/inventory/resources?token=" + SDKey + @"&filter={""name"":""" + Environment.MachineName.ToLower() + @""",""type"":""device""}&fields=[""agentKey""]"); |
| 132 | + |
| 133 | + var jObject = JArray.Parse(downloadString); |
| 134 | + |
| 135 | + string AccountName = GetAccountName(client, SDKey); |
| 136 | + |
| 137 | + if (String.IsNullOrWhiteSpace(AccountName)) |
| 138 | + { |
| 139 | + Console.WriteLine("! Unable to get account name !"); |
| 140 | + } |
| 141 | + |
| 142 | + string AgentKey; |
| 143 | + |
| 144 | + if (jObject.Any()) |
| 145 | + { |
| 146 | + Console.WriteLine("Agent Key Found!"); |
| 147 | + |
| 148 | + AgentKey = jObject.FirstOrDefault<dynamic>().agentKey; |
| 149 | + |
| 150 | + Console.WriteLine("Agent key is " + AgentKey); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + Console.WriteLine("Creating Agent Key"); |
| 155 | + |
| 156 | + string createMachineRequest = JObject.FromObject(new {name = Environment.MachineName.ToLower()}).ToString(); |
| 157 | + |
| 158 | + client.Headers.Set(HttpRequestHeader.ContentType, "application/json"); |
| 159 | + |
| 160 | + string response = client.UploadString("https://api.serverdensity.io/inventory/devices?token=" + SDKey, createMachineRequest); |
| 161 | + |
| 162 | + AgentKey = JObject.Parse(response)["agentKey"].ToString(); |
| 163 | + |
| 164 | + Console.WriteLine("Agent key is " + AgentKey); |
| 165 | + } |
| 166 | + |
| 167 | + var configLocation = Path.Combine(installLocation, "settings.json"); |
| 168 | + |
| 169 | + if (!File.Exists(configLocation)) |
| 170 | + { |
| 171 | + throw new Exception("Config file does not exist!"); |
| 172 | + } |
| 173 | + |
| 174 | + string config = |
| 175 | + @"{ |
| 176 | + ""ServerDensityUrl"": ""https://" + AccountName + @".serverdensity.io"", |
| 177 | + ""ServerDensityKey"": """ + AgentKey + @""", |
| 178 | + ""CheckInterval"": 60, |
| 179 | + ""PluginManifestLocation"": """ + PluginManifestLocation + @""" |
| 180 | +}"; |
| 181 | + |
| 182 | + File.WriteAllText(configLocation, config); |
| 183 | + |
| 184 | + File.WriteAllText(Path.Combine(installLocation, "tether.exe.config"), File.ReadAllText(Path.Combine(installLocation, "tether.exe.config")).Replace("Trace", "Error")); |
| 185 | + |
| 186 | + Process.Start( Path.Combine(installLocation, "Tether.exe"), "install"); |
| 187 | + |
| 188 | + Process.Start("net", "start \"ThreeOneThree.Tether\"").WaitForExit(); |
| 189 | + |
| 190 | + Console.WriteLine("Service Started"); |
| 191 | + } |
| 192 | + |
| 193 | + private static void ExtractFilesToLocation(string fileName, string Path) |
| 194 | + { |
| 195 | + if (!Directory.Exists(Path)) |
| 196 | + { |
| 197 | + Directory.CreateDirectory(Path); |
| 198 | + } |
| 199 | + |
| 200 | + using (var sevenZipArchive = ZipArchive.Open(fileName)) |
| 201 | + { |
| 202 | + using (var reader = sevenZipArchive.ExtractAllEntries()) |
| 203 | + { |
| 204 | + reader.WriteAllToDirectory(Path, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + public static string GetAccountName(WebClient client, string SDKey) |
| 209 | + { |
| 210 | + var downloadString = client.DownloadString("https://api.serverdensity.io/users/users?token=" + SDKey); |
| 211 | + |
| 212 | + var jObject = JArray.Parse(downloadString); |
| 213 | + |
| 214 | + foreach (dynamic jToken in jObject.Cast<dynamic>().Where(jToken => jToken.accountName != null && !string.IsNullOrWhiteSpace(jToken.accountName.ToString()))) { |
| 215 | + return jToken.accountName; |
| 216 | + } |
| 217 | + |
| 218 | + return ""; |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments