Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions lib/create-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,45 @@ 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;

module.exports = function (createOptions, docker, config, done) {
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that ~ is too clever :P i had to google this. learn something new everyday...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I personally really don't like it because it obscures the logic....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make it clearer next time we're working in the file, i think it's fine for now -- anyone seriously looking at this code is gonna google and learn

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree with @jaredly, but at the same time my brain reads ~....indexOf(x) as contains. Because I've read a lot of javascript and node modules and it is a very common trick.

I hope this succeed domenic/Array.prototype.contains some day.

});

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)
Expand Down Expand Up @@ -83,3 +118,20 @@ module.exports = function (createOptions, docker, config, done) {
}
})
}

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)
}
]);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"author": "Jared Forsyth <[email protected]>",
"license": "MIT",
"dependencies": {
"async": "~0.9.0",
"debug": "^1.0.4",
"dockerode": "^2.0.1",
"dockerode-optionator": "^1.0.0",
Expand Down
9 changes: 4 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ function run(spawn, command, args, done) {
})
}

init(null, null, function (err, docker) {
createSlave(docker, {}, function (err, spawn, kill) {
init({}, function (err, docker) {
createSlave(docker, {
image: 'strider/strider-docker-slave'
}, function (err, spawn, kill) {
if (err) {
throw err
}
var command = 'git'
var args = ['clone', 'https://github.com/notablemind/loco.git', '.']
var command = 'sh'
var args = ['-c', 'echo ho && sleep 30 && echo hi']
var args = ['-c', 'git clone https://github.com/notablemind/loco.git . && ls']
run(spawn, command, args, function () {
run(spawn, 'echo', ['hello'], function () {
process.exit(0)
Expand Down