-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·172 lines (147 loc) · 4.55 KB
/
app.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const {
bootstrap,
docker,
helpers,
} = require("@kaholo/plugin-library");
const {
getLoginEnvironmentVariables,
createDockerLoginCommand,
parseDockerImageString,
execCommand,
getDockerImage,
resolveEnvironmentalVariablesObject,
prepareContainerCommand,
} = require("./helpers");
const constants = require("./consts.json");
async function build({
TAG: imageTag,
PATH: buildPathInfo,
dockerfileName = "Dockerfile",
}) {
const workingDirectoryPath = (buildPathInfo ?? await helpers.analyzePath("./")).absolutePath;
const dockerFilePathInfo = await helpers.analyzePath(`${workingDirectoryPath}/${dockerfileName}`);
if (!dockerFilePathInfo.exists || dockerFilePathInfo.type !== "file") {
throw new Error(`No Dockerfile was found at ${dockerFilePathInfo.absolutePath} on the Kaholo agent.`);
}
const cmd = `docker build ${imageTag ? `-t ${imageTag} ` : ""}${buildPathInfo.absolutePath}`;
await execCommand(cmd);
if (imageTag) {
return getDockerImage(imageTag);
}
return constants.EMPTY_RETURN_VALUE;
}
async function run(params) {
const {
imageName,
command,
environmentalVariables = {},
secretEnvVariables = {},
} = params;
const workingDirectoryInfo = params.workingDirectory || await helpers.analyzePath("./");
const workingDirectory = workingDirectoryInfo.absolutePath;
if (workingDirectoryInfo.type !== "directory") {
throw new Error(`Working Directory must be a directory, provided path type: "${workingDirectoryInfo.type}"`);
}
const resolvedEnv = resolveEnvironmentalVariablesObject(
environmentalVariables,
secretEnvVariables,
);
const commandName = "docker";
const commandArgs = [
"run",
"--rm",
];
if (Object.keys(resolvedEnv).length > 0) {
const environmentVariablesParams = docker.buildEnvironmentVariableArguments(environmentalVariables).join(" ");
commandArgs.push(environmentVariablesParams);
}
commandArgs.push(
"-v",
`'${workingDirectory}':'${workingDirectory}'`,
"--workdir",
`'${workingDirectory}'`,
imageName,
prepareContainerCommand(command),
);
const cmd = `${commandName} ${commandArgs.join(" ")}`;
return execCommand(cmd, {
...process.env,
...resolvedEnv,
});
}
async function pull({
image,
USER: username,
PASSWORD: password,
}) {
const environmentVariables = getLoginEnvironmentVariables(username, password);
const parsedImage = parseDockerImageString(image);
const dockerPullCommand = `docker pull ${parsedImage.imagestring}`;
const credentialsGiven = (username && password);
const command = (
(credentialsGiven)
? `${createDockerLoginCommand(parsedImage.hostport)} && ${dockerPullCommand}`
: dockerPullCommand
);
console.info(`Running command: ${command}`);
await execCommand(command, environmentVariables, credentialsGiven);
return getDockerImage(parsedImage.imagestring);
}
async function pushImage({
image,
USER: username,
PASSWORD: password,
}) {
const parsedImage = parseDockerImageString(image);
const dockerPushCommand = `docker push ${parsedImage.imagestring}`;
const environmentVariables = getLoginEnvironmentVariables(username, password);
const credentialsGiven = true; // required in config.json
const command = `${createDockerLoginCommand(parsedImage.hostport)} && ${dockerPushCommand}`;
console.info(`Running command: ${command}`);
return execCommand(command, environmentVariables, credentialsGiven);
}
async function tag({
sourceImage,
targetImage,
}) {
const command = `docker tag ${sourceImage} ${targetImage}`;
await execCommand(command);
return getDockerImage(targetImage);
}
async function cmdExec({
PARAMS: inputCommand,
USER: username,
PASSWORD: password,
registryUrl,
attemptJson,
}) {
const commandsToExecute = [];
let environmentVariables = {};
let shredCredentials = false;
const useAuthentication = username && password;
if (useAuthentication) {
commandsToExecute.push(createDockerLoginCommand(registryUrl));
environmentVariables = getLoginEnvironmentVariables(username, password);
shredCredentials = true;
}
let userCommand = inputCommand.startsWith("docker ") ? inputCommand : `docker ${inputCommand}`;
if (attemptJson && !/ --format ['"]{{json . }}['"]/g.test(userCommand)) {
userCommand += " --format \"{{json . }}\"";
}
commandsToExecute.push(userCommand);
const command = commandsToExecute.join(" && ");
return execCommand(
command,
environmentVariables,
shredCredentials,
attemptJson,
);
}
module.exports = bootstrap({
build,
run,
pull,
pushImage,
tag,
cmdExec,
});