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