Esempio n. 1
0
        private Management.Compute.Models.Role CreatePersistentVMRole(Tuple <Model.PersistentVM, bool, bool> tuple, CloudStorageAccount currentStorage)
        {
            Model.PersistentVM persistentVM = tuple.Item1;
            bool isVMImage = tuple.Item3;

            var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, persistentVM.RoleName);

            if (!isVMImage)
            {
                if (persistentVM.OSVirtualHardDisk.MediaLink == null && string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName))
                {
                    persistentVM.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
                }
            }

            foreach (var datadisk in persistentVM.DataVirtualHardDisks.Where(d => d.MediaLink == null && string.IsNullOrEmpty(d.DiskName)))
            {
                datadisk.MediaLink = mediaLinkFactory.Create();
            }

            var result = new Management.Compute.Models.Role
            {
                AvailabilitySetName = persistentVM.AvailabilitySetName,
                OSVirtualHardDisk   = isVMImage ? null : Mapper.Map(persistentVM.OSVirtualHardDisk, new Management.Compute.Models.OSVirtualHardDisk()),
                RoleName            = persistentVM.RoleName,
                RoleSize            = persistentVM.RoleSize,
                RoleType            = persistentVM.RoleType,
                Label = persistentVM.Label,
                ProvisionGuestAgent         = persistentVM.ProvisionGuestAgent,
                ResourceExtensionReferences = persistentVM.ProvisionGuestAgent != null && persistentVM.ProvisionGuestAgent.Value ? Mapper.Map <List <ResourceExtensionReference> >(persistentVM.ResourceExtensionReferences) : null,
                VMImageName = isVMImage ? persistentVM.OSVirtualHardDisk.SourceImageName : null
            };

            if (result.OSVirtualHardDisk != null)
            {
                result.OSVirtualHardDisk.IOType = null;
            }

            if (persistentVM.DataVirtualHardDisks != null && persistentVM.DataVirtualHardDisks.Any())
            {
                persistentVM.DataVirtualHardDisks.ForEach(c =>
                {
                    var dataDisk = Mapper.Map(c, new Microsoft.WindowsAzure.Management.Compute.Models.DataVirtualHardDisk());
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    result.DataVirtualHardDisks.Add(dataDisk);
                });
                result.DataVirtualHardDisks.ForEach(d => d.IOType = null);
            }
            else
            {
                result.DataVirtualHardDisks = null;
            }

            if (persistentVM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(persistentVM.ConfigurationSets).ForEach(c => result.ConfigurationSets.Add(c));
            }

            return(result);
        }
