Skip to content

Commit 79b9bbf

Browse files
author
Monty A
committed
Initial commit!
1 parent 81ba20b commit 79b9bbf

File tree

7 files changed

+373
-0
lines changed

7 files changed

+373
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,11 @@ FakesAssemblies/
194194

195195
# Visual Studio 6 workspace options file
196196
*.opt
197+
198+
# Tabs studio
199+
*.tss
200+
201+
# Side-effects of the dodgey build process
202+
Generic.zip
203+
SDInstaller.exe*
204+
SDInstaller.dll*

SDInstaller.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SDInstaller", "SDInstaller\SDInstaller.csproj", "{E7D4ACC0-AF2C-4ACE-9413-B142D8E0E271}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E7D4ACC0-AF2C-4ACE-9413-B142D8E0E271}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E7D4ACC0-AF2C-4ACE-9413-B142D8E0E271}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E7D4ACC0-AF2C-4ACE-9413-B142D8E0E271}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E7D4ACC0-AF2C-4ACE-9413-B142D8E0E271}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

SDInstaller/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
5+
</startup>
6+
</configuration>

SDInstaller/Program.cs

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SDInstaller")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SDInstaller")]
13+
[assembly: AssemblyCopyright("Copyright © 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e7d4acc0-af2c-4ace-9413-b142d8e0e271")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

SDInstaller/SDInstaller.csproj

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E7D4ACC0-AF2C-4ACE-9413-B142D8E0E271}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SDInstaller</RootNamespace>
11+
<AssemblyName>SDInstaller</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<TargetFrameworkProfile />
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
39+
<Private>True</Private>
40+
</Reference>
41+
<Reference Include="SharpCompress, Version=0.10.3.0, Culture=neutral, PublicKeyToken=beaf6f427e128133, processorArchitecture=MSIL">
42+
<HintPath>..\packages\sharpcompress.0.11.1\lib\net40\SharpCompress.dll</HintPath>
43+
<Private>True</Private>
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.Core" />
47+
<Reference Include="System.ServiceProcess" />
48+
<Reference Include="System.Xml.Linq" />
49+
<Reference Include="System.Data.DataSetExtensions" />
50+
<Reference Include="Microsoft.CSharp" />
51+
<Reference Include="System.Data" />
52+
<Reference Include="System.Net.Http" />
53+
<Reference Include="System.Xml" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Compile Include="Program.cs" />
57+
<Compile Include="Properties\AssemblyInfo.cs" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="App.config" />
61+
<None Include="packages.config" />
62+
</ItemGroup>
63+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
64+
<PropertyGroup>
65+
<PostBuildEvent>"$(SolutionDir)packages\ILRepack.2.0.5\tools\ILRepack.exe" /wildcards /out:"$(SolutionDir)$(TargetFileName)" "$(ProjectDir)$(OutDir)$(TargetFileName)" "$(ProjectDir)$(OutDir)*.dll"</PostBuildEvent>
66+
</PropertyGroup>
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>

SDInstaller/packages.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="ILRepack" version="2.0.5" targetFramework="net452" />
4+
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net4" />
5+
<package id="sharpcompress" version="0.11.1" targetFramework="net45" />
6+
</packages>

0 commit comments

Comments
 (0)