Skip to content

Commit 3b66046

Browse files
committed
Merge branch 'development' into 'master'
Development See merge request !1
2 parents 0af7ace + 6d3a880 commit 3b66046

18 files changed

+638
-103
lines changed

DeployClient/API.cs

+85-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Net;
56
using System.Net.Http;
67
using System.Threading.Tasks;
78
using System.Web.Script.Serialization;
@@ -18,13 +19,51 @@ private static HttpClient BuildClient()
1819

1920
client.BaseAddress = new Uri(new Uri(Properties.Settings.Default.TargetUri), "DesktopModules/PolyDeploy/API/");
2021
client.DefaultRequestHeaders.Add("x-api-key", Properties.Settings.Default.APIKey);
22+
client.Timeout = TimeSpan.FromSeconds(25);
2123

2224
return client;
2325
}
2426

25-
public static async Task<Dictionary<string, dynamic>> CIInstall(List<KeyValuePair<string, Stream>> streams)
27+
public static string CreateSession()
2628
{
27-
string endpoint = "CI/Install";
29+
string endpoint = "CI/CreateSession";
30+
31+
using (HttpClient client = BuildClient())
32+
{
33+
string json = client.GetStringAsync(endpoint).Result;
34+
35+
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
36+
37+
Dictionary<string, dynamic> session = jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
38+
39+
string sessionGuid = null;
40+
41+
if (session.ContainsKey("Guid"))
42+
{
43+
sessionGuid = session["Guid"];
44+
}
45+
46+
return sessionGuid;
47+
}
48+
}
49+
50+
public static Dictionary<string, dynamic> GetSession(string sessionGuid)
51+
{
52+
string endpoint = string.Format("CI/GetSession?sessionGuid={0}", sessionGuid);
53+
54+
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
55+
56+
using (HttpClient client = BuildClient())
57+
{
58+
string json = client.GetStringAsync(endpoint).Result;
59+
60+
return jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
61+
}
62+
}
63+
64+
public static void AddPackages(string sessionGuid, List<KeyValuePair<string, Stream>> streams)
65+
{
66+
string endpoint = string.Format("CI/AddPackages?sessionGuid={0}", sessionGuid);
2867

2968
using (HttpClient client = BuildClient())
3069
{
@@ -35,13 +74,53 @@ public static async Task<Dictionary<string, dynamic>> CIInstall(List<KeyValuePai
3574
form.Add(new StreamContent(keyValuePair.Value), "none", keyValuePair.Key);
3675
}
3776

38-
HttpResponseMessage response = await client.PostAsync(endpoint, form);
77+
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
78+
}
79+
}
3980

40-
string json = await response.Content.ReadAsStringAsync();
81+
public static void AddPackageAsync(string sessionGuid, Stream stream, string filename)
82+
{
83+
string endpoint = string.Format("CI/AddPackages?sessionGuid={0}", sessionGuid);
4184

42-
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
85+
using (HttpClient client = BuildClient())
86+
{
87+
MultipartFormDataContent form = new MultipartFormDataContent();
4388

44-
return jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
89+
form.Add(new StreamContent(stream), "none", filename);
90+
91+
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
92+
}
93+
}
94+
95+
public static bool Install(string sessionGuid, out Dictionary<string, dynamic> response)
96+
{
97+
string endpoint = string.Format("CI/Install?sessionGuid={0}", sessionGuid);
98+
99+
bool success = false;
100+
101+
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
102+
103+
response = null;
104+
105+
using (HttpClient client = BuildClient())
106+
{
107+
try
108+
{
109+
HttpResponseMessage httpResponse = client.GetAsync(endpoint).Result;
110+
111+
if (httpResponse.StatusCode.Equals(HttpStatusCode.OK))
112+
{
113+
success = true;
114+
string json = httpResponse.Content.ReadAsStringAsync().Result;
115+
response = jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
116+
}
117+
}
118+
catch (Exception ex)
119+
{
120+
// Nothing to do.
121+
}
122+
123+
return success;
45124
}
46125
}
47126
}

