public void SetUp() { fileSystem = Substitute.For <ICalamariFileSystem>(); substituter = Substitute.For <IFileSubstituter>(); variables = new CalamariVariableDictionary(); deployment = new RunningDeployment(TestEnvironment.ConstructRootedPath("packages"), variables) { StagingDirectory = StagingDirectory }; }
public RunScriptCommand( CalamariVariableDictionary variables, CombinedScriptEngine scriptEngine) { Options.Add("package=", "Path to the package to extract that contains the package.", v => packageFile = Path.GetFullPath(v)); Options.Add("script=", $"Path to the script to execute. If --package is used, it can be a script inside the package.", v => scriptFileArg = v); Options.Add("scriptParameters=", $"Parameters to pass to the script.", v => scriptParametersArg = v); VariableDictionaryUtils.PopulateOptions(Options); this.variables = variables; this.scriptEngine = scriptEngine; }
private void SubstituteVariablesInScript(string scriptFileName, CalamariVariableDictionary variables) { if (!File.Exists(scriptFileName)) { throw new CommandException("Could not find script file: " + scriptFileName); } var substituter = new FileSubstituter(CalamariPhysicalFileSystem.GetPhysicalFileSystem()); substituter.PerformSubstitution(scriptFileName, variables); }
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); var variables = new CalamariVariableDictionary(variablesFile, sensitiveVariablesFile, sensitiveVariablesPassword); var fileSystem = new WindowsPhysicalFileSystem(); var replacer = new ConfigurationVariablesReplacer(variables.GetFlag(SpecialVariables.Package.IgnoreVariableReplacementErrors)); var jsonReplacer = new JsonConfigurationVariableReplacer(); var scriptEngine = new CombinedScriptEngine(); var substituter = new FileSubstituter(fileSystem); var commandLineRunner = new CommandLineRunner(new SplitCommandOutput(new ConsoleCommandOutput(), new ServiceMessageCommandOutput(variables))); var configurationTransformer = ConfigurationTransformer.FromVariables(variables); var transformFileLocator = new TransformFileLocator(fileSystem); var conventions = new List <IConvention> { new ContributeEnvironmentVariablesConvention(), new LogVariablesConvention(), new ExtractPackageToStagingDirectoryConvention(new GenericPackageExtractorFactory().createStandardGenericPackageExtractor(), fileSystem), new ConfiguredScriptConvention(DeploymentStages.PreDeploy, fileSystem, scriptEngine, commandLineRunner), new PackagedScriptConvention(DeploymentStages.PreDeploy, fileSystem, scriptEngine, commandLineRunner), new SubstituteInFilesConvention(fileSystem, substituter), new ConfigurationTransformsConvention(fileSystem, configurationTransformer, transformFileLocator), new ConfigurationVariablesConvention(fileSystem, replacer), new JsonConfigurationVariablesConvention(jsonReplacer, fileSystem), new PackagedScriptConvention(DeploymentStages.Deploy, fileSystem, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.Deploy, fileSystem, scriptEngine, commandLineRunner), new AzureWebAppConvention(), new PackagedScriptConvention(DeploymentStages.PostDeploy, fileSystem, scriptEngine, commandLineRunner), new ConfiguredScriptConvention(DeploymentStages.PostDeploy, fileSystem, scriptEngine, commandLineRunner), }; var deployment = new RunningDeployment(packageFile, variables); var conventionRunner = new ConventionProcessor(deployment, conventions); conventionRunner.RunConventions(); return(0); }
private int InvokeScript(CalamariVariableDictionary variables) { var validatedScriptFilePath = AssertScriptFileExists(); var scriptEngine = new CombinedScriptEngine(); var runner = new CommandLineRunner( new SplitCommandOutput(new ConsoleCommandOutput(), new ServiceMessageCommandOutput(variables))); var result = scriptEngine.Execute(new Script(validatedScriptFilePath, scriptParameters), variables, runner); return(result.ExitCode); }
static IEnumerable <string> GetVariables(CalamariVariableDictionary variables) { return(variables.GetNames().Select(variable => { var variableValue = variables.IsSensitive(variable) ? DecryptValueCommand(variables.Get(variable)) : $"decode(\"{EncodeValue(variables.Get(variable))}\")"; return $"decode(\"{EncodeValue(variable)}\") : {variableValue}"; })); }
public void ShouldIncludeEncryptedSensitiveVariables() { var result = new CalamariVariableDictionary(firstInsensitiveVariablesFileName, new List <string>() { firstSensitiveVariablesFileName, secondSensitiveVariablesFileName }, encryptionPassword); Assert.AreEqual("firstSensitiveVariableValue", result.Get("firstSensitiveVariableName")); Assert.AreEqual("secondSensitiveVariableValue", result.Get("secondSensitiveVariableName")); Assert.AreEqual("firstInsensitiveVariableValue", result.Get("firstInsensitiveVariableName")); }
private VariableDictionary AddEnvironmentVariables() { var variables = new CalamariVariableDictionary(); var convention = new ContributeEnvironmentVariablesConvention(); convention.Install(new RunningDeployment("C:\\Package.nupkg", variables)); Assert.That(variables.GetNames().Count, Is.GreaterThan(3)); Assert.That(variables.GetRaw(SpecialVariables.Tentacle.Agent.InstanceName), Is.EqualTo("#{env:TentacleInstanceName}")); return(variables); }
public CommandResult Execute(Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var powerShellEngine = new PowerShellScriptEngine(); if (variables.Get(SpecialVariables.Account.AccountType).StartsWith("Azure")) { return(new AzurePowerShellContext().ExecuteScript(powerShellEngine, script, variables, commandLineRunner)); } return(powerShellEngine.Execute(script, variables, commandLineRunner)); }
public CommandResult Execute(string scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var powerShellEngine = new PowerShellScriptEngine(); if (variables.Get(SpecialVariables.Account.AccountType) == "AzureSubscription") { return(new AzurePowerShellContext().ExecuteScript(powerShellEngine, scriptFile, variables, commandLineRunner)); } return(powerShellEngine.Execute(scriptFile, variables, commandLineRunner)); }
CalamariResult ExecuteScript(IScriptWrapper wrapper, string scriptName, CalamariVariableDictionary variables) { var capture = new CaptureCommandOutput(); var runner = new CommandLineRunner(capture); wrapper.NextWrapper = new TerminalScriptWrapper(new PowerShellScriptEngine()); var result = wrapper.ExecuteScript(new Script(scriptName), ScriptSyntax.PowerShell, variables, runner, new StringDictionary()); //var result = psse.Execute(new Script(scriptName), variables, runner); return(new CalamariResult(result.ExitCode, capture)); }
string GetMandatoryVariable(CalamariVariableDictionary variables, string variableName) { var value = variables.Get(variableName); if (string.IsNullOrWhiteSpace(value)) { throw new CommandException($"Variable {variableName} was not supplied"); } return(value); }
public CommandResult Execute(Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner, Dictionary <string, string> environmentVars = null) { var environmentVariablesIncludingProxy = environmentVars ?? new Dictionary <string, string>(); foreach (var proxyVariable in ProxyEnvironmentVariablesGenerator.GenerateProxyEnvironmentVariables()) { environmentVariablesIncludingProxy[proxyVariable.Key] = proxyVariable.Value; } var prepared = PrepareExecution(script, variables, environmentVariablesIncludingProxy); CommandResult result = null; foreach (var execution in prepared) { if (variables.IsSet(SpecialVariables.CopyWorkingDirectoryIncludingKeyTo)) { CopyWorkingDirectory(variables, execution.CommandLineInvocation.WorkingDirectory, execution.CommandLineInvocation.Arguments); } try { if (execution.CommandLineInvocation.Isolate) { using (SemaphoreFactory.Get().Acquire("CalamariSynchronizeProcess", "Waiting for other process to finish executing script")) { result = commandLineRunner.Execute(execution.CommandLineInvocation); } } else { result = commandLineRunner.Execute(execution.CommandLineInvocation); } if (result.ExitCode != 0) { return(result); } } finally { foreach (var temporaryFile in execution.TemporaryFiles) { var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem(); fileSystem.DeleteFile(temporaryFile, FailureOptions.IgnoreFailure); } } } return(result); }
static IEnumerable <string> GetVariableSwitchConditions(CalamariVariableDictionary variables) { return(variables.GetNames().Select(variable => { var variableValue = variables.IsSensitive(variable) ? DecryptValueCommand(variables.Get(variable)) : string.Format("decode_servicemessagevalue \"{0}\"", EncodeValue(variables.Get(variable))); return string.Format(" \"{1}\"){0} {2} ;;{0}", Environment.NewLine, EncodeValue(variable), variableValue); })); }
public CommandResult Execute( Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner, StringDictionary environmentVars = null) { var syntax = ValidateScriptType(script); return(BuildWrapperChain(syntax) .ExecuteScript(script, syntax, variables, commandLineRunner, environmentVars)); }
static void WriteVariableDictionary(CalamariVariableDictionary variables, StringBuilder output) { output.AppendLine("$OctopusParameters = New-Object 'System.Collections.Generic.Dictionary[String,String]' (,[System.StringComparer]::OrdinalIgnoreCase)"); foreach (var variableName in variables.GetNames().Where(name => !SpecialVariables.IsLibraryScriptModule(name))) { var variableValue = variables.IsSensitive(variableName) ? EncryptVariable(variables.Get(variableName)) : EncodeValue(variables.Get(variableName)); output.Append("$OctopusParameters[").Append(EncodeValue(variableName)).Append("] = ").AppendLine(variableValue); } }
public void SetUp() { fileSystem = new WindowsPhysicalFileSystem(); configurationTransformer = Substitute.For <IConfigurationTransformer>(); var deployDirectory = BuildConfigPath(null); variables = new CalamariVariableDictionary(); variables.Set(SpecialVariables.OriginalPackageDirectoryPath, deployDirectory); deployment = new RunningDeployment(deployDirectory, variables); }
public void ShouldBeEnabled(string accountType, string connectionEndpoint, ScriptSyntax syntax, bool expected) { var variables = new CalamariVariableDictionary { { SpecialVariables.Account.AccountType, accountType }, { SpecialVariables.Action.ServiceFabric.ConnectionEndpoint, connectionEndpoint } }; var target = new AzurePowerShellContext(variables); var actual = target.IsEnabled(syntax); actual.Should().Be(expected); }
private static string DeclareVariables(CalamariVariableDictionary variables) { var output = new StringBuilder(); WriteScriptModules(variables, output); output.AppendLine(); WriteVariableDictionary(variables, output); output.AppendLine(); WriteLocalVariables(variables, output); return(output.ToString()); }
static string WriteVariableDictionary(CalamariVariableDictionary variables) { var builder = new StringBuilder(); foreach (var variable in variables.GetNames()) { var variableValue = variables.IsSensitive(variable) ? EncryptVariable(variables.Get(variable)) : EncodeValue(variables.Get(variable)); builder.Append("\t\t\tthis[").Append(EncodeValue(variable)).Append("] = ").Append(variableValue).AppendLine(";"); } return(builder.ToString()); }
public override int Execute(string[] commandLineArguments) { Options.Parse(commandLineArguments); var variables = new CalamariVariableDictionary(variablesFile, sensitiveVariablesFile, sensitiveVariablesPassword); variables.EnrichWithEnvironmentVariables(); variables.LogVariables(); ExtractPackage(variables); SubstituteVariablesInScript(variables); return(InvokeScript(variables)); }
public void ShouldBeEnabled(string clusterUrl, string aksClusterName, string eksClusterName, ScriptSyntax syntax, bool expected) { var variables = new CalamariVariableDictionary { { SpecialVariables.ClusterUrl, clusterUrl }, { SpecialVariables.AksClusterName, aksClusterName }, { SpecialVariables.EksClusterName, eksClusterName } }; var target = new KubernetesContextScriptWrapper(variables); var actual = target.IsEnabled(syntax); actual.Should().Be(expected); }
public void ShouldIncludeCleartextSensitiveVariables() { var sensitiveVariables = new Dictionary <string, string> { { "sensitiveVariableName", "sensitiveVariableValue" } }; File.WriteAllText(sensitiveVariablesFileName, JsonConvert.SerializeObject(sensitiveVariables)); var result = new CalamariVariableDictionary(insensitiveVariablesFileName, sensitiveVariablesFileName, null); Assert.AreEqual("sensitiveVariableValue", result.Get("sensitiveVariableName")); Assert.AreEqual("insensitiveVariableValue", result.Get("insensitiveVariableName")); }
public override int Execute(string[] commandLineArguments) { Options.Parse(commandLineArguments); var variables = new CalamariVariableDictionary(variablesFile, sensitiveVariablesFile, sensitiveVariablesPassword); variables.EnrichWithEnvironmentVariables(); variables.LogVariables(); ImportCertificate(variables); return(0); }
public void ShouldBeEnabledIfScriptSyntaxIsPreferredSyntaxOnEnvironment() { var variables = new CalamariVariableDictionary { { SpecialVariables.ClusterUrl, "Url" }, { SpecialVariables.AksClusterName, "" }, { SpecialVariables.EksClusterName, "" } }; var target = new KubernetesContextScriptWrapper(variables); var actual = target.IsEnabled(ScriptSyntaxHelper.GetPreferredScriptSyntaxForEnvironment()); actual.Should().BeTrue(); }
public void SetUp() { extractor = Substitute.For <IPackageExtractor>(); extractor.GetMetadata(PackageLocation).Returns(new PackageMetadata { Id = "Acme.Web", Version = "1.0.0" }); fileSystem = Substitute.For <ICalamariFileSystem>(); fileSystem.RemoveInvalidFileNameChars(Arg.Any <string>()).Returns(c => c.Arg <string>().Replace("!", "")); variables = new CalamariVariableDictionary(); convention = new ExtractPackageToApplicationDirectoryConvention(extractor, fileSystem, new SystemSemaphore()); }
public void ShouldOutputDiagnosticsLoggingIfEnabled() { var calamariFileSystem = Substitute.For <ICalamariFileSystem>(); var deploymentVariables = new CalamariVariableDictionary(); deploymentVariables.Set(SpecialVariables.Action.Azure.CloudServicePackagePath, @"MyPackage.1.0.0.nupkg"); deploymentVariables.Set(SpecialVariables.Package.AdditionalXmlConfigurationTransforms, @"MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config => MyApplication.ProcessingServer.WorkerRole.dll.config"); deploymentVariables.Set(SpecialVariables.Package.AutomaticallyRunConfigurationTransformationFiles, "True"); deploymentVariables.Set(SpecialVariables.Environment.Name, "my-test-env"); deploymentVariables.Set(SpecialVariables.Package.EnableDiagnosticsConfigTransformationLogging, "True"); deploymentVariables.Set(SpecialVariables.Package.EnabledFeatures, SpecialVariables.Features.ConfigurationTransforms); var runningDeployment = new RunningDeployment(@"c:\temp\MyPackage.1.0.0.nupkg", deploymentVariables); //mock the world calamariFileSystem.DirectoryExists(@"c:\temp").Returns(true); calamariFileSystem.FileExists(@"c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config").Returns(true); calamariFileSystem.FileExists(@"c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.config").Returns(true); calamariFileSystem.EnumerateFilesRecursively(@"c:\temp", "*.config") .Returns(new[] { @"c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config", @"c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.config" }); calamariFileSystem.EnumerateFiles(@"c:\temp", "MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config", @"MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config") .Returns(new[] { @"c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config" }); calamariFileSystem.EnumerateFiles(@"c:\temp", "MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config", @"MyApplication.ProcessingServer.WorkerRole.dll.my-test-env") .Returns(new[] { @"c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config" }); //these variables would normally be set by ExtractPackageToStagingDirectoryConvention Log.SetOutputVariable(SpecialVariables.Package.Output.InstallationDirectoryPath, "c:\\temp", runningDeployment.Variables); Log.SetOutputVariable(SpecialVariables.OriginalPackageDirectoryPath, "c:\\temp", runningDeployment.Variables); var log = new InMemoryLog(); var transformer = Substitute.For <IConfigurationTransformer>(); var fileLocator = new TransformFileLocator(calamariFileSystem, log); new ConfigurationTransformsConvention(calamariFileSystem, transformer, fileLocator, log).Install(runningDeployment); //not completely testing every scenario here, but this is a reasonable half way point to make sure it works without going overboard log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @"Recursively searching for transformation files that match *.config in folder 'c:\temp'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @"Found config file 'c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - checking against transform 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config => MyApplication.ProcessingServer.WorkerRole.dll.config'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - Skipping as file name 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config' does not match the target pattern 'MyApplication.ProcessingServer.WorkerRole.dll.config'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - checking against transform 'Release'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - skipping as neither transform 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.Release.config' nor transform 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.Release' could be found in 'c:\temp'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - checking against transform 'my-test-env'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - skipping as neither transform 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.my-test-env.config' nor transform 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.my-test-env' could be found in 'c:\temp'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @"Found config file 'c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.config'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - checking against transform 'MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config => MyApplication.ProcessingServer.WorkerRole.dll.config'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Info && m.FormattedMessage == @"Transforming 'c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.config' using 'c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config'."); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - checking against transform 'Release'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - skipping as neither transform 'MyApplication.ProcessingServer.WorkerRole.dll.Release.config' nor transform 'MyApplication.ProcessingServer.WorkerRole.dll.Release' could be found in 'c:\temp'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - checking against transform 'my-test-env'"); log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == @" - Skipping as target 'c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.config' has already been transformed by transform 'c:\temp\MyApplication.ProcessingServer.WorkerRole.dll.my-test-env.config'"); }
protected override ScriptExecution PrepareExecution(Script script, CalamariVariableDictionary variables, StringDictionary environmentVars = null) { var workingDirectory = Path.GetDirectoryName(script.File); var configurationFile = BashScriptBootstrapper.PrepareConfigurationFile(workingDirectory, variables); var bootstrapFile = BashScriptBootstrapper.PrepareBootstrapFile(script, configurationFile, workingDirectory); return(new ScriptExecution( new CommandLineInvocation( BashScriptBootstrapper.FindBashExecutable(), BashScriptBootstrapper.FormatCommandArguments(bootstrapFile), workingDirectory, environmentVars), new[] { bootstrapFile, configurationFile } )); }
private static bool IsDebuggingEnabled(CalamariVariableDictionary variables) { var powershellDebugMode = variables[SpecialVariables.Action.PowerShell.DebugMode]; if (string.IsNullOrEmpty(powershellDebugMode)) { return(false); } if (powershellDebugMode.Equals("False", StringComparison.OrdinalIgnoreCase) || powershellDebugMode.Equals("None", StringComparison.OrdinalIgnoreCase)) { return(false); } return(true); }
public CommandResult Execute(Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var workingDirectory = Path.GetDirectoryName(script.File); var configurationFile = BashScriptBootstrapper.PrepareConfigurationFile(workingDirectory, variables); var boostrapFile = BashScriptBootstrapper.PrepareBootstrapFile(script, configurationFile, workingDirectory); using (new TemporaryFile(configurationFile)) using (new TemporaryFile(boostrapFile)) { return(commandLineRunner.Execute(new CommandLineInvocation( BashScriptBootstrapper.FindBashExecutable(), BashScriptBootstrapper.FormatCommandArguments(boostrapFile), workingDirectory))); } }