build-strap 4.0.0-dev.46 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -34
- package/dist/src/ant.d.ts +19 -0
- package/dist/src/ant.js +18 -0
- package/dist/src/cert.d.ts +12 -0
- package/dist/src/cert.js +91 -0
- package/dist/src/clean.d.ts +4 -0
- package/dist/src/clean.js +11 -0
- package/dist/src/copy.d.ts +7 -0
- package/dist/src/copy.js +53 -0
- package/dist/src/cp.d.ts +17 -0
- package/dist/src/cp.js +88 -0
- package/dist/src/docker.container.d.ts +73 -0
- package/dist/src/docker.container.js +247 -0
- package/dist/src/docker.d.ts +68 -0
- package/dist/src/docker.js +188 -0
- package/dist/src/docker.network.d.ts +42 -0
- package/dist/src/docker.network.js +85 -0
- package/dist/src/docker.volume.d.ts +26 -0
- package/dist/src/docker.volume.js +62 -0
- package/dist/src/dockerCleanup.d.ts +38 -0
- package/dist/src/dockerCleanup.js +77 -0
- package/dist/src/dockerCompose.d.ts +154 -0
- package/dist/src/dockerCompose.js +439 -0
- package/dist/src/fs.d.ts +5 -0
- package/dist/src/fs.js +38 -0
- package/dist/src/git.d.ts +9 -0
- package/dist/src/git.js +24 -0
- package/dist/src/index.d.ts +20 -0
- package/dist/src/index.js +20 -0
- package/dist/src/maps.d.ts +33 -0
- package/dist/src/maps.js +41 -0
- package/dist/src/npm.d.ts +35 -0
- package/dist/src/npm.js +104 -0
- package/dist/src/pkg.d.ts +33 -0
- package/dist/src/pkg.js +33 -0
- package/dist/src/publish.d.ts +27 -0
- package/dist/src/publish.js +57 -0
- package/dist/src/run.d.ts +13 -0
- package/dist/src/run.js +44 -0
- package/dist/src/tgz.d.ts +13 -0
- package/dist/src/tgz.js +67 -0
- package/dist/src/version.d.ts +34 -0
- package/dist/src/version.js +101 -0
- package/dist/src/yarn.d.ts +26 -0
- package/dist/src/yarn.js +75 -0
- package/package.json +74 -20
- package/ant.js +0 -43
- package/ant.js.flow +0 -42
- package/artifactory.js +0 -617
- package/artifactory.js.flow +0 -616
- package/cert.conf +0 -20
- package/cert.js +0 -153
- package/cert.js.flow +0 -152
- package/clean.js +0 -20
- package/clean.js.flow +0 -19
- package/copy.js +0 -80
- package/copy.js.flow +0 -79
- package/cp.js +0 -173
- package/cp.js.flow +0 -172
- package/docker.container.js +0 -390
- package/docker.container.js.flow +0 -389
- package/docker.js +0 -365
- package/docker.js.flow +0 -364
- package/docker.network.js +0 -160
- package/docker.network.js.flow +0 -159
- package/docker.volume.js +0 -136
- package/docker.volume.js.flow +0 -136
- package/dockerCleanup.js +0 -152
- package/dockerCleanup.js.flow +0 -151
- package/dockerCompose.js +0 -766
- package/dockerCompose.js.flow +0 -765
- package/flow.js +0 -21
- package/flow.js.flow +0 -20
- package/fs.js +0 -95
- package/fs.js.flow +0 -96
- package/git.js +0 -35
- package/git.js.flow +0 -34
- package/hg.js +0 -35
- package/hg.js.flow +0 -34
- package/index.js +0 -27
- package/index.js.flow +0 -26
- package/inline-resources.js +0 -158
- package/inline-resources.js.flow +0 -157
- package/maps.js +0 -112
- package/maps.js.flow +0 -111
- package/npm.js +0 -145
- package/npm.js.flow +0 -144
- package/pkg.js +0 -81
- package/pkg.js.flow +0 -80
- package/publish.js +0 -145
- package/publish.js.flow +0 -144
- package/run.js +0 -90
- package/run.js.flow +0 -89
- package/src/ant.js +0 -42
- package/src/artifactory.js +0 -616
- package/src/cert.conf +0 -20
- package/src/cert.js +0 -152
- package/src/clean.js +0 -19
- package/src/copy.js +0 -79
- package/src/cp.js +0 -172
- package/src/docker.container.js +0 -389
- package/src/docker.js +0 -364
- package/src/docker.network.js +0 -159
- package/src/docker.volume.js +0 -136
- package/src/dockerCleanup.js +0 -151
- package/src/dockerCompose.js +0 -765
- package/src/flow.js +0 -20
- package/src/fs.js +0 -96
- package/src/git.js +0 -34
- package/src/hg.js +0 -34
- package/src/index.js +0 -26
- package/src/inline-resources.js +0 -157
- package/src/maps.js +0 -111
- package/src/npm.js +0 -144
- package/src/pkg.js +0 -80
- package/src/publish.js +0 -144
- package/src/run.js +0 -89
- package/src/test.js +0 -55
- package/src/tgz.js +0 -101
- package/src/version.js +0 -142
- package/src/yarn.js +0 -123
- package/test.js +0 -56
- package/test.js.flow +0 -55
- package/tgz.js +0 -102
- package/tgz.js.flow +0 -101
- package/version.js +0 -143
- package/version.js.flow +0 -142
- package/yarn.js +0 -124
- package/yarn.js.flow +0 -123
package/src/docker.js
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import bytes from 'bytes';
|
|
4
|
-
import isReachable from 'is-reachable';
|
|
5
|
-
import moment from 'moment';
|
|
6
|
-
import { buildLog } from './run';
|
|
7
|
-
import { getCfg, getPkgName, getPkgScope } from './pkg';
|
|
8
|
-
import { spawn } from './cp';
|
|
9
|
-
import { getVersion, getDevBranch } from './version';
|
|
10
|
-
import {
|
|
11
|
-
dockerContainerRunDaemon,
|
|
12
|
-
dockerContainerKill,
|
|
13
|
-
} from './docker.container';
|
|
14
|
-
import { dockerNetworkConnect } from './docker.network';
|
|
15
|
-
|
|
16
|
-
export type DockerConfig = {
|
|
17
|
-
registry?: string,
|
|
18
|
-
repository?: string,
|
|
19
|
-
name?: string,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export function getDockerConfig(): DockerConfig {
|
|
23
|
-
return getCfg().docker || {};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const parseDockerDate = (date: string): Date =>
|
|
27
|
-
moment(date, 'YYYY-MM-DD HH:mm:ss ZZ').toDate();
|
|
28
|
-
|
|
29
|
-
export async function dockerBuild(
|
|
30
|
-
tags: string[] = ['latest'],
|
|
31
|
-
buildArgs: string[] = [],
|
|
32
|
-
target: ?string = null,
|
|
33
|
-
extraArgs?: ?(string[]) = [],
|
|
34
|
-
repo: string = getDockerRepo(),
|
|
35
|
-
dockerFile: string = './Dockerfile',
|
|
36
|
-
workDir: string = '.',
|
|
37
|
-
): Promise<string> {
|
|
38
|
-
const args = [
|
|
39
|
-
'build',
|
|
40
|
-
...['-f', path.resolve(dockerFile)],
|
|
41
|
-
...(target ? ['--target', target] : []),
|
|
42
|
-
...tags.reduce((ar, t) => [...ar, '-t', `${repo}:${t}`], []),
|
|
43
|
-
...buildArgs.reduce((ar, a) => [...ar, '--build-arg', a], []),
|
|
44
|
-
...(extraArgs || []),
|
|
45
|
-
path.resolve(workDir),
|
|
46
|
-
];
|
|
47
|
-
|
|
48
|
-
return spawn('docker', args, { stdio: 'inherit' });
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export async function dockerTag(
|
|
52
|
-
imageId: string,
|
|
53
|
-
tags: string[] = ['latest'],
|
|
54
|
-
repo?: string = getDockerRepo(),
|
|
55
|
-
): Promise<void> {
|
|
56
|
-
await Promise.all(
|
|
57
|
-
tags.map(async (t) => {
|
|
58
|
-
await spawn('docker', ['tag', imageId, `${repo}:${t}`]);
|
|
59
|
-
buildLog(`Tagged image ${imageId} as ${repo}:${t}`);
|
|
60
|
-
}),
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Apply the standard convention for tagging the docker image for the project,
|
|
66
|
-
* based on the source control branch being built.
|
|
67
|
-
* For releases, applies `latest`, `M.m.p`, `M.m`, `M`
|
|
68
|
-
* For release candidates, applies `latest-rc`
|
|
69
|
-
* For feature branches, applies `latest-feature`
|
|
70
|
-
* For development branch, applies `latest-dev`
|
|
71
|
-
* All other branches, no tag is applies
|
|
72
|
-
* @param {string} imageId id of the docker image to which to apply the tags
|
|
73
|
-
* @param {string} repo optional override of the docker repo url to use (derived from package.json by default)
|
|
74
|
-
*/
|
|
75
|
-
export async function dockerTagVersion(
|
|
76
|
-
imageId: string,
|
|
77
|
-
repo?: string = getDockerRepo(),
|
|
78
|
-
) {
|
|
79
|
-
const { isRelease, major, minor, patch, branch } = await getVersion();
|
|
80
|
-
// determine what tags to apply
|
|
81
|
-
if (isRelease) {
|
|
82
|
-
await dockerTag(
|
|
83
|
-
imageId,
|
|
84
|
-
['latest', `${major}`, `${major}.${minor}`, `${major}.${minor}.${patch}`],
|
|
85
|
-
repo,
|
|
86
|
-
);
|
|
87
|
-
} else if (branch === (await getDevBranch())) {
|
|
88
|
-
await dockerTag(imageId, ['latest-dev'], repo);
|
|
89
|
-
} else if (branch.match(/^(release|patch)-/)) {
|
|
90
|
-
await dockerTag(imageId, ['latest-rc'], repo);
|
|
91
|
-
} else if (branch.match(/^feature-/)) {
|
|
92
|
-
await dockerTag(imageId, ['latest-feature'], repo);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export type DockerImage = {
|
|
97
|
-
repository: string,
|
|
98
|
-
tag: string,
|
|
99
|
-
id: string,
|
|
100
|
-
digest: string,
|
|
101
|
-
created: Date,
|
|
102
|
-
size: number,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export type DockerImageFilter = (i: DockerImage) => boolean;
|
|
106
|
-
|
|
107
|
-
export async function dockerImages(
|
|
108
|
-
repo?: ?string = getDockerRepo(),
|
|
109
|
-
filter?: ?DockerImageFilter = null,
|
|
110
|
-
): Promise<DockerImage[]> {
|
|
111
|
-
return (
|
|
112
|
-
await spawn(
|
|
113
|
-
'docker',
|
|
114
|
-
['images', '--format', `{{json .}}`, ...(repo ? [repo] : [])],
|
|
115
|
-
{ captureOutput: true },
|
|
116
|
-
)
|
|
117
|
-
)
|
|
118
|
-
.split('\n')
|
|
119
|
-
.filter((line) => !!line.trim())
|
|
120
|
-
.map((l) => {
|
|
121
|
-
const {
|
|
122
|
-
Repository: repository,
|
|
123
|
-
Tag: tag,
|
|
124
|
-
ID: id,
|
|
125
|
-
Digest: digest,
|
|
126
|
-
CreatedAt,
|
|
127
|
-
Size,
|
|
128
|
-
} = JSON.parse(l.trim());
|
|
129
|
-
return {
|
|
130
|
-
repository,
|
|
131
|
-
tag,
|
|
132
|
-
id,
|
|
133
|
-
digest,
|
|
134
|
-
created: parseDockerDate(CreatedAt),
|
|
135
|
-
size: bytes.parse(Size),
|
|
136
|
-
};
|
|
137
|
-
})
|
|
138
|
-
.filter(
|
|
139
|
-
(m) =>
|
|
140
|
-
(m && m.id) != null &&
|
|
141
|
-
(typeof filter === 'function' ? filter(m) : true),
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export async function getDockerId(
|
|
146
|
-
tag: string = 'latest',
|
|
147
|
-
repo?: ?string,
|
|
148
|
-
): Promise<string> {
|
|
149
|
-
return (await dockerImages(repo, (m) => m.tag === tag)).map((m) => m.id)[0];
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export async function getUntaggedDockerIds(
|
|
153
|
-
repo?: ?string,
|
|
154
|
-
filter?: ?DockerImageFilter = null,
|
|
155
|
-
): Promise<string[]> {
|
|
156
|
-
return (
|
|
157
|
-
await dockerImages(
|
|
158
|
-
repo,
|
|
159
|
-
(m) =>
|
|
160
|
-
m.tag === '<none>' && (typeof filter === 'function' ? filter(m) : true),
|
|
161
|
-
)
|
|
162
|
-
).map((m) => m.id);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export async function getDockerTags(
|
|
166
|
-
id?: ?string,
|
|
167
|
-
repo?: ?string,
|
|
168
|
-
fullName: boolean = false,
|
|
169
|
-
filter?: ?DockerImageFilter = null,
|
|
170
|
-
): Promise<string[]> {
|
|
171
|
-
return (
|
|
172
|
-
await dockerImages(
|
|
173
|
-
repo,
|
|
174
|
-
(m) =>
|
|
175
|
-
(!id || m.id === id) &&
|
|
176
|
-
(typeof filter === 'function' ? filter(m) : true),
|
|
177
|
-
)
|
|
178
|
-
).map((m) => (fullName ? `${m.repository}:${m.tag}` : m.tag));
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export async function getDockerDigest(
|
|
182
|
-
imageId: string,
|
|
183
|
-
repo?: string,
|
|
184
|
-
): Promise<string> {
|
|
185
|
-
return (await dockerImages(repo, (m) => m.id === imageId)).map(
|
|
186
|
-
(m) => m.digest,
|
|
187
|
-
)[0];
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export async function dockerLogin(
|
|
191
|
-
user: string,
|
|
192
|
-
password: string,
|
|
193
|
-
registry: string,
|
|
194
|
-
): Promise<void> {
|
|
195
|
-
const reg = registry || getDockerConfig().registry;
|
|
196
|
-
await spawn(
|
|
197
|
-
'docker',
|
|
198
|
-
[
|
|
199
|
-
'login',
|
|
200
|
-
'-u',
|
|
201
|
-
user || process.env.ARTIFACTORY_USER || '',
|
|
202
|
-
'-p',
|
|
203
|
-
password || process.env.ARTIFACTORY_PASSWORD || '',
|
|
204
|
-
...(reg ? [reg] : []),
|
|
205
|
-
],
|
|
206
|
-
{ stdio: 'inherit' },
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export async function dockerPush(
|
|
211
|
-
tags?: string[] = ['latest'],
|
|
212
|
-
repo?: string = getDockerRepo(),
|
|
213
|
-
) {
|
|
214
|
-
// do this sequentially, so you don't push the same image simultaneously
|
|
215
|
-
await tags.reduce(async (prev, tag) => {
|
|
216
|
-
await prev;
|
|
217
|
-
await spawn('docker', ['push', `${repo}:${tag}`], { stdio: 'inherit' });
|
|
218
|
-
}, Promise.resolve());
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export type DockerPullOptions = {
|
|
222
|
-
image: string,
|
|
223
|
-
offline?: boolean,
|
|
224
|
-
testUrl?: ?string,
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
export async function dockerPull({
|
|
228
|
-
image,
|
|
229
|
-
offline = process.argv.includes('--offline'),
|
|
230
|
-
testUrl,
|
|
231
|
-
}: DockerPullOptions) {
|
|
232
|
-
if (offline || !(await isReachable(testUrl || 'https://hub.docker.com'))) {
|
|
233
|
-
if (offline === false) {
|
|
234
|
-
throw new Error('Offline, cannot docker pull');
|
|
235
|
-
}
|
|
236
|
-
buildLog('It looks like you are offline, skipping docker pull');
|
|
237
|
-
} else {
|
|
238
|
-
// throws on failure
|
|
239
|
-
await spawn('docker', ['pull', image], { stdio: 'inherit' });
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export async function dockerRmi(
|
|
244
|
-
images: string[] = [],
|
|
245
|
-
ignoreErrors: boolean = true,
|
|
246
|
-
): Promise<void> {
|
|
247
|
-
// must do these sequentially, or they will interfere with each other
|
|
248
|
-
await images.reduce(async (prev, image) => {
|
|
249
|
-
await prev;
|
|
250
|
-
await spawn('docker', ['rmi', image], { stdio: 'inherit' }).catch((err) => {
|
|
251
|
-
if (ignoreErrors) {
|
|
252
|
-
buildLog(
|
|
253
|
-
`Warning (ignored Error): Failed to rm image(s): ${err.message}`,
|
|
254
|
-
);
|
|
255
|
-
} else {
|
|
256
|
-
throw err;
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
}, Promise.resolve());
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export function getDockerRepo(): string {
|
|
263
|
-
const { registry, repository: dkrRepo, name: dkrName } = getDockerConfig();
|
|
264
|
-
const repository = dkrRepo || getPkgScope();
|
|
265
|
-
const name = dkrName || getPkgName(false);
|
|
266
|
-
if (!repository || !name) {
|
|
267
|
-
throw new Error(
|
|
268
|
-
'Docker configuration is missing or incomplete in package.json',
|
|
269
|
-
);
|
|
270
|
-
}
|
|
271
|
-
return `${registry ? `${registry}/` : ''}${repository}/${name}`;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export type PullAndRunContainerOptions = {
|
|
275
|
-
...DockerPullOptions,
|
|
276
|
-
runArgs?: string[],
|
|
277
|
-
cmd?: string[],
|
|
278
|
-
network?: ?string,
|
|
279
|
-
alias?: ?string,
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
export async function dockerPullAndRunContainer(
|
|
283
|
-
image: string,
|
|
284
|
-
options: PullAndRunContainerOptions = {},
|
|
285
|
-
): Promise<string> {
|
|
286
|
-
const {
|
|
287
|
-
runArgs = [],
|
|
288
|
-
cmd = [],
|
|
289
|
-
network,
|
|
290
|
-
alias,
|
|
291
|
-
offline = process.argv.includes('--offline'),
|
|
292
|
-
testUrl,
|
|
293
|
-
} = options;
|
|
294
|
-
|
|
295
|
-
try {
|
|
296
|
-
await dockerPull({ image, offline, testUrl });
|
|
297
|
-
} catch (err) {
|
|
298
|
-
buildLog(`docker pull failed (${err.message}), attempting to continue...`);
|
|
299
|
-
}
|
|
300
|
-
const { id } = await dockerContainerRunDaemon({ image, runArgs, cmd });
|
|
301
|
-
if (network) {
|
|
302
|
-
await dockerNetworkConnect(network, id, alias);
|
|
303
|
-
}
|
|
304
|
-
return id;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export type CopyFilePathConfig = {
|
|
308
|
-
from: string,
|
|
309
|
-
to: string,
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
export type CopyFilesFromDockerImageConfig = {
|
|
313
|
-
imageId: string,
|
|
314
|
-
filePaths: CopyFilePathConfig[],
|
|
315
|
-
ignoreErrors?: ?boolean,
|
|
316
|
-
};
|
|
317
|
-
|
|
318
|
-
export async function copyFilesFromDockerImage({
|
|
319
|
-
imageId,
|
|
320
|
-
filePaths,
|
|
321
|
-
ignoreErrors,
|
|
322
|
-
}: CopyFilesFromDockerImageConfig): Promise<any> {
|
|
323
|
-
const container = await dockerContainerRunDaemon({
|
|
324
|
-
image: imageId,
|
|
325
|
-
runArgs: ['-it', '--rm', '--entrypoint=/bin/bash'],
|
|
326
|
-
cmd: ['read', '-p', 'pause'],
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
buildLog(`Copying Files from Docker Image - imageId (${imageId})`);
|
|
330
|
-
|
|
331
|
-
const spawnOptions = {
|
|
332
|
-
stdio: 'inherit',
|
|
333
|
-
pipeOutput: true,
|
|
334
|
-
captureOutput: false,
|
|
335
|
-
};
|
|
336
|
-
const copyErrors = [];
|
|
337
|
-
|
|
338
|
-
try {
|
|
339
|
-
filePaths.forEach(async (fp) => {
|
|
340
|
-
// Try to copy from the Debug Folder
|
|
341
|
-
try {
|
|
342
|
-
await spawn(
|
|
343
|
-
'docker',
|
|
344
|
-
['cp', `${container.id}:${fp.from}`, fp.to],
|
|
345
|
-
spawnOptions,
|
|
346
|
-
);
|
|
347
|
-
buildLog(`Copied ${fp.from} => ${fp.to}`);
|
|
348
|
-
} catch (error) {
|
|
349
|
-
copyErrors.push(error);
|
|
350
|
-
buildLog(`Error copying file ${fp.from}`);
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
} finally {
|
|
354
|
-
try {
|
|
355
|
-
await dockerContainerKill(container.id);
|
|
356
|
-
} catch (error) {
|
|
357
|
-
copyErrors.push(error);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
if (copyErrors.length > 0 && ignoreErrors !== true) {
|
|
362
|
-
throw new Error('Some files failed to copy');
|
|
363
|
-
}
|
|
364
|
-
}
|
package/src/docker.network.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import throttle from 'lodash/throttle';
|
|
3
|
-
import { spawn } from './cp';
|
|
4
|
-
import { parseDockerDate } from './docker';
|
|
5
|
-
|
|
6
|
-
export type DockerNetwork = {|
|
|
7
|
-
id: string,
|
|
8
|
-
name: string,
|
|
9
|
-
driver: string,
|
|
10
|
-
scope: string,
|
|
11
|
-
created: Date,
|
|
12
|
-
IPv6: boolean,
|
|
13
|
-
labels: string[],
|
|
14
|
-
exists: () => Promise<boolean>,
|
|
15
|
-
rm: () => Promise<void>,
|
|
16
|
-
|};
|
|
17
|
-
|
|
18
|
-
type DockerNetworkLsOptions = {
|
|
19
|
-
filter?: (network: DockerNetwork) => boolean,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// calling this function in rapid succession can lead to errors.
|
|
23
|
-
// prefer throttled version
|
|
24
|
-
export async function _dockerNetworkLs({
|
|
25
|
-
filter,
|
|
26
|
-
}: DockerNetworkLsOptions = {}): Promise<DockerNetwork[]> {
|
|
27
|
-
return (
|
|
28
|
-
await spawn('docker', ['network', 'ls', '--format', '{{json .}}'], {
|
|
29
|
-
captureOutput: true,
|
|
30
|
-
})
|
|
31
|
-
)
|
|
32
|
-
.split('\n')
|
|
33
|
-
.filter((line) => !!line.trim())
|
|
34
|
-
.map((line) => {
|
|
35
|
-
const {
|
|
36
|
-
ID: id,
|
|
37
|
-
Name: name,
|
|
38
|
-
Driver: driver,
|
|
39
|
-
Scope: scope,
|
|
40
|
-
CreatedAt,
|
|
41
|
-
IPv6,
|
|
42
|
-
Labels,
|
|
43
|
-
} = JSON.parse(line.trim());
|
|
44
|
-
return {
|
|
45
|
-
id,
|
|
46
|
-
name,
|
|
47
|
-
driver,
|
|
48
|
-
scope,
|
|
49
|
-
created: parseDockerDate(CreatedAt),
|
|
50
|
-
IPv6: IPv6 === 'true',
|
|
51
|
-
labels: Labels.split(',').map((l) => l.trim()),
|
|
52
|
-
exists: async () => !!(await dockerNetworkFind(id)),
|
|
53
|
-
rm: async () => dockerNetworkRm(id),
|
|
54
|
-
};
|
|
55
|
-
})
|
|
56
|
-
.filter((n) => n.id && (typeof filter === 'function' ? filter(n) : true));
|
|
57
|
-
}
|
|
58
|
-
export const dockerNetworkLs: (
|
|
59
|
-
DockerNetworkLsOptions | void,
|
|
60
|
-
) => Promise<DockerNetwork[]> = throttle(_dockerNetworkLs, 500, {
|
|
61
|
-
trailing: false,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
export async function dockerNetworkFind(
|
|
65
|
-
networkName: string,
|
|
66
|
-
options?: DockerNetworkLsOptions,
|
|
67
|
-
): Promise<?DockerNetwork> {
|
|
68
|
-
return (await dockerNetworkLs(options)).find(
|
|
69
|
-
(n) => n.name === networkName || n.id === networkName,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export type DockerNetworkCreateOptions = {|
|
|
74
|
-
driver?: string,
|
|
75
|
-
driverOpts?: { [string]: string },
|
|
76
|
-
ipRange?: string,
|
|
77
|
-
gateways?: string[],
|
|
78
|
-
subnets?: string[],
|
|
79
|
-
auxAddresses?: string[],
|
|
80
|
-
ingress?: boolean,
|
|
81
|
-
internal?: boolean,
|
|
82
|
-
ipv6?: boolean,
|
|
83
|
-
labels?: string[],
|
|
84
|
-
|};
|
|
85
|
-
|
|
86
|
-
export async function dockerNetworkCreate(
|
|
87
|
-
networkName: string,
|
|
88
|
-
{
|
|
89
|
-
driver,
|
|
90
|
-
driverOpts = {},
|
|
91
|
-
ipRange,
|
|
92
|
-
gateways = [],
|
|
93
|
-
subnets = [],
|
|
94
|
-
auxAddresses = [],
|
|
95
|
-
ingress,
|
|
96
|
-
internal,
|
|
97
|
-
ipv6,
|
|
98
|
-
labels = [],
|
|
99
|
-
}: DockerNetworkCreateOptions = {},
|
|
100
|
-
): Promise<?DockerNetwork> {
|
|
101
|
-
const existing = await dockerNetworkFind(networkName);
|
|
102
|
-
if (existing) return existing;
|
|
103
|
-
await spawn('docker', [
|
|
104
|
-
'network',
|
|
105
|
-
'create',
|
|
106
|
-
...labels.flatMap((l) => ['--label', l]),
|
|
107
|
-
...gateways.flatMap((g) => ['--gateway', g]),
|
|
108
|
-
...subnets.flatMap((s) => ['--subnet', s]),
|
|
109
|
-
...auxAddresses.flatMap((a) => ['--aux-address', a]),
|
|
110
|
-
...(ipRange ? ['--ip-range', ipRange] : []),
|
|
111
|
-
...(ingress ? ['--ingress'] : []),
|
|
112
|
-
...(internal ? ['--internal'] : []),
|
|
113
|
-
...(ipv6 ? ['--ipv6'] : []),
|
|
114
|
-
...(driver ? ['-d', driver] : []),
|
|
115
|
-
...Object.entries(driverOpts).flatMap(([k, v]) => [
|
|
116
|
-
'-o',
|
|
117
|
-
`${k}=${(v: any)}`,
|
|
118
|
-
]),
|
|
119
|
-
networkName,
|
|
120
|
-
]);
|
|
121
|
-
return dockerNetworkFind(networkName);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Removes a network by id
|
|
126
|
-
* @param {string} id The id of the network
|
|
127
|
-
*/
|
|
128
|
-
export async function dockerNetworkRm(id: string) {
|
|
129
|
-
await spawn('docker', ['network', 'rm', id]);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Removes all networks with the given name
|
|
134
|
-
* @param {string} networkName The name of the network
|
|
135
|
-
*/
|
|
136
|
-
export async function dockerNetworkDelete(networkName: string) {
|
|
137
|
-
let existing;
|
|
138
|
-
// eslint-disable-next-line no-cond-assign, no-await-in-loop
|
|
139
|
-
while ((existing = await dockerNetworkFind(networkName))) {
|
|
140
|
-
// eslint-disable-next-line no-await-in-loop
|
|
141
|
-
await existing.rm();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export async function dockerNetworkConnect(
|
|
146
|
-
networkName: string,
|
|
147
|
-
containerId: string,
|
|
148
|
-
alias?: ?string,
|
|
149
|
-
) {
|
|
150
|
-
const net = await dockerNetworkCreate(networkName);
|
|
151
|
-
if (!net) throw new Error('Failed to create docker network');
|
|
152
|
-
await spawn('docker', [
|
|
153
|
-
'network',
|
|
154
|
-
'connect',
|
|
155
|
-
...(alias ? ['--alias', alias] : []),
|
|
156
|
-
net.id,
|
|
157
|
-
containerId,
|
|
158
|
-
]);
|
|
159
|
-
}
|
package/src/docker.volume.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import throttle from 'lodash/throttle';
|
|
3
|
-
import { spawn } from './cp';
|
|
4
|
-
|
|
5
|
-
type DockerVolumeInspectOutput = {
|
|
6
|
-
[string]: any,
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type DockerVolume = {|
|
|
10
|
-
name: string,
|
|
11
|
-
driver: string,
|
|
12
|
-
scope: string,
|
|
13
|
-
labels: string[],
|
|
14
|
-
mountPoint: string,
|
|
15
|
-
inspect: () => Promise<DockerVolumeInspectOutput>,
|
|
16
|
-
rm: () => Promise<void>,
|
|
17
|
-
|};
|
|
18
|
-
|
|
19
|
-
type DockerVolumeLsOptions = {
|
|
20
|
-
filter?: (vol: DockerVolume) => boolean,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
// calling this function in rapid succession can lead to errors.
|
|
24
|
-
// prefer throttled version
|
|
25
|
-
export async function _dockerVolumeLs({
|
|
26
|
-
filter,
|
|
27
|
-
}: DockerVolumeLsOptions = {}): Promise<DockerVolume[]> {
|
|
28
|
-
return (
|
|
29
|
-
await spawn('docker', ['volume', 'ls', '--format', '{{json .}}'], {
|
|
30
|
-
captureOutput: true,
|
|
31
|
-
})
|
|
32
|
-
)
|
|
33
|
-
.split('\n')
|
|
34
|
-
.filter((line) => !!line.trim())
|
|
35
|
-
.map((line) => {
|
|
36
|
-
const {
|
|
37
|
-
Name: name,
|
|
38
|
-
Driver: driver,
|
|
39
|
-
Scope: scope,
|
|
40
|
-
Labels,
|
|
41
|
-
Mountpoint: mountPoint,
|
|
42
|
-
} = JSON.parse(line.trim());
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
name,
|
|
46
|
-
driver,
|
|
47
|
-
scope,
|
|
48
|
-
labels: Labels.split(',').map((l) => l.trim()),
|
|
49
|
-
mountPoint,
|
|
50
|
-
inspect: async () => (await dockerVolumeInspect(name))[0],
|
|
51
|
-
rm: async () => dockerVolumeRm(name),
|
|
52
|
-
};
|
|
53
|
-
})
|
|
54
|
-
.filter((v) => v.name && (typeof filter === 'function' ? filter(v) : true));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export const dockerVolumeLs: (
|
|
58
|
-
DockerVolumeLsOptions | void,
|
|
59
|
-
) => Promise<DockerVolume[]> = throttle(_dockerVolumeLs, 500, {
|
|
60
|
-
trailing: false,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
export async function dockerVolumeFind(
|
|
64
|
-
search: string,
|
|
65
|
-
options?: DockerVolumeLsOptions,
|
|
66
|
-
): Promise<?DockerVolume> {
|
|
67
|
-
return (await dockerVolumeLs(options)).find(
|
|
68
|
-
(v) => v.name === search || v.name.startsWith(search),
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export type DockerVolumeCreateOptions = {|
|
|
73
|
-
labels?: string[],
|
|
74
|
-
driver?: string,
|
|
75
|
-
driverOpts?: { [string]: string },
|
|
76
|
-
|};
|
|
77
|
-
export async function dockerVolumeCreate(
|
|
78
|
-
name: string,
|
|
79
|
-
{ labels = [], driver, driverOpts = {} }: DockerVolumeCreateOptions = {},
|
|
80
|
-
): Promise<?DockerVolume> {
|
|
81
|
-
const existing = await dockerVolumeFind(name);
|
|
82
|
-
if (existing) return existing;
|
|
83
|
-
await spawn('docker', [
|
|
84
|
-
'volume',
|
|
85
|
-
'create',
|
|
86
|
-
...labels.flatMap((l) => ['--label', l]),
|
|
87
|
-
...(driver ? ['-d', driver] : []),
|
|
88
|
-
...Object.entries(driverOpts).flatMap(([k, v]) => [
|
|
89
|
-
'-o',
|
|
90
|
-
`${k}=${(v: any)}`,
|
|
91
|
-
]),
|
|
92
|
-
name,
|
|
93
|
-
]);
|
|
94
|
-
return dockerVolumeFind(name);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export async function dockerVolumeRm(
|
|
98
|
-
id: string | string[],
|
|
99
|
-
ignoreErrors: boolean = true,
|
|
100
|
-
) {
|
|
101
|
-
const ids = Array.isArray(id) ? id : [id];
|
|
102
|
-
try {
|
|
103
|
-
await spawn('docker', ['volume', 'rm', ...ids]);
|
|
104
|
-
} catch (e) {
|
|
105
|
-
if (!ignoreErrors) {
|
|
106
|
-
throw new Error(`Failed to remove volume(s): ${e.message}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// calling this function in rapid succession can lead to errors.
|
|
112
|
-
// prefer throttled version
|
|
113
|
-
export async function _dockerVolumeInspect(
|
|
114
|
-
id: string | string[],
|
|
115
|
-
): Promise<DockerVolumeInspectOutput[]> {
|
|
116
|
-
const ids = Array.isArray(id) ? id : [id];
|
|
117
|
-
return (
|
|
118
|
-
JSON.parse(
|
|
119
|
-
(
|
|
120
|
-
await spawn(
|
|
121
|
-
'docker',
|
|
122
|
-
['volume', 'inspect', '--format', '{{json .}}', ...ids],
|
|
123
|
-
{ captureOutput: true },
|
|
124
|
-
)
|
|
125
|
-
).trim(),
|
|
126
|
-
) || []
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export const dockerVolumeInspect: (
|
|
131
|
-
string | string[],
|
|
132
|
-
) => Promise<DockerVolumeInspectOutput[]> = throttle(
|
|
133
|
-
_dockerVolumeInspect,
|
|
134
|
-
500,
|
|
135
|
-
{ trailing: true },
|
|
136
|
-
);
|