forked from Strider-CD/strider-docker-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-container.js
More file actions
137 lines (119 loc) · 3.88 KB
/
create-container.js
File metadata and controls
137 lines (119 loc) · 3.88 KB
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
var stream = require('stream');
var EventEmitter = require('events').EventEmitter;
var es = require('event-stream')
var debug = require('debug')('strider-docker-runner:create-container')
var async = require('async');
var demuxer = require('./demuxer')
var inspect = require('util').inspect;
function isImageLocally(docker, image, done) {
var withoutTag = image.split(':')[0];
var fullname = image === withoutTag ?
image + ':latest' :
image;
docker.listImages({ filter: withoutTag }, function (err, images) {
if (err) return done(err);
var found = images.some(function (img) {
return ~img.RepoTags.indexOf(fullname);
});
done(null, found);
});
}
function pull (docker, image, done) {
docker.pull(image, function (err, streamc) {
if (err) return done(err)
streamc
.pipe(es.map(function (data, cb) {
cb(null, JSON.parse(data))
}))
.on('data', function (event) {
debug('pull event: ' + inspect(event));
})
.on('end', function () {
done();
});
});
}
function create (createOptions, docker, config, done) {
docker.createContainer(createOptions, function (err, container) {
if (err) return done(new Error(err));
debug('[runner:docker] container id', container.id)
container.attach({
stream: true, stdin: true,
stdout: true, stderr: true
}, attached);
function attached (err, streamc) {
if (err) return done(err)
if (!streamc) return done(new Error("Failed to attach container stream"))
// start, and wait for it to be done
container.start({
Privileged: config.privileged,
PublishAllPorts: config.publishPorts,
Dns: config.dns,
}, function(err, data) {
if(err) return done(new Error(err))
container.wait(function(err, data) {
debug('done with the container', err, data)
container.stop(function (err, _) {
debug('Stopped the container!')
})
})
done(err, spawn.bind(null, streamc), kill)
})
}
function kill (done) {
container.remove({
force: true, // Stop container and remove
v: true // Remove any attached volumes
}, done)
}
function spawn (streamc, command, args, options, done) {
var proc = new EventEmitter();
proc.kill = function () {
streamc.write(JSON.stringify({type: 'kill'})+'\n')
}
proc.stdout = new stream.PassThrough();
proc.stderr = new stream.PassThrough();
proc.stdin = streamc;
var stdout = new stream.PassThrough()
var stderr = new stream.PassThrough()
done(null, proc)
var demux = demuxer(streamc, stdout, stderr)
stdout
.pipe(es.split())
.pipe(es.parse())
.pipe(es.mapSync(function (data) {
debug('got an event', data)
if (data.event === 'stdout') {
proc.stdout.write(data.data)
}
if (data.event === 'stderr') {
proc.stderr.write(data.data)
}
if (data.event === 'exit') {
proc.emit('exit', data.code)
streamc.removeListener('readable', demux)
stdout.unpipe()
}
}))
debug("running command", command);
debug("with args", args);
streamc.write(JSON.stringify({command: command, args: args, type: 'start'}) + '\n');
}
})
}
module.exports = function (createOptions, docker, config, done) {
async.waterfall([
function (callback) {
isImageLocally(docker, createOptions.Image, callback);
}, function (isLocally, callback) {
if (isLocally) {
debug('image is already locally')
return callback();
}
debug('Unable to find image "%s" locally', createOptions.Image);
pull(docker, createOptions.Image, callback);
}, function () {
create(createOptions, docker, config, done)
}
]);
}