@wocker/ws 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/README.md +11 -137
  2. package/bin/ws.js +7 -3
  3. package/lib/App.d.ts +10 -7
  4. package/lib/App.js +99 -44
  5. package/lib/controllers/ImageController.d.ts +4 -2
  6. package/lib/controllers/ImageController.js +27 -25
  7. package/lib/controllers/PluginController.d.ts +14 -0
  8. package/lib/controllers/PluginController.js +84 -0
  9. package/lib/controllers/PresetController.d.ts +21 -0
  10. package/lib/controllers/PresetController.js +169 -0
  11. package/lib/controllers/ProjectController.d.ts +11 -3
  12. package/lib/controllers/ProjectController.js +653 -536
  13. package/lib/{plugins/ProxyPlugin.d.ts → controllers/ProxyController.d.ts} +12 -11
  14. package/lib/controllers/ProxyController.js +273 -0
  15. package/lib/controllers/index.d.ts +3 -0
  16. package/lib/controllers/index.js +19 -25
  17. package/lib/env.js +33 -22
  18. package/lib/index.d.ts +2 -0
  19. package/lib/index.js +29 -28
  20. package/lib/makes/Controller.js +4 -8
  21. package/lib/makes/DI.d.ts +7 -0
  22. package/lib/makes/DI.js +27 -0
  23. package/lib/makes/Docker.js +299 -397
  24. package/lib/makes/FS.d.ts +1 -1
  25. package/lib/makes/FS.js +328 -301
  26. package/lib/makes/LineConvertStream.js +37 -40
  27. package/lib/makes/Logger.d.ts +5 -3
  28. package/lib/makes/Logger.js +18 -34
  29. package/lib/makes/Model.js +8 -12
  30. package/lib/makes/MySQL.js +6 -27
  31. package/lib/makes/Plugin.d.ts +1 -1
  32. package/lib/makes/Plugin.js +55 -37
  33. package/lib/makes/Preset.d.ts +46 -0
  34. package/lib/makes/Preset.js +33 -0
  35. package/lib/{models → makes}/Project.d.ts +23 -16
  36. package/lib/makes/Project.js +127 -0
  37. package/lib/makes/Repository.js +18 -21
  38. package/lib/makes/index.d.ts +3 -0
  39. package/lib/makes/index.js +23 -69
  40. package/lib/plugins/ElasticSearchPlugin.d.ts +18 -0
  41. package/lib/plugins/ElasticSearchPlugin.js +72 -0
  42. package/lib/plugins/LocaltunnelPlugin.d.ts +4 -4
  43. package/lib/plugins/LocaltunnelPlugin.js +256 -255
  44. package/lib/plugins/MaildevPlugin.d.ts +4 -2
  45. package/lib/plugins/MaildevPlugin.js +45 -41
  46. package/lib/plugins/MongodbPlugin.d.ts +6 -4
  47. package/lib/plugins/MongodbPlugin.js +303 -254
  48. package/lib/plugins/NgrokPlugin.d.ts +3 -4
  49. package/lib/plugins/NgrokPlugin.js +221 -230
  50. package/lib/plugins/PageKitePlugin.d.ts +3 -4
  51. package/lib/plugins/PageKitePlugin.js +150 -147
  52. package/lib/plugins/PostgresPlugin.d.ts +1 -1
  53. package/lib/plugins/PostgresPlugin.js +115 -88
  54. package/lib/plugins/ProxmoxPlugin.d.ts +1 -1
  55. package/lib/plugins/ProxmoxPlugin.js +50 -38
  56. package/lib/plugins/RedisPlugin.d.ts +10 -2
  57. package/lib/plugins/RedisPlugin.js +77 -39
  58. package/lib/plugins/index.d.ts +2 -6
  59. package/lib/plugins/index.js +25 -158
  60. package/lib/services/AppConfigService.d.ts +13 -3
  61. package/lib/services/AppConfigService.js +161 -71
  62. package/lib/services/AppEventsService.d.ts +5 -8
  63. package/lib/services/AppEventsService.js +26 -25
  64. package/lib/services/DockerService.d.ts +51 -0
  65. package/lib/services/DockerService.js +200 -0
  66. package/lib/services/LogService.d.ts +12 -0
  67. package/lib/services/LogService.js +38 -0
  68. package/lib/services/PluginService.d.ts +11 -0
  69. package/lib/services/PluginService.js +15 -0
  70. package/lib/services/PresetService.d.ts +14 -0
  71. package/lib/services/PresetService.js +64 -0
  72. package/lib/services/ProjectService.d.ts +12 -4
  73. package/lib/services/ProjectService.js +141 -78
  74. package/lib/services/index.d.ts +4 -0
  75. package/lib/services/index.js +21 -36
  76. package/lib/types/Config.d.ts +4 -3
  77. package/lib/types/Config.js +1 -4
  78. package/lib/types/EnvConfig.js +1 -4
  79. package/lib/types/index.js +16 -25
  80. package/lib/utils/buildOptions.js +5 -8
  81. package/lib/utils/demuxOutput.js +16 -22
  82. package/lib/utils/escapeRegExp.js +4 -7
  83. package/lib/utils/exec.d.ts +1 -1
  84. package/lib/utils/exec.js +38 -40
  85. package/lib/utils/fetch.js +45 -30
  86. package/lib/utils/followProgress.js +66 -77
  87. package/lib/utils/format-size-units.js +16 -16
  88. package/lib/utils/get-config.d.ts +1 -1
  89. package/lib/utils/get-config.js +13 -16
  90. package/lib/utils/get-cursor-position.js +22 -29
  91. package/lib/utils/image-build.js +35 -23
  92. package/lib/utils/index.d.ts +0 -4
  93. package/lib/utils/index.js +32 -235
  94. package/lib/utils/injectVariables.js +10 -13
  95. package/lib/utils/parse-table.js +20 -23
  96. package/lib/utils/set-config.d.ts +1 -1
  97. package/lib/utils/set-config.js +12 -15
  98. package/lib/utils/spawn.js +17 -20
  99. package/lib/utils/tty.js +2 -6
  100. package/lib/utils/volumeFormat.js +5 -12
  101. package/lib/utils/volumeParse.js +10 -13
  102. package/package.json +21 -47
  103. package/presets/bun/Dockerfile +11 -0
  104. package/presets/bun/config.json +3 -0
  105. package/presets/node/Dockerfile +4 -2
  106. package/presets/node/config.json +9 -7
  107. package/presets/php-apache/Dockerfile +40 -38
  108. package/presets/php-apache/config.json +10 -15
  109. package/presets/php-fpm/Dockerfile +21 -0
  110. package/presets/php-fpm/config.json +25 -0
  111. package/lib/models/Preset.d.ts +0 -19
  112. package/lib/models/Preset.js +0 -60
  113. package/lib/models/Project.js +0 -151
  114. package/lib/models/index.d.ts +0 -2
  115. package/lib/models/index.js +0 -27
  116. package/lib/plugins/MariadbPlugin.d.ts +0 -25
  117. package/lib/plugins/MariadbPlugin.js +0 -297
  118. package/lib/plugins/PresetPlugin.d.ts +0 -19
  119. package/lib/plugins/PresetPlugin.js +0 -164
  120. package/lib/plugins/ProjectPlugin.d.ts +0 -12
  121. package/lib/plugins/ProjectPlugin.js +0 -54
  122. package/lib/plugins/ProxyPlugin.js +0 -257
  123. package/lib/plugins/ServeoPlugin.d.ts +0 -36
  124. package/lib/plugins/ServeoPlugin.js +0 -260
  125. package/lib/plugins/TestPlugin.d.ts +0 -10
  126. package/lib/plugins/TestPlugin.js +0 -75
  127. package/lib/utils/promptConfirm.d.ts +0 -6
  128. package/lib/utils/promptConfirm.js +0 -21
  129. package/lib/utils/promptGroup.d.ts +0 -16
  130. package/lib/utils/promptGroup.js +0 -39
  131. package/lib/utils/promptSelect.d.ts +0 -12
  132. package/lib/utils/promptSelect.js +0 -47
  133. package/lib/utils/promptText.d.ts +0 -13
  134. package/lib/utils/promptText.js +0 -53
  135. package/plugins/mariadb/admin/conf/config.user.inc.php +0 -9
  136. package/plugins/serveo/Dockerfile +0 -17
