Skip to content

Commit a383754

Browse files
committed
docker client for services + command refactored
1 parent 05b5906 commit a383754

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

command/index.js renamed to command/BaseCommand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ module.exports = class Command {
2424

2525
_runInternal() {
2626
log.warn('Command run on base class');
27-
return Promise.resolve(); // todo : maybe reject
27+
return Promise.reject();
2828
}
2929
};

command/NetworkCommand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const log = require('../logger');
2-
const Command = require('../command');
2+
const BaseCommand = require('./BaseCommand');
33

4-
module.exports = class NetworkCommand extends Command {
4+
module.exports = class NetworkCommand extends BaseCommand {
55
constructor(name, options) {
66
super(name, options)
77
}

command/ServiceCommand.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
const log = require('../logger');
2-
const Command = require('../command');
2+
const BaseCommand = require('./BaseCommand');
3+
const docker = require('../docker/client');
34

4-
module.exports = class ServiceCommand extends Command {
5+
6+
module.exports = class ServiceCommand extends BaseCommand {
57
constructor(name, options) {
68
super(name, options)
79
}
810

911
_runInternal() {
10-
return new Promise(resolve => setTimeout(resolve, 1000))
12+
const {image} = this.options;
13+
return docker
14+
.pull(image)
15+
.then(() => docker.createContainer({
16+
'Hostname': '',
17+
'User': '',
18+
'AttachStdin': false,
19+
'AttachStdout': false,
20+
'AttachStderr': false,
21+
'Tty': false,
22+
'OpenStdin': false,
23+
'StdinOnce': false,
24+
'Env': null,
25+
'Cmd': [],
26+
'Image': image,
27+
'Volumes': {},
28+
'VolumesFrom': [],
29+
'name': this.name
30+
}))
31+
.then(container => container.start())
1132
.then(() => log.info(`service created: '${this.name}'`));
33+
1234
}
1335
};

command/VolumeCommand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const log = require('../logger');
2-
const Command = require('../command');
2+
const BaseCommand = require('./BaseCommand');
33

4-
module.exports = class VolumeCommand extends Command {
4+
module.exports = class VolumeCommand extends BaseCommand {
55
constructor(name, options) {
66
super(name, options)
77
}

docker/client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Docker = require('dockerode');
2+
module.exports = new Docker({socketPath: '/var/run/docker.sock'});

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
},
1010
"dependencies": {
1111
"debug": "~2.6.9",
12-
"docker-client": "^1.38.1",
12+
"dockerode": "^2.5.7",
1313
"express": "~4.16.0",
1414
"express-fileupload": "^1.0.0",
1515
"http-errors": "~1.6.2",
1616
"morgan": "~1.9.0",
1717
"nconf": "^0.10.0",
1818
"parser-yaml": "^0.1.1",
19-
"winston": "^3.1.0",
20-
"yaml": "^1.0.0"
19+
"winston": "^3.1.0"
2120
},
2221
"devDependencies": {
2322
"@types/node": "^10.12.0"

0 commit comments

Comments
 (0)