Skip to content

Commit 674f2b9

Browse files
committed
Pivot to sessions.
1 parent 1e32ada commit 674f2b9

15 files changed

+453
-82
lines changed

DeployClient/API.cs

+57-10
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,63 @@ private static HttpClient BuildClient()
2424
return client;
2525
}
2626

27-
public static Dictionary<string, dynamic> CIInstall(List<KeyValuePair<string, Stream>> streams)
27+
public static string CreateSession()
2828
{
29-
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 void AddPackages(string session, List<KeyValuePair<string, Stream>> streams)
51+
{
52+
string endpoint = string.Format("CI/AddPackages?session={0}", session);
53+
54+
using (HttpClient client = BuildClient())
55+
{
56+
MultipartFormDataContent form = new MultipartFormDataContent();
57+
58+
foreach (KeyValuePair<string, Stream> keyValuePair in streams)
59+
{
60+
form.Add(new StreamContent(keyValuePair.Value), "none", keyValuePair.Key);
61+
}
62+
63+
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
64+
}
65+
}
66+
67+
public static void AddPackageAsync(string session, Stream stream, string filename)
68+
{
69+
string endpoint = string.Format("CI/AddPackages?session={0}", session);
70+
71+
using (HttpClient client = BuildClient())
72+
{
73+
MultipartFormDataContent form = new MultipartFormDataContent();
74+
75+
form.Add(new StreamContent(stream), "none", filename);
76+
77+
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
78+
}
79+
}
80+
81+
public static Dictionary<string, dynamic> Install(string session)
82+
{
83+
string endpoint = string.Format("CI/Install?session={0}", session);
3084

3185
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
3286

@@ -36,14 +90,7 @@ public static Dictionary<string, dynamic> CIInstall(List<KeyValuePair<string, St
3690
{
3791
try
3892
{
39-
MultipartFormDataContent form = new MultipartFormDataContent();
40-
41-
foreach (KeyValuePair<string, Stream> keyValuePair in streams)
42-
{
43-
form.Add(new StreamContent(keyValuePair.Value), "none", keyValuePair.Key);
44-
}
45-
46-
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
93+
HttpResponseMessage response = client.GetAsync(endpoint).Result;
4794

4895
Console.WriteLine(response.RequestMessage.RequestUri);
4996

DeployClient/Program.cs

+39-12
Original file line numberDiff line numberDiff line change
@@ -107,45 +107,62 @@ static void Main(string[] args)
107107
}
108108

109109
// Inform user of encryption.
110-
WriteLine("Starting encryption...");
110+
WriteLine("Starting encryption and upload...");
111111

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

114119
foreach (string zipFile in zipFiles)
115120
{
121+
116122
using (FileStream fs = new FileStream(zipFile, FileMode.Open))
117123
{
118-
encryptedStreams.Add(new KeyValuePair<string, Stream>(Path.GetFileName(zipFile), Crypto.Encrypt(fs, Properties.Settings.Default.EncryptionKey)));
124+
Write(string.Format("\t{0} encrypting...", Path.GetFileName(zipFile)));
125+
126+
using (Stream es = Crypto.Encrypt(fs, Properties.Settings.Default.EncryptionKey))
127+
{
128+
Write("uploading...");
129+
130+
API.AddPackageAsync(session, es, Path.GetFileName(zipFile));
131+
}
132+
133+
WriteLine("done.");
119134
}
120-
WriteLine(string.Format("\tEncrypting {0}", Path.GetFileName(zipFile)));
121135
}
136+
137+
WriteLine(string.Format("Finished encryption and upload in {0} ms.", (DateTime.Now - startTime).TotalMilliseconds));
122138
WriteLine();
123139

124-
Dictionary<string, dynamic> results = API.CIInstall(encryptedStreams);
140+
WriteLine("Starting installation...");
141+
142+
DateTime installStartTime = DateTime.Now;
143+
144+
Dictionary<string, dynamic> results = API.Install(session);
125145

126146
ArrayList installed = results.ContainsKey("Installed") ? results["Installed"] : null;
127147
ArrayList failed = results.ContainsKey("Failed") ? results["Failed"] : null;
128148

129149
// Any failures?
130150
if (failed.Count > 0)
131151
{
132-
WriteLine(string.Format("{0}/{1} module archives failed to install.", failed.Count, encryptedStreams.Count));
152+
WriteLine(string.Format("{0} module archives failed to install.", failed.Count));
133153
ReadLine();
134154
Environment.Exit((int)ExitCode.InstallFailure);
135155
}
136156

137157
// Output result
138-
WriteLine(string.Format("{0}/{1} module archives installed successfully.", installed.Count, encryptedStreams.Count));
158+
WriteLine(string.Format("{0} module archives installed successfully.", installed.Count));
139159
ReadLine();
140-
141-
foreach (KeyValuePair<string, Stream> keyValuePair in encryptedStreams)
142-
{
143-
keyValuePair.Value.Dispose();
144-
}
160+
WriteLine(string.Format("Finished installation in {0} ms.", (DateTime.Now - installStartTime).TotalMilliseconds));
145161
}
146162
catch (Exception ex)
147163
{
148164
// Output exception message and stack trace.
165+
WriteLine(string.Format("Exception caught at: {0}.", DateTime.Now.ToString()));
149166
WriteException(ex);
150167

151168
ReadLine();
@@ -165,6 +182,16 @@ private static void WriteException(Exception ex, int maxDepth = 10, int depth =
165182
}
166183
}
167184

185+
private static void Write(string message)
186+
{
187+
if(IsSilent)
188+
{
189+
return;
190+
}
191+
192+
Console.Write(message);
193+
}
194+
168195
private static void WriteLine(string message = "")
169196
{
170197
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(string sessionPath, string ipAddress, string apiKey) : base(sessionPath, 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
{

PolyDeploy/Components/Deployment.cs

+28-28
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,50 @@ namespace Cantarus.Modules.PolyDeploy.Components
77
{
88
internal class Deployment
99
{
10-
protected string IntakePath
11-
{
12-
get
13-
{
14-
return Path.Combine(WorkingPath, "intake");
15-
}
16-
}
17-
18-
protected string ModulesPath
19-
{
20-
get
21-
{
22-
return Path.Combine(WorkingPath, "modules");
23-
}
24-
}
10+
//protected string IntakePath
11+
//{
12+
// get
13+
// {
14+
// return Path.Combine(WorkingPath, "intake");
15+
// }
16+
//}
17+
18+
//protected string ModulesPath
19+
//{
20+
// get
21+
// {
22+
// return Path.Combine(WorkingPath, "modules");
23+
// }
24+
//}
2525

2626
protected string TempPath
2727
{
2828
get
2929
{
30-
return Path.Combine(WorkingPath, "temp");
30+
return Path.Combine(SessionPath, "temp");
3131
}
3232
}
3333

3434
protected string IPAddress { get; set; }
35-
protected string WorkingPath { get; set; }
35+
protected string SessionPath { get; set; }
3636
protected List<string> PackageZips { get; set; }
3737

38-
public Deployment(string ipAddress)
38+
public Deployment(string sessionPath, string ipAddress)
3939
{
4040
// Store ip address for logging later.
4141
IPAddress = ipAddress;
4242

43-
// Generate a temporary directory.
44-
WorkingPath = Utilities.AvailableDirectory();
43+
// store the session path.
44+
SessionPath = sessionPath;
4545

46-
// Create working directory if it doesn't exist.
47-
CreateDirectoryIfNotExist(WorkingPath);
46+
//// Create working directory if it doesn't exist.
47+
//CreateDirectoryIfNotExist(WorkingPath);
4848

49-
// Create the intake directory if it doesn't exist.
50-
CreateDirectoryIfNotExist(IntakePath);
49+
//// Create the intake directory if it doesn't exist.
50+
//CreateDirectoryIfNotExist(IntakePath);
5151

52-
// Create the modules directory if it doesn't exist.
53-
CreateDirectoryIfNotExist(ModulesPath);
52+
//// Create the modules directory if it doesn't exist.
53+
//CreateDirectoryIfNotExist(ModulesPath);
5454

5555
// Create the temporary directory if it doesn't exist.
5656
CreateDirectoryIfNotExist(TempPath);
@@ -201,7 +201,7 @@ private InstallJob FindInstallJobWithPackage(string name, List<InstallJob> insta
201201

202202
protected List<string> IdentifyPackages()
203203
{
204-
return IdentifyPackagesInDirectory(IntakePath);
204+
return IdentifyPackagesInDirectory(SessionPath);
205205
}
206206

207207
protected List<string> IdentifyPackagesInDirectory(string directoryPath)
@@ -225,7 +225,7 @@ protected List<string> IdentifyPackagesInDirectory(string directoryPath)
225225
// Does it have other zips?
226226
if (ZipHasOtherZip(testPath))
227227
{
228-
string tempPath = Utilities.AvailableDirectory(TempPath);
228+
string tempPath = Utilities.AvailableTempDirectory(TempPath);
229229

230230
CreateDirectoryIfNotExist(tempPath);
231231

0 commit comments

Comments
 (0)