Esempio n. 2
0
        private Management.Compute.Models.Role CreatePersistentVMRole(PersistentVM persistentVM, CloudStorageAccount currentStorage)
        {
            if (!string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName) && !NetworkConfigurationSetBuilder.HasNetworkConfigurationSet(persistentVM.ConfigurationSets))
            {
//                var networkConfigurationSetBuilder = new NetworkConfigurationSetBuilder(persistentVM.ConfigurationSets);
//
//                Disk disk = this.Channel.GetDisk(CurrentSubscription.SubscriptionId, persistentVM.OSVirtualHardDisk.DiskName);
//                if (disk.OS == OS.Windows && !persistentVM.NoRDPEndpoint)
//                {
//                    networkConfigurationSetBuilder.AddRdpEndpoint();
//                }
//                else if (disk.OS == OS.Linux && !persistentVM.NoSSHEndpoint)
//                {
//                    networkConfigurationSetBuilder.AddSshEndpoint();
//                }
            }

            var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, persistentVM.RoleName);

            if (persistentVM.OSVirtualHardDisk.MediaLink == null && string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName))
            {
                persistentVM.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            foreach (var datadisk in persistentVM.DataVirtualHardDisks.Where(d => d.MediaLink == null && string.IsNullOrEmpty(d.DiskName)))
            {
                datadisk.MediaLink = mediaLinkFactory.Create();
            }

            var result = new Management.Compute.Models.Role
            {
                AvailabilitySetName = persistentVM.AvailabilitySetName,
                OSVirtualHardDisk   = Mapper.Map(persistentVM.OSVirtualHardDisk, new Management.Compute.Models.OSVirtualHardDisk()),
                RoleName            = persistentVM.RoleName,
                RoleSize            = string.IsNullOrEmpty(persistentVM.RoleSize) ? null :
                                      (VirtualMachineRoleSize?)Enum.Parse(typeof(VirtualMachineRoleSize), persistentVM.RoleSize, true),
                RoleType = persistentVM.RoleType,
                Label    = persistentVM.Label
            };

            if (persistentVM.DataVirtualHardDisks != null)
            {
                persistentVM.DataVirtualHardDisks.ForEach(c =>
                {
                    var dataDisk = Mapper.Map(c, new Microsoft.WindowsAzure.Management.Compute.Models.DataVirtualHardDisk());
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    result.DataVirtualHardDisks.Add(dataDisk);
                });
            }

            if (persistentVM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(persistentVM.ConfigurationSets).ForEach(c => result.ConfigurationSets.Add(c));
            }

            return(result);
        }
        private Management.Compute.Models.Role CreatePersistenVMRole(CloudStorageAccount currentStorage)
        {
            var vm = new Management.Compute.Models.Role
            {
                AvailabilitySetName = AvailabilitySetName,
                RoleName = String.IsNullOrEmpty(Name) ? ServiceName : Name, // default like the portal
                RoleSize = string.IsNullOrEmpty(InstanceSize) ? null :
                           (VirtualMachineRoleSize?)Enum.Parse(typeof(VirtualMachineRoleSize), InstanceSize, true),
                RoleType = "PersistentVMRole",
                Label = ServiceName,
                OSVirtualHardDisk = Mapper.Map(new OSVirtualHardDisk
                {
                    DiskName = null,
                    SourceImageName = ImageName,
                    MediaLink = string.IsNullOrEmpty(MediaLocation) ? null : new Uri(MediaLocation),
                    HostCaching = HostCaching
                }, new Management.Compute.Models.OSVirtualHardDisk())
            };

            if (vm.OSVirtualHardDisk.MediaLink == null && String.IsNullOrEmpty(vm.OSVirtualHardDisk.DiskName))
            {
                var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, vm.RoleName);
                vm.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            var netConfig = CreateNetworkConfigurationSet();

            if (ParameterSetName.Equals("Windows", StringComparison.OrdinalIgnoreCase))
            {
                var windowsConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMModel.WindowsProvisioningConfigurationSet
                {
                    AdminUsername = this.AdminUsername,
                    AdminPassword = Password,
                    ComputerName =
                        string.IsNullOrEmpty(Name) ? ServiceName : Name,
                    EnableAutomaticUpdates = true,
                    ResetPasswordOnFirstLogon = false,
                    StoredCertificateSettings = CertUtilsNewSM.GetCertificateSettings(this.Certificates, this.X509Certificates),
                    WinRM = GetWinRmConfiguration()
                };

                if (windowsConfig.StoredCertificateSettings == null)
                {
                    windowsConfig.StoredCertificateSettings = new Model.PersistentVMModel.CertificateSettingList();
                }

                netConfig.InputEndpoints.Add(new InputEndpoint {LocalPort = 3389, Protocol = "tcp", Name = "RemoteDesktop"});
                if (!this.NoWinRMEndpoint.IsPresent && !this.DisableWinRMHttps.IsPresent)
                {
                    netConfig.InputEndpoints.Add(new InputEndpoint {LocalPort = WinRMConstants.HttpsListenerPort, Protocol = "tcp", Name = WinRMConstants.EndpointName});
                }
                var configurationSets = new Collection<ConfigurationSet>{windowsConfig, netConfig};
                PersistentVMHelper.MapConfigurationSets(configurationSets).ForEach(c => vm.ConfigurationSets.Add(c));
            }
            else
            {
                var linuxConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMModel.LinuxProvisioningConfigurationSet
                {
                    HostName = string.IsNullOrEmpty(this.Name) ? this.ServiceName : this.Name,
                    UserName = this.LinuxUser,
                    UserPassword = this.Password,
                    DisableSshPasswordAuthentication = false
                };

                if (this.SSHKeyPairs != null && this.SSHKeyPairs.Count > 0 ||
                    this.SSHPublicKeys != null && this.SSHPublicKeys.Count > 0)
                {
                    linuxConfig.SSH = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMModel.LinuxProvisioningConfigurationSet.SSHSettings
                    {
                        PublicKeys = this.SSHPublicKeys, 
                        KeyPairs = this.SSHKeyPairs
                    };
                }

                var rdpEndpoint = new InputEndpoint {LocalPort = 22, Protocol = "tcp", Name = "SSH"};
                netConfig.InputEndpoints.Add(rdpEndpoint);

                var configurationSets = new Collection<ConfigurationSet> { linuxConfig, netConfig };
                PersistentVMHelper.MapConfigurationSets(configurationSets).ForEach(c => vm.ConfigurationSets.Add(c));
            }

            return vm;
        }
        private Management.Compute.Models.Role CreatePersistenVMRole(CloudStorageAccount currentStorage)
        {
            var vm = new Management.Compute.Models.Role
            {
                AvailabilitySetName         = AvailabilitySetName,
                RoleName                    = String.IsNullOrEmpty(Name) ? ServiceName : Name, // default like the portal
                RoleSize                    = InstanceSize,
                RoleType                    = "PersistentVMRole",
                Label                       = ServiceName,
                OSVirtualHardDisk           = _isVMImage ? null : Mapper.Map<Management.Compute.Models.OSVirtualHardDisk>(
                                              new OSVirtualHardDisk
                                              {
                                                  DiskName        = null,
                                                  SourceImageName = ImageName,
                                                  MediaLink       = string.IsNullOrEmpty(MediaLocation) ? null : new Uri(MediaLocation),
                                                  HostCaching     = HostCaching
                                              }),
                VMImageName                 = _isVMImage ? this.ImageName : null,
                MediaLocation               = _isVMImage && !string.IsNullOrEmpty(this.MediaLocation) ? new Uri(this.MediaLocation) : null,
                ProvisionGuestAgent         = !this.DisableGuestAgent,
                ResourceExtensionReferences = this.DisableGuestAgent ? null : Mapper.Map<List<Management.Compute.Models.ResourceExtensionReference>>(
                    new VirtualMachineExtensionImageFactory(this.ComputeClient).MakeList(
                        VirtualMachineBGInfoExtensionCmdletBase.ExtensionDefaultPublisher,
                        VirtualMachineBGInfoExtensionCmdletBase.ExtensionDefaultName,
                        VirtualMachineBGInfoExtensionCmdletBase.ExtensionDefaultVersion))
            };

            if (!_isVMImage && vm.OSVirtualHardDisk.MediaLink == null && String.IsNullOrEmpty(vm.OSVirtualHardDisk.Name))
            {
                var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, vm.RoleName);
                vm.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }
            
            string customDataBase64Str = null;
            if (!string.IsNullOrEmpty(this.CustomDataFile))
            {
                string fileName = this.TryResolvePath(this.CustomDataFile);
                customDataBase64Str = PersistentVMHelper.ConvertCustomDataFileToBase64(fileName);
            }

            var configurationSets = new Collection<ConfigurationSet>();
            var netConfig = CreateNetworkConfigurationSet();

            if (ParameterSetName.Equals(WindowsParamSet, StringComparison.OrdinalIgnoreCase))
            {
                if (this.AdminUsername != null && this.Password != null)
                {
                    var windowsConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.WindowsProvisioningConfigurationSet
                    {
                        AdminUsername = this.AdminUsername,
                        AdminPassword = SecureStringHelper.GetSecureString(Password),
                        ComputerName = string.IsNullOrEmpty(Name) ? ServiceName : Name,
                        EnableAutomaticUpdates = true,
                        ResetPasswordOnFirstLogon = false,
                        StoredCertificateSettings = CertUtilsNewSM.GetCertificateSettings(this.Certificates, this.X509Certificates),
                        WinRM = GetWinRmConfiguration(),
                        CustomData = customDataBase64Str
                    };

                    if (windowsConfig.StoredCertificateSettings == null)
                    {
                        windowsConfig.StoredCertificateSettings = new Model.CertificateSettingList();
                    }

                    configurationSets.Add(windowsConfig);
                }

                netConfig.InputEndpoints.Add(new InputEndpoint {LocalPort = 3389, Protocol = "tcp", Name = "RemoteDesktop"});
                if (!this.NoWinRMEndpoint.IsPresent && !this.DisableWinRMHttps.IsPresent)
                {
                    netConfig.InputEndpoints.Add(new InputEndpoint {LocalPort = WinRMConstants.HttpsListenerPort, Protocol = "tcp", Name = WinRMConstants.EndpointName});
                }

                configurationSets.Add(netConfig);
            }
            else if (ParameterSetName.Equals(LinuxParamSet, StringComparison.OrdinalIgnoreCase))
            {
                if (this.LinuxUser != null && this.Password != null)
                {
                    var linuxConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.LinuxProvisioningConfigurationSet
                    {
                        HostName = string.IsNullOrEmpty(this.Name) ? this.ServiceName : this.Name,
                        UserName = this.LinuxUser,
                        UserPassword = SecureStringHelper.GetSecureString(this.Password),
                        DisableSshPasswordAuthentication = false,
                        CustomData = customDataBase64Str
                    };

                    if (this.SSHKeyPairs != null && this.SSHKeyPairs.Count > 0 ||
                        this.SSHPublicKeys != null && this.SSHPublicKeys.Count > 0)
                    {
                        linuxConfig.SSH = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.LinuxProvisioningConfigurationSet.SSHSettings
                        {
                            PublicKeys = this.SSHPublicKeys,
                            KeyPairs = this.SSHKeyPairs
                        };
                    }

                    configurationSets.Add(linuxConfig);
                }

                var rdpEndpoint = new InputEndpoint {LocalPort = 22, Protocol = "tcp", Name = "SSH"};
                netConfig.InputEndpoints.Add(rdpEndpoint);
                configurationSets.Add(netConfig);
            }

            PersistentVMHelper.MapConfigurationSets(configurationSets).ForEach(c => vm.ConfigurationSets.Add(c));

            return vm;
        }
