Exemple #1
0
        public void ExecuteCommand()
        {
            ValidateParameters();

            bool removeAll = Role == null || !Role.Any();
            ExtensionConfigurationBuilder configBuilder = ExtensionManager.GetBuilder(Deployment != null ? Deployment.ExtensionConfiguration : null);

            if ((UninstallConfiguration || removeAll) && configBuilder.ExistAny(ProviderNamespace, ExtensionName))
            {
                // Remove extension for all roles
                configBuilder.RemoveAny(ProviderNamespace, ExtensionName);
                WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionName, ServiceName));

                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else if (!removeAll && configBuilder.Exist(Role, ProviderNamespace, ExtensionName))
            {
                // Remove extension for the specified roles
                bool defaultExists = configBuilder.ExistDefault(ProviderNamespace, ExtensionName);
                foreach (var r in Role)
                {
                    var singleRoleAsArray = new string[] { r };
                    if (configBuilder.Exist(singleRoleAsArray, ProviderNamespace, ExtensionName))
                    {
                        configBuilder.Remove(singleRoleAsArray, ProviderNamespace, ExtensionName);
                        WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromSpecificRoles, ExtensionName, r, ServiceName));
                    }
                    else
                    {
                        WriteWarning(string.Format(Resources.ServiceExtensionNoExistingExtensionsEnabledOnRole, ProviderNamespace, ExtensionName, r));
                    }

                    if (defaultExists)
                    {
                        WriteWarning(string.Format(Resources.ServiceExtensionRemovingSpecificAndApplyingDefault, ExtensionName, r));
                    }
                }

                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else
            {
                WriteWarning(string.Format(Resources.ServiceExtensionNoExistingExtensionsEnabledOnRoles, ProviderNamespace, ExtensionName));
            }

            if (UninstallConfiguration)
            {
                var allConfig      = ExtensionManager.GetBuilder();
                var deploymentList = (from slot in (new string[] { DeploymentSlot.Production.ToString(), DeploymentSlot.Staging.ToString() })
                                      let d = GetDeployment(slot)
                                              where d != null
                                              select d).ToList();
                deploymentList.ForEach(d => allConfig.Add(d.ExtensionConfiguration));
                ExtensionManager.Uninstall(ProviderNamespace, ExtensionName, allConfig.ToConfiguration());
            }
        }
        public void ExecuteCommand()
        {
            ValidateParameters();
            ExtensionConfigurationBuilder configBuilder = ExtensionManager.GetBuilder(Deployment != null ? Deployment.ExtensionConfiguration : null);

            if (UninstallConfiguration && configBuilder.ExistAny(ExtensionNameSpace, ExtensionType))
            {
                configBuilder.RemoveAny(ExtensionNameSpace, ExtensionType);
                WriteVerbose(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionType, ServiceName));
                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else if (configBuilder.Exist(Role, ExtensionNameSpace, ExtensionType))
            {
                configBuilder.Remove(Role, ExtensionNameSpace, ExtensionType);
                if (Role == null || !Role.Any())
                {
                    WriteVerbose(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionType, ServiceName));
                }
                else
                {
                    bool defaultExists = configBuilder.ExistDefault(ExtensionNameSpace, ExtensionType);
                    foreach (var r in Role)
                    {
                        WriteVerbose(string.Format(Resources.ServiceExtensionRemovingFromSpecificRoles, ExtensionType, r, ServiceName));
                        if (defaultExists)
                        {
                            WriteVerbose(string.Format(Resources.ServiceExtensionRemovingSpecificAndApplyingDefault, ExtensionType, r));
                        }
                    }
                }
                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else
            {
                WriteVerbose(string.Format(Resources.ServiceExtensionNoExistingExtensionsEnabledOnRoles, ExtensionNameSpace, ExtensionType));
            }

            if (UninstallConfiguration)
            {
                var allConfig      = ExtensionManager.GetBuilder();
                var deploymentList = (from slot in (new string[] { DeploymentSlotType.Production, DeploymentSlotType.Staging })
                                      let d = GetDeployment(slot)
                                              where d != null
                                              select d).ToList();
                deploymentList.ForEach(d => allConfig.Add(d.ExtensionConfiguration));
                ExtensionManager.Uninstall(ExtensionNameSpace, ExtensionType, allConfig.ToConfiguration());
            }
        }
