Represents a persistent Vm role
Inheritance: ICustomXmlSerializer
        /// <summary>
        /// Gets a VM role for Windows which 
        /// </summary>
        public override PersistentVMRole GetVmRole()
        {
            var persistentVirtualMachine = new PersistentVMRole();
            // get all of the root properties
            var firstRoot = _document.Descendants(Namespace + "RoleList").FirstOrDefault();
            var root = firstRoot.Element(Namespace + "Role");
            if(root.Attribute(TypeNamespace + "type").Value != "PersistentVMRole")
                throw new ApplicationException("non persistent vm role detected");

            persistentVirtualMachine.AvailabilityNameSet = GetStringValue(root.Element(Namespace + "AvailabilitySetName"));
            persistentVirtualMachine.RoleSize = GetEnumValue<VmSize>(root.Element(Namespace + "RoleSize"));
            persistentVirtualMachine.RoleName = GetStringValue(root.Element(Namespace + "RoleName"));
            //persistentVirtualMachine.IPAddress = GetStringValue(root.Element(Namespace + "IpAddress"));
            // get the networkconfiguration
            var configurationSets = root.Descendants(Namespace + "ConfigurationSet");
            foreach (var configurationSet in configurationSets)
            {
                persistentVirtualMachine.NetworkConfigurationSet = GetNetworkConfigurationSet(configurationSet);
            }
            // get the hard disk
            var hardDisks = root.Element(Namespace + "DataVirtualHardDisks");
            persistentVirtualMachine.HardDisks = new DataVirtualHardDisks()
                {
                    HardDiskCollection = GetHardDisks(hardDisks)
                };
            var osDisk = GetOSHardDisk(root.Element(Namespace + "OSVirtualHardDisk"));
            persistentVirtualMachine.OSHardDisk = osDisk;
            return persistentVirtualMachine;
        }
        /// <summary>
        /// Gets a VM role for Windows which 
        /// </summary>
        public override List<PersistentVMRole> GetVmRoles()
        {
            var listVm = new List<PersistentVMRole>();
            // get all of the root properties
            var roots = _document.Descendants(Namespace + "RoleList").Elements(Namespace + "Role");
            // get the virtual network name if it exists
            var network = _document.Element(Namespace + "Deployment").Element(Namespace + "VirtualNetworkName");
            string virtualNetwork = null;
            if (network != null)
            {
                virtualNetwork = network.Value;
            }
            foreach (var root in roots)
            {
                var persistentVirtualMachine = new PersistentVMRole();

                if (root.Attribute(TypeNamespace + "type").Value != "PersistentVMRole")
                    throw new ApplicationException("non persistent vm role detected");

                persistentVirtualMachine.AvailabilityNameSet =
                    GetStringValue(root.Element(Namespace + "AvailabilitySetName"));
                persistentVirtualMachine.VirtualNetworkName = virtualNetwork;

                persistentVirtualMachine.RoleSize = GetEnumValue<VmSize>(root.Element(Namespace + "RoleSize"));
                persistentVirtualMachine.RoleName = GetStringValue(root.Element(Namespace + "RoleName"));
                // get the roleinstance from the list
                var roleInstance = _document.Descendants(Namespace + "RoleInstanceList").Elements(Namespace + "RoleInstance").FirstOrDefault(
                    a => a.Element(Namespace + "RoleName").Value == persistentVirtualMachine.RoleName);
                // add the ip address
                persistentVirtualMachine.IPAddress = GetStringValue(roleInstance.Element(Namespace + "IpAddress"));
                persistentVirtualMachine.Status = GetEnumValue<RoleInstanceStatus>(roleInstance.Element(Namespace + "InstanceStatus"));
                // get the networkconfiguration
                var configurationSets = root.Descendants(Namespace + "ConfigurationSet");
                foreach (var configurationSet in configurationSets)
                {
                    persistentVirtualMachine.NetworkConfigurationSet = GetNetworkConfigurationSet(configurationSet);
                }
                // get the hard disk
                var hardDisks = root.Element(Namespace + "DataVirtualHardDisks");
                persistentVirtualMachine.HardDisks = new DataVirtualHardDisks()
                    {
                        HardDiskCollection = GetHardDisks(hardDisks)
                    };
                var osDisk = GetOSHardDisk(root.Element(Namespace + "OSVirtualHardDisk"));
                persistentVirtualMachine.OSHardDisk = osDisk;
                // check and add linux as an OS
                if (osDisk.OS.ToLower() == "linux")
                {
                    persistentVirtualMachine.OperatingSystemConfigurationSet = new LinuxConfigurationSet() { HostName = GetStringValue(roleInstance.Element(Namespace + "HostName")) };
                }

                listVm.Add(persistentVirtualMachine);
            }

            return listVm;
        }
        /// <summary>
        /// Used to create a deployment and add any persistent vm role to the deployment
        /// </summary>
        /// <param name="role">The PersistentVMRole</param>
        /// <param name="cloudServiceName">The Name of the cloud service which the role is present in</param>
        /// <returns>The Deployment that is being used</returns>
        private static Deployment AddPersistentVMRole(string cloudServiceName, PersistentVMRole role)
        {
            var namer      = new RandomAccountName();
            var deployment = new Deployment
            {
                Name = cloudServiceName,
//                                     Label = Convert.ToBase64String(Encoding.UTF8.GetBytes(cloudServiceName))
                Label = cloudServiceName
            };

            role.RoleName = namer.GetPureRandomValue();
            var roleList = new RoleList();

            roleList.Roles.Add(role);
            deployment.RoleList = roleList;
            return(deployment);
        }
Exemple #4
0
        public static PersistentVMRole GetAdHocTemplate(VirtualMachineProperties properties, ConfigurationSet operatingSystemConfigurationSet)
        {
            // build the default endpoints
            var inputEndpoints = new InputEndpoints();

            if (properties.PublicEndpoints == null)
            {
                properties.PublicEndpoints = new List <InputEndpoint>();
            }
            // add all of the endpoints
            foreach (var endpoint in properties.PublicEndpoints)
            {
                inputEndpoints.AddEndpoint(endpoint);
            }

            if (operatingSystemConfigurationSet.ConfigurationSetType == ConfigurationSetType.WindowsProvisioningConfiguration)
            {
                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
            };

            if (properties.EndpointAclRules != null)
            {
                network.EndpointAcl = new EndpointAcl(new EndpointAclRules(properties.EndpointAclRules));
            }

            if (properties.VirtualNetwork != null)
            {
                network.SubnetName = properties.VirtualNetwork.SubnetName;
            }

            OSVirtualHardDisk osDisk = OSVirtualHardDisk.GetOSImageFromTemplate(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);
                }
            }
            var pvm = new PersistentVMRole
            {
                NetworkConfigurationSet         = network,
                OperatingSystemConfigurationSet = operatingSystemConfigurationSet,
                RoleSize   = properties.VmSize,
                RoleName   = properties.RoleName,
                HardDisks  = disks,
                OSHardDisk = osDisk
            };

            if (properties.AvailabilitySet != null)
            {
                pvm.AvailabilityNameSet = properties.AvailabilitySet;
            }

            return(pvm);
        }
 /// <summary>
 /// Used to get a single deployment containing the SQL Server 2012 image
 /// </summary>
 /// <returns>A complete deployment of the SQL Server 2012 image</returns>
 public static Deployment GetDefaultSqlServer2012Deployment(string cloudServiceName, string storageAccount,
                                                            VmSize vmSize = VmSize.Small)
 {
     return(AddPersistentVMRole(cloudServiceName, PersistentVMRole.GetDefaultSqlServer2012VMRole(vmSize, storageAccount)));
 }
        private void DeleteDataDisks(PersistentVMRole vm, IBlobClient client)
        {
            // delete the data disks in the reverse order
            if (vm.HardDisks.HardDiskCollection == null) return;
            for (int i = vm.HardDisks.HardDiskCollection.Count - 1; i >= 0; i--)
            {
                var dataDiskCommand = new DeleteVirtualMachineDiskCommand(vm.HardDisks.HardDiskCollection[i].DiskName)
                {
                    SubscriptionId = SubscriptionId,
                    Certificate = ManagementCertificate
                };
                dataDiskCommand.Execute();

                int pos = vm.HardDisks.HardDiskCollection[i].MediaLink.LastIndexOf('/');
                string diskFile = vm.HardDisks.HardDiskCollection[i].MediaLink.Substring(pos + 1);
                if (client != null)
                    client.DeleteBlob(diskFile);
            }
        }
        public static PersistentVMRole GetAdHocTemplate(VirtualMachineProperties properties, ConfigurationSet operatingSystemConfigurationSet)
        {
            // build the default endpoints
            var inputEndpoints = new InputEndpoints();
            if (properties.PublicEndpoints == null)
                properties.PublicEndpoints = new List<InputEndpoint>();
            // add all of the endpoints
            foreach (var endpoint in properties.PublicEndpoints)
            {
                inputEndpoints.AddEndpoint(endpoint);
            }

            if (operatingSystemConfigurationSet.ConfigurationSetType == ConfigurationSetType.WindowsProvisioningConfiguration)
            {
                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
            };
            if (properties.EndpointAclRules != null)
            {
                network.EndpointAcl = new EndpointAcl(new EndpointAclRules(properties.EndpointAclRules));
            }

            if (properties.VirtualNetwork != null)
            {
                network.SubnetName = properties.VirtualNetwork.SubnetName;
            }

            OSVirtualHardDisk osDisk = OSVirtualHardDisk.GetOSImageFromTemplate(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);
                }
            }
            var pvm = new PersistentVMRole
            {
                NetworkConfigurationSet = network,
                OperatingSystemConfigurationSet = operatingSystemConfigurationSet,
                RoleSize = properties.VmSize,
                RoleName = properties.RoleName,
                HardDisks = disks,
                OSHardDisk = osDisk
            };

            if (properties.AvailabilitySet != null)
            {
                pvm.AvailabilityNameSet = properties.AvailabilitySet;
            }

            return pvm;
        }
 /// <summary>
 /// Used to create a deployment and add any persistent vm role to the deployment
 /// </summary>
 /// <param name="role">The PersistentVMRole</param>
 /// <param name="cloudServiceName">The Name of the cloud service which the role is present in</param>
 /// <returns>The Deployment that is being used</returns>
 private static Deployment AddPersistentVMRole(string cloudServiceName, PersistentVMRole role)
 {
     var namer = new RandomAccountName();
     var deployment = new Deployment
                          {
                              Name = cloudServiceName,
     //                                     Label = Convert.ToBase64String(Encoding.UTF8.GetBytes(cloudServiceName))
                              Label = cloudServiceName
                          };
     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 GetAdHocLinuxTemplateDeployment(List <LinuxVirtualMachineProperties> properties)
 {
     return(AddPersistentVMRole(properties[0], PersistentVMRole.AddAdhocLinuxRoleTemplates(properties)));
 }
 /// <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>
 /// Creates a deployment payload for a predefined template 
 /// </summary>
 /// <returns>A string xml representation</returns>
 protected override string CreatePayload()
 {
     var deployment = new NetworkConfigurationSet {InputEndpoints = AllEndpoints};
     var template = new PersistentVMRole {NetworkConfigurationSet = deployment, RoleName = RoleName};
     var document = new XDocument(template.GetXmlTree());
     return document.ToStringFullXmlDeclarationWithReplace();
 }
 /// <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 a VM role for Windows which
        /// </summary>
        public override List <PersistentVMRole> GetVmRoles()
        {
            var listVm = new List <PersistentVMRole>();
            // get all of the root properties
            var roots = _document.Descendants(Namespace + "RoleList").Elements(Namespace + "Role");
            // get the virtual network name if it exists
            var    network        = _document.Element(Namespace + "Deployment").Element(Namespace + "VirtualNetworkName");
            string virtualNetwork = null;

            if (network != null)
            {
                virtualNetwork = network.Value;
            }
            foreach (var root in roots)
            {
                var persistentVirtualMachine = new PersistentVMRole();

                if (root.Attribute(TypeNamespace + "type").Value != "PersistentVMRole")
                {
                    throw new ApplicationException("non persistent vm role detected");
                }

                persistentVirtualMachine.AvailabilityNameSet =
                    GetStringValue(root.Element(Namespace + "AvailabilitySetName"));
                persistentVirtualMachine.VirtualNetworkName = virtualNetwork;

                persistentVirtualMachine.RoleSize = GetEnumValue <VmSize>(root.Element(Namespace + "RoleSize"));
                persistentVirtualMachine.RoleName = GetStringValue(root.Element(Namespace + "RoleName"));
                // get the roleinstance from the list
                var roleInstance = _document.Descendants(Namespace + "RoleInstanceList").Elements(Namespace + "RoleInstance").FirstOrDefault(
                    a => a.Element(Namespace + "RoleName").Value == persistentVirtualMachine.RoleName);
                // add the ip address
                persistentVirtualMachine.IPAddress = GetStringValue(roleInstance.Element(Namespace + "IpAddress"));
                persistentVirtualMachine.Status    = GetEnumValue <RoleInstanceStatus>(roleInstance.Element(Namespace + "InstanceStatus"));
                // get the networkconfiguration
                var configurationSets = root.Descendants(Namespace + "ConfigurationSet");
                foreach (var configurationSet in configurationSets)
                {
                    persistentVirtualMachine.NetworkConfigurationSet = GetNetworkConfigurationSet(configurationSet);
                }
                // get the hard disk
                var hardDisks = root.Element(Namespace + "DataVirtualHardDisks");
                persistentVirtualMachine.HardDisks = new DataVirtualHardDisks()
                {
                    HardDiskCollection = GetHardDisks(hardDisks)
                };
                var osDisk = GetOSHardDisk(root.Element(Namespace + "OSVirtualHardDisk"));
                persistentVirtualMachine.OSHardDisk = osDisk;
                // check and add linux as an OS
                if (osDisk.OS.ToLower() == "linux")
                {
                    persistentVirtualMachine.OperatingSystemConfigurationSet = new LinuxConfigurationSet()
                    {
                        HostName = GetStringValue(roleInstance.Element(Namespace + "HostName"))
                    };
                }

                listVm.Add(persistentVirtualMachine);
            }

            return(listVm);
        }