Esempio n. 5
0
        private Management.Compute.Models.Role CreatePersistentVMRole(Tuple<Model.PersistentVM, bool, bool> tuple, CloudStorageAccount currentStorage)
        {
            Model.PersistentVM persistentVM = tuple.Item1;
            bool isVMImage = tuple.Item3;

            var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, persistentVM.RoleName);

            if (!isVMImage)
            {
                if (persistentVM.OSVirtualHardDisk.MediaLink == null && string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName))
                {
                    persistentVM.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
                }
            }

            foreach (var datadisk in persistentVM.DataVirtualHardDisks.Where(d => d.MediaLink == null && string.IsNullOrEmpty(d.DiskName)))
            {
                datadisk.MediaLink = mediaLinkFactory.Create();
            }

            var result = new Management.Compute.Models.Role
            {
                AvailabilitySetName = persistentVM.AvailabilitySetName,
                OSVirtualHardDisk = isVMImage ? null : Mapper.Map(persistentVM.OSVirtualHardDisk, new Management.Compute.Models.OSVirtualHardDisk()),
                RoleName = persistentVM.RoleName,
                RoleSize = persistentVM.RoleSize,
                RoleType = persistentVM.RoleType,
                Label = persistentVM.Label,
                ProvisionGuestAgent = persistentVM.ProvisionGuestAgent,
                ResourceExtensionReferences = persistentVM.ProvisionGuestAgent != null && persistentVM.ProvisionGuestAgent.Value ? Mapper.Map<List<ResourceExtensionReference>>(persistentVM.ResourceExtensionReferences) : null,
                VMImageName = isVMImage ? persistentVM.OSVirtualHardDisk.SourceImageName : null,
                MediaLocation = isVMImage ? persistentVM.OSVirtualHardDisk.MediaLink : null,
            };

            if (persistentVM.VMImageInput != null)
            {
                result.VMImageInput = isVMImage ? PersistentVMHelper.MapVMImageInput(persistentVM.VMImageInput) : null;
            }

            if (result.OSVirtualHardDisk != null)
            {
                result.OSVirtualHardDisk.IOType = null;
            }

            if (persistentVM.DataVirtualHardDisks != null && persistentVM.DataVirtualHardDisks.Any())
            {
                persistentVM.DataVirtualHardDisks.ForEach(c =>
                {
                    var dataDisk = Mapper.Map(c, new Microsoft.WindowsAzure.Management.Compute.Models.DataVirtualHardDisk());
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    result.DataVirtualHardDisks.Add(dataDisk);
                });
                result.DataVirtualHardDisks.ForEach(d => d.IOType = null);
            }
            else
            {
                result.DataVirtualHardDisks = null;
            }

            if (persistentVM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(persistentVM.ConfigurationSets).ForEach(c => result.ConfigurationSets.Add(c));
            }

            return result;
        }
