Repository navigation
Expand file tree
/
Copy pathjc-cli.js
More file actions
executable file
·307 lines (263 loc) · 10.1 KB
/
Copy pathjc-cli.js
File metadata and controls
executable file
·307 lines (263 loc) · 10.1 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env node
"use strict";
const fs = require('fs');
const fsp = require('fs-promise');
const chalk = require('chalk');
const program = require('commander');
const exec = require('child-process-promise').exec;
const spawn = require('child_process').spawn;
const fp = require('lodash/fp');
const moment = require('moment');
const defaultDockerComposeFile = 'docker/docker-compose.yml';
const noSyncDockerComposeFile = 'docker/docker-compose-no-sync.yml';
async function containerIdForName(name) {
const cmd = await exec(`docker ps -q -f name=${ name }`);
return cmd.stdout.trim();
}
async function containerIsRunning(name) {
const containerId = await containerIdForName(name);
return containerId !== '';
}
async function hasDockerComposeFiles() {
return await fsp.exists('docker');
}
async function spawnDC(args, container, requireRunning = true) {
return run(spawn, args, container, requireRunning);
}
async function execDC(args, container, requireRunning = true) {
return run(exec, args, container, requireRunning);
}
async function run(func, args, container, requireRunning = true) {
const isValid = await hasDockerComposeFiles();
if(!isValid) {
console.log(chalk.red('There are no docker-compose files to run. Are you in the right directory?'));
console.log(chalk.red('You must run this from the'), chalk.yellow('in-spiritus'), chalk.red('project root.'));
process.exit(1);
return;
}
const isRunning = await containerIsRunning(container);
if(isRunning || !requireRunning) {
if(isRunning) {
console.log(chalk.green('Executing on', container));
}
return func('docker-compose', args, { stdio: 'inherit' });
} else {
console.log(chalk.yellow(`The ${container} container does not seem to be running. Try to serve and then try again.`), chalk.green('$ yarn jc serve'));
console.log(chalk.yellow('Run', chalk.green('$ yarn jc --help'), 'for more info'));
console.log(chalk.yellow('If the containers failed to shut down, use the kill command'), chalk.green('$ yarn jc kill'));
}
}
async function spawnDocker(args, container, requireRunning = true) {
const isValid = await hasDockerComposeFiles();
if(!isValid) {
console.log(chalk.red('There are no docker-compose files to run. Are you in the right directory?'));
console.log(chalk.red('You must run this from the'), chalk.yellow('in-spiritus'), chalk.red('project root.'));
process.exit(1);
return;
}
const isRunning = await containerIsRunning(container);
if(isRunning || !requireRunning) {
if(isRunning) {
console.log(chalk.green('Executing on', container));
}
return spawn('docker', args, { stdio: 'inherit' });
} else {
console.log(chalk.yellow(`The ${container} container does not seem to be running. Try to serve and then try again.`), chalk.green('$ yarn jc serve'));
console.log(chalk.yellow('Run', chalk.green('$ yarn jc --help'), 'for more info'));
console.log(chalk.yellow('If the containers failed to shut down, use the kill command'), chalk.green('$ yarn jc kill'));
}
}
program
.version('0.0.6')
.command('serve')
.alias('server')
.alias('s')
.option('-n, --no-sync', "Don't start the syncing containers, sidekiq and worker")
.description('Start all docker containers')
.action(async options => {
let cmd;
if (options.sync) {
console.log(chalk.green('Starting services with sync enabled'));
cmd = await spawnDC(['-f', defaultDockerComposeFile, 'up'], undefined, false);
} else {
console.log(chalk.green('Starting services with sync disabled'));
cmd = await spawnDC(['-f', noSyncDockerComposeFile, 'up'], undefined, false);
}
process.on('close', () => {
console.log(chalk.green('All containers shutdown'));
process.exit(1);
});
function exitHandler(options, err) {
console.log(chalk.green('Waiting for containers to shutdown gracefully...'));
spawnDC(['-f', defaultDockerComposeFile, 'stop'], undefined, false);
}
process.on('exit', exitHandler.bind(null));
process.on('SIGINT', exitHandler.bind(null));
});
program
.command('kill')
.alias('k')
.description('Kill all containers')
.action(() => {
spawnDC(['-f', defaultDockerComposeFile, 'kill'], undefined, false);
});
program
.command('console')
.alias('c')
.description('Starts a rails console on the web container')
.action(async () => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'web', 'rails', 'c'], 'web');
});
program
.command('zeus [options...]')
.description('Run zeus commands on the zeus container. *Note, flags are not allowed.')
.action(options => {
const args = fp.flattenDeep([['-f', defaultDockerComposeFile, 'exec', 'zeus', 'zeus'], options]);
spawnDC(args, 'zeus');
});
program
.command('pry')
.description('Attach to the web container to use pry')
.action(async () => {
const containerId = await containerIdForName('web');
spawnDocker(['attach', containerId], 'web');
});
program
.command('bash')
.alias('b')
.description('Starts a bash shell in the container. Defaults to web container.')
.option('-c, --container <container>', 'The container to connect to.')
.action(options => {
const container = options.container || 'web';
spawnDC(['-f', defaultDockerComposeFile, 'exec', container, 'bash'], container);
});
program
.command('tail [log]')
.description('Tail logs. Defaults to web container.')
.option('-c, --container <container>', 'The container to connect to and tail.')
.option('-p, --path <path>', 'The path to prepend the file name to tail.')
.action((log, options) => {
const logFile = log || 'development';
const container = options.container || 'web';
const path = options.path || './log/';
const fullPath = options.path || `${path}${logFile}.log`;
spawnDC(['-f', defaultDockerComposeFile, 'exec', container, 'tail', '-f', fullPath], container);
});
program
.command('psql')
.description('Starts a psql session on the db contianer')
.action(() => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'db', 'psql', '-U', 'postgres'], 'db');
});
program
.command('stellar [options...]')
.description('Run stellar commands on the python container. *Note, flags are not allowed. For full use of the stellar command, open a shell using yarn jc bash')
.action(options => {
const args = fp.flattenDeep([['-f', defaultDockerComposeFile, 'exec', 'python', 'stellar'], options]);
spawnDC(args, 'python');
});
program
.command('snapshot [filename]')
.description('Create a snapshot of the dev db')
.action(async name => {
name = name || `you-pressed-${moment().unix()}`
const cmd = await spawnDC([
'-f', defaultDockerComposeFile,
'exec', '-T', 'python',
'stellar', 'snapshot', name
], 'python');
cmd.on('close', () => {
console.log(chalk.yellow('Created new snapshot:'), chalk.red(name));
console.log(chalk.yellow('To restore run:'), chalk.red(`is restore ${name}`));
});
});
program
.command('restore [name]')
.description('Restore you-pressed db to specified snapshot')
.action(async name => {
if(!name) {
console.log(chalk.red('You must specify a name'));
process.exit(1);
return;
}
console.log(chalk.yellow('Restoring you_pressed db to:'), chalk.red(name));
console.log(chalk.yellow('This may take awhile'));
const cmd = await spawnDC(['-f', defaultDockerComposeFile,
'exec', 'python',
'stellar', 'restore', name
], 'python');
cmd.on('close', () => {
console.log(chalk.green('DB in_spiritus has been restored from snapshot:'), chalk.red(name));
});
});
program
.command('migrate')
.description('Runs pending db migrations')
.action(() => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'web', 'rake', 'db:migrate'], 'web');
});
program
.command('redis')
.description('Starts a redis-cli session on the redis container')
.action(() => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'redis', 'redis-cli'], 'redis');
});
program
.command('reset')
.description('Drop db, runs rake db:reset')
.action(() => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'web', 'rake', 'db:reset'], 'web');
});
program
.command('wipe')
.description('Wipe all data and reseed without dropping')
.action(() => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'web', 'rake', 'clean:all'], 'web');
});
program
.command('rails [options...]')
.description('Run rails commands on the web container. *Note, flags are not allowed. For full use of the rails command, open a shell using yarn jc bash')
.action(options => {
const args = fp.flattenDeep([['-f', defaultDockerComposeFile, 'exec', 'web', 'rails'], options]);
spawnDC(args, 'web');
});
program
.command('rake [options...]')
.description('Run rake commands on the web container. *Note, flags are not allowed. For full use of the rails command, open a shell using yarn jc bash')
.action(options => {
const args = fp.flattenDeep([['-f', defaultDockerComposeFile, 'exec', 'web', 'rake'], options]);
spawnDC(args, 'web');
});
program
.command('test')
.alias('t')
.description('Run test suite')
.action(options => {
spawnDC(['-f', defaultDockerComposeFile, 'exec', 'web', 'rake', 'test'], 'web');
});
program
.on('--help', function () {
console.log(' Examples:');
console.log('');
console.log(' $ yarn jc serve');
console.log(' $ yarn jc bash');
console.log(' $ yarn jc bash --help');
console.log(' $ yarn jc psql');
console.log(' $ yarn jc console');
console.log(' $ yarn jc c');
console.log(' $ yarn jc reset');
console.log(' $ yarn jc snapshot MY_SNAPSHOT');
console.log(' $ yarn jc restore MY_SNAPSHOT');
console.log(' $ yarn jc kill');
console.log(' $ yarn jc rails g model Post title description date:datetime');
console.log(' $ yarn jc rake test');
console.log(' $ yarn jc rake migrate');
console.log(' $ yarn jc rake db:rollback');
console.log(' $ yarn jc wipe');
console.log(' $ yarn jc tail');
console.log('');
});
program.parse(process.argv);
if (!program.args.length) {
program.help();
}