Skip to content

Commit

Permalink
exec identity, v2.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Oct 29, 2014
1 parent 069a580 commit 4c0c45e
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 131 deletions.
55 changes: 25 additions & 30 deletions examples/exec_running_container.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
var Docker = require('../lib/docker');

var docker = new Docker({socketPath: '/var/run/docker.sock'});
var docker = new Docker({
socketPath: '/var/run/docker.sock'
});

/**
* Get env list from running container
* @param container
*/
function runExec(container) {
options = {
"AttachStdout": true,
"AttachStderr": true,
"Tty": false,
Cmd: ["env"]
};
container.exec(options, function (err, data) {
container.execstart(data.Id, function (err, stream) {
if (err != null) {
callback(err, null);
return;
}

stream.setEncoding('utf8');
stream.on('data', function (chunk) {
console.log(chunk);
});
stream.on('end', function () {
console.log('--Done--');
});
});
})
}
options = {
"AttachStdout": true,
"AttachStderr": true,
"Tty": false,
Cmd: ["env"]
};
container.exec(options, function(err, exec) {
if (err) return;

docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/ls', '/stuff'], "Volumes": {"/stuff": {}}}, function (err, container) {
container.attach({stream: true, stdout: true, stderr: true, tty: true}, function (err, stream) {
stream.pipe(process.stdout);
exec.start(function(err, stream) {
if (err) return;

container.start({"Binds": ["/home/vagrant:/stuff"]}, function (err, data) {
runExec(container);
});
stream.setEncoding('utf8');
stream.pipe(process.stdout);
});
});
}

docker.createContainer({
Image: 'ubuntu',
Cmd: ['/bin/bash']
}, function(err, container) {
container.start({}, function(err, data) {
runExec(container);
});
});
25 changes: 20 additions & 5 deletions examples/external_volume.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
var Docker = require('dockerode');
var Docker = require('../lib/docker');

var docker = new Docker({socketPath: '/var/run/docker.sock'});
var docker = new Docker({
socketPath: '/var/run/docker.sock'
});

docker.createContainer({ Image: 'ubuntu', Cmd: ['/bin/ls','/stuff'], "Volumes":{"/stuff": {}} }, function (err, container) {
container.attach({stream: true, stdout: true, stderr: true, tty: true}, function (err, stream) {
docker.createContainer({
Image: 'ubuntu',
Cmd: ['/bin/ls', '/stuff'],
"Volumes": {
"/stuff": {}
}
}, function(err, container) {
container.attach({
stream: true,
stdout: true,
stderr: true,
tty: true
}, function(err, stream) {
stream.pipe(process.stdout);

container.start({"Binds":["/home/vagrant:/stuff"]}, function (err, data) {
container.start({
"Binds": ["/home/vagrant:/stuff"]
}, function(err, data) {
console.log(data);
});
});
Expand Down
92 changes: 34 additions & 58 deletions lib/container.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('underscore');
var _ = require('underscore'),
Exec = require('./exec');

/**
* Represents a Container
Expand All @@ -10,17 +11,17 @@ var Container = function(modem, id) {
this.id = id;

this.defaultOptions = {
top: {},
start: {},
commit: {},
stop: {},
top: {},
start: {},
commit: {},
stop: {},
restart: {},
resize: {},
attach: {},
remove: {},
copy: {},
kill: {},
exec: {}
resize: {},
attach: {},
remove: {},
copy: {},
kill: {},
exec: {}
};
};

Expand All @@ -45,7 +46,9 @@ Container.prototype.inspect = function(callback) {
callback(err, data);
});
} else {
return JSON.stringify({id: this.id});
return JSON.stringify({
id: this.id
});
}
};

Expand Down Expand Up @@ -147,7 +150,6 @@ Container.prototype.start = function(opts, callback) {

/**
* Pause
* Warning: Uses an undocumented Docker endpoint. :)
* @param {Object} opts Pause options (optional)
* @param {Function} callback Callback
*/
Expand All @@ -174,7 +176,6 @@ Container.prototype.pause = function(opts, callback) {

/**
* Unpause
* Warning: Uses an undocumented Docker endpoint. :)
* @param {Object} opts Unpause options (optional)
* @param {Function} callback Callback
*/
Expand Down Expand Up @@ -206,53 +207,28 @@ Container.prototype.unpause = function(opts, callback) {
* @param {function} callback
*/
Container.prototype.exec = function(opts, callback) {
if (!callback && typeof(opts) === 'function') {
callback = opts;
opts = null;
}
var self = this;

var optsf = {
path: '/containers/' + this.id + '/exec',
method: 'POST',
statusCodes: {
201: true,
404: "no such container",
500: "server error"
},
options: _.extend({}, this.defaultOptions.exec, opts)
};

this.modem.dial(optsf, function(err, data) {
callback(err, data);
});
};
if (!callback && typeof(opts) === 'function') {
callback = opts;
opts = {};
}

/**
* Run the exec call that was setup.
*
* @param {object} opts should be passed from exec call return {Id:xxxxxx}
* @param {function} callback
*/
Container.prototype.execstart = function(id, opts, callback) {
if (!callback && typeof(opts) === 'function') {
callback = opts;
opts = null;
}
var optsf = {
path: '/exec/' + id + '/start',
method: 'POST',
isStream: true,
statusCodes: {
200: true,
404: "no such container",
500: "Container not running"
},
options: _.extend({}, this.defaultOptions.exec, opts)
};
var optsf = {
path: '/containers/' + this.id + '/exec',
method: 'POST',
statusCodes: {
201: true,
404: "no such container",
500: "server error"
},
options: _.extend({}, this.defaultOptions.exec, opts)
};

this.modem.dial(optsf, function(err, data) {
callback(err, data);
});
this.modem.dial(optsf, function(err, data) {
if(err) return callback(err, data);
callback(err, new Exec(self.modem, data.Id));
});
};

/**
Expand Down
70 changes: 70 additions & 0 deletions lib/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var _ = require('underscore');

/**
* Represents an Exec
* @param {Object} modem docker-modem
* @param {String} id Exec's ID
*/
var Exec = function(modem, id) {
this.modem = modem;
this.id = id;
};

/**
* Start the exec call that was setup.
*
* @param {object} options
* @param {function} callback
*/
Exec.prototype.start = function(opts, callback) {
if (!callback && typeof(opts) === 'function') {
callback = opts;
opts = {};
}

var optsf = {
path: '/exec/' + this.id + '/start',
method: 'POST',
isStream: true,
statusCodes: {
200: true,
404: "no such exec",
500: "container not running"
},
options: opts
};

this.modem.dial(optsf, function(err, data) {
callback(err, data);
});
};

/**
* Resize the exec call that was setup.
*
* @param {object} options
* @param {function} callback
*/
Exec.prototype.resize = function(opts, callback) {
if (!callback && typeof(opts) === 'function') {
callback = opts;
opts = null;
}
var optsf = {
path: '/exec/' + this.id + '/resize?',
method: 'POST',
statusCodes: {
200: true,
404: "no such exec",
500: "container not running"
},
options: opts
};

this.modem.dial(optsf, function(err, data) {
callback(err, data);
});
};


module.exports = Exec;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dockerode",
"description": "Docker.io / Docker remote API implementation.",
"version": "2.0.3",
"version": "2.0.4",
"author": "Pedro Dias <[email protected]>",
"maintainers": [
"apocas <[email protected]>"
Expand Down
Loading

0 comments on commit 4c0c45e

Please sign in to comment.