Ejemplo n.º 1
0
        internal void ExecuteCommandNewSM()
        {
            ServiceManagementProfile.Initialize();

            base.ExecuteCommand();

            AzureSubscription currentSubscription = CurrentContext.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();
                    if (currentStorage == null)
                    {
                        throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible);
                    }

                    DateTime dateTimeCreated = DateTime.Now;
                    string diskPartName = VM.RoleName;

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

                    string vhdname = string.Format("{0}-{1}-{2}-{3}-{4}-{5}.vhd", ServiceName, diskPartName, dateTimeCreated.Year, dateTimeCreated.Month, dateTimeCreated.Day, dateTimeCreated.Millisecond);
                    string blobEndpoint = currentStorage.BlobEndpoint.AbsoluteUri;

                    if (blobEndpoint.EndsWith("/") == false)
                    {
                        blobEndpoint += "/";
                    }

                    datadisk.MediaLink = new Uri(blobEndpoint + "vhds/" + vhdname);
                }

                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 (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);
                });
            }

            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;
                            }
                        }
                    }
                }
            }

            ExecuteClientActionNewSM(
                parameters,
                CommandRuntime.ToString(),
                () => this.ComputeClient.VirtualMachines.Update(this.ServiceName, CurrentDeploymentNewSM.Name, this.Name, parameters));
        }
        protected PSArgument[] CreateVirtualMachineUpdateParameters()
        {
            string serviceName = string.Empty;
            string deploymentName = string.Empty;
            string virtualMachineName = string.Empty;
            VirtualMachineUpdateParameters parameters = new VirtualMachineUpdateParameters();

            return ConvertFromObjectsToArguments(new string[] { "ServiceName", "DeploymentName", "VirtualMachineName", "Parameters" }, new object[] { serviceName, deploymentName, virtualMachineName, parameters });
        }
Ejemplo n.º 3
0
 public static VirtualMachineUpdateParameters GetVMUpdateParameters(Role roleToUpdate, string storageAccount,
     IEnumerable<ConfigurationSet> configSets, bool preserveOriginalConfigSets)
 {
     VirtualMachineUpdateParameters updateParameters = new VirtualMachineUpdateParameters
     {
         Label = roleToUpdate.Label,
         RoleName = roleToUpdate.RoleName,
         AvailabilitySetName = roleToUpdate.AvailabilitySetName,
         ConfigurationSets = preserveOriginalConfigSets ? roleToUpdate.ConfigurationSets : new List<ConfigurationSet>(),
         DataVirtualHardDisks = new List<DataVirtualHardDisk>(),
         OSVirtualHardDisk = new OSVirtualHardDisk(),
         ProvisionGuestAgent = roleToUpdate.ProvisionGuestAgent,
         ResourceExtensionReferences = roleToUpdate.ResourceExtensionReferences,
         RoleSize = roleToUpdate.RoleSize
     };
     if (updateParameters.ConfigurationSets == null)
     {
         updateParameters.ConfigurationSets = configSets.ToList();
     }
     else
     {
         foreach (var configurationSet in configSets)
         {
             updateParameters.ConfigurationSets.Add(configurationSet);
         }
     }
     return updateParameters;
 }
 /// <summary>
 /// The Update Role operation adds a virtual machine to an existing
 /// deployment.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/jj157187.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IVirtualMachineOperations.
 /// </param>
 /// <param name='serviceName'>
 /// The name of your service.
 /// </param>
 /// <param name='deploymentName'>
 /// The name of your deployment.
 /// </param>
 /// <param name='virtualMachineName'>
 /// The name of your virtual machine.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to the Update Virtual Machine operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<OperationResponse> BeginUpdatingAsync(this IVirtualMachineOperations operations, string serviceName, string deploymentName, string virtualMachineName, VirtualMachineUpdateParameters parameters)
 {
     return operations.BeginUpdatingAsync(serviceName, deploymentName, virtualMachineName, parameters, CancellationToken.None);
 }
 /// <summary>
 /// The Update Role operation adds a virtual machine to an existing
 /// deployment.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/jj157187.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IVirtualMachineOperations.
 /// </param>
 /// <param name='serviceName'>
 /// The name of your service.
 /// </param>
 /// <param name='deploymentName'>
 /// The name of your deployment.
 /// </param>
 /// <param name='virtualMachineName'>
 /// The name of your virtual machine.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to the Update Virtual Machine operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static OperationResponse BeginUpdating(this IVirtualMachineOperations operations, string serviceName, string deploymentName, string virtualMachineName, VirtualMachineUpdateParameters parameters)
 {
     try
     {
         return operations.BeginUpdatingAsync(serviceName, deploymentName, virtualMachineName, parameters).Result;
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
 /// <summary>
 /// The Begin Updating Role operation adds a virtual machine to an
 /// existing deployment.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/jj157187.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IVirtualMachineOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of your service.
 /// </param>
 /// <param name='deploymentName'>
 /// Required. The name of your deployment.
 /// </param>
 /// <param name='virtualMachineName'>
 /// Required. The name of your virtual machine.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Begin Updating Virtual Machine
 /// operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static OperationResponse BeginUpdating(this IVirtualMachineOperations operations, string serviceName, string deploymentName, string virtualMachineName, VirtualMachineUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IVirtualMachineOperations)s).BeginUpdatingAsync(serviceName, deploymentName, virtualMachineName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }