Beispiel #1
0
 public string GetSubstitutedTemplateContent(string relativePath, bool inPackage, IVariables variables)
 {
     return(replacement.ResolveAndSubstituteFile(
                () => resolver.Resolve(relativePath, inPackage, variables).Value,
                fileSystem.ReadFile,
                variables));
 }
Beispiel #2
0
 /// <summary>
 /// Returns the directory where the package will be installed.
 /// Also ensures the directory exists, and that there is free-space on the disk.
 /// </summary>
 public static string GetApplicationDirectory(PackageFileNameMetadata packageFileNameMetadata, IVariables variables, ICalamariFileSystem fileSystem)
 {
     return(EnsureTargetPathExistsAndIsEmpty(
                Path.Combine(GetEnvironmentApplicationDirectory(fileSystem, variables),
                             FileNameEscaper.Escape(packageFileNameMetadata.PackageId),
                             FileNameEscaper.Escape(packageFileNameMetadata.Version.ToString())),
                fileSystem));
 }
Beispiel #3
0
        public static (string bootstrapFile, string[] temporaryFiles) PrepareBootstrapFile(Script script, string configurationFile, string workingDirectory, IVariables variables)
        {
            var bootstrapFile     = Path.Combine(workingDirectory, "Bootstrap." + Guid.NewGuid().ToString().Substring(10) + "." + Path.GetFileName(script.File));
            var scriptModulePaths = PrepareScriptModules(variables, workingDirectory).ToArray();

            using (var file = new FileStream(bootstrapFile, FileMode.CreateNew, FileAccess.Write))
                using (var writer = new StreamWriter(file, Encoding.ASCII))
                {
                    writer.NewLine = LinuxNewLine;
                    writer.WriteLine("#!/bin/bash");
                    writer.WriteLine("source \"$(pwd)/" + Path.GetFileName(configurationFile) + "\"");
                    writer.WriteLine("source \"$(pwd)/" + Path.GetFileName(script.File) + "\" " + script.Parameters);
                    writer.Flush();
                }

            File.SetAttributes(bootstrapFile, FileAttributes.Hidden);
            EnsureValidUnixFile(script.File);
            return(bootstrapFile, scriptModulePaths);
        }
