public override int Execute(string[] commandLineArguments) { Options.Parse(commandLineArguments); Guard.NotNullOrWhiteSpace(packageFile, "No package file was specified. Please pass --package YourPackage.nupkg"); if (!File.Exists(packageFile)) throw new CommandException("Could not find package file: " + packageFile); if (variablesFile != null && !File.Exists(variablesFile)) throw new CommandException("Could not find variables file: " + variablesFile); Log.Info("Deploying package: " + packageFile); if (variablesFile != null) Log.Info("Using variables from: " + variablesFile); var variables = new VariableDictionary(variablesFile); var fileSystem = new WindowsPhysicalFileSystem(); var embeddedResources = new ExecutingAssemblyEmbeddedResources(); var scriptEngine = new CombinedScriptEngine(); var commandLineRunner = new CommandLineRunner(new SplitCommandOutput(new ConsoleCommandOutput(), new ServiceMessageCommandOutput(variables))); var azurePackageUploader = new AzurePackageUploader(); var certificateStore = new CalamariCertificateStore(); var cloudCredentialsFactory = new SubscriptionCloudCredentialsFactory(certificateStore); var cloudServiceConfigurationRetriever = new AzureCloudServiceConfigurationRetriever(); var substituter = new FileSubstituter(); var configurationTransformer = new ConfigurationTransformer(variables.GetFlag(SpecialVariables.Package.IgnoreConfigTransformationErrors), variables.GetFlag(SpecialVariables.Package.SuppressConfigTransformationLogging)); var replacer = new ConfigurationVariablesReplacer(); var conventions = new List<IConvention> { new ContributeEnvironmentVariablesConvention(), new LogVariablesConvention(), new ExtractPackageToStagingDirectoryConvention(new LightweightPackageExtractor(), fileSystem), new FindCloudServicePackageConvention(fileSystem), new EnsureCloudServicePackageIsCtpFormatConvention(fileSystem), new ExtractAzureCloudServicePackageConvention(fileSystem), new ChooseCloudServiceConfigurationFileConvention(fileSystem), new ConfiguredScriptConvention(DeploymentStages.PreDeploy, scriptEngine, fileSystem, commandLineRunner), new PackagedScriptConvention(DeploymentStages.PreDeploy, fileSystem, scriptEngine, commandLineRunner), new ConfigureAzureCloudServiceConvention(fileSystem, cloudCredentialsFactory, cloudServiceConfigurationRetriever), new SubstituteInFilesConvention(fileSystem, substituter), new ConfigurationTransformsConvention(fileSystem, configurationTransformer), new ConfigurationVariablesConvention(fileSystem, replacer), new PackagedScriptConvention(DeploymentStages.Deploy, fileSystem, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.Deploy, scriptEngine, fileSystem, commandLineRunner), new RePackageCloudServiceConvention(fileSystem), new UploadAzureCloudServicePackageConvention(fileSystem, azurePackageUploader, cloudCredentialsFactory), new DeployAzureCloudServicePackageConvention(fileSystem, embeddedResources, scriptEngine, commandLineRunner), new PackagedScriptConvention(DeploymentStages.PostDeploy, fileSystem, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.PostDeploy, scriptEngine, fileSystem, commandLineRunner), }; var deployment = new RunningDeployment(packageFile, variables); var conventionRunner = new ConventionProcessor(deployment, conventions); conventionRunner.RunConventions(); return 0; }
public static void LogVariables(this VariableDictionary variables) { if (variables.GetFlag(SpecialVariables.PrintVariables)) { Log.Verbose("The following variables are available:" + Environment.NewLine + variables.ToString(IsPrintable, true)); } if (variables.GetFlag(SpecialVariables.PrintEvaluatedVariables)) { Log.Verbose("The following evaluated variables are available:" + Environment.NewLine + variables.ToString(IsPrintable, false)); } }
public static void LogVariables(this VariableDictionary variables) { if (variables.GetFlag(SpecialVariables.PrintVariables)) { Log.Warn($"{SpecialVariables.PrintVariables} is enabled. This should only be used for debugging problems with variables, and then disabled again for normal deployments."); Log.Verbose("The following variables are available:" + Environment.NewLine + variables.ToString(IsPrintable, true)); } if (variables.GetFlag(SpecialVariables.PrintEvaluatedVariables)) { Log.Warn($"{SpecialVariables.PrintEvaluatedVariables} is enabled. This should only be used for debugging problems with variables, and then disabled again for normal deployments."); Log.Verbose("The following evaluated variables are available:" + Environment.NewLine + variables.ToString(IsPrintable, false)); } }
private static DeploymentSyncOptions DeploymentSyncOptions(VariableDictionary variables) { var syncOptions = new DeploymentSyncOptions { WhatIf = false, UseChecksum = variables.GetFlag(SpecialVariables.Action.Azure.UseChecksum), DoNotDelete = !variables.GetFlag(SpecialVariables.Action.Azure.RemoveAdditionalFiles), }; ApplyAppOfflineDeploymentRule(syncOptions, variables); ApplyPreserveAppDataDeploymentRule(syncOptions, variables); ApplyPreservePathsDeploymentRule(syncOptions, variables); return(syncOptions); }
static void SetAzureModuleLoadingMethod(VariableDictionary variables) { // By default use the Azure PowerShell modules bundled with Calamari // If the flag below is set to 'false', then we will rely on PowerShell module auto-loading to find the Azure modules installed on the server SetOutputVariable("OctopusUseBundledAzureModules", variables.GetFlag(SpecialVariables.Action.Azure.UseBundledAzurePowerShellModules, true).ToString(), variables); SetOutputVariable(SpecialVariables.Action.Azure.Output.ModulePath, BuiltInAzurePowershellModulePath, variables); }
private static void ApplyPreserveAppDataDeploymentRule(DeploymentSyncOptions syncOptions, VariableDictionary variables) { // If PreserveAppData variable set, then create SkipDelete rules for App_Data directory if (variables.GetFlag(SpecialVariables.Action.Azure.PreserveAppData)) { syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDataFiles", "Delete", "filePath", "\\\\App_Data\\\\.*", null)); syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDataDir", "Delete", "dirPath", "\\\\App_Data(\\\\.*|$)", null)); } }
void UpdateConfigurationWithCurrentInstanceCount(XContainer localConfigurationFile, string configurationFileName, VariableDictionary variables) { if (!variables.GetFlag(SpecialVariables.Action.Azure.UseCurrentInstanceCount)) { return; } var serviceName = variables.Get(SpecialVariables.Action.Azure.CloudServiceName); var slot = (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), variables.Get(SpecialVariables.Action.Azure.Slot)); var remoteConfigurationFile = configurationRetriever.GetConfiguration( credentialsFactory.GetCredentials(variables.Get(SpecialVariables.Action.Azure.SubscriptionId), variables.Get(SpecialVariables.Action.Azure.CertificateThumbprint), variables.Get(SpecialVariables.Action.Azure.CertificateBytes)), serviceName, slot); if (remoteConfigurationFile == null) { Log.Info("There is no current deployment of service '{0}' in slot '{1}', so existing instance counts will not be imported.", serviceName, slot); return; } var rolesByCount = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); Log.Verbose("Local instance counts (from " + Path.GetFileName(configurationFileName) + "): "); WithInstanceCounts(localConfigurationFile, (roleName, attribute) => { Log.Verbose(" - " + roleName + " = " + attribute.Value); string value; if (rolesByCount.TryGetValue(roleName, out value)) { attribute.SetValue(value); } }); Log.Verbose("Remote instance counts: "); WithInstanceCounts(remoteConfigurationFile, (roleName, attribute) => { rolesByCount[roleName] = attribute.Value; Log.Verbose(" - " + roleName + " = " + attribute.Value); }); Log.Verbose("Replacing local instance count settings with remote settings: "); WithInstanceCounts(localConfigurationFile, (roleName, attribute) => { string value; if (!rolesByCount.TryGetValue(roleName, out value)) { return; } attribute.SetValue(value); Log.Verbose(" - " + roleName + " = " + attribute.Value); }); }
public void HowToUseTheDictionary() { var variables = new VariableDictionary(); variables.Set("Foo", "Bar"); variables.Set("IsFamous", "True"); variables.Set("FriendCount", "99"); variables.Set("InstallPath", "C:\\#{Directory}"); variables.Set("Directory", "MyDirectory"); Assert.That(variables.Get("InstallPath"), Is.EqualTo("C:\\MyDirectory")); Assert.That(variables.GetRaw("InstallPath"), Is.EqualTo("C:\\#{Directory}")); Assert.That(variables.GetFlag("IsFamous"), Is.EqualTo(true)); Assert.That(variables.GetFlag("IsInfamous"), Is.EqualTo(false)); Assert.That(variables.GetFlag("IsInfamous", true), Is.EqualTo(true)); Assert.That(variables.GetInt32("FriendCount"), Is.EqualTo(99)); Assert.That(variables.GetInt32("FollowerCount"), Is.EqualTo(null)); }
public void HowToUseTheDictionary() { var variables = new VariableDictionary(); variables.Set("Foo", "Bar"); variables.Set("IsFamous", "True"); variables.Set("FriendCount", "99"); variables.Set("InstallPath", "C:\\#{Directory}"); variables.Set("Directory", "MyDirectory"); variables.Get("InstallPath").Should().Be("C:\\MyDirectory"); variables.GetRaw("InstallPath").Should().Be("C:\\#{Directory}"); variables.GetFlag("IsFamous").Should().Be(true); variables.GetFlag("IsInfamous").Should().Be(false); variables.GetFlag("IsInfamous", true).Should().Be(true); variables.GetInt32("FriendCount").Should().Be(99); variables.GetInt32("FollowerCount").Should().Be(null); }
public void UseDictionaryWithCollectionInitializer() { var variables = new VariableDictionary { { "Foo", "Bar" }, { "IsFamous", "True" }, { "FriendCount", "99" }, { "InstallPath", "C:\\#{Directory}" }, { "Directory", "MyDirectory" } }; variables.Get("InstallPath").Should().Be("C:\\MyDirectory"); variables.GetRaw("InstallPath").Should().Be("C:\\#{Directory}"); variables.GetFlag("IsFamous").Should().Be(true); variables.GetFlag("IsInfamous").Should().Be(false); variables.GetFlag("IsInfamous", true).Should().Be(true); variables.GetInt32("FriendCount").Should().Be(99); variables.GetInt32("FollowerCount").Should().Be(null); }
void UpdateConfigurationWithCurrentInstanceCount(XContainer localConfigurationFile, string configurationFileName, VariableDictionary variables) { if (!variables.GetFlag(SpecialVariables.Action.Azure.UseCurrentInstanceCount)) return; var serviceName = variables.Get(SpecialVariables.Action.Azure.CloudServiceName); var slot = (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), variables.Get(SpecialVariables.Action.Azure.Slot)); var remoteConfigurationFile = configurationRetriever.GetConfiguration( credentialsFactory.GetCredentials(variables.Get(SpecialVariables.Action.Azure.SubscriptionId), variables.Get(SpecialVariables.Action.Azure.CertificateThumbprint), variables.Get(SpecialVariables.Action.Azure.CertificateBytes)), serviceName, slot); if (remoteConfigurationFile == null) { Log.Info("There is no current deployment of service '{0}' in slot '{1}', so existing instance counts will not be imported.", serviceName, slot); return; } var rolesByCount = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); Log.Verbose("Local instance counts (from " + Path.GetFileName(configurationFileName) + "): "); WithInstanceCounts(localConfigurationFile, (roleName, attribute) => { Log.Verbose(" - " + roleName + " = " + attribute.Value); string value; if (rolesByCount.TryGetValue(roleName, out value)) { attribute.SetValue(value); } }); Log.Verbose("Remote instance counts: "); WithInstanceCounts(remoteConfigurationFile, (roleName, attribute) => { rolesByCount[roleName] = attribute.Value; Log.Verbose(" - " + roleName + " = " + attribute.Value); }); Log.Verbose("Replacing local instance count settings with remote settings: "); WithInstanceCounts(localConfigurationFile, (roleName, attribute) => { string value; if (!rolesByCount.TryGetValue(roleName, out value)) return; attribute.SetValue(value); Log.Verbose(" - " + roleName + " = " + attribute.Value); }); }
private static void ApplyAppOfflineDeploymentRule(DeploymentSyncOptions syncOptions, VariableDictionary variables) { if (variables.GetFlag(SpecialVariables.Action.Azure.AppOffline)) { var rules = Microsoft.Web.Deployment.DeploymentSyncOptions.GetAvailableRules(); DeploymentRule rule; if (rules.TryGetValue("AppOffline", out rule)) { syncOptions.Rules.Add(rule); } else { Log.Verbose("Azure Deployment API does not support `AppOffline` deployment rule."); } } }
private static void ApplyPreserveAppDataDeploymentRule(DeploymentSyncOptions syncOptions, VariableDictionary variables) { // If PreserveAppData variable set, then create SkipDelete rules for App_Data directory if (variables.GetFlag(SpecialVariables.Action.Azure.PreserveAppData)) { syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDataFiles", "Delete", "filePath", "\\\\App_Data\\\\.*", null)); syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDataDir", "Delete", "dirPath", "\\\\App_Data(\\\\.*|$)", null)); } }
private static DeploymentSyncOptions DeploymentSyncOptions(VariableDictionary variables) { var syncOptions = new DeploymentSyncOptions { WhatIf = false, UseChecksum = true, DoNotDelete = !variables.GetFlag(SpecialVariables.Action.Azure.RemoveAdditionalFiles, false) }; // If PreserveAppData variable set, then create SkipDelete rules for App_Data directory if (variables.GetFlag(SpecialVariables.Action.Azure.PreserveAppData, false)) { syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDataFiles", "Delete", "filePath", "\\\\App_Data\\\\.*", null)); syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDataDir", "Delete", "dirPath", "\\\\App_Data(\\\\.*|$)", null)); } // If PreservePaths variable set, then create SkipDelete rules for each path regex var preservePaths = variables.GetStrings(SpecialVariables.Action.Azure.PreservePaths, ';'); if (preservePaths != null) { for (var i = 0; i < preservePaths.Count; i++) { var path = preservePaths[i]; syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteFiles_" + i, "Delete", "filePath", path, null)); syncOptions.Rules.Add(new DeploymentSkipRule("SkipDeleteDir_" + i, "Delete", "dirPath", path, null)); } } return syncOptions; }
public override int Execute(string[] commandLineArguments) { Options.Parse(commandLineArguments); Guard.NotNullOrWhiteSpace(packageFile, "No package file was specified. Please pass --package YourPackage.nupkg"); if (!File.Exists(packageFile)) { throw new CommandException("Could not find package file: " + packageFile); } if (variablesFile != null && !File.Exists(variablesFile)) { throw new CommandException("Could not find variables file: " + variablesFile); } Log.Info("Deploying package: " + packageFile); if (variablesFile != null) { Log.Info("Using variables from: " + variablesFile); } var variables = new VariableDictionary(variablesFile); var fileSystem = new CalamariPhysicalFileSystem(); var scriptEngine = new ScriptEngineSelector(); var replacer = new ConfigurationVariablesReplacer(); var substituter = new FileSubstituter(); var configurationTransformer = new ConfigurationTransformer(variables.GetFlag(SpecialVariables.Package.IgnoreConfigTransformationErrors), variables.GetFlag(SpecialVariables.Package.SuppressConfigTransformationLogging)); var embeddedResources = new ExecutingAssemblyEmbeddedResources(); var iis = new InternetInformationServer(); var semaphore = new SystemSemaphore(); var commandLineRunner = new CommandLineRunner(new SplitCommandOutput(new ConsoleCommandOutput(), new ServiceMessageCommandOutput(variables))); var journal = new DeploymentJournal(fileSystem, semaphore, variables); var conventions = new List <IConvention> { new ContributeEnvironmentVariablesConvention(), new ContributePreviousInstallationConvention(journal), new LogVariablesConvention(), new AlreadyInstalledConvention(journal), new ExtractPackageToApplicationDirectoryConvention(new LightweightPackageExtractor(), fileSystem, semaphore), new FeatureScriptConvention(DeploymentStages.BeforePreDeploy, fileSystem, embeddedResources, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.PreDeploy, scriptEngine, fileSystem, commandLineRunner), new PackagedScriptConvention(DeploymentStages.PreDeploy, fileSystem, scriptEngine, commandLineRunner), new FeatureScriptConvention(DeploymentStages.AfterPreDeploy, fileSystem, embeddedResources, scriptEngine, commandLineRunner), new SubstituteInFilesConvention(fileSystem, substituter), new ConfigurationTransformsConvention(fileSystem, configurationTransformer), new ConfigurationVariablesConvention(fileSystem, replacer), new AzureConfigurationConvention(), new CopyPackageToCustomInstallationDirectoryConvention(fileSystem), new FeatureScriptConvention(DeploymentStages.BeforeDeploy, fileSystem, embeddedResources, scriptEngine, commandLineRunner), new PackagedScriptConvention(DeploymentStages.Deploy, fileSystem, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.Deploy, scriptEngine, fileSystem, commandLineRunner), new FeatureScriptConvention(DeploymentStages.AfterDeploy, fileSystem, embeddedResources, scriptEngine, commandLineRunner), new LegacyIisWebSiteConvention(fileSystem, iis), new AzureUploadConvention(), new AzureDeploymentConvention(), new FeatureScriptConvention(DeploymentStages.BeforePostDeploy, fileSystem, embeddedResources, scriptEngine, commandLineRunner), new PackagedScriptConvention(DeploymentStages.PostDeploy, fileSystem, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.PostDeploy, scriptEngine, fileSystem, commandLineRunner), new FeatureScriptConvention(DeploymentStages.AfterPostDeploy, fileSystem, embeddedResources, scriptEngine, commandLineRunner), }; var deployment = new RunningDeployment(packageFile, variables); var conventionRunner = new ConventionProcessor(deployment, conventions); try { conventionRunner.RunConventions(); if (!deployment.SkipJournal) { journal.AddJournalEntry(new JournalEntry(deployment, true)); } } catch (Exception) { if (!deployment.SkipJournal) { journal.AddJournalEntry(new JournalEntry(deployment, false)); } throw; } return(0); }
private static DeploymentSyncOptions DeploymentSyncOptions(VariableDictionary variables) { var syncOptions = new DeploymentSyncOptions { WhatIf = false, UseChecksum = true, DoNotDelete = !variables.GetFlag(SpecialVariables.Action.Azure.RemoveAdditionalFiles), }; ApplyAppOfflineDeploymentRule(syncOptions, variables); ApplyPreserveAppDataDeploymentRule(syncOptions, variables); ApplyPreservePathsDeploymentRule(syncOptions, variables); return syncOptions; }
private static void ApplyAppOfflineDeploymentRule(DeploymentSyncOptions syncOptions, VariableDictionary variables) { if (variables.GetFlag(SpecialVariables.Action.Azure.AppOffline)) { var rules = Microsoft.Web.Deployment.DeploymentSyncOptions.GetAvailableRules(); DeploymentRule rule; if (rules.TryGetValue("AppOffline", out rule)) { syncOptions.Rules.Add(rule); } else { Log.Verbose("Azure Deployment API does not support `AppOffline` deployment rule."); } } }