/
bin.js
executable file
·265 lines (222 loc) · 6 KB
/
bin.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
#!/usr/bin/env node
var tab = require('tabalot')
var relative = require('relative-date')
var pretty = require('prettysize')
var table = require('text-table')
var tree = require('pretty-tree')
var dateable = require('dateable')
var tar = require('tar-fs')
var fs = require('fs')
var path = require('path')
var ignore = require('ignore-file')
var w
var whale = function() {
if (w) return w
var conf = path.join(process.env.HOME || process.env.USERPROFILE, '.dockercfg')
var auth = fs.existsSync(conf) && JSON.parse(fs.readFileSync(conf))['https://index.docker.io/v1/']
if (auth) {
var parts = new Buffer(auth.auth, 'base64').toString().split(':')
auth.username = parts[0]
auth.password = parts[1]
}
return w = require('./')(tab.argv.host, auth)
}
var help = function(name) {
console.log(fs.readFileSync(path.join(__dirname, 'docs', name+'.txt'), 'utf-8'))
process.exit(0)
}
var onerror = function(err) {
if (!err) return
console.error(err.message || err)
process.exit(1)
}
var toName = function(c) {
return c.name
}
var names = function(cb) {
whale().ps(function(err, list) {
if (err) return cb(err)
cb(null, list.map(toName))
})
}
var images = function(cb) {
whale().images(function(err, list) {
if (err) return cb(err)
cb(null, list.map(toName))
})
}
var attach = function(name, all, kill) {
var log = whale().log(name, {all: all})
log.on('error', onerror)
log.stdout.pipe(process.stdout)
log.stderr.pipe(process.stderr)
if (!kill) return
var stop = function() {
whale().stop(name, function() {
process.exit(0)
})
}
log.on('end', stop)
process.on('SIGTERM', stop)
process.on('SIGINT', stop)
}
tab('*')
('--host', '-H', '@host')
('--help', '-h', '-?')
tab('clean')
(function(opts) {
if (opts.help) return help('clean')
whale().clean(onerror)
})
tab('tag')
('--rename', '-r')
(images)
(images)
(function(image, repo, opts) {
if (!image || !repo || opts.help) return help('tag')
whale().tag(image, repo, opts, onerror)
})
tab('pull')
(images)
(function(image, opts) {
if (!image || opts.help) return help('pull')
whale().pull(image, opts).on('error', onerror).pipe(process.stdout)
})
tab('push')
(images)
(images)
(function(image, tag, opts) {
if (!image || opts.help) return help('push')
whale().push(image, tag, opts).on('error', onerror).pipe(process.stdout)
})
tab('build')
('--no-cache')
('--no-ignore')
(images)
(function(image, opts) {
if (!image || opts.help) return help('build')
var filter = opts.ignore !== false && (ignore.sync('.dockerignore') || ignore.sync('.gitignore'))
tar.pack('.', {ignore:filter}).pipe(whale().build(image, opts)).on('error', onerror).pipe(process.stdout)
})
tab('ps')
(function(opts) {
if (opts.help) return help('ps')
whale().ps(function(err, list) {
if (err) return onerror(err)
list = list.map(function(c) {
return [
c.name,
c.id.slice(0, 12),
c.image,
c.command,
relative(c.created)
]
})
list.unshift(['NAME', 'ID', 'IMAGE', 'COMMAND', 'CREATED'])
console.log(table(list, {hsep: ' '}))
})
})
tab('events')
(function(opts) {
if (opts.help) return help('events')
whale().events({name:true}).on('data', function(data) {
var name = data.name || data.id
if (name !== data.image) name += ' (from '+data.image+')'
console.log(dateable(data.time, 'YYYY-MM-DD hh:mm:ss')+' - '+name+' '+': '+data.status)
})
})
tab('images')
(function(opts) {
if (opts.help) return help('images')
whale().images(function(err, list) {
if (err) return onerror(err)
list = list.map(function(i) {
return [
i.name,
i.id.slice(0, 12),
i.parent.slice(0, 12),
relative(i.created),
pretty(i.virtualSize)
]
})
list.unshift(['NAME', 'ID', 'PARENT ID', 'CREATED', 'VIRTUAL SIZE'])
console.log(table(list, {hsep: ' '}))
})
})
tab('remove')
(images)
(function(image, opts) {
if (!image || opts.help) return help('remove')
whale().remove(image, onerror)
})
tab('log')
('--all', '-a')
(names)
(function(name, opts) {
if (!name || opts.help) return help('log')
attach(name, opts.all, false)
})
tab('inspect')
(names)
(function(name, opts) {
if (!name || opts.help) return help('inspect')
whale().inspect(name, function(err, info) {
if (err) return onerror(err)
var name = info.name
delete info.name
info.created = relative(info.created)
info.id = info.id.slice(0, 12)
console.log(tree.plain({
label: name,
leaf: info
}))
})
})
tab('stop')
('--force')
(names)
(function(name, opts) {
if (!name || opts.help) return help('stop')
whale().stop(name, onerror)
})
tab('start')
('--volume', '-v', '@dir')
('--fork', '-f')
('--force')
('--dns', '-d')
('--env', '-e')
('--port', '-p')
('--network', '-n', ['host', 'bridge', 'none'])
(images)
(images)
(function(image, name, opts) {
if (!image || opts.help) return help('start')
if (!name) name = image
var log = opts.log || !opts.detach
var argv = opts['--'] || []
var ports = [].concat(opts.port || []).reduce(function(result, p) {
p = p.toString().split(':')
result[p[0]] = p[1] || p[0]
return result
}, {})
var env = [].concat(opts.env || []).reduce(function(result, e) {
e = e.trim().match(/^([^=]+)=(.*)$/)
if (e) result[e[1]] = e[2]
return result
}, {})
var volumes = [].concat(opts.volume || []).reduce(function(result, v) {
v = v.split(':')
result[v[0]] = v[1] || v[0]
return result
}, {})
opts.argv = argv
opts.image = image
opts.env = env
opts.volumes = volumes
opts.ports = ports
whale().start(name, opts, function(err) {
if (err) return onerror(err)
if (!opts.fork) attach(name, true, true)
})
})
tab.parse({'--':true}) || help('usage')