Exemple #1
0
        public static void SetOutputVariable(this IVariables variables, string name, string value)
        {
            variables.Set(name, value);

            // And set the output-variables.
            // Assuming we are running in a step named 'DeployWeb' and are setting a variable named 'Foo'
            // then we will set Octopus.Action[DeployWeb].Output.Foo
            var actionName = variables.Get(ActionVariables.Name);

            if (string.IsNullOrWhiteSpace(actionName))
            {
                return;
            }

            var actionScopedVariable = SpecialVariables.GetOutputVariableName(actionName, name);

            variables.Set(actionScopedVariable, value);

            // And if we are on a machine named 'Web01'
            // Then we will set Octopus.Action[DeployWeb].Output[Web01].Foo
            var machineName = variables.Get(MachineVariables.Name);

            if (string.IsNullOrWhiteSpace(machineName))
            {
                return;
            }

            var machineIndexedVariableName = SpecialVariables.GetMachineIndexedOutputVariableName(actionName, machineName, name);

            variables.Set(machineIndexedVariableName, value);
        }
        static string DeclareLocalVariables(CalamariVariableDictionary variables)
        {
            var output = new StringBuilder();

            foreach (var variableName in variables.GetNames().Where(name => !SpecialVariables.IsLibraryScriptModule(name)))
            {
                if (SpecialVariables.IsExcludedFromLocalVariables(variableName))
                {
                    continue;
                }

                // This is the way we used to fix up the identifiers - people might still rely on this behavior
                var legacyKey = new string(variableName.Where(char.IsLetterOrDigit).ToArray());

                // This is the way we should have done it
                var smartKey = new string(variableName.Where(IsValidPowerShellIdentifierChar).ToArray());

                if (legacyKey != smartKey)
                {
                    WriteVariableAssignment(output, legacyKey, variableName);
                }

                WriteVariableAssignment(output, smartKey, variableName);
            }

            return(output.ToString());
        }
Exemple #3
0
 static void WriteVariableDictionary(VariableDictionary 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)))
     {
         output.Append("$OctopusParameters[").Append(EncodeValue(variableName)).Append("] = ").AppendLine(EncodeValue(variables.Get(variableName)));
     }
 }
 static void WriteScriptModules(VariableDictionary variables, StringBuilder output)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         var name = "Library_" + new string(SpecialVariables.GetLibraryScriptModuleName(variableName).Where(char.IsLetterOrDigit).ToArray()) + "_" + DateTime.Now.Ticks;
         output.Append("New-Module -Name ").Append(name).Append(" -ScriptBlock {");
         output.AppendLine(variables.Get(variableName));
         output.AppendLine("} | Import-Module");
         output.AppendLine();
     }
 }
 static void WriteScriptModules(VariableDictionary variables, string parentDirectory, StringBuilder output)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         var name           = "Library_" + new string(SpecialVariables.GetLibraryScriptModuleName(variableName).Where(char.IsLetterOrDigit).ToArray()) + "_" + DateTime.Now.Ticks;
         var moduleFileName = $"{name}.psm1";
         var moduleFilePath = Path.Combine(parentDirectory, moduleFileName);
         CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8);
         output.AppendLine($"Import-ScriptModule '{SpecialVariables.GetLibraryScriptModuleName(variableName).EscapeSingleQuotedString()}' '{moduleFilePath.EscapeSingleQuotedString()}'");
         output.AppendLine();
     }
 }
 static IEnumerable <string> PrepareScriptModules(VariableDictionary variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         if (SpecialVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.FSharp)
         {
             var libraryScriptModuleName = SpecialVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray());
             var moduleFileName = $"{name}.fsx";
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             Log.VerboseFormat("Writing script module '{0}' as f# module {1}. Import this module via `#load \"{1}\"`.", libraryScriptModuleName, moduleFileName, name);
             CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8);
             yield return(moduleFileName);
         }
     }
 }
Exemple #7
0
 static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         if (SpecialVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Python)
         {
             var libraryScriptModuleName = SpecialVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(x => char.IsLetterOrDigit(x) || x == '_').ToArray());
             var moduleFileName = $"{name}.py";
             Log.VerboseFormat("Writing script module '{0}' as python module {1}. Import this module via `import {2}`.", libraryScriptModuleName, moduleFileName, name);
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8);
             yield return(name);
         }
     }
 }
Exemple #8
0
 static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         if (SpecialVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Bash)
         {
             var libraryScriptModuleName = SpecialVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray());
             var moduleFileName = $"{name}.sh";
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             Log.VerboseFormat("Writing script module '{0}' as bash script {1}. Import this via `source {1}`.", libraryScriptModuleName, moduleFileName, name);
             Encoding utf8WithoutBom = new UTF8Encoding(false);
             CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), utf8WithoutBom);
             EnsureValidUnixFile(moduleFilePath);
             yield return(moduleFilePath);
         }
     }
 }
        static (string encrypted, string iv) GetEncryptedVariablesString(CalamariVariableDictionary variables)
        {
            var sb = new StringBuilder();

            foreach (var variableName in variables.GetNames().Where(name => !SpecialVariables.IsLibraryScriptModule(name)))
            {
                var value          = variables.Get(variableName);
                var encryptedValue = value == null ? "nul" : EncodeAsBase64(value); // "nul" is not a valid Base64 string
                sb.Append(EncodeAsBase64(variableName)).Append("$").AppendLine(encryptedValue);
            }

            var encrypted    = VariableEncryptor.Encrypt(sb.ToString());
            var rawEncrypted = AesEncryption.ExtractIV(encrypted, out var iv);

            return(
                Convert.ToBase64String(rawEncrypted, Base64FormattingOptions.InsertLineBreaks),
                Convert.ToBase64String(iv)
                );
        }
Exemple #10
0
        static string[] WriteScriptModules(VariableDictionary variables, string parentDirectory, StringBuilder output)
        {
            var scriptModules = new List <string>();

            foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
            {
                if (SpecialVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.PowerShell)
                {
                    var libraryScriptModuleName = SpecialVariables.GetLibraryScriptModuleName(variableName);
                    var name           = "Library_" + new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray()) + "_" + DateTime.Now.Ticks;
                    var moduleFileName = $"{name}.psm1";
                    var moduleFilePath = Path.Combine(parentDirectory, moduleFileName);
                    Log.VerboseFormat("Writing script module '{0}' as PowerShell module {1}. This module will be automatically imported - functions will automatically be in scope.", libraryScriptModuleName, moduleFileName, name);
                    CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8);
                    output.AppendLine($"Import-ScriptModule '{libraryScriptModuleName.EscapeSingleQuotedString()}' '{moduleFilePath.EscapeSingleQuotedString()}'");
                    output.AppendLine();
                    scriptModules.Add(moduleFilePath);
                }
            }

            return(scriptModules.ToArray());
        }