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