-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
configuration-strategy.ts
56 lines (47 loc) · 1.79 KB
/
configuration-strategy.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
import { getContainerRuntimeConfig } from "./utils/config";
import { DockerOptions } from "dockerode";
import { URL } from "url";
import fs from "fs/promises";
import path from "path";
import { ContainerRuntimeClientStrategyResult } from "./types";
import { ContainerRuntimeClientStrategy } from "./strategy";
export class ConfigurationStrategy implements ContainerRuntimeClientStrategy {
private dockerHost!: string;
private dockerTlsVerify: string | undefined;
private dockerCertPath: string | undefined;
getName(): string {
return "ConfigurationStrategy";
}
async getResult(): Promise<ContainerRuntimeClientStrategyResult | undefined> {
const { dockerHost, dockerTlsVerify, dockerCertPath } = await getContainerRuntimeConfig();
if (!dockerHost) {
return undefined;
}
this.dockerHost = dockerHost;
this.dockerTlsVerify = dockerTlsVerify;
this.dockerCertPath = dockerCertPath;
const dockerOptions: DockerOptions = {};
const { pathname, hostname, port } = new URL(this.dockerHost);
if (hostname !== "") {
dockerOptions.host = hostname;
dockerOptions.port = port;
} else {
dockerOptions.socketPath = pathname;
}
if (this.dockerTlsVerify === "1" && this.dockerCertPath !== undefined) {
dockerOptions.ca = await fs.readFile(path.resolve(this.dockerCertPath, "ca.pem"));
dockerOptions.cert = await fs.readFile(path.resolve(this.dockerCertPath, "cert.pem"));
dockerOptions.key = await fs.readFile(path.resolve(this.dockerCertPath, "key.pem"));
}
return {
uri: this.dockerHost,
dockerOptions,
composeEnvironment: {
DOCKER_HOST: this.dockerHost,
DOCKER_TLS_VERIFY: this.dockerTlsVerify,
DOCKER_CERT_PATH: this.dockerCertPath,
},
allowUserOverrides: true,
};
}
}