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

Last change on this file since 7386 was 7386, checked in by spimming, 9 years ago

#1680:

  • Added property to refer to the corresponding subscription
  • Added new methods to azure management utils
File size: 22.4 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      service.SubscriptionId = subscriptionId;
120      return service;
121    }
122
123    public static HostedService GetHostedServiceDetailed(string subscriptionId, string thumbprint, string serviceName) {
124      string uri = String.Format(Constants.URISpecificHostedServiceFormat + "?embed-detail=true", subscriptionId, serviceName);
125      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
126      XDocument response = operation.Invoke(uri);
127      HostedService service = new HostedService();
128      service.ServiceName = response.Descendants(wa + "ServiceName").Single().Value;
129      service.Url = response.Descendants(wa + "Url").First().Value;
130      service.SubscriptionId = subscriptionId;
131      if (response.Root.Elements(wa + "HostedServiceProperties").Any()) {
132        XElement xHostedServiceProperties = response.Descendants(wa + "HostedServiceProperties").Single();
133        string loc = string.Empty, affgroup = string.Empty;
134        if (xHostedServiceProperties.HasElements) {
135          string desc = xHostedServiceProperties.Descendants(wa + "Description").Single().Value;
136          if (xHostedServiceProperties.Descendants(wa + "Location").Any()) {
137            loc = xHostedServiceProperties.Descendants(wa + "Location").Single().Value;
138          } else {
139            affgroup = xHostedServiceProperties.Descendants(wa + "AffinityGroup").Single().Value;
140          }
141          string lbl = Utils.ConvertFromBase64String(xHostedServiceProperties.Descendants(wa + "Label").Single().Value);
142          HostedServiceProperties props = new HostedServiceProperties() {
143            Description = desc,
144            Location = loc,
145            AffinityGroup = affgroup,
146            Label = lbl
147          };
148          service.HostedServiceProperties = props;
149        }
150      }
151
152      if (response.Root.Elements(wa + "Deployments").Any()) {
153        XElement xDeployments = response.Descendants(wa + "Deployments").Single();
154        if (xDeployments.HasElements) {
155          IEnumerable<Deployment> deps = (from s in xDeployments.Elements()
156                                          select GetDeploymentFromXml(s)).ToList();
157          service.Deployments = new List<Deployment>(deps);
158        } else {
159          service.Deployments = new List<Deployment>();
160        }
161      }
162
163      return service;
164    }
165
166    public static Deployment GetDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
167      string uri = String.Format(Constants.URISpecificDeploymentFormat, subscriptionId, serviceName, deploymentName);
168      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
169      XDocument response = operation.Invoke(uri);
170      Deployment deployment = GetDeploymentFromXml(response.Root);
171      return deployment;
172    }
173
174    public static string AddCertificate(string subscriptionId, string thumbprint, string serviceName, string certificateFilePath, string password) {
175      string uri = String.Format(Constants.URICertificateFormat, subscriptionId, serviceName);
176      XDocument payload = CreateAddCertificatePayload(certificateFilePath, password);
177      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
178      string requestId = operation.Invoke(uri, payload);
179      return requestId;
180    }
181
182    public static string CreateHostedServiceWithLocation(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location) {
183      return CreateHostedService(subscriptionId, thumbprint, serviceName, label, description, location, String.Empty);
184    }
185
186    public static string CreateHostedServiceWithAffinityGroup(string subscriptionId, string thumbprint, string serviceName, string label, string description, string affinityGroup) {
187      return CreateHostedService(subscriptionId, thumbprint, serviceName, label, description, String.Empty, affinityGroup);
188    }
189
190    public static string CreateDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName, string deploymentSlot, string packageUrl, string configuration, string label, int instanceCount) {
191      string uri = String.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
192      XElement xConfig = XElement.Parse(configuration);
193      SetInstanceCount(xConfig, Constants.HLSlaveRoleName, instanceCount);
194
195      XDocument payload = CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
196      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
197      string requestId = operation.Invoke(uri, payload);
198      return requestId;
199    }
200
201    public static string DownloadBlobAsString(Uri uri) {
202      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
203      request.Headers.Add(Constants.HeaderVersionName, Constants.APIVersion20110818);
204      request.Method = Constants.HttpMethodGET;
205      request.ContentType = Constants.ContentTypeAppXml;
206      string result = string.Empty;
207      WebResponse response = request.GetResponse();
208      using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
209        result = reader.ReadToEnd();
210      }
211      return result;
212    }
213
214    public static List<HostedService> DiscoverSlaveService(string subscriptionId, string thumbprint) {
215      List<HostedService> hostedServices = new List<HostedService>();
216      foreach (HostedService hostedService in ListHostedServices(subscriptionId, thumbprint)) {
217        HostedService hs = GetHostedServiceDetailed(subscriptionId, thumbprint, hostedService.ServiceName);
218        foreach (Deployment deployment in hs.Deployments) {
219          if (ContainsRoleNameInConfiguration(XDocument.Parse(deployment.Configuration), Constants.HLSlaveRoleName)) {
220            if (!hostedServices.Contains(hs)) {
221              hostedServices.Add(hs);
222            }
223          }
224        }
225      }
226      return hostedServices;
227    }
228
229    public static string DeleteHostedService(string subscriptionId, string thumbprint, string serviceName) {
230      string uri = String.Format(Constants.URISpecificHostedServiceFormat, subscriptionId, serviceName);
231      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
232      string requestId = operation.Invoke(uri, Constants.HttpMethodDELETE);
233      return requestId;
234    }
235
236    #endregion
237
238    #region Private Methods
239
240    private static int GetInstanceCount(XElement configuration, string roleName) {
241      XElement instanceElement = (from s in configuration.Elements(sh + "Role")
242                                  where s.Attribute("name").Value == roleName
243                                  select s.Element(sh + "Instances")).First();
244      int instanceCount = Convert.ToInt32(instanceElement.Attribute("count").Value);
245      return instanceCount;
246    }
247
248    private static void SetInstanceCount(XElement configuration, string roleName, int value) {
249      XElement instanceElement = (from s in configuration.Elements(sh + "Role")
250                                  where s.Attribute("name").Value == roleName
251                                  select s.Element(sh + "Instances")).First();
252      instanceElement.SetAttributeValue("count", value);
253    }
254
255    private static string CreateHostedService(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location, string affinityGroup) {
256      string uri = string.Format(Constants.URIHostedServiceFormat, subscriptionId);
257      XDocument payload = CreateHostedServicePayload(serviceName, label, description, location, affinityGroup);
258      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
259      string requestId = operation.Invoke(uri, payload);
260      return requestId;
261    }
262
263    private static XDocument CreateHostedServicePayload(string serviceName, string label, string description, string location, string affinityGroup) {
264      string base64LabelName = Utils.ConvertToBase64String(label);
265      XElement xServiceName = new XElement(wa + "ServiceName", serviceName);
266      XElement xLabel = new XElement(wa + "Label", base64LabelName);
267      XElement xDescription = new XElement(wa + "Description", description);
268      XElement createHostedService = new XElement(wa + "CreateHostedService");
269      createHostedService.Add(xServiceName);
270      createHostedService.Add(xLabel);
271      createHostedService.Add(xDescription);
272
273      if (location != String.Empty) {
274        XElement xLocation = new XElement(wa + "Location", location);
275        createHostedService.Add(xLocation);
276      } else {
277        XElement xAffinityGroup = new XElement(wa + "AffinityGroup", affinityGroup);
278        createHostedService.Add(xAffinityGroup);
279      }
280
281      XDocument payload = new XDocument();
282      payload.Add(createHostedService);
283      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
284
285      return payload;
286    }
287
288    private static XDocument CreateAddCertificatePayload(string certificateFilePath, string password) {
289      XElement xData = new XElement(wa + "Data", Convert.ToBase64String(File.ReadAllBytes(certificateFilePath)));
290      XElement xCertificateFormat = new XElement(wa + "CertificateFormat", "pfx");
291      XElement xPassword = new XElement(wa + "Password", password);
292
293      XElement addcert = new XElement(wa + "CertificateFile");
294      addcert.Add(xData);
295      addcert.Add(xCertificateFormat);
296      addcert.Add(xPassword);
297
298      XDocument payload = new XDocument();
299      payload.Add(addcert);
300      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
301
302      return payload;
303    }
304
305    private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, string pathToConfigurationFile, string label) {
306      string configurationFile = File.ReadAllText(pathToConfigurationFile);
307      XElement xConfig = XElement.Parse(configurationFile);
308      return CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
309    }
310
311    private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, XElement configuration, string label) {
312      string base64ConfigurationFile = Utils.ConvertToBase64String(configuration.ToString());
313      string base64Label = Utils.ConvertToBase64String(label);
314
315      XElement xName = new XElement(wa + "Name", deploymentName);
316      XElement xPackageUrl = new XElement(wa + "PackageUrl", packageUrl);
317      XElement xLabel = new XElement(wa + "Label", base64Label);
318      XElement xConfiguration = new XElement(wa + "Configuration", base64ConfigurationFile);
319      XElement xStartDeployment = new XElement(wa + "StartDeployment", "true");
320      XElement xTreatWarningsAsError = new XElement(wa + "TreatWarningsAsError", "false");
321
322      XElement createDeployment = new XElement(wa + "CreateDeployment");
323      createDeployment.Add(xName);
324      createDeployment.Add(xPackageUrl);
325      createDeployment.Add(xLabel);
326      createDeployment.Add(xConfiguration);
327      createDeployment.Add(xStartDeployment);
328      createDeployment.Add(xTreatWarningsAsError);
329
330      XDocument payload = new XDocument();
331      payload.Add(createDeployment);
332      payload.Declaration =
333      new XDeclaration("1.0", "UTF-8", "no");
334      return payload;
335    }
336
337    private static XElement LoadConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot) {
338      string uri = string.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
339      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
340      XDocument deployment = operation.Invoke(uri);
341      string base64Configuration = deployment.Element(wa + "Deployment").Element(wa + "Configuration").Value;
342      string stringConfiguration = Utils.ConvertFromBase64String(base64Configuration);
343      XElement configuration = XElement.Parse(stringConfiguration);
344      return configuration;
345    }
346
347    private static string SaveConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot, XElement configuration) {
348      string uri = string.Format(Constants.URIDeploymentConfigurationFormat, subscriptionId, serviceName, deploymentSlot);
349      XDocument payload = CreateConfigurationPayload(configuration);
350      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
351      string requestId = operation.Invoke(uri, payload);
352      return requestId;
353    }
354
355    private static XDocument CreateConfigurationPayload(XElement configuration) {
356      string configurationString = configuration.ToString();
357      string base64Configuration = Utils.ConvertToBase64String(configurationString);
358
359      XElement xConfiguration = new XElement(wa + "Configuration", base64Configuration);
360      XElement xChangeConfiguration = new XElement(wa + "ChangeConfiguration", xConfiguration);
361
362      XDocument payload = new XDocument();
363      payload.Add(xChangeConfiguration);
364      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
365
366      return payload;
367    }
368
369    private static XDocument GetConfigurationFromDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
370      Deployment deployment = GetDeployment(subscriptionId, thumbprint, serviceName, deploymentName);
371      XDocument config = XDocument.Parse(deployment.Configuration);
372      return config;
373    }
374
375    private static bool ContainsRoleNameInConfiguration(XDocument configuration, string roleName) {
376      if (configuration.Root.Name != sh + "ServiceConfiguration")
377        throw new ArgumentException("No valid configuration", "configuration");
378
379
380      IEnumerable<XElement> role = (from s in configuration.Root.Elements(sh + "Role")
381                                    where s.Attribute("name").Value == roleName
382                                    select s);
383      return role.Count() > 0;
384    }
385
386    private static Deployment GetDeploymentFromXml(XElement xDeployment) {
387      Deployment deployment = new Deployment();
388      deployment.Name = xDeployment.Descendants(wa + "Name").Single().Value;
389      deployment.DeploymentSlot = xDeployment.Descendants(wa + "DeploymentSlot").Single().Value;
390      deployment.PrivateID = xDeployment.Descendants(wa + "PrivateID").Single().Value;
391      deployment.Status = xDeployment.Descendants(wa + "Status").Single().Value;
392      deployment.Label = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Label").Single().Value);
393      if (xDeployment.Descendants(wa + "Url").Any()) {
394        deployment.Url = xDeployment.Descendants(wa + "Url").Single().Value;
395      }
396      deployment.Configuration = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Configuration").Single().Value);
397
398      XElement xRoleInstanceList = xDeployment.Descendants(wa + "RoleInstanceList").Single();
399      if (xRoleInstanceList.HasElements) {
400        IEnumerable<RoleInstance> instances = (from s in xRoleInstanceList.Elements()
401                                               select new RoleInstance() {
402                                                 RoleName = s.Descendants(wa + "RoleName").Single().Value,
403                                                 InstanceName = s.Descendants(wa + "InstanceName").Single().Value,
404                                                 InstanceStatus = s.Descendants(wa + "InstanceStatus").Single().Value
405                                               }).ToList();
406        deployment.RoleInstanceList = new List<RoleInstance>(instances);
407      }
408      if (xDeployment.Descendants(wa + "RoleList").Any()) {
409        XElement xRoleList = xDeployment.Descendants(wa + "RoleList").Single();
410        if (xRoleList.HasElements) {
411
412          IEnumerable<Role> roles = (from s in xRoleList.Elements()
413                                     select new Role() {
414                                       RoleName = s.Descendants(wa + "RoleName").Single().Value,
415                                       OsVersion = s.Descendants(wa + "OsVersion").Single().Value
416                                     }).ToList();
417          deployment.RoleList = new List<Role>(roles);
418        }
419      }
420
421      if (xDeployment.Descendants(wa + "UpgradeDomainCount").Any()) {
422        deployment.UpgradeDomainCount = Convert.ToInt32(xDeployment.Descendants(wa + "UpgradeDomainCount").Single().Value);
423      }
424
425      if (xDeployment.Elements("UpgradeStatus").Any()) {
426        XElement xUpgradeStatus = xDeployment.Descendants(wa + "UpgradeStatus").Single();
427        if (xUpgradeStatus.HasElements) {
428          UpgradeStatus upgradestatus = new UpgradeStatus() {
429            UpgradeType = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value,
430            CurrentUpgradeDomain = Convert.ToInt32(xUpgradeStatus.Descendants(wa + "RoleName").Single().Value),
431            CurrentUpgradeDomainState = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value
432          };
433          deployment.UpgradeStatus = upgradestatus;
434        }
435      }
436      return deployment;
437    }
438
439    #endregion
440  }
441}
Note: See TracBrowser for help on using the repository browser.