Beispiel #1
0
        private VirtualMachineDscExtensionContext GetDscExtensionContext(PSVirtualMachineExtension extension)
        {
            var context = new VirtualMachineDscExtensionContext
            {
                ResourceGroupName = extension.ResourceGroupName,
                Name               = extension.Name,
                Location           = extension.Location,
                Etag               = extension.Etag,
                Publisher          = extension.Publisher,
                ExtensionType      = extension.ExtensionType,
                TypeHandlerVersion = extension.TypeHandlerVersion,
                Id                = extension.Id,
                PublicSettings    = extension.PublicSettings,
                ProtectedSettings = extension.ProtectedSettings,
                ProvisioningState = extension.ProvisioningState,
                Statuses          = extension.Statuses
            };

            DscExtensionPublicSettings extensionPublicSettings = null;

            try
            {
                extensionPublicSettings = DscExtensionSettingsSerializer.DeserializePublicSettings(extension.PublicSettings);
            }
            catch (JsonException e)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new JsonException(
                            String.Format(
                                CultureInfo.CurrentUICulture,
                                Microsoft.Azure.Commands.Compute.Properties.Resources.AzureVMDscWrongSettingsFormat,
                                extension.PublicSettings),
                            e),
                        string.Empty,
                        ErrorCategory.ParserError,
                        null));
            }

            if (extensionPublicSettings == null)
            {
                context.ModulesUrl            = string.Empty;
                context.ConfigurationFunction = string.Empty;
                context.Properties            = null;
            }
            else
            {
                context.ModulesUrl            = extensionPublicSettings.ModulesUrl;
                context.ConfigurationFunction = extensionPublicSettings.ConfigurationFunction;
                if (extensionPublicSettings.Properties != null)
                {
                    context.Properties =
                        new Hashtable(extensionPublicSettings.Properties.ToDictionary(x => x.Name, x => x.Value));
                }
            }

            return(context);
        }
Beispiel #2
0
        private void CreateConfiguration()
        {
            var publicSettings  = new DscExtensionPublicSettings();
            var privateSettings = new DscExtensionPrivateSettings();

            publicSettings.WmfVersion = string.IsNullOrEmpty(WmfVersion) ? "latest" : WmfVersion;

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

                publicSettings.SasToken              = configurationUris.SasToken;
                publicSettings.ModulesUrl            = configurationUris.ModulesUrl;
                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(this.Location))
            {
                this.Location = GetLocationFromVm(this.ResourceGroupName, this.VMName);
            }

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

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

            while (count <= 2)
            {
                op = VirtualMachineExtensionClient.CreateOrUpdate(
                    ResourceGroupName,
                    VMName,
                    parameters);

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

            WriteObject(result);
        }
Beispiel #3
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);
        }
 /// <summary>
 /// Serialize DscPublicSettings to string.
 /// </summary>
 /// <param name="publicSettings"></param>
 /// <returns></returns>
 public static string SerializePublicSettings(DscExtensionPublicSettings publicSettings)
 {
     return JsonConvert.SerializeObject(publicSettings);
 }
        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;
                publicSettings.ConfigurationFunction = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}\\{1}",
                    Path.GetFileNameWithoutExtension(ArchiveBlobName),
                    ConfigurationName);
                Tuple<DscExtensionPublicSettings.Property[], Hashtable> settings =
                    DscSettingsSerializer.SeparatePrivateItems(ConfigurationArgument);
                publicSettings.Properties = settings.Item1;
                privateSettings.Items = settings.Item2;

                privateSettings.DataBlobUri = configurationUris.DataBlobUri;
            }

            var parameters = new VirtualMachineExtension
            {
                Location = Location,
                Name = Name ?? ExtensionNamespace + "." + ExtensionName,
                Type = VirtualMachineExtensionType,
                Publisher = ExtensionNamespace,
                ExtensionType = ExtensionName,
                TypeHandlerVersion = Version,
                // Define the public and private property bags that will be passed to the extension.
                Settings = DscSettingsSerializer.SerializePublicSettings(publicSettings),
                //PrivateConfuguration contains sensitive data in a plain text
                ProtectedSettings = DscSettingsSerializer.SerializePrivateSettings(privateSettings),
                AutoUpgradeMinorVersion = AutoUpdate.IsPresent ? true : false
            };

            //Add retry logic due to CRP service restart known issue CRP bug: 3564713
            var count = 1;
            ComputeLongRunningOperationResponse op = null;
            while (count <= 2)
            {
                op = VirtualMachineExtensionClient.CreateOrUpdate(
                        ResourceGroupName,
                        VMName,
                        parameters);

                if (ComputeOperationStatus.Failed.Equals(op.Status) && op.Error != null && "InternalExecutionError".Equals(op.Error.Code))
                {
                    count++;
                }
                else
                {
                    break;    
                }
            }
            var result = Mapper.Map<PSComputeLongRunningOperation>(op);
            WriteObject(result);
        }
Beispiel #6
0
 /// <summary>
 /// Serialize DscPublicSettings to string.
 /// </summary>
 /// <param name="publicSettings"></param>
 /// <returns></returns>
 public static string SerializePublicSettings(DscExtensionPublicSettings publicSettings)
 {
     return(JsonConvert.SerializeObject(publicSettings));
 }