|
4 | 4 | using System.Collections.Generic;
|
5 | 5 | using System.Configuration;
|
6 | 6 | using System.IO;
|
| 7 | +using System.Web.Script.Serialization; |
7 | 8 |
|
8 | 9 | namespace DeployClient
|
9 | 10 | {
|
@@ -107,52 +108,147 @@ static void Main(string[] args)
|
107 | 108 | }
|
108 | 109 |
|
109 | 110 | // Inform user of encryption.
|
110 |
| - WriteLine("Starting encryption..."); |
| 111 | + WriteLine("Starting encryption and upload..."); |
111 | 112 |
|
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; |
113 | 119 |
|
114 | 120 | foreach (string zipFile in zipFiles)
|
115 | 121 | {
|
| 122 | + |
116 | 123 | using (FileStream fs = new FileStream(zipFile, FileMode.Open))
|
117 | 124 | {
|
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."); |
119 | 136 | }
|
120 |
| - WriteLine(string.Format("\tEncrypting {0}", Path.GetFileName(zipFile))); |
121 | 137 | }
|
| 138 | + |
| 139 | + WriteLine(string.Format("Finished encryption and upload in {0} ms.", (DateTime.Now - startTime).TotalMilliseconds)); |
122 | 140 | WriteLine();
|
123 | 141 |
|
124 |
| - Dictionary<string, dynamic> results = API.CIInstall(encryptedStreams).Result; |
| 142 | + WriteLine("Starting installation..."); |
| 143 | + |
| 144 | + DateTime installStartTime = DateTime.Now; |
| 145 | + JavaScriptSerializer jsonSer = new JavaScriptSerializer(); |
125 | 146 |
|
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; |
128 | 149 |
|
129 |
| - // Any failures? |
130 |
| - if (failed.Count > 0) |
| 150 | + if (!API.Install(sessionGuid, out results)) |
131 | 151 | {
|
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); |
136 | 154 |
|
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; |
140 | 157 |
|
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 |
142 | 206 | {
|
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 | + |
144 | 214 | }
|
| 215 | + |
| 216 | + WriteLine(string.Format("Finished installation in {0} ms.", (DateTime.Now - installStartTime).TotalMilliseconds)); |
| 217 | + ReadLine(); |
145 | 218 | }
|
146 | 219 | catch (Exception ex)
|
147 | 220 | {
|
148 | 221 | // 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 | + |
151 | 225 | ReadLine();
|
152 | 226 | Environment.Exit((int)ExitCode.Error);
|
153 | 227 | }
|
154 | 228 | }
|
155 | 229 |
|
| 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 | + |
156 | 252 | private static void WriteLine(string message = "")
|
157 | 253 | {
|
158 | 254 | if(IsSilent)
|
|
0 commit comments