1 | "use strict";
|
2 |
|
3 | const argx = require('argx'),
|
4 | apemanfile = require('apemanfile'),
|
5 | apemancwd = require('apemancwd'),
|
6 | arrayreduce = require('arrayreduce'),
|
7 | arrayfilter = require('arrayfilter'),
|
8 | apemandocker = require('apemandocker'),
|
9 | async = require('async'),
|
10 | runDply = require('./deploying/run_dply'),
|
11 | listDply = require('./deploying/list_dply'),
|
12 | ttyDply = require('./deploying/tty_dply'),
|
13 | logDply = require('./deploying/log_dply'),
|
14 | removeDply = require('./deploying/remove_dply'),
|
15 | execCmd = require('./deploying/exec_cmd');
|
16 |
|
17 | let concatArray = arrayreduce.arrayConcat(),
|
18 | rejectEmpty = arrayfilter.emptyReject();
|
19 |
|
20 |
|
21 | function apemanDply(names, options, callback) {
|
22 | let args = argx(arguments);
|
23 | callback = args.pop('function') || argx.noop;
|
24 | options = args.pop('object') || {};
|
25 | names = args.remain().filter(rejectEmpty).reduce(concatArray, []);
|
26 |
|
27 | let docker = apemandocker.create(),
|
28 | cwd = apemancwd.create({prefix: 'apeman-dply'});
|
29 |
|
30 | async.waterfall([
|
31 | (callback) => {
|
32 | apemanfile.load(options.configuration, callback);
|
33 | },
|
34 | (apemanfile, callback) => {
|
35 | cwd.chdir(apemanfile.$cwd);
|
36 |
|
37 | let duplicate = [
|
38 | options.list,
|
39 | options.delete,
|
40 | options.rerun,
|
41 | options.tty,
|
42 | options.exec,
|
43 | options.print,
|
44 | options.printon
|
45 | ].filter((command) => {
|
46 | return !!command;
|
47 | }).length > 1;
|
48 | if (duplicate) {
|
49 | callback(new Error(`Invalid option: ${JSON.stringify(options, null, 2)}`));
|
50 | return;
|
51 | }
|
52 | if (options.rerun) {
|
53 | async.series([
|
54 | (callback) => {
|
55 | removeDply(apemanfile, names, {
|
56 | force: options.force
|
57 | }, callback);
|
58 | },
|
59 | (callback) => {
|
60 | runDply(apemanfile, names, {
|
61 | force: options.force
|
62 | }, callback);
|
63 | }
|
64 | ], callback);
|
65 | return;
|
66 | }
|
67 |
|
68 | if (options.exec) {
|
69 | execCmd(apemanfile, names, options.exec, {}, callback);
|
70 | return;
|
71 | }
|
72 |
|
73 | if (options.list) {
|
74 | listDply(apemanfile, names, {}, callback);
|
75 | return;
|
76 | }
|
77 | if (options.delete) {
|
78 | removeDply(apemanfile, names, {
|
79 | force: options.force
|
80 | }, callback);
|
81 | return;
|
82 | }
|
83 |
|
84 | if (options.tty) {
|
85 | ttyDply(apemanfile, names, {}, callback);
|
86 | return;
|
87 | }
|
88 |
|
89 | if (options.print) {
|
90 | logDply(apemanfile, names, {}, callback);
|
91 | return;
|
92 | }
|
93 |
|
94 | if (options.printon) {
|
95 | logDply(apemanfile, names, {
|
96 | follow: true
|
97 | }, callback);
|
98 | return;
|
99 | }
|
100 |
|
101 | runDply(apemanfile, names, {
|
102 | force: options.force
|
103 | }, callback);
|
104 | }
|
105 | ], (err) => {
|
106 | cwd.restoreAll();
|
107 | callback(err);
|
108 | });
|
109 | }
|
110 |
|
111 | module.exports = apemanDply;
|