-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathdockercomposeconnection.ts
193 lines (173 loc) · 7.29 KB
/
dockercomposeconnection.ts
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
import * as del from "del";
import * as path from "path";
import * as tl from "azure-pipelines-task-lib/task";
import * as tr from "azure-pipelines-task-lib/toolrunner";
import * as yaml from "js-yaml";
import * as DockerComposeUtils from "./dockercomposeutils";
import ContainerConnection from "azure-pipelines-tasks-docker-common/containerconnection"
import AuthenticationToken from "azure-pipelines-tasks-docker-common/registryauthenticationprovider/registryauthenticationtoken"
import * as Utils from "./utils";
export default class DockerComposeConnection extends ContainerConnection {
private dockerComposePath: string;
private dockerComposeFile: string;
private dockerComposeVersion: string;
private additionalDockerComposeFiles: string[];
private requireAdditionalDockerComposeFiles: boolean;
private projectName: string;
private finalComposeFile: string;
constructor() {
super();
this.setDockerComposePath();
this.dockerComposeFile = DockerComposeUtils.findDockerFile(tl.getInput("dockerComposeFile", true), tl.getInput("cwd"));
if (!this.dockerComposeFile) {
throw new Error("No Docker Compose file matching " + tl.getInput("dockerComposeFile") + " was found.");
}
this.dockerComposeVersion = "2";
this.additionalDockerComposeFiles = tl.getDelimitedInput("additionalDockerComposeFiles", "\n");
this.requireAdditionalDockerComposeFiles = tl.getBoolInput("requireAdditionalDockerComposeFiles");
this.projectName = tl.getInput("projectName");
}
public open(hostEndpoint?: string, authenticationToken?: AuthenticationToken): any {
super.open(hostEndpoint, authenticationToken);
if (this.hostUrl) {
process.env["DOCKER_HOST"] = this.hostUrl;
process.env["DOCKER_TLS_VERIFY"] = "1";
process.env["DOCKER_CERT_PATH"] = this.certsDir;
}
tl.getDelimitedInput("dockerComposeFileArgs", "\n").forEach(envVar => {
var tokens = envVar.split("=");
if (tokens.length < 2) {
throw new Error("Environment variable '" + envVar + "' is invalid.");
}
process.env[tokens[0].trim()] = tokens.slice(1).join("=").trim();
});
return this.getImages(true).then(images => {
var qualifyImageNames = tl.getBoolInput("qualifyImageNames");
if (!qualifyImageNames) {
return;
}
var agentDirectory = tl.getVariable("Agent.HomeDirectory");
this.finalComposeFile = path.join(agentDirectory, Utils.getFinalComposeFileName());
var services = {};
if (qualifyImageNames) {
for (var serviceName in images) {
images[serviceName] = this.getQualifiedImageNameIfRequired(images[serviceName]);
}
}
for (var serviceName in images) {
services[serviceName] = {
image: images[serviceName]
};
}
Utils.writeFileSync(this.finalComposeFile, yaml.safeDump({
version: this.dockerComposeVersion,
services: services
}, { lineWidth: -1 } as any));
});
}
public async execCommandWithLogging(command, options?: tr.IExecOptions): Promise<string> {
// setup variable to store the command output
let output = "";
command.on("stdout", data => {
output += data;
});
command.on("stderr", data => {
output += data;
});
#if NODE16
await this.execCommand(command, options);
#else
await this.execCommand(command as unknown as tr.ToolRunner, options);
#endif
return output || '\n';
}
public createComposeCommand(): tr.ToolRunner {
var command = tl.tool(this.dockerComposePath);
command.arg(["-f", this.dockerComposeFile]);
var basePath = path.dirname(this.dockerComposeFile);
this.additionalDockerComposeFiles.forEach(file => {
file = this.resolveAdditionalDockerComposeFilePath(basePath, file);
if (this.requireAdditionalDockerComposeFiles || tl.exist(file)) {
command.arg(["-f", file]);
}
});
if (this.finalComposeFile) {
command.arg(["-f", this.finalComposeFile]);
}
if (this.projectName) {
command.arg(["-p", this.projectName]);
}
return command;
}
public getCombinedConfig(imageDigestComposeFile?: string): any {
var command = this.createComposeCommand();
if (imageDigestComposeFile) {
command.arg(["-f", imageDigestComposeFile]);
}
command.arg("config");
var result = "";
command.on("stdout", data => {
result += data;
});
command.on("errline", line => {
tl.error(line);
});
return command.exec({ silent: true } as any).then(() => result);
}
public getImages(builtOnly?: boolean): any {
return this.getCombinedConfig().then(input => {
var doc = yaml.safeLoad(input);
if (doc.version) {
this.dockerComposeVersion = doc.version;
}
var projectName = this.projectName;
if (!projectName) {
projectName = path.basename(path.dirname(this.dockerComposeFile));
}
var images: any = {};
for (var serviceName in doc.services || {}) {
var service = doc.services[serviceName];
var image = service.image;
if (!image) {
image = projectName.toLowerCase().replace(/[^0-9a-z]/g, "") + "_" + serviceName;
}
if (!builtOnly || service.build) {
images[serviceName] = image;
}
}
return images;
});
}
public getVersion(): string {
return this.dockerComposeVersion;
}
public close(): void {
if (this.finalComposeFile && tl.exist(this.finalComposeFile)) {
del.sync(this.finalComposeFile, { force: true });
}
super.close();
}
private resolveAdditionalDockerComposeFilePath(dockerComposeFolderPath: string, additionalComposeFilePath: string): string {
if (!path.isAbsolute(additionalComposeFilePath)) {
additionalComposeFilePath = path.join(dockerComposeFolderPath, additionalComposeFilePath);
}
if (!tl.exist(additionalComposeFilePath)) {
tl.warning(tl.loc('AdditionalDockerComposeFileDoesNotExists', additionalComposeFilePath));
}
return additionalComposeFilePath;
}
private setDockerComposePath(): void {
//Priority to docker-compose path provided by user
this.dockerComposePath = tl.getInput('dockerComposePath');
if (!this.dockerComposePath) {
//If not use the docker-compose avilable on agent
this.dockerComposePath = tl.which("docker-compose");
if (!this.dockerComposePath) {
throw new Error("Docker Compose was not found. You can provide the path to docker-compose via 'dockerComposePath' ");
}
} else {
console.log("Using docker-compose from 'dockerComposePath' ");
}
}
}