-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
daemon.js
228 lines (216 loc) · 7.59 KB
/
daemon.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
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
'use strict';
// Modules
const _ = require('lodash');
const Cache = require('./cache');
const env = require('./env');
const Events = require('./events');
const fs = require('fs');
const Log = require('./logger');
const path = require('path');
const Promise = require('./promise');
const Shell = require('./shell');
const shell = new Shell();
// Constants
const macOSBase = '/Applications/Docker.app';
/*
* Get services wrapper
*/
const buildDockerCmd = cmd => {
switch (process.platform) {
case 'darwin':
return ['open', macOSBase];
case 'linux':
return ['sudo', 'service', 'docker'].concat(cmd);
case 'win32':
const base = process.env.ProgramW6432 || process.env.ProgramFiles;
const dockerBin = base + '\\Docker\\Docker\\Docker Desktop.exe';
return ['cmd', '/C', `"${dockerBin}"`];
}
};
/*
* Helper to build mac docker version get command
*/
const getMacProp = prop => shell.sh(['defaults', 'read', `${macOSBase}/Contents/Info.plist`, prop])
.then(data => _.trim(data))
.catch(() => null);
/*
* Creates a new Daemon instance.
*/
module.exports = class LandoDaemon {
constructor(
cache = new Cache(),
events = new Events(),
docker = env.getDockerExecutable(),
log = new Log(),
context = 'node',
compose = env.getComposeExecutable()
) {
this.cache = cache;
this.compose = compose;
this.context = context;
this.docker = docker;
this.events = events;
this.log = log;
};
/*
* Tries to active the docker engine/daemon.
*
* @since 3.0.0
* @fires pre_engine_up
* @fires post_engine_up
* @return {Promise} A Promise.
*/
up() {
/*
* Not officially documented event that allows you to do some things before
* the docker engine is booted up.
*
* @since 3.0.0
* @event pre_engine_up
*/
return this.events.emit('pre-engine-up').then(() => {
// Automatically return true if we are in the GUI and on linux because
// this requires SUDO and because the daemon should always be running on nix
if (this.context !== 'node' && process.platform === 'linux') return Promise.resolve(true);
// Turn it on if we can
return this.isUp().then(isUp => {
if (!isUp) {
const retryOpts = {max: 25, backoff: 1000};
return shell.sh(buildDockerCmd('start'))
.catch(err => {
throw Error('Could not automatically start the Docker Daemon. Please manually start it to continue.');
})
// Likely need to retry until start command completes all good
.retry(() => this.isUp().then(isUp => (!isUp) ? Promise.reject() : Promise.resolve()), retryOpts)
// Fail if retry is no good
.catch(err => {
throw Error('Could not automatically start the Docker Daemon. Please manually start it to continue.');
})
// Engine is good!
.then(() => this.log.info('engine activated.'));
}
});
})
/*
* Not officially documented event that allows you to do some things after
* the docker engine is booted up.
*
* @since 3.0.0
* @event post_engine_up
*/
.then(() => this.events.emit('post-engine-up'));
};
down() {
/*
* Event that allows you to do some things after the docker engine is booted
* up.
*
* @since 3.0.0
* @event pre_engine_down
*/
return this.events.emit('pre-engine-down')
.then(() => {
// Automatically return true if we are in browsery context and on linux because
// this requires SUDO and because the daemon should always be running on nix
if (this.context !== 'node' && process.platform === 'linux') return Promise.resolve(true);
// Automatically return if we are on Windows or Darwin because we don't
// ever want to automatically turn the VM off since users might be using
// D4M/W for other things.
//
// For now we will be shutting down any services via relevant event hooks
// that bind to critical/common ports on 127.0.0.1/localhost e.g. 80/443/53
//
// @todo: When/if we can run our own isolated docker daemon we can change
// this back.
if (process.platform === 'win32' || process.platform === 'darwin') return Promise.resolve(true);
// Shut provider down if its status is running.
return this.isUp(this.log, this.cache, this.docker).then(isUp => {
if (isUp) return shell.sh(buildDockerCmd('stop'), {mode: 'collect'});
})
// Wrap errors.
.catch(err => {
throw new Error(err, 'Error while shutting down.');
});
})
/*
* Event that allows you to do some things after the docker engine is booted
* up.
*
* @since 3.0.0
* @event post_engine_down
*/
.then(() => this.events.emit('post-engine-down'));
}
/*
* Helper to determine up and down
* NOTE: we now assume that docker has been installed by this point
* this means we also assume whatever neccessary installation checks have been
* performed and dockers existence verified
*/
isUp(log = this.log, cache = this.cache, docker = this.docker) {
// Auto return if cached and true
if (cache.get('engineup') === true) return Promise.resolve(true);
// Return true if we get a zero response and cache the result
return shell.sh([`"${docker}"`, 'info']).then(() => {
log.debug('engine is up.');
cache.set('engineup', true, {ttl: 5});
return Promise.resolve(true);
})
.catch(error => {
log.debug('engine is down with error', error);
return Promise.resolve(false);
});
};
/*
* Helper to get the versions of the things we need
*/
getVersions() {
switch (process.platform) {
case 'darwin':
return Promise.all([
getMacProp('EngineVersion'),
getMacProp('ComposeVersion'),
getMacProp('CFBundleShortVersionString'),
])
.then(data => ({
compose: data[1],
engine: data[0],
desktop: data[2],
}));
case 'linux':
return Promise.all([
shell.sh([`"${this.docker}"`, 'version', '--format', '{{.Server.Version}}']).catch(() => '18.0.0'),
shell.sh([`"${this.compose}"`, 'version', '--short']).catch(() => '11.0.0'),
])
.then(data => ({
compose: _.trim(data[1]),
engine: _.trim(data[0]),
desktop: false,
}));
case 'win32':
const versions = {
compose: '1.24.1',
engine: '19.03.1',
desktop: '2.1.0.1',
};
// Possible locations of the component config
const componentsVersionFiles = [
path.resolve(this.compose, '..', '..', 'componentsVersion.json'),
path.resolve(this.compose, '..', '..', '..', 'componentsVersion.json'),
];
// Use the first one we find
const componentsVersionFile = _.find(componentsVersionFiles, fs.existsSync);
// If we found one, use it but allow for a fallback in case these keys change
if (componentsVersionFile) {
const componentsVersion = require(componentsVersionFile);
versions.compose = _.get(componentsVersion, 'ComposeVersion', versions.compose);
versions.engine = _.get(componentsVersion, 'EngineVersion', versions.engine);
// There are two different keys that map to the docker desktip version depending
// on the version
versions.desktop = _.get(componentsVersion, 'Informational', versions.desktop);
versions.desktop = _.get(componentsVersion, 'Version', versions.desktop);
}
return Promise.resolve(versions);
}
};
};