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