1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const _ = require("lodash");
|
4 | const child_process = require("child_process");
|
5 | const nodeify_ts_1 = require("nodeify-ts");
|
6 | const cli_table_2_json_1 = require("cli-table-2-json");
|
7 | const dockermachine_cli_js_1 = require("dockermachine-cli-js");
|
8 | const exec = child_process.exec;
|
9 | const splitLines = function (input) {
|
10 | return input.replace(/\r/g, '').split('\n');
|
11 | };
|
12 | const array2Oject = function (lines) {
|
13 | return lines.reduce(function (object, linep) {
|
14 | const line = linep.trim();
|
15 | if (line.length === 0) {
|
16 | return object;
|
17 | }
|
18 | const parts = line.split(':');
|
19 | let key = parts[0];
|
20 | let value = parts.slice(1).join(':');
|
21 | key = _.snakeCase(key);
|
22 | object[key] = value.trim();
|
23 | return object;
|
24 | }, {});
|
25 | };
|
26 | const extractResult = function (result) {
|
27 | const extracterArray = [
|
28 | {
|
29 | re: / build /,
|
30 | run: function (resultp) {
|
31 | const lines = splitLines(resultp.raw);
|
32 | lines.forEach(function (line) {
|
33 | const re = /Successfully built (.*)$/;
|
34 | const str = line;
|
35 | let m;
|
36 | if ((m = re.exec(str)) !== null) {
|
37 | if (m.index === re.lastIndex) {
|
38 | re.lastIndex++;
|
39 | }
|
40 |
|
41 |
|
42 | resultp.success = true;
|
43 | resultp.imageId = m[1];
|
44 | }
|
45 | });
|
46 | resultp.response = lines;
|
47 | return resultp;
|
48 | },
|
49 | },
|
50 | {
|
51 | re: / run /,
|
52 | run: function (resultp) {
|
53 | resultp.containerId = resultp.raw.trim();
|
54 | return resultp;
|
55 | },
|
56 | },
|
57 | {
|
58 | re: / ps /,
|
59 | run: function (resultp) {
|
60 | const lines = splitLines(resultp.raw);
|
61 | resultp.containerList = cli_table_2_json_1.cliTable2Json(lines);
|
62 | return resultp;
|
63 | },
|
64 | },
|
65 | {
|
66 | re: / images /,
|
67 | run: function (resultp) {
|
68 | const lines = splitLines(resultp.raw);
|
69 |
|
70 |
|
71 | resultp.images = cli_table_2_json_1.cliTable2Json(lines);
|
72 | return resultp;
|
73 | },
|
74 | },
|
75 | {
|
76 | re: / network ls /,
|
77 | run: function (resultp) {
|
78 | const lines = splitLines(resultp.raw);
|
79 |
|
80 |
|
81 | resultp.network = cli_table_2_json_1.cliTable2Json(lines);
|
82 | return resultp;
|
83 | },
|
84 | },
|
85 | {
|
86 | re: / inspect /,
|
87 | run: function (resultp) {
|
88 | const object = JSON.parse(resultp.raw);
|
89 | resultp.object = object;
|
90 | return resultp;
|
91 | },
|
92 | },
|
93 | {
|
94 | re: / info /,
|
95 | run: function (resultp) {
|
96 | const lines = splitLines(resultp.raw);
|
97 | resultp.object = array2Oject(lines);
|
98 | return resultp;
|
99 | },
|
100 | },
|
101 | {
|
102 | re: / search /,
|
103 | run: function (resultp) {
|
104 | const lines = splitLines(resultp.raw);
|
105 | resultp.images = cli_table_2_json_1.cliTable2Json(lines);
|
106 | return resultp;
|
107 | },
|
108 | },
|
109 | {
|
110 | re: / login /,
|
111 | run: function (resultp) {
|
112 | resultp.login = resultp.raw.trim();
|
113 | return resultp;
|
114 | },
|
115 | },
|
116 | ];
|
117 | extracterArray.forEach(function (extracter) {
|
118 | const re = extracter.re;
|
119 | const str = result.command;
|
120 | let m;
|
121 | if ((m = re.exec(str)) !== null) {
|
122 | if (m.index === re.lastIndex) {
|
123 | re.lastIndex++;
|
124 | }
|
125 |
|
126 |
|
127 | return extracter.run(result);
|
128 | }
|
129 | });
|
130 | return result;
|
131 | };
|
132 | class Docker {
|
133 | constructor(options = {
|
134 | currentWorkingDirectory: undefined,
|
135 | machineName: undefined,
|
136 | }) {
|
137 | this.options = options;
|
138 | }
|
139 | command(command, callback) {
|
140 | let docker = this;
|
141 | let execCommand = 'docker ';
|
142 | let machineconfig = '';
|
143 | const promise = Promise.resolve().then(function () {
|
144 | if (docker.options.machineName) {
|
145 | const dockerMachine = new dockermachine_cli_js_1.DockerMachine();
|
146 | return dockerMachine.command('config ' + docker.options.machineName).then(function (data) {
|
147 |
|
148 | machineconfig = data.machine.config;
|
149 | });
|
150 | }
|
151 | }).then(function () {
|
152 | execCommand += ' ' + machineconfig + ' ' + command + ' ';
|
153 | let execOptions = {
|
154 | cwd: docker.options.currentWorkingDirectory,
|
155 | env: {
|
156 | DEBUG: '',
|
157 | HOME: process.env.HOME,
|
158 | PATH: process.env.PATH,
|
159 | },
|
160 | maxBuffer: 200 * 1024 * 1024,
|
161 | };
|
162 | return new Promise(function (resolve, reject) {
|
163 |
|
164 |
|
165 | exec(execCommand, execOptions, function (error, stdout, stderr) {
|
166 | if (error) {
|
167 | const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
|
168 | reject(message);
|
169 | }
|
170 |
|
171 |
|
172 | resolve({ result: stdout });
|
173 | });
|
174 | });
|
175 | }).then(function (data) {
|
176 |
|
177 | let result = {
|
178 | command: execCommand,
|
179 | raw: data.result,
|
180 | };
|
181 | return extractResult(result);
|
182 | });
|
183 | return nodeify_ts_1.default(promise, callback);
|
184 | }
|
185 | }
|
186 | exports.Docker = Docker;
|
187 | class Options {
|
188 | constructor(machineName, currentWorkingDirectory) {
|
189 | this.machineName = machineName;
|
190 | this.currentWorkingDirectory = currentWorkingDirectory;
|
191 | }
|
192 | }
|
193 | exports.Options = Options;
|