Esempio n. 6
0
        private Management.Compute.Models.Role CreatePersistentVMRole(PersistentVM persistentVM, CloudStorageAccount currentStorage)
        {
            if (!string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName) && !NetworkConfigurationSetBuilder.HasNetworkConfigurationSet(persistentVM.ConfigurationSets))
            {
//                var networkConfigurationSetBuilder = new NetworkConfigurationSetBuilder(persistentVM.ConfigurationSets);
//
//                Disk disk = this.Channel.GetDisk(CurrentSubscription.SubscriptionId, persistentVM.OSVirtualHardDisk.DiskName);
//                if (disk.OS == OS.Windows && !persistentVM.NoRDPEndpoint)
//                {
//                    networkConfigurationSetBuilder.AddRdpEndpoint();
//                }
//                else if (disk.OS == OS.Linux && !persistentVM.NoSSHEndpoint)
//                {
//                    networkConfigurationSetBuilder.AddSshEndpoint();
//                }
            }

            var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, persistentVM.RoleName);

            if (persistentVM.OSVirtualHardDisk.MediaLink == null && string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName))
            {
                persistentVM.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            foreach (var datadisk in persistentVM.DataVirtualHardDisks.Where(d => d.MediaLink == null && string.IsNullOrEmpty(d.DiskName)))
            {
                datadisk.MediaLink = mediaLinkFactory.Create();
            }

            var result = new Management.Compute.Models.Role
            {
                AvailabilitySetName = persistentVM.AvailabilitySetName,
                OSVirtualHardDisk = Mapper.Map(persistentVM.OSVirtualHardDisk, new Management.Compute.Models.OSVirtualHardDisk()),
                RoleName = persistentVM.RoleName,
                RoleSize = string.IsNullOrEmpty(persistentVM.RoleSize) ? null :
                           (VirtualMachineRoleSize?)Enum.Parse(typeof(VirtualMachineRoleSize), persistentVM.RoleSize, true),
                RoleType = persistentVM.RoleType,
                Label = persistentVM.Label
            };

            if (persistentVM.DataVirtualHardDisks != null)
            {
                persistentVM.DataVirtualHardDisks.ForEach(c => 
                {
                    var dataDisk = Mapper.Map(c, new Microsoft.WindowsAzure.Management.Compute.Models.DataVirtualHardDisk());
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    result.DataVirtualHardDisks.Add(dataDisk);
                });
            }

            if (persistentVM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(persistentVM.ConfigurationSets).ForEach(c => result.ConfigurationSets.Add(c));
            }

            return result;
        }
