-
Notifications
You must be signed in to change notification settings - Fork 1
/
terraform-cli.js
130 lines (113 loc) · 3.54 KB
/
terraform-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { resolve: resolvePath } = require("path");
const pluginLib = require("@kaholo/plugin-library");
const {
validateDirectoryPath,
convertMapToObject,
generateRandomTemporaryPath,
saveToRandomTemporaryFile,
shredTerraformVarFile,
getCurrentUserId,
asyncExec,
} = require("./helpers");
function createTerraformCommand(baseCommand, {
variableFile,
json,
additionalArgs = [],
}) {
const command = baseCommand.startsWith("terraform ") ? baseCommand.substring(10) : baseCommand;
const postArgs = [...additionalArgs];
if (variableFile) {
postArgs.push("-var-file=$TERRAFORM_VAR_FILE_MOUNT_POINT");
}
if (json) {
if (baseCommand !== "init") {
postArgs.push("-json");
} else {
console.error("JSON Output is not supported for this Terraform command.");
}
}
return `${command} ${postArgs.join(" ")}`;
}
async function execute(params) {
const {
workingDirectory,
command,
baseCommand,
variables,
secretEnvVariables,
rawOutput,
additionalArgs,
customDockerImage,
} = params;
const environmentVariables = new Map();
const absoluteWorkingDirectory = workingDirectory ? resolvePath(workingDirectory) : process.cwd();
await validateDirectoryPath(absoluteWorkingDirectory);
environmentVariables.set("TERRAFORM_DIR", absoluteWorkingDirectory);
environmentVariables.set("TERRAFORM_DIR_MOUNT_POINT", generateRandomTemporaryPath());
if (variables) {
const fileName = await saveToRandomTemporaryFile(variables);
environmentVariables.set("TERRAFORM_VAR_FILE", fileName);
environmentVariables.set("TERRAFORM_VAR_FILE_MOUNT_POINT", generateRandomTemporaryPath());
}
let terraformCommand;
// handle method runCommand
if (command) {
const commandString = command.join(" ");
terraformCommand = commandString.startsWith("terraform ") ? commandString.substring(10) : commandString;
if (!rawOutput && !commandString.includes("-json")) {
terraformCommand = `${terraformCommand} -json`;
}
}
// handle method runMainCommand
if (baseCommand) {
terraformCommand = createTerraformCommand(baseCommand, {
variableFile: environmentVariables.has("TERRAFORM_VAR_FILE_MOUNT_POINT"),
json: !rawOutput,
additionalArgs,
});
}
const dockerEnvs = secretEnvVariables ? pluginLib.parsers.keyValuePairs(secretEnvVariables) : {};
const buildDockerCommandOptions = {
image: customDockerImage,
command: terraformCommand,
user: await getCurrentUserId(),
additionalArguments: [
"-w $TERRAFORM_DIR_MOUNT_POINT",
"-v $TERRAFORM_DIR:$TERRAFORM_DIR_MOUNT_POINT",
variables ? "-v $TERRAFORM_VAR_FILE:$TERRAFORM_VAR_FILE_MOUNT_POINT" : "",
],
};
if (dockerEnvs) {
buildDockerCommandOptions.environmentVariables = dockerEnvs;
}
const dockerCommand = pluginLib.docker.buildDockerCommand(buildDockerCommandOptions);
const {
error,
parsedObjects,
} = await asyncExec({
command: dockerCommand,
onProgressFn: process.stdout.write.bind(process.stdout),
options: {
env: {
...convertMapToObject(environmentVariables),
...dockerEnvs,
},
},
});
if (environmentVariables.has("TERRAFORM_VAR_FILE")) {
await shredTerraformVarFile(environmentVariables.get("TERRAFORM_VAR_FILE"));
}
if (parsedObjects) {
return parsedObjects;
}
if (error) {
if (!rawOutput) {
console.error("\nRECOMMENDATION: Try enabling parameter Raw Output for a more meaningful error message.\n");
}
throw new Error(error);
}
return "";
}
module.exports = {
execute,
};