Skip to content

Commit 4b52f5f

Browse files
committed
pulling image + clean up + refactoring
1 parent a383754 commit 4b52f5f

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

command/BaseCommand.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ module.exports = class Command {
2323
}
2424

2525
_runInternal() {
26-
log.warn('Command run on base class');
26+
log.warn('Command run on base class. Please define "_runInternal()" on the child class');
27+
return Promise.reject();
28+
}
29+
30+
cleanUp() {
31+
log.warn('method "cleanUp()" not defined. Please define "cleanUp()" on the child class');
2732
return Promise.reject();
2833
}
2934
};

command/NetworkCommand.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const log = require('../logger');
2+
const timer = require('../helpers/timer');
23
const BaseCommand = require('./BaseCommand');
34

45
module.exports = class NetworkCommand extends BaseCommand {
@@ -7,7 +8,12 @@ module.exports = class NetworkCommand extends BaseCommand {
78
}
89

910
_runInternal() {
10-
return new Promise(resolve => setTimeout(resolve, 1000))
11+
return timer(1000)
1112
.then(() => log.info(`network created: '${this.name}'`));
1213
}
14+
15+
cleanUp() {
16+
return timer(5000)
17+
.then(() => log.info(`network removed: '${this.name}'`));
18+
}
1319
};

command/ServiceCommand.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const log = require('../logger');
2+
const timer = require('../helpers/timer');
23
const BaseCommand = require('./BaseCommand');
34
const docker = require('../docker/client');
45

@@ -11,25 +12,28 @@ module.exports = class ServiceCommand extends BaseCommand {
1112
_runInternal() {
1213
const {image} = this.options;
1314
return docker
14-
.pull(image)
15+
.pull(image, {repo: 'library'})
1516
.then(() => docker.createContainer({
16-
'Hostname': '',
17-
'User': '',
1817
'AttachStdin': false,
1918
'AttachStdout': false,
2019
'AttachStderr': false,
21-
'Tty': false,
2220
'OpenStdin': false,
2321
'StdinOnce': false,
24-
'Env': null,
2522
'Cmd': [],
2623
'Image': image,
27-
'Volumes': {},
28-
'VolumesFrom': [],
2924
'name': this.name
3025
}))
31-
.then(container => container.start())
26+
.then(container => {
27+
this.container = container;
28+
return container.start()
29+
})
3230
.then(() => log.info(`service created: '${this.name}'`));
31+
}
3332

33+
cleanUp() {
34+
return timer(5000)
35+
.then(() => this.container.stop())
36+
.then(() => this.container.remove())
37+
.then(() => log.info(`service removed: '${this.name}'`));
3438
}
3539
};

command/VolumeCommand.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const log = require('../logger');
2+
const timer = require('../helpers/timer');
23
const BaseCommand = require('./BaseCommand');
34

45
module.exports = class VolumeCommand extends BaseCommand {
@@ -7,7 +8,12 @@ module.exports = class VolumeCommand extends BaseCommand {
78
}
89

910
_runInternal() {
10-
return new Promise(resolve => setTimeout(resolve, 1000))
11+
return timer(1000)
1112
.then(() => log.info(`volume created: '${this.name}'`));
1213
}
14+
15+
cleanUp() {
16+
return timer(5000)
17+
.then(() => log.info(`volume removed: '${this.name}'`));
18+
}
1319
};

helpers/timer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = millis => new Promise(resolve => setTimeout(resolve, millis));

services/DockerService.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ module.exports = class DockerService {
5353
}
5454
}
5555

56-
await Promise.all(Object.values(commands).map(c => c.run()));
56+
const runnable = Object.values(commands);
57+
await Promise.all(runnable.map(c => c.run()));
58+
runnable.forEach(c => c.cleanUp());
5759
return {ok: true}
5860
}
5961
};

0 commit comments

Comments
 (0)