Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7354 was 7354, checked in by spimming, 13 years ago

#1680:

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