1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Net;
|
---|
27 | using System.Threading;
|
---|
28 | using System.Xml.Linq;
|
---|
29 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Clients.Hive.CloudManager.Azure {
|
---|
32 | public static class ServiceManagementOperation {
|
---|
33 |
|
---|
34 | #region Namespaces
|
---|
35 |
|
---|
36 | private static XNamespace wa = Constants.NSWindowsAzure;
|
---|
37 | private static XNamespace sc = Constants.NSSchemaMicrosoft;
|
---|
38 | private static XNamespace sh = Constants.NSServiceConfiguration;
|
---|
39 |
|
---|
40 | #endregion
|
---|
41 |
|
---|
42 | #region Public Methods
|
---|
43 |
|
---|
44 | public static Subscription GetSubscriptionInfo(string subscriptionId, string thumbprint) {
|
---|
45 | string uri = string.Format(Constants.URISubscriptionFormat, subscriptionId);
|
---|
46 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
47 | XDocument response = operation.Invoke(uri);
|
---|
48 |
|
---|
49 | Subscription subscription = new Subscription();
|
---|
50 | subscription.SubscriptionID = response.Descendants(wa + "SubscriptionID").Single().Value;
|
---|
51 | subscription.SubscriptionName = response.Descendants(wa + "SubscriptionName").Single().Value;
|
---|
52 | subscription.SubscriptionStatus = response.Descendants(wa + "SubscriptionStatus").Single().Value;
|
---|
53 | subscription.MaxCoreCount = Convert.ToInt32(response.Descendants(wa + "MaxCoreCount").Single().Value);
|
---|
54 | subscription.MaxHostedServices = Convert.ToInt32(response.Descendants(wa + "MaxHostedServices").Single().Value);
|
---|
55 | subscription.MaxStorageAccounts = Convert.ToInt32(response.Descendants(wa + "MaxStorageAccounts").Single().Value);
|
---|
56 | subscription.CurrentCoreCount = Convert.ToInt32(response.Descendants(wa + "CurrentCoreCount").Single().Value);
|
---|
57 | subscription.CurrentHostedServices = Convert.ToInt32(response.Descendants(wa + "CurrentHostedServices").Single().Value);
|
---|
58 | subscription.CurrentStorageAccounts = Convert.ToInt32(response.Descendants(wa + "CurrentStorageAccounts").Single().Value);
|
---|
59 | subscription.CertificateThumbprint = thumbprint;
|
---|
60 |
|
---|
61 | return subscription;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static List<string> ListLocations(string subscriptionId, string thumbprint) {
|
---|
65 | string uri = string.Format(Constants.URILocationsFormat, subscriptionId);
|
---|
66 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
67 | XDocument response = operation.Invoke(uri);
|
---|
68 | List<string> result = null;
|
---|
69 | if (response.Root.HasElements) {
|
---|
70 | result = (from s in response.Descendants(wa + "Name")
|
---|
71 | select s.Value).ToList();
|
---|
72 | }
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static List<AffinityGroup> ListAffinityGroups(string subscriptionId, string thumbprint) {
|
---|
77 | string uri = String.Format(Constants.URIAffinityGroupFormat, subscriptionId);
|
---|
78 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
79 | XDocument response = operation.Invoke(uri);
|
---|
80 | List<AffinityGroup> result = null;
|
---|
81 | if (response.Root.HasElements) {
|
---|
82 | result = (from s in response.Elements()
|
---|
83 | select new AffinityGroup() {
|
---|
84 | Name = s.Descendants(wa + "Name").Single().Value,
|
---|
85 | Label = Utils.ConvertFromBase64String(s.Descendants(wa + "Label").Single().Value),
|
---|
86 | Description = s.Descendants(wa + "Description").Single().Value,
|
---|
87 | Location = s.Descendants(wa + "Location").Single().Value
|
---|
88 | }).ToList();
|
---|
89 | }
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static List<HostedService> ListHostedServices(string subscriptionId, string thumbprint) {
|
---|
94 | string uri = String.Format(Constants.URIHostedServiceFormat, subscriptionId);
|
---|
95 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
96 | XDocument response = operation.Invoke(uri);
|
---|
97 | List<HostedService> result = null;
|
---|
98 | if (response.Root.HasElements) {
|
---|
99 |
|
---|
100 | result = (from s in response.Elements()
|
---|
101 | select new HostedService() {
|
---|
102 | ServiceName = s.Descendants(wa + "ServiceName").Single().Value,
|
---|
103 | Url = s.Descendants(wa + "Url").Single().Value
|
---|
104 | }).ToList();
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (result != null)
|
---|
108 | return result;
|
---|
109 | else
|
---|
110 | return new List<HostedService>();
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static List<StorageService> ListStorageServices(string subscriptionId, string thumbprint) {
|
---|
114 | string uri = string.Format(Constants.URIStorageServiceFormat, subscriptionId);
|
---|
115 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
116 | XDocument response = operation.Invoke(uri);
|
---|
117 | List<StorageService> result = null;
|
---|
118 | if (response.Root.HasElements) {
|
---|
119 | result = (from s in response.Elements()
|
---|
120 | select new StorageService() {
|
---|
121 | Url = s.Descendants(wa + "Url").Single().Value,
|
---|
122 | ServiceName = s.Descendants(wa + "ServiceName").Single().Value
|
---|
123 | }).ToList();
|
---|
124 | }
|
---|
125 | if (result != null)
|
---|
126 | return result;
|
---|
127 | else
|
---|
128 | return new List<StorageService>();
|
---|
129 | }
|
---|
130 |
|
---|
131 | public static HostedService GetHostedService(string subscriptionId, string thumbprint, string serviceName) {
|
---|
132 | string uri = String.Format(Constants.URISpecificHostedServiceFormat, subscriptionId, serviceName);
|
---|
133 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
134 | XDocument response = operation.Invoke(uri);
|
---|
135 | HostedService service = new HostedService();
|
---|
136 | service.ServiceName = response.Descendants(wa + "ServiceName").Single().Value;
|
---|
137 | service.Url = response.Descendants(wa + "Url").Single().Value;
|
---|
138 | return service;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public static HostedService GetHostedServiceDetailed(string subscriptionId, string thumbprint, string serviceName) {
|
---|
142 | string uri = String.Format(Constants.URISpecificHostedServiceFormat + "?embed-detail=true", subscriptionId, serviceName);
|
---|
143 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
144 | XDocument response = operation.Invoke(uri);
|
---|
145 | HostedService service = new HostedService();
|
---|
146 | service.ServiceName = response.Descendants(wa + "ServiceName").Single().Value;
|
---|
147 | service.Url = response.Descendants(wa + "Url").First().Value;
|
---|
148 | if (response.Root.Elements(wa + "HostedServiceProperties").Any()) {
|
---|
149 | XElement xHostedServiceProperties = response.Descendants(wa + "HostedServiceProperties").Single();
|
---|
150 | string loc = string.Empty, affgroup = string.Empty;
|
---|
151 | if (xHostedServiceProperties.HasElements) {
|
---|
152 | string desc = xHostedServiceProperties.Descendants(wa + "Description").Single().Value;
|
---|
153 | if (xHostedServiceProperties.Descendants(wa + "Location").Any()) {
|
---|
154 | loc = xHostedServiceProperties.Descendants(wa + "Location").Single().Value;
|
---|
155 | } else {
|
---|
156 | affgroup = xHostedServiceProperties.Descendants(wa + "AffinityGroup").Single().Value;
|
---|
157 | }
|
---|
158 | string lbl = Utils.ConvertFromBase64String(xHostedServiceProperties.Descendants(wa + "Label").Single().Value);
|
---|
159 | HostedServiceProperties props = new HostedServiceProperties() {
|
---|
160 | Description = desc,
|
---|
161 | Location = loc,
|
---|
162 | AffinityGroup = affgroup,
|
---|
163 | Label = lbl
|
---|
164 | };
|
---|
165 | service.HostedServiceProperties = props;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (response.Root.Elements(wa + "Deployments").Any()) {
|
---|
170 | XElement xDeployments = response.Descendants(wa + "Deployments").Single();
|
---|
171 | if (xDeployments.HasElements) {
|
---|
172 | IEnumerable<Deployment> deps = (from s in xDeployments.Elements()
|
---|
173 | select GetDeploymentFromXml(s)).ToList();
|
---|
174 | service.Deployments = new List<Deployment>(deps);
|
---|
175 | } else {
|
---|
176 | service.Deployments = new List<Deployment>();
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | return service;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public static Deployment GetDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
|
---|
184 | string uri = String.Format(Constants.URISpecificDeploymentFormat, subscriptionId, serviceName, deploymentName);
|
---|
185 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
186 | XDocument response = operation.Invoke(uri);
|
---|
187 | Deployment deployment = GetDeploymentFromXml(response.Root);
|
---|
188 | return deployment;
|
---|
189 | }
|
---|
190 |
|
---|
191 | public static StorageServiceKeys GetStorageKeys(string subscriptionId, string thumbprint, string serviceName) {
|
---|
192 | string uri = string.Format(Constants.URIStorageServiceKeys, subscriptionId, serviceName);
|
---|
193 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
194 | XDocument response = operation.Invoke(uri);
|
---|
195 | StorageServiceKeys keys = new StorageServiceKeys();
|
---|
196 | keys.Primary = response.Descendants(wa + "Primary").Single().Value;
|
---|
197 | keys.Secondary = response.Descendants(wa + "Secondary").Single().Value;
|
---|
198 | return keys;
|
---|
199 | }
|
---|
200 |
|
---|
201 | public static string AddCertificate(string subscriptionId, string thumbprint, string serviceName, string certificateFilePath, string password) {
|
---|
202 | string uri = String.Format(Constants.URICertificateFormat, subscriptionId, serviceName);
|
---|
203 | XDocument payload = CreateAddCertificatePayload(certificateFilePath, password);
|
---|
204 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
205 | string requestId = operation.Invoke(uri, payload);
|
---|
206 | return requestId;
|
---|
207 | }
|
---|
208 |
|
---|
209 | public static string CreateHostedServiceWithLocation(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location) {
|
---|
210 | return CreateHostedService(subscriptionId, thumbprint, serviceName, label, description, location, String.Empty);
|
---|
211 | }
|
---|
212 |
|
---|
213 | public static string CreateHostedServiceWithAffinityGroup(string subscriptionId, string thumbprint, string serviceName, string label, string description, string affinityGroup) {
|
---|
214 | return CreateHostedService(subscriptionId, thumbprint, serviceName, label, description, String.Empty, affinityGroup);
|
---|
215 | }
|
---|
216 |
|
---|
217 | public static string CreateDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName, string deploymentSlot, string packageUrl, string configuration, string label, int instanceCount) {
|
---|
218 | string uri = String.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
|
---|
219 | XElement xConfig = XElement.Parse(configuration);
|
---|
220 | SetInstanceCount(xConfig, Constants.HLSlaveRoleName, instanceCount);
|
---|
221 |
|
---|
222 | XDocument payload = CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
|
---|
223 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
224 | string requestId = operation.Invoke(uri, payload);
|
---|
225 | return requestId;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public static string DownloadBlobAsString(Uri uri) {
|
---|
229 | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
|
---|
230 | request.Headers.Add(Constants.HeaderVersionName, Constants.APIVersion20110818);
|
---|
231 | request.Method = Constants.HttpMethodGET;
|
---|
232 | request.ContentType = Constants.ContentTypeAppXml;
|
---|
233 | string result = string.Empty;
|
---|
234 | WebResponse response = request.GetResponse();
|
---|
235 | using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
|
---|
236 | result = reader.ReadToEnd();
|
---|
237 | }
|
---|
238 | return result;
|
---|
239 | }
|
---|
240 |
|
---|
241 | public static List<HostedService> DiscoverSlaveService(string subscriptionId, string thumbprint) {
|
---|
242 | List<HostedService> hostedServices = new List<HostedService>();
|
---|
243 | foreach (HostedService hostedService in ListHostedServices(subscriptionId, thumbprint)) {
|
---|
244 | HostedService hs = GetHostedServiceDetailed(subscriptionId, thumbprint, hostedService.ServiceName);
|
---|
245 | foreach (Deployment deployment in hs.Deployments) {
|
---|
246 | if (ContainsRoleNameInConfiguration(XDocument.Parse(deployment.Configuration), Constants.HLSlaveRoleName)) {
|
---|
247 | if (!hostedServices.Contains(hs)) {
|
---|
248 | hostedServices.Add(hs);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return hostedServices;
|
---|
254 | }
|
---|
255 |
|
---|
256 | public static string DeleteHostedService(string subscriptionId, string thumbprint, string serviceName) {
|
---|
257 | string uri = String.Format(Constants.URISpecificHostedServiceFormat, subscriptionId, serviceName);
|
---|
258 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
259 | string requestId = operation.Invoke(uri, Constants.HttpMethodDELETE);
|
---|
260 | return requestId;
|
---|
261 | }
|
---|
262 |
|
---|
263 | public static string DeleteDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
|
---|
264 | string uri = string.Format(Constants.URISpecificDeploymentFormat, subscriptionId, serviceName, deploymentName);
|
---|
265 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
266 | string requestId = operation.Invoke(uri, Constants.HttpMethodDELETE);
|
---|
267 | return requestId;
|
---|
268 | }
|
---|
269 |
|
---|
270 | public static string ChangeInstanceCount(string subscriptionId, string thumbprint, string serviceName, string deploymentSlot, string roleName, int instanceCount) {
|
---|
271 | string requestId = string.Empty;
|
---|
272 | XElement configuration = LoadConfiguration(subscriptionId, thumbprint, serviceName, deploymentSlot);
|
---|
273 | if (instanceCount != GetInstanceCount(configuration, roleName)) {
|
---|
274 | SetInstanceCount(configuration, roleName, instanceCount);
|
---|
275 | requestId = SaveConfiguration(subscriptionId, thumbprint, serviceName, deploymentSlot, configuration);
|
---|
276 | }
|
---|
277 | return requestId;
|
---|
278 | }
|
---|
279 |
|
---|
280 | public static string GetOperationStatusString(string subscriptionId, string thumbprint, string requestId) {
|
---|
281 | string status = GetOperationStatus(subscriptionId, thumbprint, requestId).Element(wa + "Status").Value;
|
---|
282 | return status;
|
---|
283 | }
|
---|
284 |
|
---|
285 | public static XElement GetOperationStatus(string subscriptionId, string thumbprint, string requestId) {
|
---|
286 | string uri = string.Format(Constants.URIGetOperationStatusFormat, subscriptionId, requestId);
|
---|
287 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
288 | XDocument operationStatus = operation.Invoke(uri);
|
---|
289 | return operationStatus.Element(wa + "Operation");
|
---|
290 | }
|
---|
291 |
|
---|
292 | public static Constants.OperationResult PollGetOperationStatus(string subscriptionId, string thumbprint, string requestId, int pollIntervallSeconds, int timeoutSeconds) {
|
---|
293 | Constants.OperationResult result = new Constants.OperationResult();
|
---|
294 | DateTime beginPollTime = DateTime.UtcNow;
|
---|
295 | TimeSpan pollInterval = new TimeSpan(0, 0, pollIntervallSeconds);
|
---|
296 | DateTime endPollTime = beginPollTime + new TimeSpan(0, 0, timeoutSeconds);
|
---|
297 |
|
---|
298 | bool done = false;
|
---|
299 | while (!done) {
|
---|
300 | XElement operationStatus = GetOperationStatus(subscriptionId, thumbprint, requestId);
|
---|
301 | result.RunningTime = DateTime.UtcNow - beginPollTime;
|
---|
302 | try {
|
---|
303 | result.Status = (Constants.OperationStatus)Enum.Parse(typeof(Constants.OperationStatus), operationStatus.Element(wa + "Status").Value);
|
---|
304 | }
|
---|
305 | catch (Exception ex) {
|
---|
306 | throw new ApplicationException(string.Format("Get Operation Status {0} returned unexpected status: {1}{2}", requestId, Environment.NewLine, operationStatus.ToString(SaveOptions.OmitDuplicateNamespaces)));
|
---|
307 | }
|
---|
308 |
|
---|
309 | switch (result.Status) {
|
---|
310 | case Constants.OperationStatus.InProgress:
|
---|
311 | Thread.Sleep((int)pollInterval.TotalMilliseconds);
|
---|
312 | break;
|
---|
313 | case Constants.OperationStatus.Failed:
|
---|
314 | result.StatusCode = (HttpStatusCode)Convert.ToInt32(operationStatus.Element(wa + "HttpStatusCode").Value);
|
---|
315 | XElement error = operationStatus.Element(wa + "Error");
|
---|
316 | result.Code = error.Element(wa + "Code").Value;
|
---|
317 | result.Message = error.Element(wa + "Message").Value;
|
---|
318 | done = true;
|
---|
319 | break;
|
---|
320 | case Constants.OperationStatus.Succeeded:
|
---|
321 | result.StatusCode = (HttpStatusCode)Convert.ToInt32(operationStatus.Element(wa + "HttpStatusCode").Value);
|
---|
322 | done = true;
|
---|
323 | break;
|
---|
324 | }
|
---|
325 |
|
---|
326 | if (!done && DateTime.UtcNow > endPollTime) {
|
---|
327 | result.Status = Constants.OperationStatus.TimedOut;
|
---|
328 | done = true;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | return result;
|
---|
332 | }
|
---|
333 |
|
---|
334 | #endregion
|
---|
335 |
|
---|
336 | #region Private Methods
|
---|
337 |
|
---|
338 | private static int GetInstanceCount(XElement configuration, string roleName) {
|
---|
339 | XElement instanceElement = (from s in configuration.Elements(sh + "Role")
|
---|
340 | where s.Attribute("name").Value == roleName
|
---|
341 | select s.Element(sh + "Instances")).First();
|
---|
342 | int instanceCount = Convert.ToInt32(instanceElement.Attribute("count").Value);
|
---|
343 | return instanceCount;
|
---|
344 | }
|
---|
345 |
|
---|
346 | private static void SetInstanceCount(XElement configuration, string roleName, int value) {
|
---|
347 | XElement instanceElement = (from s in configuration.Elements(sh + "Role")
|
---|
348 | where s.Attribute("name").Value == roleName
|
---|
349 | select s.Element(sh + "Instances")).First();
|
---|
350 | instanceElement.SetAttributeValue("count", value);
|
---|
351 | }
|
---|
352 |
|
---|
353 | private static string CreateHostedService(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location, string affinityGroup) {
|
---|
354 | string uri = string.Format(Constants.URIHostedServiceFormat, subscriptionId);
|
---|
355 | XDocument payload = CreateHostedServicePayload(serviceName, label, description, location, affinityGroup);
|
---|
356 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
357 | string requestId = operation.Invoke(uri, payload);
|
---|
358 | return requestId;
|
---|
359 | }
|
---|
360 |
|
---|
361 | private static XDocument CreateHostedServicePayload(string serviceName, string label, string description, string location, string affinityGroup) {
|
---|
362 | string base64LabelName = Utils.ConvertToBase64String(label);
|
---|
363 | XElement xServiceName = new XElement(wa + "ServiceName", serviceName);
|
---|
364 | XElement xLabel = new XElement(wa + "Label", base64LabelName);
|
---|
365 | XElement xDescription = new XElement(wa + "Description", description);
|
---|
366 | XElement createHostedService = new XElement(wa + "CreateHostedService");
|
---|
367 | createHostedService.Add(xServiceName);
|
---|
368 | createHostedService.Add(xLabel);
|
---|
369 | createHostedService.Add(xDescription);
|
---|
370 |
|
---|
371 | if (location != String.Empty) {
|
---|
372 | XElement xLocation = new XElement(wa + "Location", location);
|
---|
373 | createHostedService.Add(xLocation);
|
---|
374 | } else {
|
---|
375 | XElement xAffinityGroup = new XElement(wa + "AffinityGroup", affinityGroup);
|
---|
376 | createHostedService.Add(xAffinityGroup);
|
---|
377 | }
|
---|
378 |
|
---|
379 | XDocument payload = new XDocument();
|
---|
380 | payload.Add(createHostedService);
|
---|
381 | payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
|
---|
382 |
|
---|
383 | return payload;
|
---|
384 | }
|
---|
385 |
|
---|
386 | private static XDocument CreateAddCertificatePayload(string certificateFilePath, string password) {
|
---|
387 | XElement xData = new XElement(wa + "Data", Convert.ToBase64String(File.ReadAllBytes(certificateFilePath)));
|
---|
388 | XElement xCertificateFormat = new XElement(wa + "CertificateFormat", "pfx");
|
---|
389 | XElement xPassword = new XElement(wa + "Password", password);
|
---|
390 |
|
---|
391 | XElement addcert = new XElement(wa + "CertificateFile");
|
---|
392 | addcert.Add(xData);
|
---|
393 | addcert.Add(xCertificateFormat);
|
---|
394 | addcert.Add(xPassword);
|
---|
395 |
|
---|
396 | XDocument payload = new XDocument();
|
---|
397 | payload.Add(addcert);
|
---|
398 | payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
|
---|
399 |
|
---|
400 | return payload;
|
---|
401 | }
|
---|
402 |
|
---|
403 | private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, string pathToConfigurationFile, string label) {
|
---|
404 | string configurationFile = File.ReadAllText(pathToConfigurationFile);
|
---|
405 | XElement xConfig = XElement.Parse(configurationFile);
|
---|
406 | return CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
|
---|
407 | }
|
---|
408 |
|
---|
409 | private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, XElement configuration, string label) {
|
---|
410 | string base64ConfigurationFile = Utils.ConvertToBase64String(configuration.ToString());
|
---|
411 | string base64Label = Utils.ConvertToBase64String(label);
|
---|
412 |
|
---|
413 | XElement xName = new XElement(wa + "Name", deploymentName);
|
---|
414 | XElement xPackageUrl = new XElement(wa + "PackageUrl", packageUrl);
|
---|
415 | XElement xLabel = new XElement(wa + "Label", base64Label);
|
---|
416 | XElement xConfiguration = new XElement(wa + "Configuration", base64ConfigurationFile);
|
---|
417 | XElement xStartDeployment = new XElement(wa + "StartDeployment", "true");
|
---|
418 | XElement xTreatWarningsAsError = new XElement(wa + "TreatWarningsAsError", "false");
|
---|
419 |
|
---|
420 | XElement createDeployment = new XElement(wa + "CreateDeployment");
|
---|
421 | createDeployment.Add(xName);
|
---|
422 | createDeployment.Add(xPackageUrl);
|
---|
423 | createDeployment.Add(xLabel);
|
---|
424 | createDeployment.Add(xConfiguration);
|
---|
425 | createDeployment.Add(xStartDeployment);
|
---|
426 | createDeployment.Add(xTreatWarningsAsError);
|
---|
427 |
|
---|
428 | XDocument payload = new XDocument();
|
---|
429 | payload.Add(createDeployment);
|
---|
430 | payload.Declaration =
|
---|
431 | new XDeclaration("1.0", "UTF-8", "no");
|
---|
432 | return payload;
|
---|
433 | }
|
---|
434 |
|
---|
435 | private static XElement LoadConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot) {
|
---|
436 | string uri = string.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
|
---|
437 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
438 | XDocument deployment = operation.Invoke(uri);
|
---|
439 | string base64Configuration = deployment.Element(wa + "Deployment").Element(wa + "Configuration").Value;
|
---|
440 | string stringConfiguration = Utils.ConvertFromBase64String(base64Configuration);
|
---|
441 | XElement configuration = XElement.Parse(stringConfiguration);
|
---|
442 | return configuration;
|
---|
443 | }
|
---|
444 |
|
---|
445 | private static string SaveConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot, XElement configuration) {
|
---|
446 | string uri = string.Format(Constants.URIDeploymentConfigurationFormat, subscriptionId, serviceName, deploymentSlot);
|
---|
447 | XDocument payload = CreateConfigurationPayload(configuration);
|
---|
448 | ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
|
---|
449 | string requestId = operation.Invoke(uri, payload);
|
---|
450 | return requestId;
|
---|
451 | }
|
---|
452 |
|
---|
453 | private static XDocument CreateConfigurationPayload(XElement configuration) {
|
---|
454 | string configurationString = configuration.ToString();
|
---|
455 | string base64Configuration = Utils.ConvertToBase64String(configurationString);
|
---|
456 |
|
---|
457 | XElement xConfiguration = new XElement(wa + "Configuration", base64Configuration);
|
---|
458 | XElement xChangeConfiguration = new XElement(wa + "ChangeConfiguration", xConfiguration);
|
---|
459 |
|
---|
460 | XDocument payload = new XDocument();
|
---|
461 | payload.Add(xChangeConfiguration);
|
---|
462 | payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
|
---|
463 |
|
---|
464 | return payload;
|
---|
465 | }
|
---|
466 |
|
---|
467 | private static XDocument GetConfigurationFromDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
|
---|
468 | Deployment deployment = GetDeployment(subscriptionId, thumbprint, serviceName, deploymentName);
|
---|
469 | XDocument config = XDocument.Parse(deployment.Configuration);
|
---|
470 | return config;
|
---|
471 | }
|
---|
472 |
|
---|
473 | private static bool ContainsRoleNameInConfiguration(XDocument configuration, string roleName) {
|
---|
474 | if (configuration.Root.Name != sh + "ServiceConfiguration")
|
---|
475 | throw new ArgumentException("No valid configuration", "configuration");
|
---|
476 |
|
---|
477 |
|
---|
478 | IEnumerable<XElement> role = (from s in configuration.Root.Elements(sh + "Role")
|
---|
479 | where s.Attribute("name").Value == roleName
|
---|
480 | select s);
|
---|
481 | return role.Count() > 0;
|
---|
482 | }
|
---|
483 |
|
---|
484 | private static Deployment GetDeploymentFromXml(XElement xDeployment) {
|
---|
485 | Deployment deployment = new Deployment();
|
---|
486 | deployment.Name = xDeployment.Descendants(wa + "Name").Single().Value;
|
---|
487 | deployment.DeploymentSlot = xDeployment.Descendants(wa + "DeploymentSlot").Single().Value;
|
---|
488 | deployment.PrivateID = xDeployment.Descendants(wa + "PrivateID").Single().Value;
|
---|
489 | deployment.Status = xDeployment.Descendants(wa + "Status").Single().Value;
|
---|
490 | deployment.Label = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Label").Single().Value);
|
---|
491 | if (xDeployment.Descendants(wa + "Url").Any()) {
|
---|
492 | deployment.Url = xDeployment.Descendants(wa + "Url").Single().Value;
|
---|
493 | }
|
---|
494 | deployment.Configuration = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Configuration").Single().Value);
|
---|
495 |
|
---|
496 | XElement xRoleInstanceList = xDeployment.Descendants(wa + "RoleInstanceList").Single();
|
---|
497 | if (xRoleInstanceList.HasElements) {
|
---|
498 | IEnumerable<RoleInstance> instances = (from s in xRoleInstanceList.Elements()
|
---|
499 | select new RoleInstance() {
|
---|
500 | RoleName = s.Descendants(wa + "RoleName").Single().Value,
|
---|
501 | InstanceName = s.Descendants(wa + "InstanceName").Single().Value,
|
---|
502 | InstanceStatus = s.Descendants(wa + "InstanceStatus").Single().Value,
|
---|
503 | InstanceSize = s.Descendants(wa + "InstanceSize").Single().Value,
|
---|
504 | InstanceStateDetails = s.Descendants(wa + "InstanceStateDetails").Single().Value
|
---|
505 | }).ToList();
|
---|
506 | deployment.RoleInstanceList = new List<RoleInstance>(instances);
|
---|
507 | }
|
---|
508 | if (xDeployment.Descendants(wa + "RoleList").Any()) {
|
---|
509 | XElement xRoleList = xDeployment.Descendants(wa + "RoleList").Single();
|
---|
510 | if (xRoleList.HasElements) {
|
---|
511 |
|
---|
512 | IEnumerable<Role> roles = (from s in xRoleList.Elements()
|
---|
513 | select new Role() {
|
---|
514 | RoleName = s.Descendants(wa + "RoleName").Single().Value,
|
---|
515 | OsVersion = s.Descendants(wa + "OsVersion").Single().Value
|
---|
516 | }).ToList();
|
---|
517 | deployment.RoleList = new List<Role>(roles);
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | if (xDeployment.Descendants(wa + "UpgradeDomainCount").Any()) {
|
---|
522 | deployment.UpgradeDomainCount = Convert.ToInt32(xDeployment.Descendants(wa + "UpgradeDomainCount").Single().Value);
|
---|
523 | }
|
---|
524 |
|
---|
525 | if (xDeployment.Elements("UpgradeStatus").Any()) {
|
---|
526 | XElement xUpgradeStatus = xDeployment.Descendants(wa + "UpgradeStatus").Single();
|
---|
527 | if (xUpgradeStatus.HasElements) {
|
---|
528 | UpgradeStatus upgradestatus = new UpgradeStatus() {
|
---|
529 | UpgradeType = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value,
|
---|
530 | CurrentUpgradeDomain = Convert.ToInt32(xUpgradeStatus.Descendants(wa + "RoleName").Single().Value),
|
---|
531 | CurrentUpgradeDomainState = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value
|
---|
532 | };
|
---|
533 | deployment.UpgradeStatus = upgradestatus;
|
---|
534 | }
|
---|
535 | }
|
---|
536 | return deployment;
|
---|
537 | }
|
---|
538 |
|
---|
539 | #endregion
|
---|
540 | }
|
---|
541 | }
|
---|