Esempio n. 7
0
        private Management.Compute.Models.Role CreatePersistentVMRole(PersistentVM persistentVM, CloudStorageAccount currentStorage)
        {
            var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, persistentVM.RoleName);
            if (persistentVM.OSVirtualHardDisk.MediaLink == null && string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName))
            {
                persistentVM.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            foreach (var datadisk in persistentVM.DataVirtualHardDisks.Where(d => d.MediaLink == null && string.IsNullOrEmpty(d.DiskName)))
            {
                datadisk.MediaLink = mediaLinkFactory.Create();
            }

            var result = new Management.Compute.Models.Role
            {
                AvailabilitySetName = persistentVM.AvailabilitySetName,
                OSVirtualHardDisk = Mapper.Map(persistentVM.OSVirtualHardDisk, new Management.Compute.Models.OSVirtualHardDisk()),
                RoleName = persistentVM.RoleName,
                RoleSize = persistentVM.RoleSize,
                RoleType = persistentVM.RoleType,
                Label = persistentVM.Label,
                ProvisionGuestAgent = persistentVM.ProvisionGuestAgent,
                ResourceExtensionReferences = persistentVM.ProvisionGuestAgent != null && persistentVM.ProvisionGuestAgent.Value ? Mapper.Map<List<ResourceExtensionReference>>(persistentVM.ResourceExtensionReferences) : null
            };

            if (persistentVM.DataVirtualHardDisks != null)
            {
                persistentVM.DataVirtualHardDisks.ForEach(c => 
                {
                    var dataDisk = Mapper.Map(c, new Microsoft.WindowsAzure.Management.Compute.Models.DataVirtualHardDisk());
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    result.DataVirtualHardDisks.Add(dataDisk);
                });
            }

            if (persistentVM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(persistentVM.ConfigurationSets).ForEach(c => result.ConfigurationSets.Add(c));
            }

            return result;
        }
