public static ICloudFormationRequestBuilder Create(ITemplateResolver templateResolver, string templateFile, string templateParameterFile, bool filesInPackage, ICalamariFileSystem fileSystem, IVariables variables, string stackName, List <string> capabilities, bool disableRollback, string roleArn, IEnumerable <KeyValuePair <string, string> > tags, StackArn stack, Func <IAmazonCloudFormation> clientFactory) { var resolvedTemplate = templateResolver.Resolve(templateFile, filesInPackage, variables); var resolvedParameters = templateResolver.MaybeResolve(templateParameterFile, filesInPackage, variables); if (!string.IsNullOrWhiteSpace(templateParameterFile) && !resolvedParameters.Some()) { throw new CommandException("Could not find template parameters file: " + templateParameterFile); } return(new CloudFormationTemplate(() => variables.Evaluate(fileSystem.ReadFile(resolvedTemplate.Value)), CloudFormationParametersFile.Create(resolvedParameters, fileSystem, variables), stackName, capabilities, disableRollback, roleArn, tags, stack, clientFactory, variables)); }
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); }
internal static ICollection<PrivateKeyAccessRule> GetPrivateKeyAccessRules(IVariables variables) { // The private-key access-rules are stored as escaped JSON. However, they may contain nested // variables (for example the user-name may be an Octopus variable) which may not be escaped, // causing JSON parsing to fail. // So, we get the raw text var raw = variables.GetRaw(SpecialVariables.Certificate.PrivateKeyAccessRules); if (string.IsNullOrWhiteSpace(raw)) return new List<PrivateKeyAccessRule>(); // Unescape it (we only care about backslashes) var unescaped = raw.Replace(@"\\", @"\"); // Perform variable-substitution and re-escape var escapedAndSubstituted = variables.Evaluate(unescaped).Replace(@"\", @"\\"); return PrivateKeyAccessRule.FromJson(escapedAndSubstituted); }
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); }
public Maybe <ResolvedTemplatePath> MaybeResolve(string relativeFilePath, bool inPackage, IVariables variables) { var absolutePath = relativeFilePath.ToMaybe().Select(path => inPackage ? Path.Combine(variables.Get(KnownVariables.OriginalPackageDirectoryPath), variables.Evaluate(path)) : Path.Combine(Environment.CurrentDirectory, path)); return(absolutePath.SelectValueOr(x => !filesystem.FileExists(x) ? Maybe <ResolvedTemplatePath> .None : new ResolvedTemplatePath(x).AsSome(), Maybe <ResolvedTemplatePath> .None )); }
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> >)); }
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> >)); }