DeployClient/Program.cs

+116-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Configuration;
66
using System.IO;
7+
using System.Web.Script.Serialization;
78

89
namespace DeployClient
910
{
@@ -107,52 +108,147 @@ static void Main(string[] args)
107108
}
108109

109110
// Inform user of encryption.
110-
WriteLine("Starting encryption...");
111+
WriteLine("Starting encryption and upload...");
111112

112-
List<KeyValuePair<string, Stream>> encryptedStreams = new List<KeyValuePair<string, Stream>>();
113+
// Get a session.
114+
string sessionGuid = API.CreateSession();
115+
116+
WriteLine(string.Format("Got session: {0}", sessionGuid));
117+
118+
DateTime startTime = DateTime.Now;
113119

114120
foreach (string zipFile in zipFiles)
115121
{
122+
116123
using (FileStream fs = new FileStream(zipFile, FileMode.Open))
117124
{
118-
encryptedStreams.Add(new KeyValuePair<string, Stream>(Path.GetFileName(zipFile), Crypto.Encrypt(fs, Properties.Settings.Default.EncryptionKey)));
125+
WriteLine(string.Format("\t{0}", Path.GetFileName(zipFile)));
126+
Write("\t\t...encrypting...");
127+
128+
using (Stream es = Crypto.Encrypt(fs, Properties.Settings.Default.EncryptionKey))
129+
{
130+
Write("uploading...");
131+
132+
API.AddPackageAsync(sessionGuid, es, Path.GetFileName(zipFile));
133+
}
134+
135+
WriteLine("done.");
119136
}
120-
WriteLine(string.Format("\tEncrypting {0}", Path.GetFileName(zipFile)));
121137
}
138+
139+
WriteLine(string.Format("Finished encryption and upload in {0} ms.", (DateTime.Now - startTime).TotalMilliseconds));
122140
WriteLine();
123141

124-
Dictionary<string, dynamic> results = API.CIInstall(encryptedStreams).Result;
142+
WriteLine("Starting installation...");
143+
144+
DateTime installStartTime = DateTime.Now;
145+
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
125146

126-
ArrayList installed = results.ContainsKey("Installed") ? results["Installed"] : null;
127-
ArrayList failed = results.ContainsKey("Failed") ? results["Failed"] : null;
147+
// Start.
148+
Dictionary<string, dynamic> results = null;
128149

129-
// Any failures?
130-
if (failed.Count > 0)
150+
if (!API.Install(sessionGuid, out results))
131151
{
132-
WriteLine(string.Format("{0}/{1} module archives failed to install.", failed.Count, encryptedStreams.Count));
133-
ReadLine();
134-
Environment.Exit((int)ExitCode.InstallFailure);
135-
}
152+
DateTime abortTime = DateTime.Now.AddMinutes(10);
153+
TimeSpan interval = new TimeSpan(0, 0, 0, 2);
136154

137-
// Output result
138-
WriteLine(string.Format("{0}/{1} module archives installed successfully.", installed.Count, encryptedStreams.Count));
139-
ReadLine();
155+
int status = -1;
156+
string previousPrint = null;
140157

