public CommandResult Execute( Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner, StringDictionary environmentVars = null) { var workingDirectory = Path.GetDirectoryName(script.File); var executable = PowerShellBootstrapper.PathToPowerShellExecutable(); var bootstrapFile = PowerShellBootstrapper.PrepareBootstrapFile(script, variables); var debuggingBootstrapFile = PowerShellBootstrapper.PrepareDebuggingBootstrapFile(script); var arguments = PowerShellBootstrapper.FormatCommandArguments(bootstrapFile, debuggingBootstrapFile, variables); var userName = variables.Get(SpecialVariables.Action.PowerShell.UserName); var password = ToSecureString(variables.Get(SpecialVariables.Action.PowerShell.Password)); using (new TemporaryFile(bootstrapFile)) { using (new TemporaryFile(debuggingBootstrapFile)) { var invocation = new CommandLineInvocation( executable, arguments, workingDirectory, environmentVars, userName, password); return(commandLineRunner.Execute(invocation)); } } }
public void ShouldIncludeEncryptedSensitiveVariables() { var result = new CalamariVariableDictionary(insensitiveVariablesFileName, sensitiveVariablesFileName, encryptionPassword); Assert.AreEqual("sensitiveVariableValue", result.Get("sensitiveVariableName")); Assert.AreEqual("insensitiveVariableValue", result.Get("insensitiveVariableName")); }
protected override ScriptExecution PrepareExecution(Script script, CalamariVariableDictionary variables, Dictionary<string, string> environmentVars = null) { var workingDirectory = Path.GetDirectoryName(script.File); var executable = PowerShellBootstrapper.PathToPowerShellExecutable(); var bootstrapFile = PowerShellBootstrapper.PrepareBootstrapFile(script, variables); var debuggingBootstrapFile = PowerShellBootstrapper.PrepareDebuggingBootstrapFile(script); var arguments = PowerShellBootstrapper.FormatCommandArguments(bootstrapFile, debuggingBootstrapFile, variables); var userName = variables.Get(SpecialVariables.Action.PowerShell.UserName); var password = ToSecureString(variables.Get(SpecialVariables.Action.PowerShell.Password)); return new ScriptExecution( new CommandLineInvocation( executable, arguments, workingDirectory, environmentVars, userName, password), new[] {bootstrapFile, debuggingBootstrapFile} ); }
/// <summary> /// One of these fields must be present for a k8s step /// </summary> public bool IsEnabled(ScriptSyntax syntax) { return((!string.IsNullOrEmpty(variables.Get(SpecialVariables.ClusterUrl, "")) || !string.IsNullOrEmpty(variables.Get(SpecialVariables.AksClusterName, "")) || !string.IsNullOrEmpty(variables.Get(SpecialVariables.EksClusterName, ""))) && syntax == ScriptSyntaxHelper.GetPreferredScriptSyntaxForEnvironment()); }
void WriteVariableScriptToFile() { if (!TryGetScriptFromVariables(out var scriptBody, out var relativeScriptFile, out var scriptSyntax) && !WasProvided(variables.Get(SpecialVariables.Action.Script.ScriptFileName))) { throw new CommandException($"Could not determine script to run. Please provide either a `{SpecialVariables.Action.Script.ScriptBody}` variable, " + $"or a `{SpecialVariables.Action.Script.ScriptFileName}` variable."); } if (WasProvided(scriptBody)) { var scriptFile = Path.GetFullPath(relativeScriptFile); //Set the name of the script we are about to create to the variables collection for replacement later on variables.Set(SpecialVariables.Action.Script.ScriptFileName, relativeScriptFile); // If the script body was supplied via a variable, then we write it out to a file. // This will be deleted with the working directory. // Bash files need SheBang as first few characters. This does not play well with BOM characters var scriptBytes = scriptSyntax == ScriptSyntax.Bash ? scriptBody.EncodeInUtf8NoBom() : scriptBody.EncodeInUtf8Bom(); File.WriteAllBytes(scriptFile, scriptBytes); } }
public void ShouldSkipIfInstalled() { variables.Set(SpecialVariables.Package.SkipIfAlreadyInstalled, true.ToString()); previous = new JournalEntry("123", "env", "proj", "pkg", "0.0.9", "rp01", DateTime.Now, "C:\\PackageOld.nupkg", "C:\\App", "C:\\MyApp", true); RunConvention(); Assert.That(variables.Get(SpecialVariables.Action.SkipJournal), Is.EqualTo("true")); }
public void ShouldAddVariablesIfPreviousInstallation() { previous = new JournalEntry("123", "tenant", "env", "proj", "rp01", DateTime.Now, "C:\\App", "C:\\MyApp", true, new List <DeployedPackage> { new DeployedPackage("pkg", "0.0.9", "C:\\PackageOld.nupkg") }); RunConvention(); Assert.That(variables.Get(SpecialVariables.Tentacle.PreviousInstallation.OriginalInstalledPath), Is.EqualTo("C:\\App")); }
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")); }
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}"; })); }
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); } }
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 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 CommandResult Execute(Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var powerShellEngine = new PowerShellScriptEngine(); if (!string.IsNullOrEmpty(variables.Get(SpecialVariables.Action.ServiceFabric.ConnectionEndpoint))) { return(new AzureServiceFabricPowerShellContext().ExecuteScript(powerShellEngine, script, variables, commandLineRunner)); } else if (variables.Get(SpecialVariables.Account.AccountType).StartsWith("Azure")) { return(new AzurePowerShellContext().ExecuteScript(powerShellEngine, script, variables, commandLineRunner)); } return(powerShellEngine.Execute(script, variables, commandLineRunner)); }
private static string GetReleaseName(CalamariVariableDictionary variables) { var validChars = new Regex("[^a-zA-Z0-9-]"); var releaseName = variables.Get(SpecialVariables.Helm.ReleaseName)?.ToLower(); if (string.IsNullOrWhiteSpace(releaseName)) { releaseName = $"{variables.Get(Deployment.SpecialVariables.Action.Name)}-{variables.Get(Deployment.SpecialVariables.Environment.Name)}"; releaseName = validChars.Replace(releaseName, "").ToLowerInvariant(); } Log.SetOutputVariable("ReleaseName", releaseName, variables); Log.Info($"Using Release Name {releaseName}"); return(releaseName); }
protected override IEnumerable <ScriptExecution> PrepareExecution(Script script, CalamariVariableDictionary variables, Dictionary <string, string> environmentVars = null) { var powerShellBootstrapper = GetPowerShellBootstrapper(variables); var workingDirectory = Path.GetDirectoryName(script.File); var executable = powerShellBootstrapper.PathToPowerShellExecutable(variables); var(bootstrapFile, otherTemporaryFiles) = powerShellBootstrapper.PrepareBootstrapFile(script, variables); var debuggingBootstrapFile = powerShellBootstrapper.PrepareDebuggingBootstrapFile(script); var arguments = powerShellBootstrapper.FormatCommandArguments(bootstrapFile, debuggingBootstrapFile, variables); var userName = powerShellBootstrapper.AllowImpersonation() ? variables.Get(SpecialVariables.Action.PowerShell.UserName) : null; var password = powerShellBootstrapper.AllowImpersonation() ? ToSecureString(variables.Get(SpecialVariables.Action.PowerShell.Password)) : null; yield return(new ScriptExecution( new CommandLineInvocation( executable, arguments, workingDirectory, environmentVars, userName, password), otherTemporaryFiles.Concat(new[] { bootstrapFile, debuggingBootstrapFile }) )); }
public int ExecuteHealthCheck(CalamariVariableDictionary variables) { var account = AccountFactory.Create(variables); var cloudServiceName = variables.Get(SpecialVariables.Action.Azure.CloudServiceName); if (account is AzureAccount azureAccount) { using (var azureClient = azureAccount.CreateComputeManagementClient(certificateStore)) { var azureResponse = azureClient.HostedServices.List(); if (azureResponse.StatusCode != HttpStatusCode.OK) { throw new Exception("Azure returned HTTP status-code " + azureResponse.StatusCode); } var hostedService = azureResponse.HostedServices.FirstOrDefault(hs => hs.ServiceName == cloudServiceName); if (hostedService == null) { throw new Exception($"Hosted service with name {cloudServiceName} was not found."); } } } else if (account is AzureServicePrincipalAccount servicePrincipalAccount) { throw new Exception($"Cloud service targets cannot use Service Principal accounts, a Management Certificate account is required."); } return(0); }
static string WritePatternMatching(CalamariVariableDictionary variables) { var builder = new StringBuilder(); foreach (var variableName in variables.GetNames()) { var variableValue = variables.Get(variableName); if (variableValue == null) { builder.AppendFormat(" | \"{0}\" -> Some null", EncodeValue(variableName)); } else if (variables.IsSensitive(variableName)) { builder.AppendFormat(" | \"{0}\" -> {1} |> Some", EncodeValue(variableName), EncryptVariable(variableValue)); } else { builder.AppendFormat(" | \"{0}\" -> \"{1}\" |> decode |> Some", EncodeValue(variableName), EncodeValue(variableValue)); } builder.Append(Environment.NewLine); } builder.Append(" | _ -> None"); return(builder.ToString()); }
string GetTomcatVersion(CalamariVariableDictionary variables) { var catalinaHome = variables.Get(SpecialVariables.Action.Java.TomcatDeployCertificate.CatalinaHome) ?? Environment.GetEnvironmentVariable("CATALINA_HOME");; var catalinaPath = Path.Combine(catalinaHome, "lib", "catalina.jar"); if (!File.Exists(catalinaPath)) { throw new CommandException("TOMCAT-HTTPS-ERROR-0018: " + $"Failed to find the file {catalinaPath} " + "http://g.octopushq.com/JavaAppDeploy#tomcat-https-error-0018"); } var version = new StringBuilder(); var versionCheck = SilentProcessRunner.ExecuteCommand(JavaRuntime.CmdPath, $"-cp \"{catalinaPath}\" org.apache.catalina.util.ServerInfo", ".", (stdOut) => { Log.Verbose(stdOut); version.AppendLine(stdOut); }, Console.Error.WriteLine); if (versionCheck.ExitCode != 0) { throw new CommandException($"Attempt to obtain tomcat version failed with exit code {versionCheck.ExitCode}."); } return(version.ToString()); }
public void ShouldPrefixAppPathIfSet() { var rootPath = TestEnvironment.ConstructRootedPath("MyApp"); variables.Set("Octopus.Tentacle.Agent.ApplicationDirectoryPath", rootPath); convention.Install(new RunningDeployment(PackageLocation, variables)); Assert.That(variables.Get("OctopusOriginalPackageDirectoryPath"), Is.EqualTo(Path.Combine(rootPath, "Acme.Web", "1.0.0"))); }
public CommandResult ExecuteScript(IScriptEngine scriptEngine, Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var workingDirectory = Path.GetDirectoryName(script.File); variables.Set("OctopusAzureTargetScript", "\"" + script.File + "\""); variables.Set("OctopusAzureTargetScriptParameters", script.Parameters); SetAzureModuleLoadingMethod(variables); SetOutputVariable(SpecialVariables.Action.Azure.Output.SubscriptionId, variables.Get(SpecialVariables.Action.Azure.SubscriptionId), variables); SetOutputVariable("OctopusAzureStorageAccountName", variables.Get(SpecialVariables.Action.Azure.StorageAccountName), variables); SetOutputVariable("OctopusAzureEnvrionment", variables.Get(SpecialVariables.Action.Azure.Environment, DefaultAzureEnvironment), variables); using (new TemporaryFile(Path.Combine(workingDirectory, "AzureProfile.json"))) using (var contextScriptFile = new TemporaryFile(CreateContextScriptFile(workingDirectory))) { if (variables.Get(SpecialVariables.Account.AccountType) == "AzureServicePrincipal") { SetOutputVariable("OctopusUseServicePrincipal", true.ToString(), variables); SetOutputVariable("OctopusAzureADTenantId", variables.Get(SpecialVariables.Action.Azure.TenantId), variables); SetOutputVariable("OctopusAzureADClientId", variables.Get(SpecialVariables.Action.Azure.ClientId), variables); variables.Set("OctopusAzureADPassword", variables.Get(SpecialVariables.Action.Azure.Password)); return(scriptEngine.Execute(new Script(contextScriptFile.FilePath), variables, commandLineRunner)); } //otherwise use management certificate SetOutputVariable("OctopusUseServicePrincipal", false.ToString(), variables); using (new TemporaryFile(CreateAzureCertificate(workingDirectory, variables))) { return(scriptEngine.Execute(new Script(contextScriptFile.FilePath), variables, commandLineRunner)); } } }
IDictionary <string, (string SubjectCommonName, string CertificatePem, string PrivateKeyPem)> GetSslCertificates(IEnumerable <Binding> enabledBindings, CalamariVariableDictionary variables) { var sslCertsForEnabledBindings = new Dictionary <string, (string, string, string)>(); foreach (var httpsBinding in enabledBindings.Where(b => string.Equals("https", b.Protocol, StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(b.CertificateVariable) )) { var certificateVariable = httpsBinding.CertificateVariable; var subjectCommonName = variables.Get($"{certificateVariable}.SubjectCommonName"); var certificatePem = variables.Get($"{certificateVariable}.CertificatePem"); var privateKeyPem = variables.Get($"{certificateVariable}.PrivateKeyPem"); sslCertsForEnabledBindings.Add(certificateVariable, (subjectCommonName, certificatePem, privateKeyPem)); } return(sslCertsForEnabledBindings); }
public int ExecuteHealthCheck(CalamariVariableDictionary variables) { var account = AccountFactory.Create(variables); var resourceGroupName = variables.Get(SpecialVariables.Action.Azure.ResourceGroupName); var webAppName = variables.Get(SpecialVariables.Action.Azure.WebAppName); if (account is AzureAccount azureAccount) { ConfirmWebAppExists(azureAccount, resourceGroupName, webAppName); } else if (account is AzureServicePrincipalAccount servicePrincipalAccount) { ConfirmWebAppExists(servicePrincipalAccount, resourceGroupName, webAppName); } return(0); }
void SetRegisterApplicationTypeTimeout(CalamariVariableDictionary variables) { var registerAppTypeTimeout = variables.Get(SpecialVariables.Action.ServiceFabric.RegisterApplicationTypeTimeoutSec); if (registerAppTypeTimeout != null) { Log.SetOutputVariable("RegisterApplicationTypeTimeoutSec", registerAppTypeTimeout, variables); } }
public AwsEnvironmentGeneration(CalamariVariableDictionary variables) { account = variables.Get("Octopus.Action.AwsAccount.Variable")?.Trim(); region = variables.Get("Octopus.Action.Aws.Region")?.Trim(); // When building the context for an AWS step, there will be a variable expanded with the keys accessKey = variables.Get(account + ".AccessKey")?.Trim() ?? // When building a context with an account associated with a target, the keys are under Octopus.Action.Amazon. // The lack of any keys means we rely on an EC2 instance role. variables.Get("Octopus.Action.Amazon.AccessKey")?.Trim(); secretKey = variables.Get(account + ".SecretKey")?.Trim() ?? variables.Get("Octopus.Action.Amazon.SecretKey")?.Trim(); assumeRole = variables.Get("Octopus.Action.Aws.AssumeRole")?.Trim(); assumeRoleArn = variables.Get("Octopus.Action.Aws.AssumedRoleArn")?.Trim(); assumeRoleSession = variables.Get("Octopus.Action.Aws.AssumedRoleSession")?.Trim(); }
public override int Execute(string[] commandLineArguments) { Options.Parse(commandLineArguments); var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem(); var variables = new CalamariVariableDictionary(variablesFile, sensitiveVariablesFile, sensitiveVariablesPassword); if (!string.IsNullOrEmpty(packageFile)) { if (!fileSystem.FileExists(packageFile)) { throw new CommandException("Could not find package file: " + packageFile); } } fileSystem.FreeDiskSpaceOverrideInMegaBytes = variables.GetInt32(SpecialVariables.FreeDiskSpaceOverrideInMegaBytes); fileSystem.SkipFreeDiskSpaceCheck = variables.GetFlag(SpecialVariables.SkipFreeDiskSpaceCheck); var substituter = new FileSubstituter(fileSystem); var packageExtractor = new GenericPackageExtractorFactory().createStandardGenericPackageExtractor(); var additionalFileSubstitution = variables.Get(TerraformSpecialVariables.Action.Terraform.FileSubstitution); var runAutomaticFileSubstitution = variables.GetFlag(TerraformSpecialVariables.Action.Terraform.RunAutomaticFileSubstitution, true); var enableNoMatchWarning = variables.Get(SpecialVariables.Package.EnableNoMatchWarning); variables.Add(SpecialVariables.Package.EnableNoMatchWarning, !String.IsNullOrEmpty(enableNoMatchWarning) ? enableNoMatchWarning : (!String.IsNullOrEmpty(additionalFileSubstitution)).ToString()); var conventions = new List <IConvention> { new ContributeEnvironmentVariablesConvention(), new LogVariablesConvention(), new ExtractPackageToStagingDirectoryConvention(packageExtractor, fileSystem).When(_ => packageFile != null), new SubstituteInFilesConvention(fileSystem, substituter, _ => true, _ => FileTargetFactory(runAutomaticFileSubstitution ? DefaultTerraformFileSubstitution : string.Empty, additionalFileSubstitution)), step(fileSystem) }; var deployment = new RunningDeployment(packageFile, variables); var conventionRunner = new ConventionProcessor(deployment, conventions); conventionRunner.RunConventions(); return(0); }
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(string scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var powerShellEngine = new PowerShellScriptEngine(); if (variables.Get(SpecialVariables.Account.AccountType).StartsWith("Azure")) { return(new AzurePowerShellContext().ExecuteScript(powerShellEngine, scriptFile, variables, commandLineRunner)); } return(powerShellEngine.Execute(scriptFile, variables, commandLineRunner)); }
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 ExecuteScript(IScriptEngine scriptEngine, string scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) { var workingDirectory = Path.GetDirectoryName(scriptFile); variables.Set("OctopusAzureTargetScript", scriptFile); // If the Azure PowerShell module to use has not been explicitly configured, then default to the version // bundled with Calamari SetOutputVariable(SpecialVariables.Action.Azure.Output.ModulePath, variables.Get(SpecialVariables.Action.Azure.PowerShellModulePath, BuiltInAzurePowershellModulePath), variables); SetOutputVariable(SpecialVariables.Action.Azure.Output.SubscriptionId, variables.Get(SpecialVariables.Action.Azure.SubscriptionId), variables); SetOutputVariable(SpecialVariables.Action.Azure.Output.SubscriptionName, variables.Get(SpecialVariables.Account.Name), variables); using (new TemporaryFile(Path.Combine(workingDirectory, "AzureProfile.json"))) using (new TemporaryFile(CreateAzureCertificate(workingDirectory, variables))) using (var contextScriptFile = new TemporaryFile(CreateContextScriptFile(workingDirectory))) { return(scriptEngine.Execute(contextScriptFile.FilePath, variables, commandLineRunner)); } }
public AwsEnvironmentGeneration(CalamariVariableDictionary variables) { account = variables.Get("Octopus.Action.AwsAccount.Variable")?.Trim(); region = variables.Get("Octopus.Action.Aws.Region")?.Trim(); accessKey = variables.Get(account + ".AccessKey")?.Trim(); secretKey = variables.Get(account + ".SecretKey")?.Trim(); assumeRole = variables.Get("Octopus.Action.Aws.AssumeRole")?.Trim(); assumeRoleArn = variables.Get("Octopus.Action.Aws.AssumedRoleArn")?.Trim(); assumeRoleSession = variables.Get("Octopus.Action.Aws.AssumedRoleSession")?.Trim(); }