Esempio n. 8
0
        private Management.Compute.Models.Role CreatePersistenVMRole(CloudStorageAccount currentStorage)
        {
            var vm = new Management.Compute.Models.Role
            {
                AvailabilitySetName = AvailabilitySetName,
                RoleName            = String.IsNullOrEmpty(Name) ? ServiceName : Name,         // default like the portal
                RoleSize            = InstanceSize,
                RoleType            = "PersistentVMRole",
                Label             = ServiceName,
                OSVirtualHardDisk = _isVMImage ? null : Mapper.Map <Management.Compute.Models.OSVirtualHardDisk>(
                    new OSVirtualHardDisk
                {
                    DiskName        = null,
                    SourceImageName = ImageName,
                    MediaLink       = string.IsNullOrEmpty(MediaLocation) ? null : new Uri(MediaLocation),
                    HostCaching     = HostCaching
                }),
                VMImageName                 = _isVMImage ? this.ImageName : null,
                MediaLocation               = _isVMImage && !string.IsNullOrEmpty(this.MediaLocation) ? new Uri(this.MediaLocation) : null,
                ProvisionGuestAgent         = !this.DisableGuestAgent,
                ResourceExtensionReferences = this.DisableGuestAgent ? null : Mapper.Map <List <Management.Compute.Models.ResourceExtensionReference> >(
                    new VirtualMachineExtensionImageFactory(this.ComputeClient).MakeList(
                        VirtualMachineBGInfoExtensionCmdletBase.ExtensionDefaultPublisher,
                        VirtualMachineBGInfoExtensionCmdletBase.ExtensionDefaultName,
                        VirtualMachineBGInfoExtensionCmdletBase.ExtensionDefaultVersion))
            };

            if (!_isVMImage && vm.OSVirtualHardDisk.MediaLink == null && String.IsNullOrEmpty(vm.OSVirtualHardDisk.Name))
            {
                var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, vm.RoleName);
                vm.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            string customDataBase64Str = null;

            if (!string.IsNullOrEmpty(this.CustomDataFile))
            {
                string fileName = this.TryResolvePath(this.CustomDataFile);
                customDataBase64Str = PersistentVMHelper.ConvertCustomDataFileToBase64(fileName);
            }

            var configurationSets = new Collection <ConfigurationSet>();
            var netConfig         = CreateNetworkConfigurationSet();

            if (ParameterSetName.Equals(WindowsParamSet, StringComparison.OrdinalIgnoreCase))
            {
                if (this.AdminUsername != null && this.Password != null)
                {
                    var windowsConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.WindowsProvisioningConfigurationSet
                    {
                        AdminUsername             = this.AdminUsername,
                        AdminPassword             = SecureStringHelper.GetSecureString(Password),
                        ComputerName              = string.IsNullOrEmpty(Name) ? ServiceName : Name,
                        EnableAutomaticUpdates    = true,
                        ResetPasswordOnFirstLogon = false,
                        StoredCertificateSettings = CertUtilsNewSM.GetCertificateSettings(this.Certificates, this.X509Certificates),
                        WinRM      = GetWinRmConfiguration(),
                        CustomData = customDataBase64Str
                    };

                    if (windowsConfig.StoredCertificateSettings == null)
                    {
                        windowsConfig.StoredCertificateSettings = new Model.CertificateSettingList();
                    }

                    configurationSets.Add(windowsConfig);
                }

                netConfig.InputEndpoints.Add(new InputEndpoint {
                    LocalPort = 3389, Protocol = "tcp", Name = "RemoteDesktop"
                });
                if (!this.NoWinRMEndpoint.IsPresent && !this.DisableWinRMHttps.IsPresent)
                {
                    netConfig.InputEndpoints.Add(new InputEndpoint {
                        LocalPort = WinRMConstants.HttpsListenerPort, Protocol = "tcp", Name = WinRMConstants.EndpointName
                    });
                }

                configurationSets.Add(netConfig);
            }
            else if (ParameterSetName.Equals(LinuxParamSet, StringComparison.OrdinalIgnoreCase))
            {
                if (this.LinuxUser != null && this.Password != null)
                {
                    var linuxConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.LinuxProvisioningConfigurationSet
                    {
                        HostName     = string.IsNullOrEmpty(this.Name) ? this.ServiceName : this.Name,
                        UserName     = this.LinuxUser,
                        UserPassword = SecureStringHelper.GetSecureString(this.Password),
                        DisableSshPasswordAuthentication = false,
                        CustomData = customDataBase64Str
                    };

                    if (this.SSHKeyPairs != null && this.SSHKeyPairs.Count > 0 ||
                        this.SSHPublicKeys != null && this.SSHPublicKeys.Count > 0)
                    {
                        linuxConfig.SSH = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.LinuxProvisioningConfigurationSet.SSHSettings
                        {
                            PublicKeys = this.SSHPublicKeys,
                            KeyPairs   = this.SSHKeyPairs
                        };
                    }

                    configurationSets.Add(linuxConfig);
                }

                var rdpEndpoint = new InputEndpoint {
                    LocalPort = 22, Protocol = "tcp", Name = "SSH"
                };
                netConfig.InputEndpoints.Add(rdpEndpoint);
                configurationSets.Add(netConfig);
            }

            PersistentVMHelper.MapConfigurationSets(configurationSets).ForEach(c => vm.ConfigurationSets.Add(c));

            return(vm);
        }
