Repository navigation
Expand file tree
/
Copy pathdocker.js
More file actions
160 lines (146 loc) · 5.04 KB
/
Copy pathdocker.js
File metadata and controls
160 lines (146 loc) · 5.04 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @classification UNCLASSIFIED
*
* @module scripts.docker
*
* @copyright Copyright (C) 2018, Lockheed Martin Corporation
*
* @license Apache-2.0
*
* @owner Connor Doyle
*
* @author Josh Kaplan
* @author Leah De Laurell
*
* @description Builds and runs docker containers.
*/
/* eslint-disable jsdoc/require-description-complete-sentence */
// Rule disabled to allow list in description
// Error Check - Check if file was run directly or global M object is undefined
if (module.parent == null || typeof M === 'undefined') {
// File was run directly, print error message and exit process
// eslint-disable-next-line no-console
console.log('\nError: please use mbee to run this script by using the '
+ 'following command. \n\nnode mbee docker\n');
process.exit(-1);
}
// Node modules
const { spawn, spawnSync } = require('child_process');
/**
* @description The Docker command can be used to build a Docker image or run a Docker
* container. It supports the command line arguments:
* --clean
* --build
* --run
*
* @param {string} args - Additional options to pass into the docker function.
*/
function docker(args) {
// Removes the previous docker build.
if (args.includes('--clean')) {
// Stop the running container
let cmd = spawnSync('docker', ['stop', M.config.docker.container.name], { stdio: 'inherit' });
console.log('stdout:', cmd.stdout); // eslint-disable-line no-console
console.log('stderr:', cmd.stderr); // eslint-disable-line no-console
console.log('Docker container stopped'); // eslint-disable-line no-console
// Remove the container image
cmd = spawnSync('docker', ['rm', M.config.docker.container.name], { stdio: 'inherit' });
console.log('stdout:', cmd.stdout); // eslint-disable-line no-console
console.log('stderr:', cmd.stderr); // eslint-disable-line no-console
console.log('Docker container removed'); // eslint-disable-line no-console
}
// Build the Docker image
else if (args.includes('--build')) {
M.log.info('Building Docker Image ...');
// Build docker image
const buildArgs = [
'build',
'-f', M.config.docker.Dockerfile,
'-t', M.config.docker.image.name, '.'
];
// Run build process
const cmd = spawn('docker', buildArgs, { stdio: 'inherit' });
cmd.on('data', (data) => {
console.log(data.toString()); // eslint-disable-line no-console
});
cmd.on('exit', (code) => {
// Check if exit code NOT 0
if (code !== 0) {
// exit code NOT 0, fail
M.log.error('Docker build failed');
process.exit(code);
}
else {
M.log.info('Docker Image Built.');
}
});
}
// Run the Docker container
else if (args.includes('--run')) {
M.log.info('Running Docker Container ...');
// Build the "docker run" command
let rargs = [
'run',
'-d',
'-it',
'--restart=always',
'-e', `MBEE_ENV=${M.env}`
].concat(args.slice(1));
// Check if the database is in docker container
if (M.config.docker.db.enabled) {
// http and docker http enabled, open specified ports
rargs = rargs.concat(['-p', `${M.config.docker.db.port}:${M.config.db.port}`]);
}
if (M.config.server.http.enabled && M.config.docker.http.enabled) {
// http and docker http enabled, open specified ports
rargs = rargs.concat(['-p', `${M.config.docker.http.port}:${M.config.server.http.port}`]);
}
// Check if server https and docker https are enabled
if (M.config.server.https.enabled && M.config.docker.https.enabled) {
// https and docker https enabled, open specified ports
rargs = rargs.concat(['-p', `${M.config.docker.https.port}:${M.config.server.https.port}`]);
}
// Set docker image name
rargs = rargs.concat(['--name', M.config.docker.container.name]);
rargs = rargs.concat([M.config.docker.image.name]);
// Run the Docker container
const cmd = spawn('docker', rargs, { stdio: 'inherit' });
// eslint-disable-next-line no-console
cmd.on('data', (data) => { console.log(data.toString()); });
cmd.on('exit', (code) => {
// Check if exit code NOT 0
if (code !== 0) {
// exit code NOT 0, fail
M.log.error('Docker run failed');
process.exit(code);
}
});
M.log.info('Docker Container Running in Background.');
}
// Get the Docker logs
else if (args.includes('--get-logs')) {
M.log.info('Getting docker logs ...');
// Build the "docker run" command
const rargs = [
'logs',
M.config.docker.container.name
];
// Call the Docker logs command
const cmd = spawn('docker', rargs, { stdio: 'inherit' });
// eslint-disable-next-line no-console
cmd.on('data', (data) => { console.log(data.toString()); });
cmd.on('exit', (code) => {
// Check if exit code NOT 0
if (code !== 0) {
// exit code NOT 0, fail
M.log.error('Docker logs failed');
process.exit(code);
}
});
M.log.info('End of Docker logs.');
}
else {
M.log.info('Invalid arguments');
}
}
module.exports = docker;