141-
foreach (KeyValuePair<string, Stream> keyValuePair in encryptedStreams)
158+
// While the process isn't complete and we haven't exceeded our abort time.
159+
while (status < 2 && DateTime.Now < abortTime)
160+
{
161+
// Get response.
162+
Dictionary<string, dynamic> response = API.GetSession(sessionGuid);
163+
164+
// Is there a status key?
165+
if (response.ContainsKey("Status"))
166+
{
167+
// Yes, get the status.
168+
status = response["Status"];
169+
}
170+
171+
// Is there a response key?
172+
if (response.ContainsKey("Response"))
173+
{
174+
// Yes, get the response.
175+
results = jsonSer.Deserialize<Dictionary<string, dynamic>>(response["Response"]);
176+
}
177+
178+
// As long as we have something.
179+
if (status != -1 && results != null)
180+
{
181+
// Get the installed and failed lists.
182+
ArrayList installed = results.ContainsKey("Installed") ? results["Installed"] : null;
183+
ArrayList failed = results.ContainsKey("Failed") ? results["Failed"] : null;
184+
185+
// Give some feedback on it, only if it's changed.
186+
string print = string.Format("\t{0} module archives processed, {0}/{1} succeeded.", installed.Count + failed.Count, installed.Count);
187+
188+
if (print != previousPrint)
189+
{
190+
WriteLine(print);
191+
previousPrint = print;
192+
}
193+
}
194+
195+
// Is finished?
196+
if (status == 2)
197+
{
198+
break;
199+
}
200+
201+
// Sleep.
202+
System.Threading.Thread.Sleep(interval);
203+
}
204+
}
205+
else
142206
{
143-
keyValuePair.Value.Dispose();
207+
// Get the installed and failed lists.
208+
ArrayList installed = results.ContainsKey("Installed") ? results["Installed"] : null;
209+
ArrayList failed = results.ContainsKey("Failed") ? results["Failed"] : null;
210+
211+
// Give some feedback on it.
212+
WriteLine(string.Format("\t{0} module archives processed, {0}/{1} succeeded.", installed.Count + failed.Count, installed.Count));
213+
144214
}
215+
216+
WriteLine(string.Format("Finished installation in {0} ms.", (DateTime.Now - installStartTime).TotalMilliseconds));
217+
ReadLine();
145218
}
146219
catch (Exception ex)
147220
{
148221
// Output exception message and stack trace.
149-
WriteLine(ex.Message);
150-
WriteLine(ex.StackTrace);
222+
WriteLine(string.Format("Exception caught at: {0}.", DateTime.Now.ToString()));
223+
WriteException(ex);
224+
151225
ReadLine();
152226
Environment.Exit((int)ExitCode.Error);
153227
}
154228
}
155229

230+
private static void WriteException(Exception ex, int maxDepth = 10, int depth = 0)
231+
{
232+
WriteLine(ex.Message);
233+
WriteLine(ex.StackTrace);
234+
235+
if (depth < maxDepth && ex.InnerException != null)
236+
{
237+
depth++;
238+
WriteException(ex.InnerException, maxDepth, depth);
239+
}
240+
}
241+
242+
private static void Write(string message)
243+
{
244+
if(IsSilent)
245+
{
246+
return;
247+
}
248+
249+
Console.Write(message);
250+
}
251+
156252
private static void WriteLine(string message = "")
157253
{
158254
if(IsSilent)

PolyDeploy/Components/CIDeploy.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class CIDeploy : Deployment
1515
{
1616
private APIUser APIUser { get; set; }
1717

18-
public CIDeploy(string ipAddress, string apiKey) : base(ipAddress)
18+
public CIDeploy(Session session, string ipAddress, string apiKey) : base(session, ipAddress)
1919
{
2020
// Retrieve our API user.
2121
APIUser = APIUserController.GetByAPIKey(apiKey);
@@ -27,16 +27,16 @@ public CIDeploy(string ipAddress, string apiKey) : base(ipAddress)
2727
}
2828
}
2929

30-
public void DecryptAndAddZip(Stream encryptedStream, string filename)
31-
{
32-
using (Stream ds = Crypto.Decrypt(encryptedStream, APIUser.EncryptionKey))
33-
{
34-
using (FileStream fs = File.Create(Path.Combine(IntakePath, filename)))
35-
{
36-
ds.CopyTo(fs);
37-
}
38-
}
39-
}
30+
//public void DecryptAndAddZip(Stream encryptedStream, string filename)
31+
//{
32+
// using (Stream ds = Crypto.Decrypt(encryptedStream, APIUser.EncryptionKey))
33+
// {
34+
// using (FileStream fs = File.Create(Path.Combine(IntakePath, filename)))
35+
// {
36+
// ds.CopyTo(fs);
37+
// }
38+
// }
39+
//}
4040

4141
protected override void LogAnyFailures(List<InstallJob> jobs)
4242
{

0 commit comments

Comments
 (0)