Esempio n. 9
0
        internal void ExecuteCommandNewSM()
        {
            ServiceManagementProfile.Initialize();

            base.ExecuteCommand();

            AzureSubscription currentSubscription = Profile.Context.Subscription;
            if (CurrentDeploymentNewSM == null)
            {
                throw new ApplicationException(String.Format(Resources.CouldNotFindDeployment, ServiceName, Model.DeploymentSlotType.Production));
            }

            // Auto generate disk names based off of default storage account 
            foreach (var datadisk in this.VM.DataVirtualHardDisks)
            {
                if (datadisk.MediaLink == null && string.IsNullOrEmpty(datadisk.DiskName))
                {
                    CloudStorageAccount currentStorage = currentSubscription.GetCloudStorageAccount(Profile);
                    if (currentStorage == null)
                    {
                        throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible);
                    }

                    string diskPartName = VM.RoleName;
                    if (datadisk.DiskLabel != null)
                    {
                        diskPartName += "-" + datadisk.DiskLabel;
                    }

                    var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, diskPartName);
                    datadisk.MediaLink = mediaLinkFactory.Create();
                }

                if (VM.DataVirtualHardDisks.Count > 1)
                {
                    // To avoid duplicate disk names
                    System.Threading.Thread.Sleep(1);
                }
            }

            var parameters = new VirtualMachineUpdateParameters
            {
                AvailabilitySetName = VM.AvailabilitySetName,
                Label = VM.Label,
                OSVirtualHardDisk = Mapper.Map<OSVirtualHardDisk>(VM.OSVirtualHardDisk),
                RoleName = VM.RoleName,
                RoleSize = VM.RoleSize,
                ProvisionGuestAgent = VM.ProvisionGuestAgent,
                ResourceExtensionReferences = VM.ProvisionGuestAgent != null && VM.ProvisionGuestAgent.Value ? Mapper.Map<List<ResourceExtensionReference>>(VM.ResourceExtensionReferences) : null
            };

            if (parameters.OSVirtualHardDisk != null)
            {
                parameters.OSVirtualHardDisk.IOType = null;
            }

            if (VM.DataVirtualHardDisks != null)
            {
                parameters.DataVirtualHardDisks = new List<DataVirtualHardDisk>();
                VM.DataVirtualHardDisks.ForEach(c =>
                {
                    var dataDisk = Mapper.Map<DataVirtualHardDisk>(c);
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    parameters.DataVirtualHardDisks.Add(dataDisk);
                });
                parameters.DataVirtualHardDisks.ForEach(d => d.IOType = null);
            }

            if (VM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(VM.ConfigurationSets).ForEach(c => parameters.ConfigurationSets.Add(c));
            }

            if (VM.DataVirtualHardDisksToBeDeleted != null && VM.DataVirtualHardDisksToBeDeleted.Any())
            {
                var vmRole = CurrentDeploymentNewSM.Roles.First(r => r.RoleName == this.Name);
                if (vmRole != null)
                {
                    foreach (var dataDiskToBeDeleted in VM.DataVirtualHardDisksToBeDeleted)
                    {
                        int lun = dataDiskToBeDeleted.Lun;
                        try
                        {
                            this.ComputeClient.VirtualMachineDisks.DeleteDataDisk(
                                this.ServiceName,
                                CurrentDeploymentNewSM.Name,
                                vmRole.RoleName,
                                lun,
                                true);
                        }
                        catch (CloudException ex)
                        {
                            WriteWarning(string.Format(Resources.CannotDeleteVirtualMachineDataDiskForLUN, lun));

                            if (ex.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
                            {
                                throw;
                            }
                        }
                    }
                }
            }

            if (VM.DebugSettings != null)
            {
                parameters.DebugSettings = new DebugSettings
                {
                    BootDiagnosticsEnabled = VM.DebugSettings.BootDiagnosticsEnabled
                };
            }

            ExecuteClientActionNewSM(
                parameters,
                CommandRuntime.ToString(),
                () => this.ComputeClient.VirtualMachines.Update(this.ServiceName, CurrentDeploymentNewSM.Name, this.Name, parameters));
        }
