Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1680:

  • List-Operations added to Interface and AzureProvider
  • AddCertificate- and AddAzureService-Dialog added
File size: 18.8 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    #endregion
172
173    #region Private Methods
174
175    private static int GetInstanceCount(XElement configuration, string roleName) {
176      XElement instanceElement = (from s in configuration.Elements(sh + "Role")
177                                  where s.Attribute("name").Value == roleName
178                                  select s.Element(sh + "Instances")).First();
179      int instanceCount = Convert.ToInt32(instanceElement.Attribute("count").Value);
180      return instanceCount;
181    }
182
183    private static void SetInstanceCount(XElement configuration, string roleName, int value) {
184      XElement instanceElement = (from s in configuration.Elements(sh + "Role")
185                                  where s.Attribute("name").Value == roleName
186                                  select s.Element(sh + "Instances")).First();
187      instanceElement.SetAttributeValue("count", value);
188    }
189
190    private static string CreateHostedService(string subscriptionId, string thumbprint, string serviceName, string label, string description, string location, string affinityGroup) {
191      string uri = string.Format(Constants.URIHostedServiceFormat, subscriptionId);
192      XDocument payload = CreateHostedServicePayload(serviceName, label, description, location, affinityGroup);
193      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
194      string requestId = operation.Invoke(uri, payload);
195      return requestId;
196    }
197
198    private static XDocument CreateHostedServicePayload(string serviceName, string label, string description, string location, string affinityGroup) {
199      string base64LabelName = Utils.ConvertToBase64String(label);
200      XElement xServiceName = new XElement(wa + "ServiceName", serviceName);
201      XElement xLabel = new XElement(wa + "Label", base64LabelName);
202      XElement xDescription = new XElement(wa + "Description", description);
203      XElement createHostedService = new XElement(wa + "CreateHostedService");
204      createHostedService.Add(xServiceName);
205      createHostedService.Add(xLabel);
206      createHostedService.Add(xDescription);
207
208      if (location != String.Empty) {
209        XElement xLocation = new XElement(wa + "Location", location);
210        createHostedService.Add(xLocation);
211      } else {
212        XElement xAffinityGroup = new XElement(wa + "AffinityGroup", affinityGroup);
213        createHostedService.Add(xAffinityGroup);
214      }
215
216      XDocument payload = new XDocument();
217      payload.Add(createHostedService);
218      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
219
220      return payload;
221    }
222
223    private static XDocument CreateAddCertificatePayload(string certificateFilePath, string password) {
224      XElement xData = new XElement(wa + "Data", Convert.ToBase64String(File.ReadAllBytes(certificateFilePath)));
225      XElement xCertificateFormat = new XElement(wa + "CertificateFormat", "pfx");
226      XElement xPassword = new XElement(wa + "Password", password);
227
228      XElement addcert = new XElement(wa + "CertificateFile");
229      addcert.Add(xData);
230      addcert.Add(xCertificateFormat);
231      addcert.Add(xPassword);
232
233      XDocument payload = new XDocument();
234      payload.Add(addcert);
235      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
236
237      return payload;
238    }
239
240    private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, string pathToConfigurationFile, string label) {
241      string configurationFile = File.ReadAllText(pathToConfigurationFile);
242      XElement xConfig = XElement.Parse(configurationFile);
243      return CreateDeploymentPayload(deploymentName, packageUrl, xConfig, label);
244    }
245
246    private static XDocument CreateDeploymentPayload(string deploymentName, string packageUrl, XElement configuration, string label) {
247      string base64ConfigurationFile = Utils.ConvertToBase64String(configuration.ToString());
248      string base64Label = Utils.ConvertToBase64String(label);
249
250      XElement xName = new XElement(wa + "Name", deploymentName);
251      XElement xPackageUrl = new XElement(wa + "PackageUrl", packageUrl);
252      XElement xLabel = new XElement(wa + "Label", base64Label);
253      XElement xConfiguration = new XElement(wa + "Configuration", base64ConfigurationFile);
254      XElement xStartDeployment = new XElement(wa + "StartDeployment", "true");
255      XElement xTreatWarningsAsError = new XElement(wa + "TreatWarningsAsError", "false");
256
257      XElement createDeployment = new XElement(wa + "CreateDeployment");
258      createDeployment.Add(xName);
259      createDeployment.Add(xPackageUrl);
260      createDeployment.Add(xLabel);
261      createDeployment.Add(xConfiguration);
262      createDeployment.Add(xStartDeployment);
263      createDeployment.Add(xTreatWarningsAsError);
264
265      XDocument payload = new XDocument();
266      payload.Add(createDeployment);
267      payload.Declaration =
268      new XDeclaration("1.0", "UTF-8", "no");
269      return payload;
270    }
271
272    private static XElement LoadConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot) {
273      string uri = string.Format(Constants.URIDeploymentFormat, subscriptionId, serviceName, deploymentSlot);
274      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
275      XDocument deployment = operation.Invoke(uri);
276      string base64Configuration = deployment.Element(wa + "Deployment").Element(wa + "Configuration").Value;
277      string stringConfiguration = Utils.ConvertFromBase64String(base64Configuration);
278      XElement configuration = XElement.Parse(stringConfiguration);
279      return configuration;
280    }
281
282    private static string SaveConfiguration(String subscriptionId, String thumbprint, String serviceName, String deploymentSlot, XElement configuration) {
283      string uri = string.Format(Constants.URIDeploymentConfigurationFormat, subscriptionId, serviceName, deploymentSlot);
284      XDocument payload = CreateConfigurationPayload(configuration);
285      ServiceWebRequest operation = new ServiceWebRequest(thumbprint);
286      string requestId = operation.Invoke(uri, payload);
287      return requestId;
288    }
289
290    private static XDocument CreateConfigurationPayload(XElement configuration) {
291      string configurationString = configuration.ToString();
292      string base64Configuration = Utils.ConvertToBase64String(configurationString);
293
294      XElement xConfiguration = new XElement(wa + "Configuration", base64Configuration);
295      XElement xChangeConfiguration = new XElement(wa + "ChangeConfiguration", xConfiguration);
296
297      XDocument payload = new XDocument();
298      payload.Add(xChangeConfiguration);
299      payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");
300
301      return payload;
302    }
303
304    private static XDocument GetConfigurationFromDeployment(string subscriptionId, string thumbprint, string serviceName, string deploymentName) {
305      Deployment deployment = GetDeployment(subscriptionId, thumbprint, serviceName, deploymentName);
306      XDocument config = XDocument.Parse(deployment.Configuration);
307      return config;
308    }
309
310    private static bool ContainsRoleNameInConfiguration(XDocument configuration, string roleName) {
311      if (configuration.Root.Name != sh + "ServiceConfiguration")
312        throw new ArgumentException("No valid configuration", "configuration");
313
314
315      IEnumerable<XElement> role = (from s in configuration.Root.Elements(sh + "Role")
316                                    where s.Attribute("name").Value == roleName
317                                    select s);
318      return role.Count() > 0;
319    }
320
321    private static Deployment GetDeploymentFromXml(XElement xDeployment) {
322      Deployment deployment = new Deployment();
323      deployment.Name = xDeployment.Descendants(wa + "Name").Single().Value;
324      deployment.DeploymentSlot = xDeployment.Descendants(wa + "DeploymentSlot").Single().Value;
325      deployment.PrivateID = xDeployment.Descendants(wa + "PrivateID").Single().Value;
326      deployment.Status = xDeployment.Descendants(wa + "Status").Single().Value;
327      deployment.Label = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Label").Single().Value);
328      if (xDeployment.Descendants(wa + "Url").Any()) {
329        deployment.Url = xDeployment.Descendants(wa + "Url").Single().Value;
330      }
331      deployment.Configuration = Utils.ConvertFromBase64String(xDeployment.Descendants(wa + "Configuration").Single().Value);
332
333      XElement xRoleInstanceList = xDeployment.Descendants(wa + "RoleInstanceList").Single();
334      if (xRoleInstanceList.HasElements) {
335        IEnumerable<RoleInstance> instances = (from s in xRoleInstanceList.Elements()
336                                               select new RoleInstance() {
337                                                 RoleName = s.Descendants(wa + "RoleName").Single().Value,
338                                                 InstanceName = s.Descendants(wa + "InstanceName").Single().Value,
339                                                 InstanceStatus = s.Descendants(wa + "InstanceStatus").Single().Value
340                                               }).ToList();
341        deployment.RoleInstanceList = new List<RoleInstance>(instances);
342      }
343      if (xDeployment.Descendants(wa + "RoleList").Any()) {
344        XElement xRoleList = xDeployment.Descendants(wa + "RoleList").Single();
345        if (xRoleList.HasElements) {
346
347          IEnumerable<Role> roles = (from s in xRoleList.Elements()
348                                     select new Role() {
349                                       RoleName = s.Descendants(wa + "RoleName").Single().Value,
350                                       OsVersion = s.Descendants(wa + "OsVersion").Single().Value
351                                     }).ToList();
352          deployment.RoleList = new List<Role>(roles);
353        }
354      }
355
356      if (xDeployment.Descendants(wa + "UpgradeDomainCount").Any()) {
357        deployment.UpgradeDomainCount = Convert.ToInt32(xDeployment.Descendants(wa + "UpgradeDomainCount").Single().Value);
358      }
359
360      if (xDeployment.Elements("UpgradeStatus").Any()) {
361        XElement xUpgradeStatus = xDeployment.Descendants(wa + "UpgradeStatus").Single();
362        if (xUpgradeStatus.HasElements) {
363          UpgradeStatus upgradestatus = new UpgradeStatus() {
364            UpgradeType = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value,
365            CurrentUpgradeDomain = Convert.ToInt32(xUpgradeStatus.Descendants(wa + "RoleName").Single().Value),
366            CurrentUpgradeDomainState = xUpgradeStatus.Descendants(wa + "RoleName").Single().Value
367          };
368          deployment.UpgradeStatus = upgradestatus;
369        }
370      }
371      return deployment;
372    }
373
374    #endregion
375  }
376}
Note: See TracBrowser for help on using the repository browser.