Exemple #3
0
        protected void RemoveExtension()
        {
            ExtensionConfigurationBuilder configBuilder = ExtensionManager.GetBuilder(Deployment != null ? Deployment.ExtensionConfiguration : null);

            if (UninstallConfiguration && configBuilder.ExistAny(ProviderNamespace, ExtensionName))
            {
                configBuilder.RemoveAny(ProviderNamespace, ExtensionName);
                WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionName, ServiceName));
                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else if (configBuilder.Exist(Role, ProviderNamespace, ExtensionName))
            {
                configBuilder.Remove(Role, ProviderNamespace, ExtensionName);
                if (Role == null || !Role.Any())
                {
                    WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionName, ServiceName));
                }
                else
                {
                    bool defaultExists = configBuilder.ExistDefault(ProviderNamespace, ExtensionName);
                    foreach (var r in Role)
                    {
                        WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromSpecificRoles, ExtensionName, r, ServiceName));
                        if (defaultExists)
                        {
                            WriteWarning(string.Format(Resources.ServiceExtensionRemovingSpecificAndApplyingDefault, ExtensionName, r));
                        }
                    }
                }

                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else
            {
                WriteWarning(string.Format(Resources.ServiceExtensionNoExistingExtensionsEnabledOnRoles, ProviderNamespace, ExtensionName));
            }

            if (UninstallConfiguration)
            {
                var allConfig      = ExtensionManager.GetBuilder();
                var deploymentList = (from slot in (new string[] { DeploymentSlot.Production.ToString(), DeploymentSlot.Staging.ToString() })
                                      let d = GetDeployment(slot)
                                              where d != null
                                              select d).ToList();
                deploymentList.ForEach(d => allConfig.Add(d.ExtensionConfiguration));
                ExtensionManager.Uninstall(ProviderNamespace, ExtensionName, allConfig.ToConfiguration());
            }
        }
        public ExtensionConfiguration Add(DeploymentGetResponse deployment, DeploymentGetResponse peerDeployment, ExtensionConfigurationInput[] inputs, string slot)
        {
            string errorConfigInput = null;

            if (!Validate(inputs, out errorConfigInput))
            {
                throw new Exception(string.Format(Resources.ServiceExtensionCannotApplyExtensionsInSameType, errorConfigInput));
            }

            var oldExtConfig     = deployment.ExtensionConfiguration;
            var oldPeerExtConfig = peerDeployment.ExtensionConfiguration;

            ExtensionConfigurationBuilder configBuilder = this.GetBuilder();

            foreach (ExtensionConfigurationInput context in inputs)
            {
                if (context != null)
                {
                    ExtensionConfiguration currentConfig = this.InstallExtension(context, slot, deployment, peerDeployment);

                    foreach (var r in currentConfig.AllRoles)
                    {
                        if (!this.GetBuilder(oldExtConfig).ExistAny(r.Id))
                        {
                            configBuilder.AddDefault(r.Id);
                        }
                    }
                    foreach (var r in currentConfig.NamedRoles)
                    {
                        foreach (var e in r.Extensions)
                        {
                            if (!this.GetBuilder(oldExtConfig).ExistAny(e.Id))
                            {
                                configBuilder.Add(r.RoleName, e.Id);
                            }
                        }
                    }
                }
            }
            var extConfig = configBuilder.ToConfiguration();

            return(extConfig);
        }
        public Microsoft.WindowsAzure.Management.Compute.Models.ExtensionConfiguration Set(DeploymentGetResponse currentDeployment, ExtensionConfigurationInput[] inputs, string slot)
        {
            string errorConfigInput = null;

            if (!Validate(inputs, out errorConfigInput))
            {
                throw new Exception(string.Format(Resources.ServiceExtensionCannotApplyExtensionsInSameType, errorConfigInput));
            }

            var oldExtConfig = currentDeployment != null ? currentDeployment.ExtensionConfiguration : new ExtensionConfiguration();

            ExtensionConfigurationBuilder configBuilder = this.GetBuilder();

            foreach (ExtensionConfigurationInput context in inputs)
            {
                if (context != null)
                {
                    Microsoft.WindowsAzure.Management.Compute.Models.ExtensionConfiguration currentConfig = this.InstallExtension(context, slot, oldExtConfig);
                    foreach (var r in currentConfig.AllRoles)
                    {
                        if (currentDeployment == null || !this.GetBuilder(currentDeployment.ExtensionConfiguration).ExistAny(r.Id))
                        {
                            configBuilder.AddDefault(r.Id);
                        }
                    }
                    foreach (var r in currentConfig.NamedRoles)
                    {
                        foreach (var e in r.Extensions)
                        {
                            if (currentDeployment == null || !this.GetBuilder(currentDeployment.ExtensionConfiguration).ExistAny(e.Id))
                            {
                                configBuilder.Add(r.RoleName, e.Id);
                            }
                        }
                    }
                }
            }

            var extConfig = configBuilder.ToConfiguration();

            return(extConfig);
        }
        protected void RemoveAntimalwareExtension()
        {
            ProviderNamespace = AntimalwareExtensionProviderNamespace;
            ExtensionName     = AntimalwareExtensionName;

            ExtensionConfigurationBuilder configBuilder = ExtensionManager.GetBuilder(Deployment != null ? Deployment.ExtensionConfiguration : null);

            if (UninstallConfiguration && configBuilder.ExistAny(ProviderNamespace, ExtensionName))
            {
                configBuilder.RemoveAny(ProviderNamespace, ExtensionName);
                WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionName, ServiceName));
                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else if (configBuilder.Exist(Role, ProviderNamespace, ExtensionName))
            {
                configBuilder.Remove(Role, ProviderNamespace, ExtensionName);
                if (Role == null || !Role.Any())
                {
                    WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromAllRoles, ExtensionName, ServiceName));
                }
                else
                {
                    bool defaultExists = configBuilder.ExistDefault(ProviderNamespace, ExtensionName);
                    foreach (var r in Role)
                    {
                        WriteWarning(string.Format(Resources.ServiceExtensionRemovingFromSpecificRoles, ExtensionName, r, ServiceName));
                        if (defaultExists)
                        {
                            WriteWarning(string.Format(Resources.ServiceExtensionRemovingSpecificAndApplyingDefault, ExtensionName, r));
                        }
                    }
                }

                ChangeDeployment(configBuilder.ToConfiguration());
            }
            else
            {
                WriteWarning(string.Format(Resources.ServiceExtensionNoExistingExtensionsEnabledOnRoles, ProviderNamespace, ExtensionName));
            }
        }
        /// <summary>
        /// The configuration must be defined in either allRoles or namedRoles.
        /// Otherwise, it will fail for trying to apply the same extension.
        /// We only apply the fix here but not in ExtensionManager, so other commands won't get affected.
        /// </summary>
        /// <param name="configInput">The configuration used for InstallExtension()</param>
        /// <param name="extConfig">The extension config after InstallExtension()</param>
        private Microsoft.WindowsAzure.Management.Compute.Models.ExtensionConfiguration PostProcessExtensionConfigAfterInstallExtension(
            ExtensionConfigurationInput configInput,
            Microsoft.WindowsAzure.Management.Compute.Models.ExtensionConfiguration extConfig)
        {
            ExtensionConfigurationBuilder builder = ExtensionManager.GetBuilder(extConfig);

            if (configInput.Roles.All(r => r.Default))
            {
                // If the configuration applies to all roles, remove the ones defined in each named roles
                foreach (var role in Deployment.Roles)
                {
                    builder.Remove(role.RoleName, ProviderNamespace, ExtensionName);
                }
            }
            else
            {
                // If the configuration applies to some specific roles and there is already extension defined in allRoles,
                // we remove the setting from allRoles and move it to specific namedRoles.
                if (builder.ExistDefault(ProviderNamespace, ExtensionName))
                {
                    var diagnosticExtensionId = extConfig.AllRoles.FirstOrDefault(ext =>
                    {
                        var e = ExtensionManager.GetExtension(ext.Id);
                        return(e != null && e.ProviderNamespace == ProviderNamespace && e.Type == ExtensionName);
                    }).Id;
                    builder.RemoveDefault(diagnosticExtensionId);

                    foreach (var role in Deployment.Roles)
                    {
                        // The role is previously configured by allRoles, move it to the namedRole itself
                        if (!configInput.Roles.Exists(r => r.RoleName == role.RoleName))
                        {
                            builder.Add(role.RoleName, diagnosticExtensionId);
                        }
                    }
                }
            }

            return(builder.ToConfiguration());
        }
        public ExtensionConfiguration InstallExtension(ExtensionConfigurationInput context, string slot,
                                                       DeploymentGetResponse deployment, DeploymentGetResponse peerDeployment)
        {
            Func <DeploymentGetResponse, ExtensionConfiguration> func = (d) => d == null ? null : d.ExtensionConfiguration;

            ExtensionConfiguration extConfig           = func(deployment);
            ExtensionConfiguration secondSlotExtConfig = func(peerDeployment);

            ExtensionConfigurationBuilder builder = GetBuilder(extConfig);
            ExtensionConfigurationBuilder secondSlotConfigBuilder = null;

            if (secondSlotExtConfig != null)
            {
                secondSlotConfigBuilder = GetBuilder(secondSlotExtConfig);
            }

            foreach (ExtensionRole r in context.Roles)
            {
                var extensionIds = (from index in Enumerable.Range(0, ExtensionIdLiveCycleCount)
                                    select r.GetExtensionId(context.Type, slot, index)).ToList();

                string availableId = context.Id
                                     ?? (from extensionId in extensionIds
                                         where
                                         !builder.ExistAny(extensionId) &&
                                         (secondSlotConfigBuilder == null ||
                                          !secondSlotConfigBuilder.ExistAny(extensionId))
                                         select extensionId).FirstOrDefault();

                var extensionList = (from id in extensionIds
                                     let e = GetExtension(id)
                                             where e != null
                                             select e).ToList();

                string thumbprint          = context.CertificateThumbprint;
                string thumbprintAlgorithm = context.ThumbprintAlgorithm;

                if (context.X509Certificate != null)
                {
                    thumbprint = context.X509Certificate.Thumbprint;
                }
                else
                {
                    GetThumbprintAndAlgorithm(extensionList, availableId, ref thumbprint, ref thumbprintAlgorithm);
                }

                context.CertificateThumbprint = string.IsNullOrWhiteSpace(context.CertificateThumbprint) ? thumbprint : context.CertificateThumbprint;
                context.ThumbprintAlgorithm   = string.IsNullOrWhiteSpace(context.ThumbprintAlgorithm) ? thumbprintAlgorithm : context.ThumbprintAlgorithm;

                if (!string.IsNullOrWhiteSpace(context.CertificateThumbprint) && string.IsNullOrWhiteSpace(context.ThumbprintAlgorithm))
                {
                    context.ThumbprintAlgorithm = ThumbprintAlgorithmStr;
                }
                else if (string.IsNullOrWhiteSpace(context.CertificateThumbprint) && !string.IsNullOrWhiteSpace(context.ThumbprintAlgorithm))
                {
                    context.ThumbprintAlgorithm = string.Empty;
                }

                var existingExtension = extensionList.Find(e => e.Id == availableId);
                if (existingExtension != null)
                {
                    DeleteExtension(availableId);
                }

                if (r.Default)
                {
                    Cmdlet.WriteVerbose(string.Format(Resources.ServiceExtensionSettingForDefaultRole, context.Type));
                }
                else
                {
                    Cmdlet.WriteVerbose(string.Format(Resources.ServiceExtensionSettingForSpecificRole, context.Type, r.RoleName));
                }

                AddExtension(new HostedServiceAddExtensionParameters
                {
                    Id                   = availableId,
                    Thumbprint           = context.CertificateThumbprint,
                    ThumbprintAlgorithm  = context.ThumbprintAlgorithm,
                    ProviderNamespace    = context.ProviderNameSpace,
                    Type                 = context.Type,
                    PublicConfiguration  = context.PublicConfiguration,
                    PrivateConfiguration = context.PrivateConfiguration,
                    Version              = string.IsNullOrEmpty(context.Version) ? ExtensionDefaultVersion : context.Version
                });

                if (r.Default)
                {
                    builder.RemoveDefault(context.ProviderNameSpace, context.Type);
                    builder.AddDefault(availableId);
                }
                else
                {
                    builder.Remove(r.RoleName, context.ProviderNameSpace, context.Type);
                    builder.Add(r.RoleName, availableId);
                }
            }

            return(builder.ToConfiguration());
        }
        public ExtensionConfiguration InstallExtension(ExtensionConfigurationInput context, string slot, Microsoft.WindowsAzure.Management.Compute.Models.ExtensionConfiguration extConfig)
        {
            ExtensionConfigurationBuilder builder = GetBuilder(extConfig);

            foreach (ExtensionRole r in context.Roles)
            {
                var extensionIds = (from index in Enumerable.Range(0, ExtensionIdLiveCycleCount)
                                    select r.GetExtensionId(context.Type, slot, index)).ToList();

                string availableId = (from extensionId in extensionIds
                                      where !builder.ExistAny(extensionId)
                                      select extensionId).FirstOrDefault();

                var extensionList = (from id in extensionIds
                                     let e = GetExtension(id)
                                             where e != null
                                             select e).ToList();

                string thumbprint          = context.CertificateThumbprint;
                string thumbprintAlgorithm = context.ThumbprintAlgorithm;

                if (context.X509Certificate != null)
                {
                    thumbprint = context.X509Certificate.Thumbprint;
                }
                else
                {
                    GetThumbprintAndAlgorithm(extensionList, availableId, ref thumbprint, ref thumbprintAlgorithm);
                }

                context.CertificateThumbprint = string.IsNullOrWhiteSpace(context.CertificateThumbprint) ? thumbprint : context.CertificateThumbprint;
                context.ThumbprintAlgorithm   = string.IsNullOrWhiteSpace(context.ThumbprintAlgorithm) ? thumbprintAlgorithm : context.ThumbprintAlgorithm;

                if (!string.IsNullOrWhiteSpace(context.CertificateThumbprint) && string.IsNullOrWhiteSpace(context.ThumbprintAlgorithm))
                {
                    context.ThumbprintAlgorithm = ThumbprintAlgorithmStr;
                }
                else if (string.IsNullOrWhiteSpace(context.CertificateThumbprint) && !string.IsNullOrWhiteSpace(context.ThumbprintAlgorithm))
                {
                    context.ThumbprintAlgorithm = string.Empty;
                }

                var existingExtension = extensionList.Find(e => e.Id == availableId);
                if (existingExtension != null)
                {
                    DeleteExtension(availableId);
                }

                if (r.Default)
                {
                    Cmdlet.WriteVerbose(string.Format(Resources.ServiceExtensionSettingForDefaultRole, context.Type));
                }
                else
                {
                    Cmdlet.WriteVerbose(string.Format(Resources.ServiceExtensionSettingForSpecificRole, context.Type, r.RoleName));
                }

                AddExtension(new HostedServiceAddExtensionParameters
                {
                    Id                   = availableId,
                    Thumbprint           = context.CertificateThumbprint,
                    ThumbprintAlgorithm  = context.ThumbprintAlgorithm,
                    ProviderNamespace    = context.ProviderNameSpace,
                    Type                 = context.Type,
                    PublicConfiguration  = context.PublicConfiguration,
                    PrivateConfiguration = context.PrivateConfiguration
                });

                if (r.Default)
                {
                    builder.RemoveDefault(context.ProviderNameSpace, context.Type);
                    builder.AddDefault(availableId);
                }
                else
                {
                    builder.Remove(r.RoleName, context.ProviderNameSpace, context.Type);
                    builder.Add(r.RoleName, availableId);
                }
            }

            return(builder.ToConfiguration());
        }