Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Azure/ServiceManagementOperation.cs @ 7374

Last change on this file since 7374 was 7374, checked in by spimming, 12 years ago

#1680:

  • Methods to create a deployment added
  • Download blob block from public blob container
  • Constants for deployment and storage api version added
File size: 21.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Net;
27using System.Xml.Linq;
28using HeuristicLab.Clients.Hive.CloudManager.Model;
29
30namespace HeuristicLab.Clients.Hive.CloudManager.Azure {
31  public static class ServiceManagementOperation {
32
33    #region Namespaces
34
35    private static XNamespace wa = Constants.NSWindowsAzure;
36    private static XNamespace sc = Constants.NSSchemaMicrosoft;
37    private static XNamespace sh = Constants.NSServiceConfiguration;
38
39    #endregion
40
41    #region Public Methods
42
43    public static Subscription GetSubscriptionInfo(string subscriptionId, string thumbprint) {
44      string uri = string.Format(Constants.URISubscriptionFormat, subscriptionId);
45      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
46      XDocument response = operation.Invoke(uri);
47
48      Subscription subscription = new Subscription();
49      subscription.SubscriptionID = response.Descendants(wa + "SubscriptionID").Single().Value;
50      subscription.SubscriptionName = response.Descendants(wa + "SubscriptionName").Single().Value;
51      subscription.SubscriptionStatus = response.Descendants(wa + "SubscriptionStatus").Single().Value;
52      subscription.MaxCoreCount = Convert.ToInt32(response.Descendants(wa + "MaxCoreCount").Single().Value);
53      subscription.MaxHostedServices = Convert.ToInt32(response.Descendants(wa + "MaxHostedServices").Single().Value);
54      subscription.MaxStorageAccounts = Convert.ToInt32(response.Descendants(wa + "MaxStorageAccounts").Single().Value);
55      subscription.CurrentCoreCount = Convert.ToInt32(response.Descendants(wa + "CurrentCoreCount").Single().Value);
56      subscription.CurrentHostedServices = Convert.ToInt32(response.Descendants(wa + "CurrentHostedServices").Single().Value);
57      subscription.CurrentStorageAccounts = Convert.ToInt32(response.Descendants(wa + "CurrentStorageAccounts").Single().Value);
58      subscription.CertificateThumbprint = thumbprint;
59
60      return subscription;
61    }
62
63    public static List<string> ListLocations(string subscriptionId, string thumbprint) {
64      string uri = string.Format(Constants.URILocationsFormat, subscriptionId);
65      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
66      XDocument response = operation.Invoke(uri);
67      List<string> result = null;
68      if (response.Root.HasElements) {
69        result = (from s in response.Descendants(wa + "Name")
70                  select s.Value).ToList();
71      }
72      return result;
73    }
74
75    public static List<AffinityGroup> ListAffinityGroups(string subscriptionId, string thumbprint) {
76      string uri = String.Format(Constants.URIAffinityGroupFormat, subscriptionId);
77      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
78      XDocument response = operation.Invoke(uri);
79      List<AffinityGroup> result = null;
80      if (response.Root.HasElements) {
81        result = (from s in response.Elements()
82                  select new AffinityGroup() {
83                    Name = s.Descendants(wa + "Name").Single().Value,
84                    Label = Utils.ConvertFromBase64String(s.Descendants(wa + "Label").Single().Value),
85                    Description = s.Descendants(wa + "Description").Single().Value,
86                    Location = s.Descendants(wa + "Location").Single().Value
87                  }).ToList();
88      }
89      return result;
90    }
91
92    public static List<HostedService> ListHostedServices(string subscriptionId, string thumbprint) {
93      string uri = String.Format(Constants.URIHostedServiceFormat, subscriptionId);
94      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
95      XDocument response = operation.Invoke(uri);
96      List<HostedService> result = null;
97      if (response.Root.HasElements) {
98
99        result = (from s in response.Elements()
100                  select new HostedService() {
101                    ServiceName = s.Descendants(wa + "ServiceName").Single().Value,
102                    Url = s.Descendants(wa + "Url").Single().Value
103                  }).ToList();
104      }
105
106      if (result != null)
107        return result;
108      else
109        return new List<HostedService>();
110    }
111
112    public static HostedService GetHostedService(string subscriptionId, string thumbprint, string serviceName) {
113      string uri = String.Format(Constants.URISpecificHostedServiceFormat, subscriptionId, serviceName);
114      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
115      XDocument response = operation.Invoke(uri);
116      HostedService service = new HostedService();
117      service.ServiceName = response.Descendants(wa + "ServiceName").Single().Value;
118      service.Url = response.Descendants(wa + "Url").Single().Value;
119      return service;
120    }
121
122    public static HostedService GetHostedServiceDetailed(string subscriptionId, string thumbprint, string serviceName) {
123      string uri = String.Format(Constants.URISpecificHostedServiceFormat + "?embed-detail=true", subscriptionId, serviceName);
124      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
125      XDocument response = operation.Invoke(uri);
126      HostedService service = new HostedService();
127      service.ServiceName = response.Descendants(wa + "ServiceName").Single().Value;
128      service.Url = response.Descendants(wa + "Url").First().Value;
129      if (response.Root.Elements(wa + "HostedServiceProperties").Any()) {
130        XElement xHostedServiceProperties = response.Descendants(wa + "HostedServiceProperties").Single();
131        string loc = string.Empty, affgroup = string.Empty;
132        if (xHostedServiceProperties.HasElements) {
133          string desc = xHostedServiceProperties.Descendants(wa + "Description").Single().Value;
134          if (xHostedServiceProperties.Descendants(wa + "Location").Any()) {
135            loc = xHostedServiceProperties.Descendants(wa + "Location").Single().Value;
136          } else {
137            affgroup = xHostedServiceProperties.Descendants(wa + "AffinityGroup").Single().Value;
138          }
139          string lbl = Utils.ConvertFromBase64String(xHostedServiceProperties.Descendants(wa + "Label").Single().Value);
140          HostedServiceProperties props = new HostedServiceProperties() {
141            Description = desc,
142            Location = loc,
143            AffinityGroup = affgroup,
144            Label = lbl
145          };
146          service.HostedServiceProperties = props;
147        }
148      }
149
150      if (response.Root.Elements(wa + "Deployments").Any()) {
151        XElement xDeployments = response.Descendants(wa + "Deployments").Single();
152        if (xDeployments.HasElements) {
153          IEnumerable<Deployment> deps = (from s in xDeployments.Elements()
154                                          select GetDeploymentFromXml(s)).ToList();
155          service.Deployments = new List<Deployment>(deps);
156        } else {
157          service.Deployments = new List<Deployment>();
158        }
159      }
160
161      return service;
162    }
163
164    public static Deployment GetDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
165      string uri = String.Format(Constants.URISpecificDeploymentFormat, subscriptionId, serviceName, deploymentName);
166      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
167      XDocument response = operation.Invoke(uri);
168      Deployment deployment = GetDeploymentFromXml(response.Root);
169      return deployment;
170    }
171
172    public static string AddCertificate(string subscriptionId, string thumbprint, string serviceName, string certificateFilePath, string password) {
173      string uri = String.Format(Constants.URICertificateFormat, subscriptionId, serviceName);
174      XDocument payload = CreateAddCertificatePayload(certificateFilePath, password);
175      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
176      string requestId = operation.Invoke(uri, payload);
177      return requestId;
178    }
179
180    public static string CreateHostedServiceWithLocation(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location) {
181      return CreateHostedService(subscriptionId, thumbprint, serviceName, label, description, location, String.Empty);
182    }
183
184    public static string CreateHostedServiceWithAffinityGroup(string subscriptionId, string thumbprint, string serviceName, string label, string description, string affinityGroup) {
185      return CreateHostedService(subscriptionId, thumbprint, serviceName, label, description, String.Empty, affinityGroup);
186    }
187
188    public static string CreateDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName, string deploymentSlot, string packageUrl, string configuration, string label, int instanceCount) {
189      string uri = String.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
190      XElement xConfig = XElement.Parse(configuration);
191      SetInstanceCount(xConfig, Constants.HLSlaveRoleName, instanceCount);
192
193      XDocument payload = CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
194      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
195      string requestId = operation.Invoke(uri, payload);
196      return requestId;
197    }
198
199    public static string DownloadBlobAsString(Uri uri) {
200      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
201      request.Headers.Add(Constants.HeaderVersionName, Constants.APIVersion20110818);
202      request.Method = Constants.HttpMethodGET;
203      request.ContentType = Constants.ContentTypeAppXml;
204      string result = string.Empty;
205      WebResponse response = request.GetResponse();
206      using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
207        result = reader.ReadToEnd();
208      }
209      return result;
210    }
211
212    #endregion
213
214    #region Private Methods
215
216    private static int GetInstanceCount(XElement configuration, string roleName) {
217      XElement instanceElement = (from s in configuration.Elements(sh + "Role")
218                                  where s.Attribute("name").Value == roleName
219                                  select s.Element(sh + "Instances")).First();
220      int instanceCount = Convert.ToInt32(instanceElement.Attribute("count").Value);
221      return instanceCount;
222    }
223
224    private static void SetInstanceCount(XElement configuration, string roleName, int value) {
225      XElement instanceElement = (from s in configuration.Elements(sh + "Role")
226                                  where s.Attribute("name").Value == roleName
227                                  select s.Element(sh + "Instances")).First();
228      instanceElement.SetAttributeValue("count", value);
229    }
230
231    private static string CreateHostedService(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location, string affinityGroup) {
232      string uri = string.Format(Constants.URIHostedServiceFormat, subscriptionId);
233      XDocument payload = CreateHostedServicePayload(serviceName, label, description, location, affinityGroup);
234      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
235      string requestId = operation.Invoke(uri, payload);
236      return requestId;
237    }
238
239    private static XDocument CreateHostedServicePayload(string serviceName, string label, string description, string location, string affinityGroup) {
240      string base64LabelName = Utils.ConvertToBase64String(label);
241      XElement xServiceName = new XElement(wa + "ServiceName", serviceName);
242      XElement xLabel = new XElement(wa + "Label", base64LabelName);
243      XElement xDescription = new XElement(wa + "Description", description);
244      XElement createHostedService = new XElement(wa + "CreateHostedService");
245      createHostedService.Add(xServiceName);
246      createHostedService.Add(xLabel);
247      createHostedService.Add(xDescription);
248
249      if (location != String.Empty) {
250        XElement xLocation = new XElement(wa + "Location", location);
251        createHostedService.Add(xLocation);
252      } else {
253        XElement xAffinityGroup = new XElement(wa + "AffinityGroup", affinityGroup);
254        createHostedService.Add(xAffinityGroup);
255      }
256
257      XDocument payload = new XDocument();
258      payload.Add(createHostedService);
259      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
260
261      return payload;
262    }
263
264    private static XDocument CreateAddCertificatePayload(string certificateFilePath, string password) {
265      XElement xData = new XElement(wa + "Data", Convert.ToBase64String(File.ReadAllBytes(certificateFilePath)));
266      XElement xCertificateFormat = new XElement(wa + "CertificateFormat", "pfx");
267      XElement xPassword = new XElement(wa + "Password", password);
268
269      XElement addcert = new XElement(wa + "CertificateFile");
270      addcert.Add(xData);
271      addcert.Add(xCertificateFormat);
272      addcert.Add(xPassword);
273
274      XDocument payload = new XDocument();
275      payload.Add(addcert);
276      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
277
278      return payload;
279    }
280
281    private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, string pathToConfigurationFile, string label) {
282      string configurationFile = File.ReadAllText(pathToConfigurationFile);
283      XElement xConfig = XElement.Parse(configurationFile);
284      return CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
285    }
286
287    private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, XElement configuration, string label) {
288      string base64ConfigurationFile = Utils.ConvertToBase64String(configuration.ToString());
289      string base64Label = Utils.ConvertToBase64String(label);
290
291      XElement xName = new XElement(wa + "Name", deploymentName);
292      XElement xPackageUrl = new XElement(wa + "PackageUrl", packageUrl);
293      XElement xLabel = new XElement(wa + "Label", base64Label);
294      XElement xConfiguration = new XElement(wa + "Configuration", base64ConfigurationFile);
295      XElement xStartDeployment = new XElement(wa + "StartDeployment", "true");
296      XElement xTreatWarningsAsError = new XElement(wa + "TreatWarningsAsError", "false");
297
298      XElement createDeployment = new XElement(wa + "CreateDeployment");
299      createDeployment.Add(xName);
300      createDeployment.Add(xPackageUrl);
301      createDeployment.Add(xLabel);
302      createDeployment.Add(xConfiguration);
303      createDeployment.Add(xStartDeployment);
304      createDeployment.Add(xTreatWarningsAsError);
305
306      XDocument payload = new XDocument();
307      payload.Add(createDeployment);
308      payload.Declaration =
309      new XDeclaration("1.0", "UTF-8", "no");
310      return payload;
311    }
312
313    private static XElement LoadConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot) {
314      string uri = string.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
315      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
316      XDocument deployment = operation.Invoke(uri);
317      string base64Configuration = deployment.Element(wa + "Deployment").Element(wa + "Configuration").Value;
318      string stringConfiguration = Utils.ConvertFromBase64String(base64Configuration);
319      XElement configuration = XElement.Parse(stringConfiguration);
320      return configuration;
321    }
322
323    private static string SaveConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot, XElement configuration) {
324      string uri = string.Format(Constants.URIDeploymentConfigurationFormat, subscriptionId, serviceName, deploymentSlot);
325      XDocument payload = CreateConfigurationPayload(configuration);
326      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
327      string requestId = operation.Invoke(uri, payload);
328      return requestId;
329    }
330
331    private static XDocument CreateConfigurationPayload(XElement configuration) {
332      string configurationString = configuration.ToString();
333      string base64Configuration = Utils.ConvertToBase64String(configurationString);
334
335      XElement xConfiguration = new XElement(wa + "Configuration", base64Configuration);
336      XElement xChangeConfiguration = new XElement(wa + "ChangeConfiguration", xConfiguration);
337
338      XDocument payload = new XDocument();
339      payload.Add(xChangeConfiguration);
340      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
341
342      return payload;
343    }
344
345    private static XDocument GetConfigurationFromDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
346      Deployment deployment = GetDeployment(subscriptionId, thumbprint, serviceName, deploymentName);
347      XDocument config = XDocument.Parse(deployment.Configuration);
348      return config;
349    }
350
351    private static bool ContainsRoleNameInConfiguration(XDocument configuration, string roleName) {
352      if (configuration.Root.Name != sh + "ServiceConfiguration")
353        throw new ArgumentException("No valid configuration", "configuration");
354
355
356      IEnumerable<XElement> role = (from s in configuration.Root.Elements(sh + "Role")
357                                    where s.Attribute("name").Value == roleName
358                                    select s);
359      return role.Count() > 0;
360    }
361
362    private static Deployment GetDeploymentFromXml(XElement xDeployment) {
363      Deployment deployment = new Deployment();
364      deployment.Name = xDeployment.Descendants(wa + "Name").Single().Value;
365      deployment.DeploymentSlot = xDeployment.Descendants(wa + "DeploymentSlot").Single().Value;
366      deployment.PrivateID = xDeployment.Descendants(wa + "PrivateID").Single().Value;
367      deployment.Status = xDeployment.Descendants(wa + "Status").Single().Value;
368      deployment.Label = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Label").Single().Value);
369      if (xDeployment.Descendants(wa + "Url").Any()) {
370        deployment.Url = xDeployment.Descendants(wa + "Url").Single().Value;
371      }
372      deployment.Configuration = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Configuration").Single().Value);
373
374      XElement xRoleInstanceList = xDeployment.Descendants(wa + "RoleInstanceList").Single();
375      if (xRoleInstanceList.HasElements) {
376        IEnumerable<RoleInstance> instances = (from s in xRoleInstanceList.Elements()
377                                               select new RoleInstance() {
378                                                 RoleName = s.Descendants(wa + "RoleName").Single().Value,
379                                                 InstanceName = s.Descendants(wa + "InstanceName").Single().Value,
380                                                 InstanceStatus = s.Descendants(wa + "InstanceStatus").Single().Value
381                                               }).ToList();
382        deployment.RoleInstanceList = new List<RoleInstance>(instances);
383      }
384      if (xDeployment.Descendants(wa + "RoleList").Any()) {
385        XElement xRoleList = xDeployment.Descendants(wa + "RoleList").Single();
386        if (xRoleList.HasElements) {
387
388          IEnumerable<Role> roles = (from s in xRoleList.Elements()
389                                     select new Role() {
390                                       RoleName = s.Descendants(wa + "RoleName").Single().Value,
391                                       OsVersion = s.Descendants(wa + "OsVersion").Single().Value
392                                     }).ToList();
393          deployment.RoleList = new List<Role>(roles);
394        }
395      }
396
397      if (xDeployment.Descendants(wa + "UpgradeDomainCount").Any()) {
398        deployment.UpgradeDomainCount = Convert.ToInt32(xDeployment.Descendants(wa + "UpgradeDomainCount").Single().Value);
399      }
400
401      if (xDeployment.Elements("UpgradeStatus").Any()) {
402        XElement xUpgradeStatus = xDeployment.Descendants(wa + "UpgradeStatus").Single();
403        if (xUpgradeStatus.HasElements) {
404          UpgradeStatus upgradestatus = new UpgradeStatus() {
405            UpgradeType = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value,
406            CurrentUpgradeDomain = Convert.ToInt32(xUpgradeStatus.Descendants(wa + "RoleName").Single().Value),
407            CurrentUpgradeDomainState = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value
408          };
409          deployment.UpgradeStatus = upgradestatus;
410        }
411      }
412      return deployment;
413    }
414
415    #endregion
416  }
417}
Note: See TracBrowser for help on using the repository browser.