/
index.ts
233 lines (210 loc) · 6.7 KB
/
index.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { SYSTEM_INSUFFICIENT_MEMORY } from '../../../constants/error-messages';
import { getPkgReleases } from '../../../datasource';
import { logger } from '../../../logger';
import * as versioning from '../../../versioning';
import {
DockerOptions,
ExecConfig,
Opt,
VolumeOption,
VolumesPair,
rawExec,
} from '../common';
const prefetchedImages = new Set<string>();
async function prefetchDockerImage(taggedImage: string): Promise<void> {
if (!prefetchedImages.has(taggedImage)) {
logger.debug(`Fetching Docker image: ${taggedImage}`);
prefetchedImages.add(taggedImage);
await rawExec(`docker pull ${taggedImage}`, { encoding: 'utf-8' });
logger.debug(`Finished fetching Docker image`);
}
}
export function resetPrefetchedImages(): void {
prefetchedImages.clear();
}
function expandVolumeOption(x: VolumeOption): VolumesPair | null {
if (typeof x === 'string') {
return [x, x];
}
if (Array.isArray(x) && x.length === 2) {
const [from, to] = x;
if (typeof from === 'string' && typeof to === 'string') {
return [from, to];
}
}
return null;
}
function volumesEql(x: VolumesPair, y: VolumesPair): boolean {
const [xFrom, xTo] = x;
const [yFrom, yTo] = y;
return xFrom === yFrom && xTo === yTo;
}
function uniq<T = unknown>(
array: T[],
eql = (x: T, y: T): boolean => x === y
): T[] {
return array.filter((x, idx, arr) => {
return arr.findIndex((y) => eql(x, y)) === idx;
});
}
function prepareVolumes(volumes: VolumeOption[] = []): string[] {
const expanded: (VolumesPair | null)[] = volumes.map(expandVolumeOption);
const filtered: VolumesPair[] = expanded.filter((vol) => vol !== null);
const unique: VolumesPair[] = uniq<VolumesPair>(filtered, volumesEql);
return unique.map(([from, to]) => {
return `-v "${from}":"${to}"`;
});
}
function prepareCommands(commands: Opt<string>[]): string[] {
return commands.filter((command) => command && typeof command === 'string');
}
async function getDockerTag(
depName: string,
constraint: string,
scheme: string
): Promise<string> {
const { isValid, isVersion, matches, sortVersions } = versioning.get(scheme);
if (!isValid(constraint)) {
logger.warn({ constraint }, `Invalid ${scheme} version constraint`);
return 'latest';
}
logger.debug(
{ constraint },
`Found ${scheme} version constraint - checking for a compatible ${depName} image to use`
);
const imageReleases = await getPkgReleases({ datasource: 'docker', depName });
if (imageReleases && imageReleases.releases) {
let versions = imageReleases.releases.map((release) => release.version);
versions = versions.filter(
(version) => isVersion(version) && matches(version, constraint)
);
versions = versions.sort(sortVersions);
if (versions.length) {
const version = versions.pop();
logger.debug(
{ constraint, version },
`Found compatible ${scheme} version`
);
return version;
}
} /* istanbul ignore next */ else {
logger.error(`No ${depName} releases found`);
return 'latest';
}
logger.warn(
{ constraint },
'Failed to find a tag satisfying constraint, using "latest" tag instead'
);
return 'latest';
}
function getContainerName(image: string): string {
return image.replace(/\//g, '_');
}
export async function removeDockerContainer(image): Promise<void> {
const containerName = getContainerName(image);
let cmd = `docker ps --filter name=${containerName} -aq`;
try {
const res = await rawExec(cmd, {
encoding: 'utf-8',
});
const containerId = res?.stdout?.trim() || '';
// istanbul ignore if
if (containerId.length) {
logger.debug({ containerId }, 'Removing container');
cmd = `docker rm -f ${containerId}`;
await rawExec(cmd, {
encoding: 'utf-8',
});
} else {
logger.trace({ image, containerName }, 'No running containers to remove');
}
} catch (err) /* istanbul ignore next */ {
logger.trace({ err }, 'removeDockerContainer err');
logger.info(
{ image, containerName, cmd },
'Could not remove Docker container'
);
}
}
// istanbul ignore next
export async function removeDanglingContainers(): Promise<void> {
try {
const res = await rawExec(`docker ps --filter label=renovate_child -aq`, {
encoding: 'utf-8',
});
if (res?.stdout?.trim().length) {
const containerIds = res.stdout
.trim()
.split('\n')
.map((container) => container.trim())
.filter(Boolean);
logger.debug({ containerIds }, 'Removing dangling child containers');
await rawExec(`docker rm -f ${containerIds.join(' ')}`, {
encoding: 'utf-8',
});
} else {
logger.debug('No dangling containers to remove');
}
} catch (err) /* istanbul ignore next */ {
if (err.errno === 'ENOMEM') {
throw new Error(SYSTEM_INSUFFICIENT_MEMORY);
}
if (err.stderr?.includes('Cannot connect to the Docker daemon')) {
logger.info('No docker daemon found');
} else {
logger.warn({ err }, 'Error removing dangling containers');
}
}
}
export async function generateDockerCommand(
commands: string[],
options: DockerOptions,
config: ExecConfig
): Promise<string> {
const { image, envVars, cwd, tagScheme, tagConstraint } = options;
const volumes = options.volumes || [];
const preCommands = options.preCommands || [];
const postCommands = options.postCommands || [];
const { localDir, cacheDir, dockerUser } = config;
const result = ['docker run --rm'];
const containerName = getContainerName(image);
result.push(`--name=${containerName}`);
result.push(`--label=renovate_child`);
if (dockerUser) {
result.push(`--user=${dockerUser}`);
}
result.push(...prepareVolumes([localDir, cacheDir, ...volumes]));
if (envVars) {
result.push(
...uniq(envVars)
.filter((x) => typeof x === 'string')
.map((e) => `-e ${e}`)
);
}
if (cwd) {
result.push(`-w "${cwd}"`);
}
let tag;
if (options.tag) {
tag = options.tag;
} else if (tagConstraint) {
const tagVersioning = tagScheme || 'semver';
tag = await getDockerTag(image, tagConstraint, tagVersioning);
logger.debug(
{ image, tagConstraint, tagVersioning, tag },
'Resolved tag constraint'
);
} else {
logger.debug({ image }, 'No tag or tagConstraint specified');
}
const taggedImage = tag ? `${image}:${tag}` : `${image}`;
await prefetchDockerImage(taggedImage);
result.push(taggedImage);
const bashCommand = [
...prepareCommands(preCommands),
...commands,
...prepareCommands(postCommands),
].join(' && ');
result.push(`bash -l -c "${bashCommand.replace(/"/g, '\\"')}"`); // lgtm [js/incomplete-sanitization]
return result.join(' ');
}