Ejemplo n.º 1
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (string.IsNullOrEmpty(this.Location))
            {
                this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
            }

            var parameters = new VirtualMachineExtension
            {
                Location  = this.Location,
                Publisher = VirtualMachineSqlServerExtensionContext.ExtensionPublishedNamespace,
                VirtualMachineExtensionType = VirtualMachineSqlServerExtensionContext.ExtensionPublishedName,
                TypeHandlerVersion          = string.IsNullOrEmpty(this.Version) ? VirtualMachineSqlServerExtensionContext.ExtensionDefaultVersion : this.Version,
                Settings          = this.GetPublicConfiguration(),
                ProtectedSettings = this.GetPrivateConfiguration(),
            };

            // Add retry logic due to CRP service restart known issue CRP bug: 3564713
            // Similair approach taken in DSC cmdlet as well
            var count = 1;

            Rest.Azure.AzureOperationResponse <VirtualMachineExtension> op = null;
            while (count <= 2)
            {
                try
                {
                    op = VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
                        ResourceGroupName,
                        VMName,
                        Name ?? VirtualMachineSqlServerExtensionContext.ExtensionPublishedNamespace + "." + VirtualMachineSqlServerExtensionContext.ExtensionPublishedName,
                        parameters).GetAwaiter().GetResult();
                }
                catch (Rest.Azure.CloudException ex)
                {
                    var errorReturned = JsonConvert.DeserializeObject <ComputeLongRunningOperationError>(ex.Response.Content);

                    if ("Failed".Equals(errorReturned.Status) &&
                        errorReturned.Error != null && "InternalExecutionError".Equals(errorReturned.Error.Code))
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            var result = Mapper.Map <PSAzureOperationResponse>(op);

            WriteObject(result);
        }
Ejemplo n.º 2
0
        private void CreateConfiguration()
        {
            var publicSettings  = new DscExtensionPublicSettings();
            var privateSettings = new DscExtensionPrivateSettings();

            if (!string.IsNullOrEmpty(ArchiveBlobName))
            {
                ConfigurationUris configurationUris = UploadConfigurationDataToBlob();

                publicSettings.SasToken   = configurationUris.SasToken;
                publicSettings.ModulesUrl = configurationUris.ModulesUrl;

                Hashtable privacySetting = new Hashtable();
                privacySetting.Add("DataCollection", DataCollection);
                publicSettings.Privacy = privacySetting;

                publicSettings.ConfigurationFunction = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}\\{1}",
                    Path.GetFileNameWithoutExtension(ArchiveBlobName),
                    ConfigurationName);
                Tuple <DscExtensionPublicSettings.Property[], Hashtable> settings =
                    DscExtensionSettingsSerializer.SeparatePrivateItems(ConfigurationArgument);
                publicSettings.Properties = settings.Item1;
                privateSettings.Items     = settings.Item2;

                privateSettings.DataBlobUri = configurationUris.DataBlobUri;

                if (!string.IsNullOrEmpty(WmfVersion))
                {
                    publicSettings.WmfVersion = WmfVersion;
                }
            }

            if (string.IsNullOrEmpty(this.Location))
            {
                this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
            }

            var parameters = new VirtualMachineExtension
            {
                Location  = this.Location,
                Publisher = DscExtensionCmdletConstants.ExtensionPublishedNamespace,
                VirtualMachineExtensionType = DscExtensionCmdletConstants.ExtensionPublishedName,
                TypeHandlerVersion          = Version,
                // Define the public and private property bags that will be passed to the extension.
                Settings = publicSettings,
                //PrivateConfuguration contains sensitive data in a plain text
                ProtectedSettings       = privateSettings,
                AutoUpgradeMinorVersion = AutoUpdate.IsPresent
            };

            //Add retry logic due to CRP service restart known issue CRP bug: 3564713
            var count = 1;

            Rest.Azure.AzureOperationResponse <VirtualMachineExtension> op = null;

            while (true)
            {
                try
                {
                    op = VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
                        ResourceGroupName,
                        VMName,
                        Name ?? DscExtensionCmdletConstants.ExtensionPublishedNamespace + "." + DscExtensionCmdletConstants.ExtensionPublishedName,
                        parameters).GetAwaiter().GetResult();

                    break;
                }
                catch (Rest.Azure.CloudException ex)
                {
                    var errorReturned = JsonConvert.DeserializeObject <PSComputeLongRunningOperation>(
                        ex.Response.Content);

                    if ("Failed".Equals(errorReturned.Status) &&
                        errorReturned.Error != null && "InternalExecutionError".Equals(errorReturned.Error.Code))
                    {
                        count++;
                        if (count <= 2)
                        {
                            continue;
                        }
                    }

                    ThrowTerminatingError(new ErrorRecord(ex, "InvalidResult", ErrorCategory.InvalidResult, null));
                }
            }

            var result = ComputeAutoMapperProfile.Mapper.Map <PSAzureOperationResponse>(op);

            WriteObject(result);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();


            VirtualMachine vm = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);

            if (vm != null && vm.Resources != null)
            {
                VirtualMachineExtension extension = vm.Resources.Where(x => x.Publisher.Equals(VirtualMachineSqlServerExtensionContext.ExtensionPublishedNamespace)).FirstOrDefault();
                if (extension != null)
                {
                    this.Name    = extension.Name;
                    this.Version = extension.TypeHandlerVersion;
                }

                this.Location = vm.Location;
            }

            var parameters = new VirtualMachineExtension
            {
                Location  = this.Location,
                Publisher = VirtualMachineSqlServerExtensionContext.ExtensionPublishedNamespace,
                VirtualMachineExtensionType = VirtualMachineSqlServerExtensionContext.ExtensionPublishedType,
                TypeHandlerVersion          = string.IsNullOrEmpty(this.Version) ? VirtualMachineSqlServerExtensionContext.ExtensionDefaultVersion : this.Version,
                Settings          = this.GetPublicConfiguration(),
                ProtectedSettings = this.GetPrivateConfiguration(),
            };

            // Add retry logic due to CRP service restart known issue CRP bug: 3564713
            // Similair approach taken in DSC cmdlet as well
            var count = 1;

            Rest.Azure.AzureOperationResponse <VirtualMachineExtension> op = null;
            while (count <= 2)
            {
                try
                {
                    op = VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
                        ResourceGroupName,
                        VMName,
                        Name ?? VirtualMachineSqlServerExtensionContext.ExtensionPublishedName,
                        parameters).GetAwaiter().GetResult();
                    break;
                }
                catch (Rest.Azure.CloudException ex)
                {
                    var errorReturned = JsonConvert.DeserializeObject <PSComputeLongRunningOperation>(ex.Response.Content);
                    if ("Failed".Equals(errorReturned.Status) &&
                        errorReturned.Error != null && "InternalExecutionError".Equals(errorReturned.Error.Code))
                    {
                        count++;
                    }
                    else
                    {
                        base.ThrowTerminatingError(new ErrorRecord(ex, "InvalidResult", ErrorCategory.InvalidResult, null));
                    }
                }
            }
            var result = Mapper.Map <PSAzureOperationResponse>(op);

            WriteObject(result);
        }