public void Execute() { var properties = new WindowsVirtualMachineProperties() { AdministratorPassword = _applicationFactory.Password, RoleName = _applicationFactory.RoleName, Certificate = _applicationFactory.ManagementCertificate, Location = LocationConstants.NorthEurope, UseExistingCloudService = true, SubscriptionId = _applicationFactory.SubscriptionId, CloudServiceName = _applicationFactory.CloudServiceName, PublicEndpoints = new List<InputEndpoint>(new[] {new InputEndpoint() { EndpointName = "web", LocalPort = 80, Port = 80, Protocol = Protocol.TCP }}), VirtualMachineType = VirtualMachineTemplates.WindowsServer2008R2SP1, VmSize = VmSize.Small, StorageAccountName = "elastastorage", DataDisks = new List<DataVirtualHardDisk>(){new DataVirtualHardDisk(){LogicalDiskSizeInGB = 100}} }; var client = new WindowsVirtualMachineClient(_applicationFactory.SubscriptionId, _applicationFactory.ManagementCertificate); var newClient = client.CreateNewVirtualMachineFromTemplateGallery(properties); }
/// <summary> /// Creates a new virtual machine from a gallery template /// </summary> /// <param name="properties">Can be any gallery template</param> public IVirtualMachineClient CreateNewVirtualMachineFromTemplateGallery(WindowsVirtualMachineProperties properties) { // for the time being we're going to adopt the default powershell cmdlet behaviour and always create a new cloud services EnsureVirtualMachineProperties(properties); if (!properties.UseExistingCloudService) { var cloudServiceCommand = new CreateCloudServiceCommand(properties.CloudServiceName,"Created by Fluent Management", properties.Location) { SubscriptionId = properties.SubscriptionId, Certificate = properties.Certificate }; cloudServiceCommand.Execute(); } // continue to the create the virtual machine in the cloud service var command = new CreateWindowsVirtualMachineDeploymentCommand(properties) { SubscriptionId = properties.SubscriptionId, Certificate = properties.Certificate }; command.Execute(); // start the role up -- this could take a while the previous two operations are fairly lightweight // and the provisioning doesn't occur until the role starts not when it is created var startCommand = new StartVirtualMachineCommand(properties) { SubscriptionId = properties.SubscriptionId, Certificate = properties.Certificate }; startCommand.Execute(); // create a new client and return this so that properties can be populated automatically return new VirtualMachineClient(properties); }
// https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/ /// <summary> /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal StopVirtualMachineCommand(WindowsVirtualMachineProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; HttpCommand = string.Format("{0}/deployments/{1}/roleinstances/{2}/Operations", properties.CloudServiceName, properties.DeploymentName, properties.RoleName); Properties = properties; }
// DELETE https://management.core.windows.net/<subscription-id>/services/disks/<disk-name> /// <summary> /// {subscriptionID}/services/hostedservices/{serviceName}/deployments/{deploymentName}/Roles/{roleName}/DataDisks/{lun /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal DeleteVirtualMachineDataDiskCommand(WindowsVirtualMachineProperties properties, int lun) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; HttpCommand = String.Format("{0}/deployments/{1}/Roles/{2}/DataDisks/{3}", properties.CloudServiceName, properties.DeploymentName, properties.RoleName, lun); HttpVerb = HttpVerbDelete; }
// GET https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/<deployment-name>/roleinstances/<roleinstance-name>/ModelFile?FileType=RDP /// <summary> /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal DownloadWindowsRemoteDesktopCommand(WindowsVirtualMachineProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; HttpCommand = properties.CloudServiceName + "/deployments/" + properties.DeploymentName + "/roleinstances/" + properties.RoleName + "/ModelFile?FileType=RDP"; Properties = properties; HttpVerb = HttpVerbGet; }
// https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/ /// <summary> /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal RestartVirtualMachineCommand(WindowsVirtualMachineProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; //https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/<deployment-name>/roles/<role-name>/Operations HttpCommand = string.Format("{0}/deployments/{1}/roles/{2}/Operations", properties.CloudServiceName, properties.DeploymentName, properties.RoleName); Properties = properties; }
// DELETE https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/<deployment-name>/roles/<role-name> /// <summary> /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal DeleteVirtualMachineDeploymentCommand(WindowsVirtualMachineProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; HttpCommand = String.Format("{0}/deployments/{1}", properties.CloudServiceName, properties.DeploymentName); HttpVerb = HttpVerbDelete; Properties = properties; }
// GET https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deploymentslots/Production /// <summary> /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal GetWindowsVirtualMachineContextCommand(WindowsVirtualMachineProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; HttpCommand = properties.CloudServiceName + "/deploymentslots/Production"; Properties = properties; HttpVerb = HttpVerbGet; }
// https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/ /// <summary> /// Used to construct the command to create a virtual machine deployment including the creation of a role /// </summary> internal CreateWindowsVirtualMachineDeploymentCommand(WindowsVirtualMachineProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-03-01"; OperationId = "hostedservices"; ServiceType = "services"; HttpCommand = properties.CloudServiceName + "/deployments"; VirtualMachineType = properties.VirtualMachineType; CloudServiceName = properties.CloudServiceName; VhdStorageAccount = properties.StorageAccountName; Size = properties.VmSize; Properties = properties; }
public void Execute() { var properties = new WindowsVirtualMachineProperties() { AdministratorPassword = _applicationFactory.Password, RoleName = _applicationFactory.RoleName, Certificate = _applicationFactory.ManagementCertificate, SubscriptionId = _applicationFactory.SubscriptionId, CloudServiceName = _applicationFactory.CloudServiceName, DeploymentName = _applicationFactory.DeploymentName }; var client = new WindowsVirtualMachineClient(properties); var vm = client.VirtualMachine; client.DeleteVirtualMachine(true, true); System.Console.WriteLine("Deleted virtual machine + disks"); }
public void Execute() { var properties = new WindowsVirtualMachineProperties() { AdministratorPassword = "******", RoleName = "fluentmanagementvm", Certificate = _certificate, Location = LocationConstants.NorthEurope, UseExistingCloudService = true, SubscriptionId = _subscriptionId, CloudServiceName = _cloudServiceName, PublicEndpoints = new Dictionary<string, int>(){{"web",80}}, VirtualMachineType = VirtualMachineTemplates.WindowsServer2012, VmSize = VmSize.Small, StorageAccountName = "elastastorage", DataDisks = new List<DataVirtualHardDisk>(){new DataVirtualHardDisk(){LogicalDiskSizeInGB = 100}} }; var client = new VirtualMachineClient(_subscriptionId, _certificate); var newClient = client.CreateNewVirtualMachineFromTemplateGallery(properties); }
public BuildVirtualMachine(string subscriptionId, string publishSettingsFile, string rdpFile, string storageAccountName, string storageLocationName, string cloudServiceName, string deploymentName, string roleName, string administratorPassWord) { var settings = PublishSettingsExtractor.GetFromFile(publishSettingsFile); _certificate = settings.AddPublishSettingsToPersonalMachineStore(); _subscriptionId = subscriptionId; _rdpFile = rdpFile; _storageAccountName = storageAccountName; _storageLocationName = storageLocationName; _properties = new WindowsVirtualMachineProperties { AdministratorPassword = administratorPassWord, RoleName = roleName, DeploymentName = deploymentName, Certificate = _certificate, Location = LocationConstants.NorthEurope, UseExistingCloudService = false, SubscriptionId = subscriptionId, CloudServiceName = cloudServiceName, PublicEndpoints = new List<InputEndpoint>(new[] { new InputEndpoint { EndpointName = "web", LocalPort = 80, Port = 80, Protocol = Protocol.TCP } }), VirtualMachineType = VirtualMachineTemplates.WindowsServer2012, VmSize = VmSize.Medium, StorageAccountName = _storageAccountName, DataDisks = new List<DataVirtualHardDisk>(new[] { new DataVirtualHardDisk { LogicalDiskSizeInGB = 100 } }) }; }
/// <summary> /// This gets the host OS image of Windows Server Data Centre and SQL Server 2012 /// </summary> /// <param name="properties">The path to the media space in blob storage where the host vhd will be placed</param> /// <returns>An OSVirtualHardDisk instance</returns> public static OSVirtualHardDisk GetWindowsOSImageFromTemplate(WindowsVirtualMachineProperties properties) { /*<OSVirtualHardDisk> <MediaLink>http://elastacacheweb.blob.core.windows.net/vhds/elastasql.vhd</MediaLink> <SourceImageName>MSFT__Sql-Server-11EVAL-11.0.2215.0-05152012-en-us-30GB.vhd</SourceImageName> </OSVirtualHardDisk>*/ string templateDetails = null; switch (properties.VirtualMachineType) { case VirtualMachineTemplates.BiztalkServer2012: templateDetails = VmConstants.VmTemplateBiztalk; break; case VirtualMachineTemplates.SqlServer2012: templateDetails = VmConstants.VmTemplateSqlServer2012Eval; break; case VirtualMachineTemplates.WindowsServer2008R2SP1: templateDetails = VmConstants.VmTemplateWin2K8SP1DataCentreServer; break; case VirtualMachineTemplates.WindowsServer2012: templateDetails = VmConstants.VmTemplateWin2012DataCentreServer; break; } if(templateDetails == null && properties.CustomTemplateName == null) throw new FluentManagementException("no template specified cannot proceed", "CreateWindowsVirtualMachineDeploymentCommand"); if (properties.CustomTemplateName != null) templateDetails = properties.CustomTemplateName; var namer = new RandomAccountName(); return new OSVirtualHardDisk { DiskLabel = "OsDisk", DiskName = "OsDisk", MediaLink = String.Format("http://{0}.blob.core.windows.net/vhds/{1}{2}.vhd", properties.StorageAccountName, namer.GetNameFromInitString("os"), DateTime.Now.ToString("ddmmyy")), SourceImageName = templateDetails, HostCaching = HostCaching.ReadWrite, }; }
public void Execute() { var properties = new WindowsVirtualMachineProperties() { AdministratorPassword = _applicationFactory.Password, RoleName = _applicationFactory.RoleName, Certificate = _applicationFactory.ManagementCertificate, SubscriptionId = _applicationFactory.SubscriptionId, CloudServiceName = _applicationFactory.CloudServiceName, DeploymentName = _applicationFactory.DeploymentName }; var client = new WindowsVirtualMachineClient(properties); var vm = client.VirtualMachine; System.Console.WriteLine("Virtual machine client"); System.Console.WriteLine("======================"); System.Console.WriteLine("Role name: {0}", vm.RoleName); System.Console.WriteLine("Role size: {0}", vm.RoleSize.ToString()); string rdpFile = Path.Combine(_applicationFactory.PublishSettingsRoot, "vm.rdp"); client.SaveRemoteDesktopFile(rdpFile); System.Console.WriteLine("RDP file saved to: {0}", rdpFile); }
/// <summary> /// Used to create a deployment and add any persistent vm role to the deployment /// </summary> /// <param name="properties"></param> /// <param name="role">The PersistentVMRole</param> /// <returns>The Deployment that is being used</returns> private static Deployment AddPersistentVMRole(WindowsVirtualMachineProperties properties, PersistentVMRole role) { var namer = new RandomAccountName(); var deployment = new Deployment { Name = properties.DeploymentName, // Label = Convert.ToBase64String(Encoding.UTF8.GetBytes(cloudServiceName)) Label = properties.DeploymentName }; role.RoleName = role.RoleName ?? namer.GetPureRandomValue(); var roleList = new RoleList(); roleList.Roles.Add(role); deployment.RoleList = roleList; return deployment; }
/// <summary> /// Gets an ad-hoc deployment for a Windows templated VM instance /// </summary> /// <param name="properties">The VM properties touse for the deployment</param> /// <returns>A valid deployment for the command</returns> public static Deployment GetAdHocWindowsTemplateDeployment(WindowsVirtualMachineProperties properties) { return AddPersistentVMRole(properties, new [] {PersistentVMRole.AddAdhocWindowsRoleTemplate(properties)}); }
/// <summary> /// Constructs a VirtualMachinenClient /// </summary> /// <param name="properties">A valid VirtualMachineProperties object</param> public VirtualMachineClient(WindowsVirtualMachineProperties properties) { Properties = properties; }
/// <summary> /// Checks whether the necessary properties are populated /// </summary> private void EnsureVirtualMachineProperties(WindowsVirtualMachineProperties properties) { if(properties.Certificate == null || String.IsNullOrEmpty(properties.SubscriptionId) || String.IsNullOrEmpty(properties.CloudServiceName) || String.IsNullOrEmpty(properties.StorageAccountName) || String.IsNullOrEmpty(properties.Location)) throw new FluentManagementException("Either certificate, subscription id cloud service name or storage account name not present in properties", "CreateWindowsVirtualMachineDeploymentCommand"); }
public static PersistentVMRole AddAdhocWindowsRoleTemplate(WindowsVirtualMachineProperties properties) { // build the default endpoints var inputEndpoints = new InputEndpoints(); inputEndpoints.AddEndpoint(InputEndpoint.GetDefaultRemoteDesktopSettings()); foreach (var endpoint in properties.PublicEndpoints) { // just in case they've add RDP again if (endpoint.Value != 1433) { inputEndpoints.AddEndpoint(new InputEndpoint() { EndpointName = endpoint.Key, LocalPort = endpoint.Value, // currently we'll only support TCP Protocol = Protocol.TCP }); } } // add the endpoints collections to a network configuration set var network = new NetworkConfigurationSet { InputEndpoints = inputEndpoints }; // build the windows configuration set var windows = new WindowsConfigurationSet { AdminPassword = properties.AdministratorPassword ?? "ElastaPassword101", ResetPasswordOnFirstLogon = true }; OSVirtualHardDisk osDisk = OSVirtualHardDisk.GetWindowsOSImageFromTemplate(properties); var disks = new DataVirtualHardDisks(); for (int i = 0; i < properties.DataDisks.Count; i++) { var label = properties.DataDisks[i].DiskLabel ?? "DataDisk" + i; var name = properties.DataDisks[i].DiskName ?? "DataDisk" + i; var size = properties.DataDisks[i].LogicalDiskSizeInGB < 30 ? 30 : properties.DataDisks[i].LogicalDiskSizeInGB; var disk = DataVirtualHardDisk.GetDefaultDataDisk(properties.StorageAccountName, size, i, name, label); disks.HardDiskCollection.Add(disk); } return new PersistentVMRole { NetworkConfigurationSet = network, OperatingSystemConfigurationSet = windows, RoleSize = properties.VmSize, RoleName = properties.RoleName, HardDisks = disks, OSHardDisk = osDisk }; }
public static PersistentVMRole AddAdhocWindowsRoleTemplate(WindowsVirtualMachineProperties properties) { // build the default endpoints var inputEndpoints = new InputEndpoints(); if(properties.PublicEndpoints == null) properties.PublicEndpoints = new List<InputEndpoint>(); foreach (var endpoint in properties.PublicEndpoints) { inputEndpoints.AddEndpoint(endpoint); } if (properties.PublicEndpoints.All(endpoint => endpoint.Port != 3389)) inputEndpoints.AddEndpoint(InputEndpoint.GetDefaultRemoteDesktopSettings()); // add the endpoints collections to a network configuration set var network = new NetworkConfigurationSet { InputEndpoints = inputEndpoints }; // build the windows configuration set var windows = new WindowsConfigurationSet { AdminPassword = properties.AdministratorPassword ?? "ElastaPassword101", ResetPasswordOnFirstLogon = true }; OSVirtualHardDisk osDisk = OSVirtualHardDisk.GetWindowsOSImageFromTemplate(properties); var disks = new DataVirtualHardDisks(); if (properties.DataDisks != null) { for (int i = 0; i < properties.DataDisks.Count; i++) { var label = properties.DataDisks[i].DiskLabel ?? "DataDisk" + i; var name = properties.DataDisks[i].DiskName ?? "DataDisk" + i; var size = properties.DataDisks[i].LogicalDiskSizeInGB < 30 ? 30 : properties.DataDisks[i].LogicalDiskSizeInGB; var disk = DataVirtualHardDisk.GetDefaultDataDisk(properties.StorageAccountName, size, i, name, label); disks.HardDiskCollection.Add(disk); } } return new PersistentVMRole { NetworkConfigurationSet = network, OperatingSystemConfigurationSet = windows, RoleSize = properties.VmSize, RoleName = properties.RoleName, HardDisks = disks, OSHardDisk = osDisk }; }
/// <summary> /// Adds the default template for a Windows Virtual Machine /// </summary> /// <param name="properties"></param> /// <returns></returns> public static PersistentVMRole AddAdhocWindowsRoleTemplate(WindowsVirtualMachineProperties properties) { // build the windows configuration set var windows = new WindowsConfigurationSet { AdminPassword = properties.AdministratorPassword ?? "ElastaPassword101", ResetPasswordOnFirstLogon = true }; return GetAdHocTemplate(properties, windows); }
internal VirtualMachineManager(string subscriptionId) { Properties = new WindowsVirtualMachineProperties {SubscriptionId = subscriptionId}; }
/// <summary> /// Adds the default template for a Windows Virtual Machine /// </summary> /// <param name="properties"></param> /// <returns></returns> public static PersistentVMRole AddAdhocWindowsRoleTemplate(WindowsVirtualMachineProperties properties) { var namer = new RandomAccountName(); // build the windows configuration set var windows = new WindowsConfigurationSet { AdminUsername = properties.AdministratorUsername ?? "admin", AdminPassword = properties.AdministratorPassword ?? "ElastaPassword101", ComputerName = properties.ComputerName ?? namer.GetPureRandomValue().ToUpper(), ResetPasswordOnFirstLogon = true }; return GetAdHocTemplate(properties, windows); }