Beispiel #4
0
        public void ModifyFile(string filePath, IVariables variables)
        {
            try
            {
                void LogReplacement(string key)
                => log.Verbose(StructuredConfigMessages.StructureFound(key));

                var replaced       = 0;
                var variablesByKey = variables
                                     .Where(v => !OctopusReservedVariablePattern.IsMatch(v.Key))
                                     .DistinctBy(v => v.Key)
                                     .ToDictionary <KeyValuePair <string, string>, string, Func <string?> >(v => v.Key,
                                                                                                            v => () =>
                {
                    LogReplacement(v.Key);
                    replaced++;
                    return(variables.Get(v.Key));
                },
                                                                                                            StringComparer.OrdinalIgnoreCase);

                // Read and transform the input file
                var fileText   = fileSystem.ReadFile(filePath, out var encoding);
                var lineEnding = fileText.GetMostCommonLineEnding();

                var outputEvents   = new List <ParsingEvent>();
                var indentDetector = new YamlIndentDetector();

                using (var reader = new StringReader(fileText))
                {
                    var scanner    = new Scanner(reader, false);
                    var parser     = new Parser(scanner);
                    var classifier = new YamlEventStreamClassifier();
                    (IYamlNode startEvent, string?replacementValue)? structureWeAreReplacing = null;
                    while (parser.MoveNext())
                    {
                        var ev = parser.Current;
                        if (ev == null)
                        {
                            continue;
                        }

                        indentDetector.Process(ev);

                        if (ev is Comment c)
                        {
                            ev = c.RestoreLeadingSpaces();
                        }

                        var node = classifier.Process(ev);

                        if (structureWeAreReplacing == null)
                        {
                            // Not replacing: searching for things to replace, copying events to output.

                            if (node is YamlNode <Scalar> scalar &&
                                variablesByKey.TryGetValue(scalar.Path, out var newValue))
                            {
                                outputEvents.Add(scalar.Event.ReplaceValue(newValue()));
                            }
                            else if (node is YamlNode <MappingStart> mappingStart &&
                                     variablesByKey.TryGetValue(mappingStart.Path, out var mappingReplacement))
                            {
                                structureWeAreReplacing = (mappingStart, mappingReplacement());
                            }
                            else if (node is YamlNode <SequenceStart> sequenceStart &&
                                     variablesByKey.TryGetValue(sequenceStart.Path, out var sequenceReplacement))
                            {
                                structureWeAreReplacing = (sequenceStart, sequenceReplacement());
                            }
 protected string ReplaceToHex(IVariables variables, string existingFile)
 {
     return(Replace(variables, existingFile, path => File.ReadAllBytes(path).ToReadableHexDump()));
 }
Beispiel #6
0
 public DockerImagePackageDownloader(IScriptEngine scriptEngine, ICalamariFileSystem fileSystem, ICommandLineRunner commandLineRunner, IVariables variables, ILog log)
 {
     this.scriptEngine      = scriptEngine;
     this.fileSystem        = fileSystem;
     this.commandLineRunner = commandLineRunner;
     this.variables         = variables;
     this.log = log;
 }
 public void RestoreVars()
 {
     Vars = OrgVars;
 }
Beispiel #8
0
        public static string SubstituteAndEscapeAllVariablesInJson(string jsonInputs, IVariables variables, ILog log)
        {
            if (!TemplateParser.TryParseTemplate(jsonInputs, out var template, out string error))
            {
                throw new CommandException($"Variable expression could not be parsed. Error: {error}");
            }

            jsonInputs = template.ToString(); // we parse the template back to string to have a consistent representation of Octostache expressions
            foreach (var templateToken in template.Tokens)
            {
                string evaluated = variables.Evaluate(templateToken.ToString());
                if (templateToken.GetArguments().Any() && evaluated == templateToken.ToString())
                {
                    log.Warn($"Expression {templateToken.ToString()} could not be evaluated, check that the referenced variables exist.");
                }
                jsonInputs = jsonInputs.Replace($"\"{templateToken.ToString()}\"", JsonConvert.ToString(evaluated));
            }

            return(jsonInputs);
        }
Beispiel #9
0
 public static CloudFormationTemplate Create(ResolvedTemplatePath path, ITemplateInputs <Parameter> parameters, ICalamariFileSystem filesSystem, IVariables variables)
 {
     Guard.NotNull(path, "Path must not be null");
     return(new CloudFormationTemplate(() => variables.Evaluate(filesSystem.ReadFile(path.Value)), parameters, JsonConvert.DeserializeObject <List <StackFormationNamedOutput> >));
 }
Beispiel #10
0
        private static CalamariResult ExecuteScript(IScriptExecutor psse, string scriptName, IVariables variables)
        {
            var runner = new TestCommandLineRunner(ConsoleLog.Instance, variables);
            var result = psse.Execute(new Script(scriptName), variables, runner);

            return(new CalamariResult(result.ExitCode, runner.Output));
        }
Beispiel #11
0
 public static CloudFormationParametersFile Create(Maybe <ResolvedTemplatePath> path, ICalamariFileSystem fileSystem, IVariables variables)
 {
     return(new CloudFormationParametersFile(() => path.Select(x => variables.Evaluate(fileSystem.ReadFile(x.Value))), JsonConvert.DeserializeObject <List <Parameter> >));
 }
Beispiel #12
0
 public CommandLineRunner(ILog log, IVariables variables)
 {
     this.log       = log;
     this.variables = variables;
 }
Beispiel #13
0
 public TerminalScriptWrapper(IScriptExecutor scriptExecutor, IVariables variables)
 {
     this.scriptExecutor = scriptExecutor;
     this.variables      = variables;
 }
        public override string PathToPowerShellExecutable(IVariables variables)
        {
            var customVersion = variables[PowerShellVariables.CustomPowerShellVersion];

            try
            {
                var availablePowerShellVersions = new[]
                {
                    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
                    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
                }
                .Where(p => p != null)
                .Distinct()
                .Select(pf => Path.Combine(pf, "PowerShell"))
                .Where(fileSystem.DirectoryExists)
                .SelectMany(fileSystem.EnumerateDirectories)
                .Select <string, (string path, string versionId, int?majorVersion, string remaining)>(d =>
                {
                    var directoryName = fileSystem.GetDirectoryName(d);

                    // Directories are typically versions like "6" or they might also have a prerelease component like "7-preview"
                    var splitString      = directoryName.Split(new[] { '-' }, 2);
                    var majorVersionPart = splitString[0];
                    var preRelease       =
                        splitString.Length < 2
                                ? string.Empty
                                : splitString[1]; // typically a prerelease tag, like "preview"

                    if (int.TryParse(majorVersionPart, out var majorVersion))
                    {
                        return(d, directoryName, majorVersion, preRelease);
                    }
                    return(d, directoryName, null, preRelease);
                })
                .ToList();

                var latestPowerShellVersionDirectory = availablePowerShellVersions
                                                       .Where(p => string.IsNullOrEmpty(customVersion) || p.versionId == customVersion)
                                                       .OrderByDescending(p => p.majorVersion)
                                                       .ThenBy(p => p.remaining)
                                                       .Select(p => p.path)
                                                       .FirstOrDefault();

                if (!string.IsNullOrEmpty(customVersion) && latestPowerShellVersionDirectory == null)
                {
                    throw new PowerShellVersionNotFoundException(customVersion, availablePowerShellVersions.Select(v => v.versionId));
                }

                if (latestPowerShellVersionDirectory == null)
                {
                    return(EnvPowerShellPath);
                }

                var pathToPwsh = Path.Combine(latestPowerShellVersionDirectory, EnvPowerShellPath);

                return(fileSystem.FileExists(pathToPwsh) ? pathToPwsh : EnvPowerShellPath);
            }
            catch (PowerShellVersionNotFoundException)
            {
                throw;
            }
            catch (Exception)
            {
                return(EnvPowerShellPath);
            }
        }
 public ParseContextFile(ParseContext parent, IVariables vars, String fn)
     : base(parent, vars, File.ReadAllText(fn = getFullFileName(parent, fn)))
 {
     FileName = fn;
     BaseDir  = Path.GetDirectoryName(fn);
 }
Beispiel #16
0
        public async Task GetAndPipeOutputVariablesWithRetry(Func <Task <Maybe <Stack> > > query, IVariables variables, bool wait, int retryCount, TimeSpan waitPeriod)
        {
            for (var retry = 0; retry < retryCount; ++retry)
            {
                var(result, success) = await GetOutputVariables(query);

                if (success || !wait)
                {
                    PipeOutputs(result, variables);
                    break;
                }

                // Wait for a bit for and try again
                await Task.Delay(waitPeriod);
            }
        }
 public ParseContextVar(ParseContext parent, IVariables vars, String key, String content)
     : base(parent, vars, content)
 {
     VarName = key;
 }
 protected string Replace(IVariables variables, string existingFile)
 {
     return(Replace(variables, existingFile, File.ReadAllText));
 }
Beispiel #19
0
 public MidnightScenario(
     IVariables variables)
 {
     _variables = variables;
 }
Beispiel #20
0
 protected abstract IEnumerable <ScriptExecution> PrepareExecution(Script script,
                                                                   IVariables variables,
                                                                   Dictionary <string, string>?environmentVars = null);
Beispiel #21
0
 public KubernetesContextScriptWrapper(IVariables variables)
 {
     this.fileSystem        = new WindowsPhysicalFileSystem();
     this.embeddedResources = new AssemblyEmbeddedResources();
     this.variables         = variables;
 }
Beispiel #22
0
        protected CalamariResult ExecuteScript(IScriptExecutor psse, string scriptName, IVariables variables)
        {
            var runner = new TestCommandLineRunner(new InMemoryLog(), variables);
            var result = psse.Execute(new Script(scriptName), variables, runner);

            return(new CalamariResult(result.ExitCode, runner.Output));
        }
Beispiel #23
0
 public ExtractPackage(ICombinedPackageExtractor combinedPackageExtractor, ICalamariFileSystem fileSystem, IVariables variables, ILog log)
 {
     this.combinedPackageExtractor = combinedPackageExtractor;
     this.fileSystem = fileSystem;
     this.variables  = variables;
     this.log        = log;
 }
Beispiel #24
0
 public SubstituteInFiles(ICalamariFileSystem fileSystem, IFileSubstituter substituter, IVariables variables)
 {
     this.fileSystem  = fileSystem;
     this.substituter = substituter;
     this.variables   = variables;
 }
Beispiel #25
0
        protected override IEnumerable <ScriptExecution> PrepareExecution(Script script, IVariables variables,
                                                                          Dictionary <string, string> environmentVars = null)
        {
            var workingDirectory  = Path.GetDirectoryName(script.File);
            var configurationFile = BashScriptBootstrapper.PrepareConfigurationFile(workingDirectory, variables);

            var(bootstrapFile, otherTemporaryFiles) = BashScriptBootstrapper.PrepareBootstrapFile(script, configurationFile, workingDirectory, variables);

            var invocation = new CommandLineInvocation(
                BashScriptBootstrapper.FindBashExecutable(),
                BashScriptBootstrapper.FormatCommandArguments(Path.GetFileName(bootstrapFile))
                )
            {
                WorkingDirectory = workingDirectory,
                EnvironmentVars  = environmentVars
            };

            yield return(new ScriptExecution(
                             invocation,
                             otherTemporaryFiles.Concat(new[] { bootstrapFile, configurationFile })
                             ));
        }
Beispiel #26
0
 public string GetTemplateContent(string relativePath, bool inPackage, IVariables variables)
 {
     return(resolver.Resolve(relativePath, inPackage, variables)
            .Map(x => x.Value)
            .Map(fileSystem.ReadFile));
 }
Beispiel #27
0
 public AwsScriptWrapper(ILog log, IVariables variables)
 {
     this.log       = log;
     this.variables = variables;
 }
Beispiel #28
0
 public void SetUp()
 {
     journal = Substitute.For <IDeploymentJournal>();
     journal.GetLatestInstallation(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>()).Returns(_ => previous);
     variables = new CalamariVariables();
 }
Beispiel #29
0
        public static (string bootstrapFile, string[] temporaryFiles) PrepareBootstrapFile(string scriptFilePath, string configurationFile, string workingDirectory, IVariables variables)
        {
            var bootstrapFile     = Path.Combine(workingDirectory, "Bootstrap." + Guid.NewGuid().ToString().Substring(10) + "." + Path.GetFileName(scriptFilePath));
            var scriptModulePaths = PrepareScriptModules(variables, workingDirectory).ToArray();

            using (var file = new FileStream(bootstrapFile, FileMode.CreateNew, FileAccess.Write))
                using (var writer = new StreamWriter(file, Encoding.UTF8))
                {
                    writer.WriteLine("#load \"" + configurationFile.Replace("\\", "\\\\") + "\"");
                    writer.WriteLine("open Octopus");
                    writer.WriteLine("Octopus.initializeProxy()");
                    writer.WriteLine("#load \"" + scriptFilePath.Replace("\\", "\\\\") + "\"");
                    writer.Flush();
                }

            File.SetAttributes(bootstrapFile, FileAttributes.Hidden);
            return(bootstrapFile, scriptModulePaths);
        }
Beispiel #30
0
		/// <summary>
		///   加入變數類別方法
		/// </summary>
		/// <param name="var">變數擴充介面</param>
		public void AddVariable(IVariables var) {
			lock (__cVariables) {
				__cVariables.Add(var);
			}
		}
 public override string PathToPowerShellExecutable(IVariables variables)
 {
     return("pwsh");
 }