-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcmd.js
More file actions
94 lines (85 loc) · 2.69 KB
/
cmd.js
File metadata and controls
94 lines (85 loc) · 2.69 KB
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
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const { exec } = require('child_process');
const Configstore = require('configstore');
const path = require('path');
const pkg = require('../../../../package.json');
const common = require('../common/cmd');
const removeContainer = require('../common/removeContainer');
const dockerImage = `cloudlibz/clocal-gcp-datastore:latest`;
const defaultPort = 8081;
const action = (cmd, first, second, command) => {
switch (cmd) {
case 'start':
start(command);
break;
case 'stop':
stop()
break;
default:
console.log(`command invalid ${cmd} ${first} ${second}`);
}
};
const start = () => {
console.log(common.figlet());
try {
console.log(chalk.blueBright(`Starting gcp datastore on port ${defaultPort}...`));
const config = new Configstore(path.join(pkg.name, '.containerList'));
exec(`docker run -p 8081:8081 -t -d ${dockerImage}`, (err, stdout, stderr) => {
config.set('datastore', stdout.trim());
const dockerId = config.get('datastore');
if (err){
removeContainer(dockerId);
return console.log(chalk.bgRed(`failed to start\n${stderr}`));
}
exec(
`docker exec -t -d ${dockerId} bash scripts/start.sh`,
(err, stdout, stderr) => {
if (err){
removeContainer(dockerId);
return console.log(chalk.bgRed(`failed to execute\n${stderr}`));
}
console.log(stdout);
if (!err) console.log(chalk.green.bgWhiteBright(`gcp datastore started ...`));
}
);
});
} catch (err) {
console.log(chalk.blueBright.bgRed(err));
}
};
const stop = () => {
try {
console.log(chalk.blueBright('stopping gcp datastore ...'));
const config = new Configstore(path.join(pkg.name, '.containerList'));
const dockerId = config.get('datastore');
if (dockerId) {
exec(`docker container rm -f ${dockerId}`, (err, stdout, stderr) => {
if (err) console.log(chalk.bgRed(`failed to stop\n${stderr}`));
if (dockerId === stdout.trim()) {
console.log(
chalk.green.bgWhiteBright(`stopped gcp datastore. ${dockerId}`)
);
} else {
console.log(
chalk.blueBright.bgRed(
'something unintended happen. Try to manually stop the docker containers ...'
)
);
}
config.delete('datastore');
});
} else {
console.log(
chalk.blueBright.bgRed('clocal gcp datastore is not running ...')
);
}
} catch (err) {
console.log(chalk.blueBright.bgRed(err));
}
};
module.exports = {
commandName: 'datastore <cmd> [first] [second]',
action: action
};