string CreateContextScriptFile(string workingDirectory) { var azureContextScriptFile = Path.Combine(workingDirectory, "Octopus.AzureCloudServiceContext.ps1"); var contextScript = embeddedResources.GetEmbeddedResourceText(GetType().Assembly, $"{GetType().Assembly.GetName().Name}.Scripts.AzureCloudServiceContext.ps1"); fileSystem.OverwriteFile(azureContextScriptFile, contextScript); return(azureContextScriptFile); }
void ArrangeOriginalConfigurationFileForSuccess(string configurationFilePath, string content, Action <string> captureResultingConfiguration) { variables.Set(SpecialVariables.Action.Azure.Output.ConfigurationFile, configurationFilePath); fileSystem.FileExists(configurationFilePath).Returns(true); fileSystem.ReadFile(configurationFilePath).Returns(content); fileSystem.OverwriteFile(configurationFilePath, Arg.Do <string>(captureResultingConfiguration)); }
string CreateContextScriptFile(string workingDirectory) { var azureContextScriptFile = Path.Combine(workingDirectory, "Octopus.AzureContext.ps1"); var contextScript = embeddedResources.GetEmbeddedResourceText("Calamari.Azure.Scripts.AzureContext.ps1"); fileSystem.OverwriteFile(azureContextScriptFile, contextScript); return(azureContextScriptFile); }
private void CreateDeployment(string extractedFrom, string extractedTo, DateTimeOffset date, string retentionPolicySet) { fileSystem.EnsureDirectoryExists(extractedTo); fileSystem.OverwriteFile(Path.Combine(extractedTo, "an_artifact.txt"), "lorem ipsum"); fileSystem.OverwriteFile(extractedFrom, "lorem ipsum"); deploymentJournal.AddJournalEntry(new JournalEntry(new XElement("Deployment", new XAttribute("Id", Guid.NewGuid().ToString()), new XAttribute("EnvironmentId", "blah"), new XAttribute("ProjectId", "blah"), new XAttribute("PackageId", "blah"), new XAttribute("PackageVersion", "blah"), new XAttribute("InstalledOn", date.UtcDateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)), new XAttribute("ExtractedFrom", extractedFrom), new XAttribute("ExtractedTo", extractedTo), new XAttribute("RetentionPolicySet", retentionPolicySet), new XAttribute("WasSuccessFul", true.ToString()) ))); }
public void Install(RunningDeployment deployment) { var features = deployment.Variables.GetStrings(SpecialVariables.Package.EnabledFeatures).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); if (!features.Contains(SpecialVariables.Features.CustomScripts)) { return; } foreach (ScriptType scriptType in Enum.GetValues(typeof(ScriptType))) { var scriptName = GetScriptName(deploymentStage, scriptType); string error; var scriptBody = deployment.Variables.Get(scriptName, out error); if (!string.IsNullOrEmpty(error)) { Log.VerboseFormat("Parsing script for phase {0} with Octostache returned the following error: `{1}`", deploymentStage, error); } if (string.IsNullOrWhiteSpace(scriptBody)) { continue; } if (!scriptEngine.GetSupportedTypes().Contains(scriptType)) { throw new CommandException($"{scriptType} scripts are not supported on this platform ({deploymentStage})"); } var scriptFile = Path.Combine(deployment.CurrentDirectory, scriptName); fileSystem.OverwriteFile(scriptFile, scriptBody, Encoding.UTF8); // Execute the script Log.VerboseFormat("Executing '{0}'", scriptFile); var result = scriptEngine.Execute(new Script(scriptFile), deployment.Variables, commandLineRunner); if (result.ExitCode != 0) { throw new CommandException($"{deploymentStage} script returned non-zero exit code: {result.ExitCode}"); } if (result.HasErrors && deployment.Variables.GetFlag(SpecialVariables.Action.FailScriptOnErrorOutput, false)) { throw new CommandException($"{deploymentStage} script returned zero exit code but had error output."); } if (deployment.Variables.GetFlag(SpecialVariables.DeleteScriptsOnCleanup, true)) { // And then delete it (this means if the script failed, it will persist, which may assist debugging) fileSystem.DeleteFile(scriptFile, FailureOptions.IgnoreFailure); } } }
string CreateContextScriptFile(string workingDirectory, ScriptSyntax scriptSyntax) { var registrations = variables.Get(ScriptFunctionsVariables.Registration); var results = JsonConvert.DeserializeObject <IList <ScriptFunctionRegistration> >(registrations); var azureContextScriptFile = Path.Combine(workingDirectory, $"Octopus.FunctionAppenderContext.{scriptSyntax.FileExtension()}"); var contextScript = codeGenFunctionsRegistry.GetCodeGenerator(scriptSyntax).Generate(results); fileSystem.OverwriteFile(azureContextScriptFile, contextScript); return(azureContextScriptFile); }
public static string PrepareBootstrapFile(Script script, CalamariVariableDictionary variables) { var parent = Path.GetDirectoryName(Path.GetFullPath(script.File)); var name = Path.GetFileName(script.File); var bootstrapFile = Path.Combine(parent, "Bootstrap." + name); var builder = new StringBuilder(BootstrapScriptTemplate); builder.Replace("{{TargetScriptFile}}", script.File.EscapeSingleQuotedString()) .Replace("{{ScriptParameters}}", script.Parameters) .Replace("{{VariableDeclarations}}", DeclareVariables(variables)) .Replace("{{ScriptModules}}", DeclareScriptModules(variables, parent)); builder = SetupDebugBreakpoints(builder, variables); CalamariFileSystem.OverwriteFile(bootstrapFile, builder.ToString(), new UTF8Encoding(true)); File.SetAttributes(bootstrapFile, FileAttributes.Hidden); return(bootstrapFile); }
protected void Run(RunningDeployment deployment) { var features = deployment.Variables.GetStrings(SpecialVariables.Package.EnabledFeatures).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); if (!features.Any()) { return; } var assembly = typeof(FeatureScriptConventionBase).GetTypeInfo().Assembly; var embeddedResourceNames = new HashSet <string>(embeddedResources.GetEmbeddedResourceNames(assembly)); foreach (var featureScript in features.SelectMany(GetScriptNames)) { // Determine the embedded-resource name var scriptEmbeddedResource = GetEmbeddedResourceName(featureScript); // If there is a matching embedded resource if (!embeddedResourceNames.Contains(scriptEmbeddedResource)) { continue; } var scriptFile = Path.Combine(deployment.CurrentDirectory, featureScript); // To execute the script, we need a physical file on disk. // If one already exists, we don't recreate it, as this provides a handy // way to override behaviour. if (!fileSystem.FileExists(scriptFile)) { Log.VerboseFormat("Creating '{0}' from embedded resource", scriptFile); fileSystem.OverwriteFile(scriptFile, embeddedResources.GetEmbeddedResourceText(assembly, scriptEmbeddedResource)); } else { Log.WarnFormat("Did not overwrite '{0}', it was already on disk", scriptFile); } // Execute the script Log.VerboseFormat("Executing '{0}'", scriptFile); var result = scriptEngine.Execute(new Script(scriptFile), deployment.Variables, commandLineRunner); // And then delete it Log.VerboseFormat("Deleting '{0}'", scriptFile); fileSystem.DeleteFile(scriptFile, FailureOptions.IgnoreFailure); if (result.ExitCode != 0) { throw new CommandException(string.Format("Script '{0}' returned non-zero exit code: {1}", scriptFile, result.ExitCode)); } } }
public void PerformSubstitution(string sourceFile, VariableDictionary variables, string targetFile) { var source = fileSystem.ReadFile(sourceFile); var encoding = GetEncoding(sourceFile, variables); string error; var result = variables.Evaluate(source, out error); if (!string.IsNullOrEmpty(error)) { Log.VerboseFormat("Parsing file '{0}' with Octostache returned the following error: `{1}`", sourceFile, error); } fileSystem.OverwriteFile(targetFile, result, encoding); }
public void PerformSubstitution(string sourceFile, IVariables variables, string targetFile) { log.Verbose($"Performing variable substitution on '{sourceFile}'"); var source = fileSystem.ReadFile(sourceFile, out var sourceFileEncoding); var encoding = GetEncoding(variables, sourceFileEncoding); var result = variables.Evaluate(source, out var error, false); if (!string.IsNullOrEmpty(error)) { log.VerboseFormat("Parsing file '{0}' with Octostache returned the following error: `{1}`", sourceFile, error); } fileSystem.OverwriteFile(targetFile, result, encoding); }
static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory) { foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule)) { if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Python) { var libraryScriptModuleName = ScriptVariables.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); } } }
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 Install(RunningDeployment deployment) { var features = deployment.Variables.GetStrings(SpecialVariables.Package.EnabledFeatures).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); if (!features.Any()) { return; } var embeddedResourceNames = new HashSet <string>(embeddedResources.GetEmbeddedResourceNames()); foreach (var featureScript in features.SelectMany(GetScriptNames)) { // Determine the embedded-resource name var scriptEmbeddedResource = GetEmbeddedResourceName(featureScript); // If there is a matching embedded resource if (!embeddedResourceNames.Contains(scriptEmbeddedResource)) { continue; } var scriptFile = Path.Combine(deployment.CurrentDirectory, featureScript); // To execute the script, we need a physical file on disk. // If one already exists, we don't recreate it, as this provides a handy // way to override behaviour. if (!fileSystem.FileExists(scriptFile)) { fileSystem.OverwriteFile(scriptFile, embeddedResources.GetEmbeddedResourceText(scriptEmbeddedResource)); } // Execute the script Log.VerboseFormat("Executing '{0}'", scriptFile); var result = scriptEngineSelector.SelectEngine(scriptFile).Execute(scriptFile, deployment.Variables, commandLineRunner); // And then delete it fileSystem.DeleteFile(scriptFile, DeletionOptions.TryThreeTimesIgnoreFailure); if (result.ExitCode != 0) { throw new CommandException(string.Format("Script '{0}' returned non-zero exit code: {1}", scriptFile, result.ExitCode)); } } }
public void Install(RunningDeployment deployment) { var features = deployment.Variables.GetStrings(SpecialVariables.Package.EnabledFeatures).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); if (!features.Contains(SpecialVariables.Features.CustomScripts)) { return; } foreach (var scriptName in scriptEngine.GetSupportedExtensions() .Select(extension => GetScriptName(deploymentStage, extension))) { string error; var scriptBody = deployment.Variables.Get(scriptName, out error); if (!string.IsNullOrEmpty(error)) { Log.VerboseFormat("Parsing script for phase {0} with Octostache returned the following error: `{1}`", deploymentStage, error); } if (string.IsNullOrWhiteSpace(scriptBody)) { continue; } var scriptFile = Path.Combine(deployment.CurrentDirectory, scriptName); fileSystem.OverwriteFile(scriptFile, scriptBody, Encoding.UTF8); // Execute the script Log.VerboseFormat("Executing '{0}'", scriptFile); var result = scriptEngine.Execute(new Script(scriptFile), deployment.Variables, commandLineRunner); if (result.ExitCode != 0) { throw new CommandException(string.Format("Script '{0}' returned non-zero exit code: {1}", scriptFile, result.ExitCode)); } if (deployment.Variables.GetFlag(SpecialVariables.DeleteScriptsOnCleanup, true)) { // And then delete it (this means if the script failed, it will persist, which may assist debugging) fileSystem.DeleteFile(scriptFile, FailureOptions.IgnoreFailure); } } }
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); } } }
public void Install(RunningDeployment deployment) { var variables = deployment.Variables; // Set output variables for our script to access. log.SetOutputVariable("PublishProfileFile", variables.Get(SpecialVariables.Action.ServiceFabric.PublishProfileFile, "PublishProfiles\\Cloud.xml"), variables); log.SetOutputVariable("DeployOnly", variables.Get(SpecialVariables.Action.ServiceFabric.DeployOnly, defaultValue: false.ToString()), variables); log.SetOutputVariable("UnregisterUnusedApplicationVersionsAfterUpgrade", variables.Get(SpecialVariables.Action.ServiceFabric.UnregisterUnusedApplicationVersionsAfterUpgrade, defaultValue: false.ToString()), variables); log.SetOutputVariable("OverrideUpgradeBehavior", variables.Get(SpecialVariables.Action.ServiceFabric.OverrideUpgradeBehavior, defaultValue: "None"), variables); log.SetOutputVariable("OverwriteBehavior", variables.Get(SpecialVariables.Action.ServiceFabric.OverwriteBehavior, defaultValue: "SameAppTypeAndVersion"), variables); log.SetOutputVariable("SkipPackageValidation", variables.Get(SpecialVariables.Action.ServiceFabric.SkipPackageValidation, defaultValue: false.ToString()), variables); log.SetOutputVariable("CopyPackageTimeoutSec", variables.Get(SpecialVariables.Action.ServiceFabric.CopyPackageTimeoutSec, defaultValue: 0.ToString()), variables); SetRegisterApplicationTypeTimeout(variables); // Package should have been extracted to the staging dir (as per the ExtractPackageToStagingDirectoryConvention). var targetPath = Path.Combine(Environment.CurrentDirectory, "staging"); log.SetOutputVariable("ApplicationPackagePath", targetPath, variables); if (deployment.Variables.GetFlag(SpecialVariables.Action.ServiceFabric.LogExtractedApplicationPackage)) { LogExtractedPackage(deployment.CurrentDirectory); } // The user may supply the script, to override behaviour. var scriptFile = Path.Combine(deployment.CurrentDirectory, "DeployToServiceFabric.ps1"); if (!fileSystem.FileExists(scriptFile)) { // Use our bundled version. fileSystem.OverwriteFile(scriptFile, embeddedResources.GetEmbeddedResourceText(GetType().Assembly, $"{GetType().Assembly.GetName().Name}.Scripts.DeployAzureServiceFabricApplication.ps1")); } var result = scriptEngine.Execute(new Script(scriptFile), deployment.Variables, commandLineRunner); fileSystem.DeleteFile(scriptFile, FailureOptions.IgnoreFailure); if (result.ExitCode != 0) { throw new CommandException(string.Format("Script '{0}' returned non-zero exit code: {1}", scriptFile, result.ExitCode)); } }
void ExecuteFeatureScripts(RunningDeployment deployment, string feature, HashSet <string> embeddedResourceNames) { foreach (var featureScript in GetScriptNames(feature)) { // Determine the embedded-resource name var scriptEmbeddedResource = GetEmbeddedResourceName(featureScript); // If there is a matching embedded resource if (!embeddedResourceNames.Contains(scriptEmbeddedResource)) { continue; } var scriptFile = Path.Combine(deployment.CurrentDirectory, featureScript); // To execute the script, we need a physical file on disk. // If one already exists, we don't recreate it, as this provides a handy // way to override behaviour. if (!fileSystem.FileExists(scriptFile)) { Log.VerboseFormat("Creating '{0}' from embedded resource", scriptFile); fileSystem.OverwriteFile(scriptFile, embeddedResources.GetEmbeddedResourceText(Assembly, scriptEmbeddedResource)); } else { Log.WarnFormat("Did not overwrite '{0}', it was already on disk", scriptFile); } // Execute the script Log.VerboseFormat("Executing '{0}'", scriptFile); var result = scriptEngine.Execute(new Script(scriptFile), deployment.Variables, commandLineRunner); // And then delete it Log.VerboseFormat("Deleting '{0}'", scriptFile); fileSystem.DeleteFile(scriptFile, FailureOptions.IgnoreFailure); if (result.ExitCode != 0) { throw new CommandException(string.Format("Script '{0}' returned non-zero exit code: {1}", scriptFile, result.ExitCode)); } } }
static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory) { foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule)) { if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.FSharp) { var libraryScriptModuleName = ScriptVariables.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); var contents = variables.Get(variableName); if (contents == null) { throw new InvalidOperationException($"Value for variable {variableName} could not be found."); } CalamariFileSystem.OverwriteFile(moduleFilePath, contents, Encoding.UTF8); yield return(moduleFileName); } } }
public void ModifyFile(string filePath, IVariables variables) { try { var json = LoadJson(filePath); var map = new JsonUpdateMap(log); map.Load(json.root); map.Update(variables); fileSystem.OverwriteFile(filePath, textWriter => { textWriter.NewLine = json.lineEnding == StringExtensions.LineEnding.Unix ? "\n" : "\r\n"; var jsonWriter = new JsonTextWriter(textWriter); jsonWriter.Formatting = Formatting.Indented; json.root.WriteTo(jsonWriter); }, json.encoding); } catch (JsonReaderException e) { throw new StructuredConfigFileParseException(e.Message, e); } }
public void Install(RunningDeployment deployment) { var cmd = BuildHelmCommand(deployment); var fileName = SyntaxSpecificFileName(deployment); using (new TemporaryFile(fileName)) { fileSystem.OverwriteFile(fileName, cmd); var result = scriptEngine.Execute(new Script(fileName), deployment.Variables, commandLineRunner); if (result.ExitCode != 0) { throw new CommandException(string.Format( "Helm Upgrade returned non-zero exit code: {0}. Deployment terminated.", result.ExitCode)); } if (result.HasErrors && deployment.Variables.GetFlag(Deployment.SpecialVariables.Action.FailScriptOnErrorOutput, false)) { throw new CommandException( $"Helm Upgrade returned zero exit code but had error output. Deployment terminated."); } } }
public void Install(RunningDeployment deployment) { var features = deployment.Variables.GetStrings(SpecialVariables.Package.EnabledFeatures).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); if (!features.Contains(SpecialVariables.Features.CustomScripts)) { return; } foreach (var scriptName in scriptEngineSelector.GetSupportedExtensions() .Select(extension => GetScriptName(deploymentStage, extension))) { var scriptBody = deployment.Variables.Get(scriptName); if (string.IsNullOrWhiteSpace(scriptBody)) { continue; } var scriptFile = Path.Combine(deployment.CurrentDirectory, scriptName); fileSystem.OverwriteFile(scriptFile, scriptBody); // Execute the script Log.VerboseFormat("Executing '{0}'", scriptFile); var result = scriptEngineSelector.SelectEngine(scriptFile).Execute(scriptFile, deployment.Variables, commandLineRunner); if (result.ExitCode != 0) { throw new CommandException(string.Format("Script '{0}' returned non-zero exit code: {1}", scriptFile, result.ExitCode)); } // And then delete it (this means if the script failed, it will persist, which may assist debugging) fileSystem.DeleteFile(scriptFile, DeletionOptions.TryThreeTimesIgnoreFailure); } }
string CreateContextScriptFile(string workingDirectory, ScriptSyntax syntax) { string contextFile; switch (syntax) { case ScriptSyntax.Bash: contextFile = "AzureContext.sh"; break; case ScriptSyntax.PowerShell: contextFile = "AzureContext.ps1"; break; default: throw new InvalidOperationException($"No Azure context wrapper exists for {syntax}"); } var azureContextScriptFile = Path.Combine(workingDirectory, $"Octopus.{contextFile}"); var contextScript = embeddedResources.GetEmbeddedResourceText(GetType().Assembly, $"Calamari.Azure.Scripts.{contextFile}"); fileSystem.OverwriteFile(azureContextScriptFile, contextScript); return(azureContextScriptFile); }
void SaveConfigurationFile(XDocument document, string configurationFilePath) { fileSystem.OverwriteFile(configurationFilePath, document.ToString()); }