forked from vweevers/node-docker-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
267 lines (220 loc) · 6.57 KB
/
index.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict';
const path = require('path')
, fs = require('fs')
, env = process.env
, cp = require('child_process')
, camelCase = require('camel-case')
, parallel = require('run-parallel-limit')
, xtend = require('xtend')
, deprecate = require('deprecate')
const HOST_NON_EXISTENT = /host does not exist/i
, ALREADY_RUNNING = /already running/i
, ALREADY_STOPPED = /already stopped/
, NEWLINE = /\r?\n/
, LIST_COLUMNS_SEP = ','
const LIST_COLUMNS
= [ 'Name'
, 'Active'
, 'ActiveHost'
, 'ActiveSwarm'
, 'DriverName'
, 'State'
, 'URL'
, 'Swarm'
, 'Error'
, 'DockerVersion'
, 'ResponseTime' ]
class Machine {
constructor(opts) {
opts = Machine.options(opts)
this.name = opts.name || env.DOCKER_MACHINE_NAME || 'default'
}
static options(opts) {
if (typeof opts === 'string') return { name: opts }
else return opts || {}
}
static command(args, done) {
return cp.execFile('docker-machine', [].concat(args), {
cwd: env.DOCKER_TOOLBOX_INSTALL_PATH || '.',
encoding: 'utf8'
}, done)
}
static status(name, done) {
Machine.command(['status', name], (err, stdout) => {
if (err) done(err)
else done(null, stdout.trim().toLowerCase())
})
}
static isRunning(name, done) {
Machine.status(name, (err, status) => {
done(err, status === 'running')
})
}
static create(machineName, options, done) {
if (typeof options === 'function') {
done = options
// Using "virtualbox" as the default driver
options = {
driver: 'virtualbox'
}
}
var args = [];
args.push('create');
for (var key in options) {
if (options.hasOwnProperty(key)) {
args.push(`--${key}`, options[key]);
}
}
args.push(machineName);
var process = Machine.command(args, (err) => {
if (err) done(err)
else done();
})
process.stdout.on('data', function(data) {
console.log(data);
});
process.stderr.on('data', function(data) {
done("Machine creation failed. " + JSON.stringify(data));
});
}
static start(name, done) {
Machine.command(['start', name], (err) => {
if (HOST_NON_EXISTENT.test(err)) {
done(new Error(`Docker host "${name}" does not exist`))
} else if (ALREADY_RUNNING.test(err)) {
done()
} else {
done(err)
}
})
}
static stop(name, done) {
Machine.command(['stop', name], (err) => {
if (HOST_NON_EXISTENT.test(err)) {
done(new Error(`Docker host "${name}" does not exist`))
} else if (ALREADY_STOPPED.test(err)) {
done()
} else {
done(err)
}
})
}
static kill(name, done) {
Machine.command(['kill', name], (err) => {
if (HOST_NON_EXISTENT.test(err)) {
done(new Error(`Docker host "${name}" does not exist`))
} else if (ALREADY_STOPPED.test(err)) {
done()
} else {
done(err)
}
})
}
static env(name, opts, done) {
if (typeof opts === 'function') done = opts, opts = {}
const args = ['env']
if (opts.json) {
deprecate(
'The "json" option has been renamed to "parse" and',
'will be removed in node-docker-machine v3.x.x.'
)
opts = xtend(opts, { parse: true })
}
if (opts.parse) args.push('--shell', 'bash')
else if (opts.shell) args.push('--shell', opts.shell)
args.push(name)
Machine.command(args, function (err, stdout) {
if (err) return done(err)
if (!opts.parse) return done(null, stdout.trim())
const res = {}
stdout.split(/\n+/).forEach(line => {
const m = /^export (.+)="([^"]+)/i.exec(line)
if (m) res[m[1]] = m[2]
})
done(null, res)
})
}
static ssh(name, cmd, done) {
if (Array.isArray(cmd)) {
cmd = cmd.join(' ')
} else if (typeof cmd !== 'string') {
throw new TypeError('Command must be an array or string')
}
cmd = cmd.trim()
if (!cmd) throw new TypeError('Command may not be empty')
Machine.command(['ssh', name, cmd], done)
}
static inspect(name, done) {
Machine.command(['inspect', name], (err, stdout) => {
if (err) return done(err)
try {
var data = JSON.parse(stdout.trim())
} catch (err) {
return done(err)
}
done(null, merge({}, data))
})
}
static list(opts, done) {
if (typeof opts === 'function') done = opts, opts = {}
// Build template, escape values with URL encoding
const template = LIST_COLUMNS.map(name => {
if (name === 'ResponseTime') {
return `{{ .${name} | printf "%d" }}`
} else {
return `{{ .${name} | urlquery }}`
}
}).join(LIST_COLUMNS_SEP)
const args = ['ls', '-f', template]
// Optionally add a timeout (in seconds)
// to deal with docker/machine#1696.
if (opts.timeout) args.push('-t', String(opts.timeout))
Machine.command(args, (err, stdout) => {
if (err) return done(err)
const machines = stdout.split(NEWLINE).filter(Boolean).map(line => {
const values = line.split(LIST_COLUMNS_SEP)
, machine = {}
LIST_COLUMNS.forEach((name, i) => {
const key = camelCase(name)
const val = values[i]
machine[key] = val === '' ? null : decodeURIComponent(val)
})
// ResponseTime is in nanoseconds
machine.responseTime = parseInt(machine.responseTime) / 1e6
machine.state = machine.state.toLowerCase()
machine.activeHost = machine.activeHost === 'true'
machine.activeSwarm = machine.activeSwarm === 'true'
if (machine.dockerVersion === 'Unknown') {
machine.dockerVersion = null
}
return machine
})
if (!opts.inspect) return done(null, machines)
// Add additional metadata from `docker-machine inspect <name>`
parallel(machines.map(machine => next => {
Machine.inspect(machine.name, (err, data) => {
if (err) next(err)
else next(null, xtend(machine, data))
})
}), 4, done)
})
}
}
;['status', 'isRunning', 'start', 'stop', 'kill', 'env', 'ssh', 'inspect'].forEach(method => {
Machine.prototype[method] = function () {
const args = Array.from(arguments)
args.unshift(this.name)
Machine[method].apply(null, args)
}
})
module.exports = Machine
function merge(node, data) {
for(let key in data) {
const val = data[key]
node[camelCase(key)] = isObject(val) ? merge({}, val) : val
}
return node
}
function isObject(obj) {
return typeof obj === 'object' && obj !== null
}