Esempio n. 10
0
        private Management.Compute.Models.Role CreatePersistenVMRole(CloudStorageAccount currentStorage)
        {
            var vm = new Management.Compute.Models.Role
            {
                AvailabilitySetName = AvailabilitySetName,
                RoleName = String.IsNullOrEmpty(Name) ? ServiceName : Name, // default like the portal
                RoleSize = string.IsNullOrEmpty(InstanceSize) ? null :
                           (VirtualMachineRoleSize?)Enum.Parse(typeof(VirtualMachineRoleSize), InstanceSize, true),
                RoleType = "PersistentVMRole",
                Label = ServiceName,
                OSVirtualHardDisk = Mapper.Map(new OSVirtualHardDisk
                {
                    DiskName = null,
                    SourceImageName = ImageName,
                    MediaLink = string.IsNullOrEmpty(MediaLocation) ? null : new Uri(MediaLocation),
                    HostCaching = HostCaching
                }, new Management.Compute.Models.OSVirtualHardDisk())
            };

            if (vm.OSVirtualHardDisk.MediaLink == null && String.IsNullOrEmpty(vm.OSVirtualHardDisk.DiskName))
            {
                var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, vm.RoleName);
                vm.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            var netConfig = CreateNetworkConfigurationSet();

            if (ParameterSetName.Equals("Windows", StringComparison.OrdinalIgnoreCase))
            {
                var windowsConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMModel.WindowsProvisioningConfigurationSet
                {
                    AdminUsername = this.AdminUsername,
                    AdminPassword = Password,
                    ComputerName =
                        string.IsNullOrEmpty(Name) ? ServiceName : Name,
                    EnableAutomaticUpdates = true,
                    ResetPasswordOnFirstLogon = false,
                    StoredCertificateSettings = CertUtilsNewSM.GetCertificateSettings(this.Certificates, this.X509Certificates),
                    WinRM = GetWinRmConfiguration()
                };

                if (windowsConfig.StoredCertificateSettings == null)
                {
                    windowsConfig.StoredCertificateSettings = new Model.PersistentVMModel.CertificateSettingList();
                }

                netConfig.InputEndpoints.Add(new InputEndpoint {LocalPort = 3389, Protocol = "tcp", Name = "RemoteDesktop"});
                if (!this.NoWinRMEndpoint.IsPresent && !this.DisableWinRMHttps.IsPresent)
                {
                    netConfig.InputEndpoints.Add(new InputEndpoint {LocalPort = WinRMConstants.HttpsListenerPort, Protocol = "tcp", Name = WinRMConstants.EndpointName});
                }
                var configurationSets = new Collection<ConfigurationSet>{windowsConfig, netConfig};
                PersistentVMHelper.MapConfigurationSets(configurationSets).ForEach(c => vm.ConfigurationSets.Add(c));
            }
            else
            {
                var linuxConfig = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMModel.LinuxProvisioningConfigurationSet
                {
                    HostName = string.IsNullOrEmpty(this.Name) ? this.ServiceName : this.Name,
                    UserName = this.LinuxUser,
                    UserPassword = this.Password,
                    DisableSshPasswordAuthentication = false
                };

                if (this.SSHKeyPairs != null && this.SSHKeyPairs.Count > 0 ||
                    this.SSHPublicKeys != null && this.SSHPublicKeys.Count > 0)
                {
                    linuxConfig.SSH = new Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMModel.LinuxProvisioningConfigurationSet.SSHSettings
                    {
                        PublicKeys = this.SSHPublicKeys, 
                        KeyPairs = this.SSHKeyPairs
                    };
                }

                var rdpEndpoint = new InputEndpoint {LocalPort = 22, Protocol = "tcp", Name = "SSH"};
                netConfig.InputEndpoints.Add(rdpEndpoint);

                var configurationSets = new Collection<ConfigurationSet> { linuxConfig, netConfig };
                PersistentVMHelper.MapConfigurationSets(configurationSets).ForEach(c => vm.ConfigurationSets.Add(c));
            }

            return vm;
        }