@@ -1,418 +1,320 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Docker = void 0;
7
- var _dockerode = _interopRequireDefault(require("dockerode"));
8
- var _imageBuild = require("../utils/image-build");
9
- var _followProgress = require("../utils/followProgress");
10
- var _FS = require("./FS");
11
- var _Logger = require("./Logger");
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
- const docker = new _dockerode.default({
14
- socketPath: "/var/run/docker.sock"
7
+ const dockerode_1 = __importDefault(require("dockerode"));
8
+ const image_build_1 = require("../utils/image-build");
9
+ const followProgress_1 = require("../utils/followProgress");
10
+ const FS_1 = require("./FS");
11
+ const Logger_1 = require("./Logger");
12
+ const docker = new dockerode_1.default({
13
+ socketPath: "/var/run/docker.sock"
15
14
  });
16
15
  class Docker {
17
- static docker = docker;
18
- static async exec(name, args, tty = true) {
19
- const container = docker.getContainer(name);
20
- const exec = await container.exec({
21
- AttachStdin: true,
22
- AttachStdout: true,
23
- AttachStderr: tty,
24
- Tty: tty,
25
- Cmd: args
26
- });
27
- const stream = await exec.start({
28
- hijack: true,
29
- stdin: tty,
30
- Tty: tty
31
- });
32
- if (tty) {
33
- stream.setEncoding("utf-8");
34
- process.stdin.resume();
35
- if (process.stdin.setRawMode) {
36
- process.stdin.setRawMode(true);
37
- }
38
- process.stdin.setEncoding("utf-8");
39
- process.stdin.pipe(stream);
40
- stream.pipe(process.stdout);
41
- stream.on("error", err => {
42
- _Logger.Logger.error(err.message);
43
- });
44
- stream.on("end", async () => {
45
- process.stdin.setRawMode(false);
46
- });
47
- stream.on("end", async () => {
48
- process.exit();
49
- });
50
- }
51
-
52
- // setTimeout(() => {
53
- // Logger.info("Exit");
54
- //
55
- // process.exit();
56
- // }, 4000);
57
-
58
- return stream;
59
- }
60
- static async getImage(name) {
61
- // let images = await exec(
62
- // "docker image ls" +
63
- // " --filter=reference=\"" + name + "\""
64
- // ).then((res:any) => {
65
- // let pos = {
66
- // 0: "repository",
67
- // 1: "tag",
68
- // 2: "imageId",
69
- // 3: "created",
70
- // 4: "size"
71
- // };
72
- //
73
- // res = res.stdout.split("\n").filter((line) => {
74
- // return line.split(new RegExp("\\s\\s+")).filter((item) => {
75
- // return item !== "";
76
- // }).length > 0;
77
- // }).map((line, index) => {
78
- // if(index === 0) {
79
- // return null;
80
- // }
81
- //
82
- // let data = line.split(new RegExp("\\s\\s+"));
83
- //
84
- // let row = {};
85
- //
86
- // for(let i in data) {
87
- // let name = pos[i];
88
- //
89
- // row[name] = data[i];
90
- // }
91
- //
92
- // return row;
93
- // }).filter((line) => {
94
- // if(!line) {
95
- // return false;
96
- // }
97
- //
98
- // return true;
99
- // });
100
- //
101
- // return res;
102
- // }).catch((err:any) => {
103
- // console.error(err);
104
- //
105
- // return [];
106
- // });
107
- //
108
- // return lodash.get(images, "[0]", null);
109
-
110
- return docker.getImage(name);
111
- }
112
- static async getContainer(name) {
113
- const containers = await docker.listContainers({
114
- all: true,
115
- filters: {
116
- name: [name]
117
- }
118
- });
119
- const container = containers.find(container => {
120
- return container.Names.indexOf("/" + name) >= 0;
121
- });
122
- if (container) {
123
- return docker.getContainer(container.Id);
16
+ static async exec(name, args, tty = true) {
17
+ const container = docker.getContainer(name);
18
+ const exec = await container.exec({
19
+ AttachStdin: true,
20
+ AttachStdout: true,
21
+ AttachStderr: tty,
22
+ Tty: tty,
23
+ Cmd: args
24
+ });
25
+ const stream = await exec.start({
26
+ hijack: true,
27
+ stdin: tty,
28
+ Tty: tty
29
+ });
30
+ if (tty) {
31
+ stream.setEncoding("utf-8");
32
+ process.stdin.resume();
33
+ if (process.stdin.setRawMode) {
34
+ process.stdin.setRawMode(true);
35
+ }
36
+ process.stdin.setEncoding("utf-8");
37
+ process.stdin.pipe(stream);
38
+ stream.pipe(process.stdout);
39
+ stream.on("error", (err) => {
40
+ Logger_1.Logger.error(err.message);
41
+ });
42
+ stream.on("end", async () => {
43
+ process.stdin.setRawMode(false);
44
+ });
45
+ stream.on("end", async () => {
46
+ process.exit();
47
+ });
48
+ }
49
+ return stream;
124
50
  }
125
- return null;
126
- // let res = await utils.exec(
127
- // "docker container ls" +
128
- // " --filter=name=\"" + name + "\""
129
- // );
130
-
131
- // let containers = utils.parseTable(res.stdout, {
132
- // 0: "containerId",
133
- // 1: "image",
134
- // 2: "command",
135
- // 3: "created",
136
- // 4: "status",
137
- // 5: "ports",
138
- // 6: "names"
139
- // });
140
-
141
- // await new Promise((resolve) => {
142
- // docker.listContainers({
143
- // "all": true
144
- // }, (err, containers) => {
145
- // console.log(containers);
146
- //
147
- // resolve();
148
- // });
149
- // });
150
- }
151
-
152
- static async removeContainer(name) {
153
- const container = await Docker.getContainer(name);
154
- if (!container) {
155
- return;
51
+ static async getImage(name) {
52
+ return docker.getImage(name);
156
53
  }
157
- const {
158
- State: {
159
- Status
160
- }
161
- } = await container.inspect();
162
- if (Status === "running") {
163
- try {
164
- await container.stop();
165
- } catch (err) {
166
- _Logger.Logger.error("Docker.removeContainer: ", err.message);
167
- }
54
+ static async getContainer(name) {
55
+ const containers = await docker.listContainers({
56
+ all: true,
57
+ filters: {
58
+ name: [name]
59
+ }
60
+ });
61
+ const container = containers.find((container) => {
62
+ return container.Names.indexOf("/" + name) >= 0;
63
+ });
64
+ if (container) {
65
+ return docker.getContainer(container.Id);
66
+ }
67
+ return null;
168
68
  }
169
- try {
170
- await container.remove();
171
- } catch (err) {
172
- _Logger.Logger.error("Docker.removeContainer: ", err.message);
69
+ static async removeContainer(name) {
70
+ const container = await Docker.getContainer(name);
71
+ if (!container) {
72
+ return;
73
+ }
74
+ const { State: { Status } } = await container.inspect();
75
+ if (Status === "running") {
76
+ try {
77
+ await container.stop();
78
+ }
79
+ catch (err) {
80
+ Logger_1.Logger.error("Docker.removeContainer: ", err.message);
81
+ }
82
+ }
83
+ try {
84
+ await container.remove();
85
+ }
86
+ catch (err) {
87
+ Logger_1.Logger.error("Docker.removeContainer: ", err.message);
88
+ }
173
89
  }
174
- }
175
- static async getContainerList(options) {
176
- const {
177
- name,
178
- projectId
179
- } = options || {};
180
- const filters = {};
181
- if (name) {
182
- filters.name = [`/${name}`];
90
+ static async getContainerList(options) {
91
+ const { name, projectId } = options || {};
92
+ const filters = {};
93
+ if (name) {
94
+ filters.name = [`/${name}`];
95
+ }
96
+ if (projectId) {
97
+ if (!filters.label) {
98
+ filters.label = [];
99
+ }
100
+ filters.label.push(`projectId=${projectId}`);
101
+ }
102
+ return docker.listContainers({
103
+ all: true,
104
+ filters: JSON.stringify(filters)
105
+ });
183
106
  }
184
- if (projectId) {
185
- if (!filters.label) {
186
- filters.label = [];
187
- }
188
- filters.label.push(`projectId=${projectId}`);
107
+ static async attach(name) {
108
+ const container = await Docker.getContainer(name);
109
+ const stream = await container.attach({
110
+ logs: true,
111
+ stream: true,
112
+ hijack: true,
113
+ stdin: true,
114
+ stdout: true,
115
+ stderr: true
116
+ });
117
+ process.stdin.resume();
118
+ process.stdin.setEncoding("utf8");
119
+ process.stdin.setRawMode(true);
120
+ process.stdin.pipe(stream);
121
+ process.stdin.on("data", (data) => {
122
+ if (data.toString() === "\u0003") {
123
+ process.stdin.setRawMode(false);
124
+ }
125
+ });
126
+ stream.setEncoding("utf8");
127
+ stream.pipe(process.stdout);
128
+ const [width, height] = process.stdout.getWindowSize();
129
+ await container.resize({
130
+ w: width,
131
+ h: height
132
+ });
133
+ stream.on("end", async () => {
134
+ process.exit();
135
+ });
136
+ process.stdout.on("resize", () => {
137
+ const [width, height] = process.stdout.getWindowSize();
138
+ container.resize({
139
+ w: width,
140
+ h: height
141
+ });
142
+ });
189
143
  }
190
- return docker.listContainers({
191
- all: true,
192
- filters: JSON.stringify(filters)
193
- });
194
- }
195
- static async attach(name) {
196
- const container = await Docker.getContainer(name);
197
- const stream = await container.attach({
198
- logs: true,
199
- stream: true,
200
- hijack: true,
201
- stdin: true,
202
- stdout: true,
203
- stderr: true
204
- });
205
- process.stdin.resume();
206
- process.stdin.setEncoding("utf8");
207
- process.stdin.setRawMode(true);
208
- process.stdin.pipe(stream);
209
- process.stdin.on("data", data => {
210
- if (data.toString() === "\u0003") {
211
- process.stdin.setRawMode(false);
212
- }
213
- });
214
- stream.setEncoding("utf8");
215
- stream.pipe(process.stdout);
216
- const [width, height] = process.stdout.getWindowSize();
217
- await container.resize({
218
- w: width,
219
- h: height
220
- });
221
- stream.on("end", async () => {
222
- process.exit();
223
- });
224
- process.stdout.on("resize", () => {
225
- const [width, height] = process.stdout.getWindowSize();
226
- container.resize({
227
- w: width,
228
- h: height
229
- });
230
- });
231
- }
232
- static async attachStream(stream) {
233
- process.stdin.resume();
234
- process.stdin.setEncoding("utf8");
235
- process.stdin.pipe(stream);
236
- if (process.stdin.isTTY) {
237
- process.stdin.setRawMode(true);
144
+ static async attachStream(stream) {
145
+ process.stdin.resume();
146
+ process.stdin.setEncoding("utf8");
147
+ process.stdin.pipe(stream);
148
+ if (process.stdin.isTTY) {
149
+ process.stdin.setRawMode(true);
150
+ }
151
+ stream.setEncoding("utf8");
152
+ stream.pipe(process.stdout);
153
+ const end = () => {
154
+ process.stdin.pause();
155
+ process.stdin.unpipe(stream);
156
+ if (process.stdin.isTTY) {
157
+ process.stdin.setRawMode(false);
158
+ }
159
+ stream.unpipe(process.stdout);
160
+ };
161
+ await new Promise((resolve, reject) => {
162
+ stream.on("end", end);
163
+ stream.on("error", end);
164
+ stream.on("end", resolve);
165
+ stream.on("error", reject);
166
+ });
238
167
  }
239
- stream.setEncoding("utf8");
240
- stream.pipe(process.stdout);
241
- const end = () => {
242
- process.stdin.pause();
243
- process.stdin.unpipe(stream);
244
- if (process.stdin.isTTY) {
245
- process.stdin.setRawMode(false);
246
- }
247
- stream.unpipe(process.stdout);
248
- };
249
- await new Promise((resolve, reject) => {
250
- stream.on("end", end);
251
- stream.on("error", end);
252
- stream.on("end", resolve);
253
- stream.on("error", reject);
254
- });
255
- }
256
- static async imageExists(tag) {
257
- const image = docker.getImage(tag);
258
- try {
259
- await image.inspect();
260
- return true;
261
- } catch (ignore) {
262
- return false;
168
+ static async imageExists(tag) {
169
+ const image = docker.getImage(tag);
170
+ try {
171
+ await image.inspect();
172
+ return true;
173
+ }
174
+ catch (ignore) {
175
+ return false;
176
+ }
263
177
  }
264
- }
265
- static async imageLs(options) {
266
- const {
267
- tag,
268
- reference,
269
- labels
270
- } = options || {};
271
- const filters = {};
272
- if (reference) {
273
- filters.reference = [...(filters.reference || []), reference];
178
+ static async imageLs(options) {
179
+ const { tag, reference, labels } = options || {};
180
+ const filters = {};
181
+ if (reference) {
182
+ filters.reference = [
183
+ ...filters.reference || [],
184
+ reference
185
+ ];
186
+ }
187
+ if (tag) {
188
+ filters.reference = [
189
+ ...filters.reference || [],
190
+ tag
191
+ ];
192
+ }
193
+ if (labels) {
194
+ filters.label = [];
195
+ for (const i in labels) {
196
+ filters.label.push(`${i}=${labels[i]}`);
197
+ }
198
+ }
199
+ return docker.listImages({
200
+ filters: JSON.stringify(filters)
201
+ });
274
202
  }
275
- if (tag) {
276
- filters.reference = [...(filters.reference || []), tag];
203
+ static async imageBuild(options) {
204
+ await (0, image_build_1.imageBuild)(options);
277
205
  }
278
- if (labels) {
279
- filters.label = [];
280
- for (const i in labels) {
281
- filters.label.push(`${i}=${labels[i]}`);
282
- }
206
+ static async imageBuild2(options) {
207
+ const { tag, labels = {}, buildArgs = {}, context, src } = options;
208
+ const files = await FS_1.FS.readdirFiles(context, {
209
+ recursive: true
210
+ });
211
+ const stream = await docker.buildImage({
212
+ context,
213
+ src: files
214
+ }, {
215
+ t: tag,
216
+ labels,
217
+ dockerfile: src,
218
+ buildargs: buildArgs
219
+ });
220
+ return stream;
283
221
  }
284
- return docker.listImages({
285
- filters: JSON.stringify(filters)
286
- });
287
- }
288
- static async imageBuild(options) {
289
- // await docker.buildImage({
290
- // context:
291
- // });
292
-
293
- await (0, _imageBuild.imageBuild)(options);
294
- }
295
- static async imageBuild2(options) {
296
- const {
297
- tag,
298
- labels = {},
299
- buildArgs = {},
300
- context,
301
- src
302
- } = options;
303
- const files = await _FS.FS.readdirFiles(context, {
304
- recursive: true
305
- });
306
- const stream = await docker.buildImage({
307
- context,
308
- src: files
309
- }, {
310
- t: tag,
311
- labels,
312
- dockerfile: src,
313
- buildargs: buildArgs
314
- });
315
- return stream;
316
- }
317
- static async imageRm(name) {
318
- const image = await docker.getImage(name);
319
- if (!image) {
320
- return;
222
+ static async imageRm(name) {
223
+ const image = await docker.getImage(name);
224
+ if (!image) {
225
+ return;
226
+ }
227
+ await image.remove();
321
228
  }
322
- await image.remove();
323
- }
324
- static async pullImage(tag) {
325
- const isExists = await this.imageExists(tag);
326
- if (isExists) {
327
- return;
229
+ static async pullImage(tag) {
230
+ const isExists = await this.imageExists(tag);
231
+ if (isExists) {
232
+ return;
233
+ }
234
+ const stream = await docker.pull(tag, {});
235
+ await (0, followProgress_1.followProgress)(stream);
328
236
  }
329
- const stream = await docker.pull(tag, {});
330
- await (0, _followProgress.followProgress)(stream);
331
- }
332
- static async createContainer(options) {
333
- const {
334
- name,
335
- tty,
336
- image,
337
- projectId,
338
- restart,
339
- extraHosts,
340
- networkMode,
341
- links = [],
342
- env = {},
343
- volumes = [],
344
- ports = [],
345
- cmd = []
346
- } = options;
347
- return await docker.createContainer({
348
- name,
349
- Image: image,
350
- Hostname: name,
351
- Labels: {
352
- ...(projectId ? {
353
- projectId
354
- } : {})
355
- },
356
- AttachStdin: true,
357
- AttachStdout: true,
358
- AttachStderr: true,
359
- OpenStdin: true,
360
- StdinOnce: false,
361
- Tty: tty,
362
- Cmd: cmd,
363
- Env: Object.keys(env).map(key => {
364
- const value = env[key];
365
- return `${key}=${value}`;
366
- }),
367
- ExposedPorts: ports.reduce((res, value) => {
368
- const [,, containerPort] = /(\d+):(\d+)/.exec(value) || [];
369
- if (containerPort) {
370
- res[`${containerPort}/tcp`] = {};
237
+ static async createContainer(options) {
238
+ const { name, tty, image, projectId, restart, extraHosts, networkMode, links = [], env = {}, volumes = [], ports = [], cmd = [] } = options;
239
+ const network = docker.getNetwork("workspace");
240
+ try {
241
+ await network.inspect();
371
242
  }
372
- return res;
373
- }, {}),
374
- HostConfig: {
375
- NetworkMode: networkMode,
376
- ExtraHosts: extraHosts,
377
- ...(restart ? {
378
- RestartPolicy: {
379
- Name: restart
380
- }
381
- } : {}),
382
- Binds: volumes,
383
- PortBindings: ports.reduce((res, value) => {
384
- const [, hostPort, containerPort] = /(\d+):(\d+)/.exec(value) || [];
385
- if (hostPort && containerPort) {
386
- res[`${containerPort}/tcp`] = [{
387
- HostPort: hostPort
388
- }];
389
- }
390
- return res;
391
- }, {})
392
- },
393
- NetworkingConfig: {
394
- EndpointsConfig: networkMode === "host" ? {} : {
395
- workspace: {
396
- Links: links
397
- }
243
+ catch (err) {
244
+ if (err.statusCode === 404) {
245
+ await docker.createNetwork({
246
+ Name: "workspace"
247
+ });
248
+ }
249
+ }
250
+ await this.pullImage(image);
251
+ return await docker.createContainer({
252
+ name,
253
+ Image: image,
254
+ Hostname: name,
255
+ Labels: {
256
+ ...projectId ? { projectId } : {}
257
+ },
258
+ AttachStdin: true,
259
+ AttachStdout: true,
260
+ AttachStderr: true,
261
+ OpenStdin: true,
262
+ StdinOnce: false,
263
+ Tty: tty,
264
+ Cmd: cmd,
265
+ Env: Object.keys(env).map((key) => {
266
+ const value = env[key];
267
+ return `${key}=${value}`;
268
+ }),
269
+ ExposedPorts: ports.reduce((res, value) => {
270
+ const [, , containerPort] = /(\d+):(\d+)/.exec(value) || [];
271
+ if (containerPort) {
272
+ res[`${containerPort}/tcp`] = {};
273
+ }
274
+ return res;
275
+ }, {}),
276
+ HostConfig: {
277
+ NetworkMode: networkMode,
278
+ ExtraHosts: extraHosts,
279
+ ...restart ? {
280
+ RestartPolicy: {
281
+ Name: restart
282
+ }
283
+ } : {},
284
+ Binds: volumes,
285
+ PortBindings: ports.reduce((res, value) => {
286
+ const [, hostPort, containerPort] = /(\d+):(\d+)/.exec(value) || [];
287
+ if (hostPort && containerPort) {
288
+ res[`${containerPort}/tcp`] = [
289
+ { HostPort: hostPort }
290
+ ];
291
+ }
292
+ return res;
293
+ }, {})
294
+ },
295
+ NetworkingConfig: {
296
+ EndpointsConfig: networkMode === "host" ? {} : {
297
+ workspace: {
298
+ Links: links
299
+ }
300
+ }
301
+ }
302
+ });
303
+ }
304
+ static async containerRun(options, attach = false) {
305
+ const container = await Docker.createContainer(options);
306
+ if (attach) {
307
+ const stdout = await container.attach({
308
+ stream: true,
309
+ stdin: true,
310
+ stdout: true,
311
+ stderr: true
312
+ });
313
+ stdout.pipe(process.stdout);
398
314
  }
399
- }
400
- });
401
- }
402
- static async containerRun(options, attach = false) {
403
- const container = await Docker.createContainer(options);
404
- if (attach) {
405
- const stdout = await container.attach({
406
- // logs: true,
407
- stream: true,
408
- stdin: true,
409
- stdout: true,
410
- stderr: true
411
- });
412
- stdout.pipe(process.stdout);
315
+ await container.start();
316
+ return container;
413
317
  }
414
- await container.start();
415
- return container;
416
- }
417
318
  }
418
- exports.Docker = Docker;
319
+ exports.Docker = Docker;
320
+ Docker.docker = docker;