forked from acuminous/bosco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.js
108 lines (93 loc) · 3.62 KB
/
stop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var _ = require('lodash');
var async = require('async');
var RunListHelper = require('../src/RunListHelper');
var NodeRunner = require('../src/RunWrappers/Node');
var DockerRunner = require('../src/RunWrappers/Docker');
var DockerComposeRunner = require('../src/RunWrappers/DockerCompose');
var CmdHelper = require('../src/CmdHelper');
module.exports = {
name: 'stop',
description: 'Stops all of the microservices (or subset based on regex pattern)',
usage: '[-r <repoPattern>]',
};
function cmd(bosco, args, done) {
var repoPattern = bosco.options.repo;
var repoRegex = new RegExp(repoPattern);
var repoTag = bosco.options.tag;
var runningServices = [];
var repos;
if (bosco.options.list) {
repos = bosco.options.list.split(',');
} else {
CmdHelper.checkInService(bosco);
repos = bosco.getRepos();
}
function initialiseRunners(cb) {
var runners = [NodeRunner, DockerRunner, DockerComposeRunner];
async.map(runners, function loadRunner(runner, lcb) {
runner.init(bosco, lcb);
}, cb);
}
function disconnectRunners(next) {
var runners = [NodeRunner, DockerRunner];
async.map(runners, function loadRunner(runner, cb) {
runner.disconnect(cb);
}, next);
}
function stopService(repo, boscoService, services, cb) {
if (boscoService.service && boscoService.service.type === 'docker') {
if (_.contains(services, repo)) {
return DockerRunner.stop(boscoService, cb);
}
} else if (boscoService.service && boscoService.service.type === 'docker-compose') {
if (_.contains(services, 'docker-compose')) {
return DockerComposeRunner.stop(boscoService, cb);
}
} else {
if (_.contains(services, repo)) {
return NodeRunner.stop({name: repo}, cb);
}
}
return cb();
}
function stopRunningServices(cb) {
RunListHelper.getRunList(bosco, repos, repoRegex, null, repoTag, function(err, services) {
async.mapSeries(services, function(boscoService, next) {
var repo = boscoService.name;
if (!repo.match(repoRegex)) return next();
if (boscoService.service && boscoService.service.type) {
return stopService(repo, boscoService, runningServices, next);
}
RunListHelper.getServiceConfigFromGithub(bosco, boscoService.name, function(err, svcConfig) {
if (err || !svcConfig) {
bosco.warn('Unable to retrieve config for ' + boscoService.name.cyan + ' in github, continuing ...');
return next();
}
if (!svcConfig.name) svcConfig.name = boscoService.name;
stopService(repo, svcConfig, runningServices, next);
});
}, function() {
// Special case for bosco-cdn, room for improvement to make this
// generic for all custom bosco services.
if (!_.contains(runningServices, 'bosco-cdn')) return cb();
NodeRunner.stop({name: 'bosco-cdn'}, cb);
});
});
}
function getRunningServices(cb) {
NodeRunner.listRunning(false, function(err, nodeRunning) {
DockerRunner.list(false, function(err, dockerRunning) {
var flatDockerRunning = _.map(_.flatten(dockerRunning), function(item) { return item.replace('/', ''); });
DockerComposeRunner.list(false, function(err, dockerComposeRunning) {
runningServices = _.union(nodeRunning, flatDockerRunning, dockerComposeRunning);
cb();
});
});
});
}
bosco.log('Stop each microservice ' + args);
async.series([initialiseRunners, getRunningServices, stopRunningServices, disconnectRunners], function() {
if (done) return done(null, runningServices);
});
}
module.exports.cmd = cmd;