public CalamariVariableDictionary(string storageFilePath, List <string> sensitiveFilePaths, string sensitiveFilePassword, string outputVariablesFilePath = null, string outputVariablesFilePassword = null) { var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem(); if (!string.IsNullOrEmpty(storageFilePath)) { if (!fileSystem.FileExists(storageFilePath)) { throw new CommandException("Could not find variables file: " + storageFilePath); } var nonSensitiveVariables = new VariableDictionary(storageFilePath); nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name))); } if (sensitiveFilePaths.Any()) { foreach (var sensitiveFilePath in sensitiveFilePaths) { if (string.IsNullOrEmpty(sensitiveFilePath)) { continue; } var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword) ? fileSystem.ReadFile(sensitiveFilePath) : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword); try { var sensitiveVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables); foreach (var variable in sensitiveVariables) { SetSensitive(variable.Key, variable.Value); } } catch (JsonReaderException) { throw new CommandException("Unable to parse sensitive-variables as valid JSON."); } } } if (!string.IsNullOrEmpty(outputVariablesFilePath)) { var rawVariables = DecryptWithMachineKey(fileSystem.ReadFile(outputVariablesFilePath), outputVariablesFilePassword); try { var outputVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables); foreach (var variable in outputVariables) { Set(variable.Key, variable.Value); } } catch (JsonReaderException) { throw new CommandException("Unable to parse output variables as valid JSON."); } } }
static void UpdateConfigurationSettings(XContainer configurationFile, VariableDictionary variables) { Log.Verbose("Updating configuration settings..."); var foundSettings = false; WithConfigurationSettings(configurationFile, (roleName, settingName, settingValueAttribute) => { var setting = variables.Get(roleName + "/" + settingName) ?? variables.Get(roleName + "\\" + settingName) ?? variables.Get(settingName) ?? (variables.GetNames().Contains(settingName) ? "" : null); if (setting != null) { foundSettings = true; Log.Info("Updating setting for role {0}: {1} = {2}", roleName, settingName, setting); settingValueAttribute.Value = setting; } }); if (!foundSettings) { Log.Info("No settings that match provided variables were found."); } }
public CalamariVariableDictionary(string storageFilePath, string sensitiveFilePath, string sensitiveFilePassword) { var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem(); if (!string.IsNullOrEmpty(storageFilePath)) { if (!fileSystem.FileExists(storageFilePath)) throw new CommandException("Could not find variables file: " + storageFilePath); var nonSensitiveVariables = new VariableDictionary(storageFilePath); nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name))); } if (!string.IsNullOrEmpty(sensitiveFilePath)) { var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword) ? fileSystem.ReadFile(sensitiveFilePath) : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword); try { var sensitiveVariables = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawVariables); foreach (var variable in sensitiveVariables) { SetSensitive(variable.Key, variable.Value); } } catch (JsonReaderException) { throw new CommandException("Unable to parse sensitive-variables as valid JSON."); } } }
static void UpdateConfigurationSettings(XContainer configurationFile, VariableDictionary variables) { Log.Verbose("Updating configuration settings..."); var foundSettings = false; WithConfigurationSettings(configurationFile, (roleName, settingName, settingValueAttribute) => { var setting = variables.Get(roleName + "/" + settingName) ?? variables.Get(roleName + "\\" + settingName) ?? variables.Get(settingName) ?? (variables.GetNames().Contains(settingName) ? "" : null); if (setting != null) { foundSettings = true; Log.Info("Updating setting for role {0}: {1} = {2}", roleName, settingName, setting); settingValueAttribute.Value = setting; } }); if (!foundSettings) { Log.Info("No settings that match provided variables were found."); } }
public void Update(VariableDictionary variables) { bool VariableNameIsNotASystemVariable(string v) { if (v.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase)) { // Only include variables starting with 'Octopus' // if it also has a colon (:) if (v.StartsWith("Octopus:", StringComparison.OrdinalIgnoreCase)) { return(map.ContainsKey(v)); } else { return(false); } } return(map.ContainsKey(v)); } foreach (var name in variables.GetNames().Where(VariableNameIsNotASystemVariable)) { try { map[name](variables.Get(name)); } catch (Exception e) { Log.WarnFormat("Unable to set value for {0}. The following error occurred: {1}", name, e.Message); } } }
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 string WriteVariableDictionary(VariableDictionary variables) { var builder = new StringBuilder(); foreach (var variable in variables.GetNames()) { builder.AppendLine(" this[" + EncodeValue(variable) + "] = " + EncodeValue(variables.Get(variable)) + ";"); } return(builder.ToString()); }
private VariableDictionary AddEnvironmentVariables() { var variables = new VariableDictionary(); 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; }
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(); } }
public static ScriptSyntax GetLibraryScriptModuleLanguage(VariableDictionary variables, string variableName) { var expectedName = variableName.Replace("Octopus.Script.Module[", "Octopus.Script.Module.Language["); var syntaxVariable = variables.GetNames().FirstOrDefault(x => x == expectedName); if (syntaxVariable == null) { return(ScriptSyntax.PowerShell); } return((ScriptSyntax)Enum.Parse(typeof(ScriptSyntax), variables[syntaxVariable])); }
public void ShouldAddEnvironmentVariables() { var variables = new VariableDictionary(); var convention = new ContributeEnvironmentVariablesConvention(); convention.Install(new RunningDeployment("C:\\Package.nupkg", variables)); Assert.That(variables.GetNames().Count, Is.GreaterThan(3)); Assert.That(variables.Evaluate("My OS is #{env:OS}"), Is.StringStarting("My OS is Windows")); Assert.That(variables.GetRaw(SpecialVariables.Tentacle.Agent.InstanceName), Is.EqualTo("#{env:TentacleInstanceName}")); }
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(); } }
public CalamariVariableDictionary(string storageFilePath, string sensitiveFilePath, string sensitiveFilePassword, string base64Variables = null) { var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem(); if (!string.IsNullOrEmpty(storageFilePath)) { if (!fileSystem.FileExists(storageFilePath)) { throw new CommandException("Could not find variables file: " + storageFilePath); } var nonSensitiveVariables = new VariableDictionary(storageFilePath); nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name))); } if (!string.IsNullOrEmpty(sensitiveFilePath)) { var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword) ? fileSystem.ReadFile(sensitiveFilePath) : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword); try { var sensitiveVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables); foreach (var variable in sensitiveVariables) { SetSensitive(variable.Key, variable.Value); } } catch (JsonReaderException) { throw new CommandException("Unable to parse sensitive-variables as valid JSON."); } } if (!string.IsNullOrEmpty(base64Variables)) { try { var json = Encoding.UTF8.GetString(Convert.FromBase64String(base64Variables)); var variables = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); foreach (var variable in variables) { Set(variable.Key, variable.Value); } } catch (JsonReaderException) { throw new CommandException("Unable to parse jsonVariables as valid JSON."); } } }
static List<string> ApplyChanges(XNode doc, VariableDictionary variables) { var changes = new List<string>(); foreach (var variable in variables.GetNames()) { changes.AddRange( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='appSettings']/*[local-name()='add']", "key", variable, "value", variables).Concat( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='connectionStrings']/*[local-name()='add']", "name", variable, "connectionString", variables).Concat( ReplaceStonglyTypeApplicationSetting(doc, "//*[local-name()='applicationSettings']//*[local-name()='setting']", "name", variable, variables)))); } return changes; }
static List <string> ApplyChanges(XNode doc, VariableDictionary variables) { var changes = new List <string>(); foreach (var variable in variables.GetNames()) { changes.AddRange( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='appSettings']/*[local-name()='add']", "key", variable, "value", variables).Concat( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='connectionStrings']/*[local-name()='add']", "name", variable, "connectionString", variables).Concat( ReplaceStonglyTypeApplicationSetting(doc, "//*[local-name()='applicationSettings']//*[local-name()='setting']", "name", variable, variables)))); } return(changes); }
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); } } }
public void Update(VariableDictionary variables) { foreach (var name in variables.GetNames().Where(x => !x.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase))) { if (map.ContainsKey(name)) { try { map[name](variables.Get(name)); } catch (Exception e) { Log.WarnFormat("Unable to set value for {0}. The following error occurred: {1}", name, e.Message); } } } }
private static string ToString(this VariableDictionary variables, Func <string, bool> nameFilter, bool useRawValue) { var text = new StringBuilder(); foreach (var name in variables.GetNames()) { if (!nameFilter(name)) { continue; } text.AppendFormat("[{0}] = '{1}'", name, useRawValue ? variables.GetRaw(name) : variables.Get(name)); text.AppendLine(); } return(text.ToString()); }
public void Generate(string appSettingsFilePath, VariableDictionary variables) { var root = LoadJson(appSettingsFilePath); var names = variables.GetNames(); names.Sort(StringComparer.OrdinalIgnoreCase); foreach (var name in names) { if (name.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase)) continue; SetValueRecursive(root, name, name, variables.Get(name)); } SaveJson(appSettingsFilePath, root); }
static IEnumerable <string> PrepareScriptModules(VariableDictionary 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); } } }
public void Generate(string appSettingsFilePath, VariableDictionary variables) { var root = LoadJson(appSettingsFilePath); var names = variables.GetNames(); names.Sort(StringComparer.OrdinalIgnoreCase); foreach (var name in names) { if (name.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase)) { continue; } SetValueRecursive(root, name, name, variables.Get(name)); } SaveJson(appSettingsFilePath, root); }
public void ModifyConfigurationFile(string configurationFilePath, VariableDictionary variables) { XDocument doc; using (var reader = XmlReader.Create(configurationFilePath)) { doc = XDocument.Load(reader, LoadOptions.PreserveWhitespace); } var changes = new List <string>(); foreach (var variable in variables.GetNames()) { changes.AddRange( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='appSettings']/*[local-name()='add']", "key", variable, "value", variables).Concat( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='connectionStrings']/*[local-name()='add']", "name", variable, "connectionString", variables).Concat( ReplaceStonglyTypeApplicationSetting(doc, "//*[local-name()='applicationSettings']//*[local-name()='setting']", "name", variable, variables)))); } if (!changes.Any()) { Log.Info("No matching setting or connection string names were found in: {0}", configurationFilePath); return; } Log.Info("Updating appSettings and connectionStrings in: {0}", configurationFilePath); foreach (var change in changes) { Log.Verbose(change); } var xws = new XmlWriterSettings { OmitXmlDeclaration = doc.Declaration == null, Indent = true }; using (var writer = XmlWriter.Create(configurationFilePath, xws)) { doc.Save(writer); } }
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()); }
public void ModifyConfigurationFile(string configurationFilePath, VariableDictionary variables) { XDocument doc; using (var reader = XmlReader.Create(configurationFilePath, XmlUtils.DtdSafeReaderSettings)) { doc = XDocument.Load(reader, LoadOptions.PreserveWhitespace); } var changes = new List<string>(); foreach (var variable in variables.GetNames()) { changes.AddRange( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='appSettings']/*[local-name()='add']", "key", variable, "value", variables).Concat( ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='connectionStrings']/*[local-name()='add']", "name", variable, "connectionString", variables).Concat( ReplaceStonglyTypeApplicationSetting(doc, "//*[local-name()='applicationSettings']//*[local-name()='setting']", "name", variable, variables)))); } if (!changes.Any()) { Log.Info("No matching setting or connection string names were found in: {0}", configurationFilePath); return; } Log.Info("Updating appSettings and connectionStrings in: {0}", configurationFilePath); foreach (var change in changes) { Log.Verbose(change); } var xws = new XmlWriterSettings { OmitXmlDeclaration = doc.Declaration == null, Indent = true }; using (var writer = XmlWriter.Create(configurationFilePath, xws)) { doc.Save(writer); } }
static void WriteLocalVariables(VariableDictionary variables, StringBuilder output) { 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); } }
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 string WriteVariableDictionary(VariableDictionary variables) { var builder = new StringBuilder(); foreach (var variable in variables.GetNames()) { builder.AppendLine(" this[" + EncodeValue(variable) + "] = " + EncodeValue(variables.Get(variable)) + ";"); } return builder.ToString(); }
public static void MergeWith(this VariableDictionary variables, VariableDictionary other) { other.GetNames().ForEach(name => variables.Set(name, other.GetRaw(name))); }
static void WriteLocalVariables(VariableDictionary variables, StringBuilder output) { 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); } }
public void Merge(VariableDictionary other) { other.GetNames().ForEach(name => Set(name, other.GetRaw(name))); }
static IEnumerable<string> GetVariableSwitchConditions(VariableDictionary variables) { return variables.GetNames().Select(variable => string.Format(" \"{1}\"){0} decode_servicemessagevalue \"{2}\" ;;{0}", Environment.NewLine, EncodeValue(variable), EncodeValue(variables.Get(variable)))); }
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))); } }