-
Notifications
You must be signed in to change notification settings - Fork 31
/
compose.js
92 lines (81 loc) · 2.72 KB
/
compose.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
const yaml = require('js-yaml');
const fs = require('fs');
const stream = require('stream');
const secrets = require('./lib/secrets');
const volumes = require('./lib/volumes');
const configs = require('./lib/configs');
const networks = require('./lib/networks');
const services = require('./lib/services');
const tools = require('./lib/tools');
class Compose {
constructor(dockerode, file, projectName) {
this.docker = dockerode;
if (file === undefined || projectName === undefined) {
throw new Error('please specify a file and a project name');
}
this.file = file;
this.projectName = projectName;
try {
this.recipe = yaml.load(fs.readFileSync(file, 'utf8'));
} catch (e) {
throw e;
}
}
async down(options) {
var output = {};
try {
output.file = this.file;
output.services = await services.down(this.docker, this.projectName, this.recipe, output, options);
output.networks = await networks.down(this.docker, this.projectName, this.recipe, output);
if (options !== undefined) {
if (options.volumes) {
output.volumes = await volumes.down(this.docker, this.projectName, this.recipe, output);
}
}
return output;
} catch (e) {
throw e;
}
}
async up(options) {
var output = {};
try {
output.file = this.file;
output.secrets = await secrets(this.docker, this.projectName, this.recipe, output);
output.volumes = await volumes.up(this.docker, this.projectName, this.recipe, output);
output.configs = await configs(this.docker, this.projectName, this.recipe, output);
output.networks = await networks.up(this.docker, this.projectName, this.recipe, output);
output.services = await services.up(this.docker, this.projectName, this.recipe, output, options);
return output;
} catch (e) {
throw e;
}
}
async pull(serviceN, options) {
options = options || {};
var streams = [];
var serviceNames = (serviceN === undefined || serviceN === null) ? tools.sortServices(this.recipe) : [serviceN];
for (var serviceName of serviceNames) {
var service = this.recipe.services[serviceName];
try {
var streami = await this.docker.pull(service.image);
streams.push(streami);
if (options.verbose === true) {
streami.pipe(process.stdout);
}
if (options.streams !== true) {
if (options.verbose === true) {
streami.pipe(process.stdout);
} else {
streami.pipe(stream.PassThrough());
}
await new Promise(fulfill => streami.once('end', fulfill));
}
} catch (e) {
throw e;
}
}
return streams;
}
}
module.exports = Compose;