bit

Untitled

bit
Mar 20th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Microsoft.WindowsAzure;
  11. using Microsoft.WindowsAzure.Management.Compute;
  12. using Microsoft.WindowsAzure.Management.Compute.Models;
  13.  
  14. namespace PMGAzure4
  15. {
  16. class Program
  17. {
  18. public static string subscriptionId = "zzz";
  19.  
  20. public static string base64EncodedCert = "yyy";
  21.  
  22.  
  23. static void Main(string[] args)
  24. {
  25.  
  26.  
  27.  
  28. string vmName = "PMGATL0319-2";
  29.  
  30. string location = "East US";
  31.  
  32. string storageAccountName = "storageATL";
  33.  
  34. string imageFamiliyName = "Windows Server 2012 Datacenter";
  35.  
  36. string userName = "adminadmin";
  37. string password = "someKind$$$";
  38.  
  39.  
  40.  
  41. SubscriptionCloudCredentials subCloudCred = getCredentials(subscriptionId, base64EncodedCert);
  42.  
  43. ComputeManagementClient client = new ComputeManagementClient(subCloudCred);
  44. //You need a hosted service VM.
  45.  
  46. try
  47. {
  48. createCloudServiceByLocation(vmName, location, subCloudCred);
  49. }
  50. catch (CloudException ce)
  51. {
  52. Console.WriteLine(ce.Message);
  53. }
  54.  
  55. if (client.HostedServices.Get(vmName) == null)
  56. {
  57. createCloudServiceByLocation(vmName, location, subCloudCred);
  58. }
  59.  
  60.  
  61. var OSVHD = client.VirtualMachineImages.CreateOSVHD(vmName, vmName, storageAccountName, imageFamiliyName);
  62.  
  63. var VMROle = client.VirtualMachines.CreateVMRole(vmName, vmName, VirtualMachineRoleSize.Small, userName, password, OSVHD);
  64.  
  65. List<Role> roleList = new List<Role>{
  66. VMROle
  67. };
  68. client.VirtualMachines.CreateVMDeployment(vmName, vmName, roleList);
  69. Console.WriteLine("Create VM success");
  70.  
  71.  
  72. Console.ReadLine();
  73. }
  74.  
  75.  
  76. public static void createCloudServiceByLocation(string cloudServiceName, string location, SubscriptionCloudCredentials subCloudCred)
  77. {
  78. ComputeManagementClient client = new ComputeManagementClient(subCloudCred);
  79. HostedServiceCreateParameters hostedServiceCreateParams = new HostedServiceCreateParameters
  80. {
  81. ServiceName = cloudServiceName,
  82. Location = location,
  83. Label = EncodeToBase64(cloudServiceName),
  84. };
  85. try
  86. {
  87. client.HostedServices.Create(hostedServiceCreateParams);
  88. }
  89. catch (CloudException e)
  90. {
  91. throw e;
  92. }
  93.  
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100. public static void createCloudServiceByAffinityGroup(string cloudServiceName, string affinityGroupName, SubscriptionCloudCredentials subCloudCred)
  101. {
  102. ComputeManagementClient client = new ComputeManagementClient(subCloudCred);
  103. HostedServiceCreateParameters hostedServiceCreateParams = new HostedServiceCreateParameters
  104. {
  105. ServiceName = cloudServiceName,
  106. AffinityGroup = affinityGroupName,
  107. Label = EncodeToBase64(cloudServiceName),
  108. };
  109. try
  110. {
  111. client.HostedServices.Create(hostedServiceCreateParams);
  112. }
  113. catch (CloudException e)
  114. {
  115. throw e;
  116. }
  117.  
  118.  
  119. }
  120. public static string EncodeToBase64(string toEncode)
  121. {
  122. byte[] toEncodeAsBytes
  123. = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
  124. string returnValue
  125. = System.Convert.ToBase64String(toEncodeAsBytes);
  126. return returnValue;
  127. }
  128.  
  129.  
  130. public static SubscriptionCloudCredentials getCredentials(string subscriptionId, string base64EncodedCertificate)
  131. {
  132. return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate)));
  133. }
  134. /// <summary>
  135. /// Instantiate a new VM Role
  136. /// </summary>
  137. ///
  138.  
  139.  
  140. }
  141.  
  142. public static class MyVirtualMachineExtension
  143. {
  144.  
  145.  
  146. public static Role CreateVMRole(this IVirtualMachineOperations client, string cloudServiceName, string roleName, string roleSize, string userName, string password, OSVirtualHardDisk osVHD)
  147. {
  148. Role vmRole = new Role
  149. {
  150. RoleType = VirtualMachineRoleType.PersistentVMRole.ToString(),
  151. RoleName = roleName,
  152. Label = roleName,
  153. RoleSize = roleSize,
  154. ConfigurationSets = new List<ConfigurationSet>(),
  155. OSVirtualHardDisk = osVHD
  156. };
  157. ConfigurationSet configSet = new ConfigurationSet
  158. {
  159. ConfigurationSetType = ConfigurationSetTypes.WindowsProvisioningConfiguration,
  160. EnableAutomaticUpdates = true,
  161. ResetPasswordOnFirstLogon = false,
  162. ComputerName = roleName,
  163. AdminUserName = userName,
  164. AdminPassword = password,
  165. InputEndpoints = new BindingList<InputEndpoint>
  166. {
  167. new InputEndpoint { LocalPort = 3389, Name = "RDP", Protocol = "tcp" },
  168. new InputEndpoint { LocalPort = 80, Port = 80, Name = "web", Protocol = "tcp" }
  169. }
  170. };
  171. vmRole.ConfigurationSets.Add(configSet);
  172. return vmRole;
  173. }
  174.  
  175.  
  176.  
  177. public static OSVirtualHardDisk CreateOSVHD(this IVirtualMachineImageOperations operation, string cloudserviceName, string vmName, string storageAccount, string imageFamiliyName)
  178. {
  179. try
  180. {
  181. var osVHD = new OSVirtualHardDisk
  182. {
  183. MediaLink = GetVhdUri(string.Format("{0}.blob.core.windows.net/vhds", storageAccount), cloudserviceName, vmName),
  184. SourceImageName = GetSourceImageNameByFamliyName(operation, imageFamiliyName)
  185. };
  186. return osVHD;
  187. }
  188. catch (CloudException e)
  189. {
  190.  
  191.  
  192. throw e;
  193. }
  194.  
  195.  
  196. }
  197.  
  198.  
  199. private static string GetSourceImageNameByFamliyName(this IVirtualMachineImageOperations operation, string imageFamliyName)
  200. {
  201. var disk = operation.List().Where(o => o.ImageFamily == imageFamliyName).FirstOrDefault();
  202. if (disk != null)
  203. {
  204. return disk.Name;
  205. }
  206. else
  207. {
  208. throw new CloudException(string.Format("Can't find {0} OS image in current subscription"));
  209. }
  210. }
  211.  
  212.  
  213. private static Uri GetVhdUri(string blobcontainerAddress, string cloudServiceName, string vmName, bool cacheDisk = false, bool https = false)
  214. {
  215. var now = DateTime.UtcNow;
  216. string dateString = now.Year + "-" + now.Month + "-" + now.Day;
  217.  
  218.  
  219. var address = string.Format("http{0}://{1}/{2}-{3}-{4}-{5}-650.vhd", https ? "s" : string.Empty, blobcontainerAddress, cloudServiceName, vmName, cacheDisk ? "-CacheDisk" : string.Empty, dateString);
  220. return new Uri(address);
  221. }
  222.  
  223.  
  224. public static void CreateVMDeployment(this IVirtualMachineOperations operations, string cloudServiceName, string deploymentName, List<Role> roleList, DeploymentSlot slot = DeploymentSlot.Production)
  225. {
  226.  
  227.  
  228. try
  229. {
  230. VirtualMachineCreateDeploymentParameters createDeploymentParams = new VirtualMachineCreateDeploymentParameters
  231. {
  232.  
  233.  
  234. Name = deploymentName,
  235. Label = cloudServiceName,
  236. Roles = roleList,
  237. DeploymentSlot = slot
  238. };
  239. operations.CreateDeployment(cloudServiceName, createDeploymentParams);
  240. }
  241. catch (CloudException ce)
  242. {
  243. WebException we = (WebException)ce.InnerException;
  244. Console.WriteLine(ce.Response.ToString());
  245.  
  246. // var err = new StreamReader(ce.Response.GetResponseStream()).ReadToEnd();
  247.  
  248. // Console.WriteLine(err);
  249.  
  250. }
  251. }
  252. }
  253.  
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment