Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create var file for template variables #7511

Merged
merged 9 commits into from
Jun 29, 2018
5 changes: 2 additions & 3 deletions Tasks/PackerBuildV0/src/operations/packerBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export async function run(packerHost: packerHost): Promise<any> {
var variableProviders = packerHost.getTemplateVariablesProviders();
for (var provider of variableProviders) {
var variables = await provider.getTemplateVariables(packerHost);
variables.forEach((value: string, key: string) => {
command.arg(["-var", util.format("%s=%s", key, value)]);
});
let varFilePath = utils.createTemplateVarFile(variables);
command.arg(util.format("%s=%s", '-var-file', varFilePath));
}

command.arg(packerHost.getTemplateFileProvider().getTemplateFileLocation(packerHost));
Expand Down
6 changes: 3 additions & 3 deletions Tasks/PackerBuildV0/src/operations/packerValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from "path";
import * as util from "util";
import * as tl from "vsts-task-lib/task";
import packerHost from "../packerHost";
import * as utils from "../utilities"

export async function run(packerHost: packerHost): Promise<void> {
var command = packerHost.createPackerTool();
Expand All @@ -13,9 +14,8 @@ export async function run(packerHost: packerHost): Promise<void> {
var variableProviders = packerHost.getTemplateVariablesProviders();
for (var provider of variableProviders) {
var variables = await provider.getTemplateVariables(packerHost);
variables.forEach((value: string, key: string) => {
command.arg(["-var", util.format("%s=%s", key, value)]);
});
let varFilePath = utils.createTemplateVarFile(variables);
command.arg(util.format("%s=%s", '-var-file', varFilePath));
}

command.arg(packerHost.getTemplateFileProvider().getTemplateFileLocation(packerHost));
Expand Down
11 changes: 11 additions & 0 deletions Tasks/PackerBuildV0/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ export function readJsonFile(filePath: string): string {
return content;
}

export function createTemplateVarFile (templateVariables: Map<string, string>): string {
let filePath: string = path.resolve(tl.getVariable('Agent.TempDirectory'), Math.random().toString(36).replace('0.', '') + '.json');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of random, you can also use Date.now() which is good enough

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using Date.now(), but since we create 2 separate var files one after the other, there were name collisions a few times. That is why I am using to Math.random.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

let res = {};
templateVariables.forEach((value: string, key: string) => {
res[key] = value
});
let content: string = JSON.stringify(res);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test with special characters in values - space, quotes etc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this on ubuntu 18.04 and win 10. The var file created had whitespaces and quotes in its path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also test for variable values containing special chars. They should be properly escaped when using Json.Stringify and packer should be able to consume them successfully

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

tl.writeFile(filePath, content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a writeFile utility which will be useful in l0 mocking

return filePath
}

export function writeFile(filePath: string, content: string): void {
tl.writeFile(filePath, content);
}
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PackerBuildV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 0,
"Patch": 14
"Patch": 15
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PackerBuildV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 0,
"Patch": 14
"Patch": 15
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down