Skip to content

Commit 49f4036

Browse files
committed
docker pull fix + some refactoring
1 parent f31c5f0 commit 49f4036

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

command/NetworkCommand.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ module.exports = class NetworkCommand extends BaseCommand {
1212
.then(() => log.info(`network created: '${this.name}'`));
1313
}
1414

15-
cleanUp() {
16-
return timer(5000)
17-
.then(() => log.info(`network removed: '${this.name}'`));
15+
async cleanUp() {
16+
log.info(`network removed: '${this.name}'`);
1817
}
1918
};

command/ServiceCommand.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,34 @@ module.exports = class ServiceCommand extends BaseCommand {
99
super(name, options)
1010
}
1111

12-
_runInternal() {
12+
async _runInternal() {
1313
const {image} = this.options;
14-
return docker
15-
.pull(image, {repo: 'library'})
16-
.then(() => docker.createContainer({
17-
'AttachStdin': false,
18-
'AttachStdout': false,
19-
'AttachStderr': false,
20-
'OpenStdin': false,
21-
'StdinOnce': false,
22-
'Cmd': [],
23-
'Image': image,
24-
'name': this.name
25-
}))
26-
.then(container => {
27-
this.container = container;
28-
return container.start()
29-
})
30-
.then(() => log.info(`service created: '${this.name}'`));
14+
await docker.pull(image);
15+
16+
log.info(`Creating container: '${this.name}' (${image})`);
17+
const container = await docker.createContainer({
18+
'AttachStdin': false,
19+
'AttachStdout': false,
20+
'AttachStderr': false,
21+
'OpenStdin': false,
22+
'StdinOnce': false,
23+
'Cmd': [],
24+
'Image': image,
25+
'name': this.name
26+
});
27+
this.container = container;
28+
29+
log.info(`Starting container: '${this.name}' (${image})`);
30+
await container.start();
31+
32+
log.info(`Service created: '${this.name}' (${image})`);
3133
}
3234

33-
cleanUp() {
34-
return timer(5000)
35-
.then(() => this.container.stop())
36-
.then(() => this.container.remove())
37-
.then(() => log.info(`service removed: '${this.name}'`));
35+
async cleanUp() {
36+
if (this.container) {
37+
await this.container.stop();
38+
await this.container.remove();
39+
log.info(`Service removed: '${this.name}' (${image})`)
40+
}
3841
}
3942
};

command/VolumeCommand.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ module.exports = class VolumeCommand extends BaseCommand {
1212
.then(() => log.info(`volume created: '${this.name}'`));
1313
}
1414

15-
cleanUp() {
16-
return timer(5000)
17-
.then(() => log.info(`volume removed: '${this.name}'`));
15+
async cleanUp() {
16+
log.info(`network removed: '${this.name}'`);
1817
}
1918
};

docker/client.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
const config = require('../config');
2+
const log = require('../logger');
23
const Docker = require('dockerode');
34

4-
module.exports = new Docker(config.docker);
5+
class DockerClient extends Docker {
6+
constructor(options) {
7+
super(options)
8+
}
9+
10+
async pull(image) {
11+
if (!image.includes(':')) {
12+
image = `${image}:latest`;
13+
}
14+
const stream = await super.pull(image);
15+
return await new Promise((resolve, reject) => this.modem.followProgress(
16+
stream,
17+
(err, output) => err ? reject(err) : resolve(output),
18+
event => log.info(event.status)
19+
));
20+
}
21+
}
22+
23+
module.exports = new DockerClient(config.docker);

services/DockerService.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const log = require('../logger');
22
const yaml = require('parser-yaml');
3+
const timer = require('../helpers/timer');
34
const ServiceCommand = require('../command/ServiceCommand');
45
const NetworkCommand = require('../command/NetworkCommand');
56
const VolumeCommand = require('../command/VolumeCommand');
@@ -10,7 +11,7 @@ module.exports = class DockerService {
1011
return this.applyDirectives(directives);
1112
}
1213

13-
static async applyDirectives({services, volumes, networks}) {
14+
static async applyDirectives({services, volumes = {}, networks = {}}) {
1415
const commands = {};
1516

1617
for (const name in services) {
@@ -33,20 +34,24 @@ module.exports = class DockerService {
3334

3435
if (dependencies) {
3536
for (const dependency of dependencies) {
36-
command.addDependency(getCommand(dependency, services, commands, ServiceCommand))
37+
if (services[dependency]) {
38+
command.addDependency(getCommand(dependency, services, commands, ServiceCommand))
39+
}
3740
}
3841
}
3942

4043
if (depNetworks) {
4144
for (const network of depNetworks) {
42-
command.addDependency(getCommand(network, networks, commands, NetworkCommand))
45+
if (networks[network]) {
46+
command.addDependency(getCommand(network, networks, commands, NetworkCommand))
47+
}
4348
}
4449
}
4550

4651
if (depVolumes) {
4752
for (const name of depVolumes) {
4853
const volume = name.split(':')[0];
49-
if (volumes.hasOwnProperty(volume)) {
54+
if (volumes[volume]) {
5055
command.addDependency(getCommand(volume, volumes, commands, VolumeCommand))
5156
}
5257
}
@@ -55,7 +60,7 @@ module.exports = class DockerService {
5560

5661
const runnable = Object.values(commands);
5762
await Promise.all(runnable.map(c => c.run()));
58-
runnable.forEach(c => c.cleanUp());
63+
timer(5000).then(() => runnable.forEach(c => c.cleanUp()));
5964
return {ok: true}
6065
}
6166
};

0 commit comments

Comments
 (0)