-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (134 loc) · 3.96 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
var fs = require('fs')
, os = require('os')
, path = require('path')
, sh = require('child_process').exec
, spawn = require('child_process').spawn
// haveImage() - Does an image exist in Docker
//
// *imageName* - Name of image to look for
// *cb* - function callback function(err) {}
//
var haveImage = function(imageName, cb) {
sh('docker images | cut -d " " -f 1', function(err, res){
if (err) {
return cb(new Error("error listing docker images: " + err), null)
}
if (res.indexOf(imageName) == -1){
return cb(new Error("image not found: " + imageName), null)
}
cb(null, null)
})
}
// commit() - Commit an image to docker
//
// *opts* is an object with params:
// *imageName* - name of image to commit as
// *tag* - tag to commit as (default: latest)
// *containerId* - container id to commit
// *cmd* - Cmd to commit
// *ports* - Ports to expose
// *message* - Commit message
// *author* - Commit author
//
// *cb* is a function callback function(err) {}
//
var commit = function(opts, cb) {
if (!opts) {
return cb(new Error("must specify options object"))
}
if (!opts.imageName) {
return cb(new Error("must specify imageName"))
}
if (!opts.containerId) {
return cb(new Error("must specify containerId"))
}
var tag = opts.tag || "latest"
var run = []
var cmd = "docker commit "
if (typeof opts.cmd === 'string') {
run = ["/bin/bash", "-c"].concat(opts.cmd.split(' '))
}
if (Array.isArray(opts.cmd)) {
run = opts.cmd
}
var ports = opts.ports || []
var message = opts.message || ""
var author = opts.author || "StriderCD Provisioning Service <[email protected]>"
if (run || ports) {
var runStr = JSON.stringify({Cmd: run, PortSpecs: ports})
cmd += "-run='" + runStr + "' "
}
if (message) {
cmd += "-m='" + message + "' "
}
if (author) {
cmd += "-author='" + author + "' "
}
cmd += opts.containerId + " " + opts.imageName + " " + tag
console.log("DEBUG: running cmd ", cmd)
sh(cmd, cb)
}
// kill() - Kill a running docker container.
//
// *containerId* - ID of container to kill
// *cb* - function callback function(err) {}
//
var kill = function(containerId, cb) {
sh("docker kill " + containerId, function(err, stdout, stderr) {
cb(err, stdout + stderr)
})
}
// wait() - Wait for a docker container to stop then return exit code.
//
// *containerId* - ID of container to wait for
// *cb* - function callback function(err) {}
//
var wait = function(containerId, cb) {
sh("docker wait " + containerId, function(err, stdout) {
cb(err, stdout.replace('\n', ''))
})
}
// runInContainer() - Run a command in a container.
//
// *imageName* - Name of image
// *cmd* - Command to stream from, running inside container
// *piping* - If you are piping into stdin, you must set to true (optional)
// *cb* - Called when sub-process completes. Function callback function(exitCode, containerId, stdout, stderr) {}
//
// returns a ChildProcess object.
//
var runInContainer = function(imageName, cmd, pipingToStdin, cb) {
var stdout = ""
var stderr = ""
var cidFile = path.join("/tmp", "provisioning_" + process.pid.toString() + ".cid")
var proc = spawn("docker", ["run", "-cidfile", cidFile, "-i", "-a", "stdin", "-a", "stdout", "-a", "stderr",
imageName, '/bin/bash', '-c', cmd ])
if (arguments.length === 3) {
cb = pipingToStdin
pipingToStdin = false
}
proc.stdout.setEncoding('utf8')
proc.stderr.setEncoding('utf8')
proc.stdout.on('data', function(data) {
stdout += data
})
proc.stderr.on('data', function(data) {
stderr += data
})
proc.on('close', function(code) {
var cid = fs.readFileSync(cidFile, 'utf8')
cid = cid.replace('\n', '')
fs.unlink(cidFile, function() {
cb(code, cid, stdout, stderr)
})
})
if (!pipingToStdin) proc.stdin.end()
return proc
}
module.exports = {
haveImage: haveImage,
commit: commit,
kill: kill,
runInContainer: runInContainer,
wait: wait
}