1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const image_1 = require("./image");
|
4 | /**
|
5 | * Class representing container execution
|
6 | */
|
7 | class Exec {
|
8 | /**
|
9 | * Create an execution
|
10 | * @param {Modem} modem Modem to connect to the remote service
|
11 | * @param {Container} container Container that owns the execution (optional)
|
12 | * @param {string} id Id of the execution
|
13 | */
|
14 | constructor(modem, container, id) {
|
15 | this.data = {};
|
16 | this.modem = modem;
|
17 | this.container = container;
|
18 | this.id = id;
|
19 | }
|
20 | /**
|
21 | * Create an exec instance in a container
|
22 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-container
|
23 | * @param {Object} opts Query params in the request (optional)
|
24 | * @return {Promise} Promise return the new exec instance
|
25 | */
|
26 | create(opts) {
|
27 | const call = {
|
28 | path: `/containers/${this.container.id}/exec?`,
|
29 | method: 'POST',
|
30 | options: opts,
|
31 | statusCodes: {
|
32 | 200: true,
|
33 | 201: true,
|
34 | 404: 'no such container',
|
35 | 409: 'container is paused',
|
36 | 500: 'server error'
|
37 | }
|
38 | };
|
39 | return new Promise((resolve, reject) => {
|
40 | this.modem.dial(call, (err, conf) => {
|
41 | if (err)
|
42 | return reject(err);
|
43 | const exec = new Exec(this.modem, this.container, conf.Id);
|
44 | exec.data = conf;
|
45 | resolve(exec);
|
46 | });
|
47 | });
|
48 | }
|
49 | /**
|
50 | * Start an exec instance
|
51 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/exec-start
|
52 | * @param {Object} opts Query params in the request (optional)
|
53 | * @return {Promise} Promise return the stream to the execution
|
54 | */
|
55 | start(opts = {}) {
|
56 | const call = {
|
57 | path: `/exec/${this.id}/start?`,
|
58 | method: 'POST',
|
59 | options: opts,
|
60 | isStream: true,
|
61 | hijack: opts.hijack,
|
62 | openStdin: opts.stdin,
|
63 | statusCodes: {
|
64 | 200: true,
|
65 | 404: 'no such exec instance',
|
66 | 409: 'container is paused'
|
67 | }
|
68 | };
|
69 | return new Promise((resolve, reject) => {
|
70 | this.modem.dial(call, (err, stream) => {
|
71 | if (err)
|
72 | return reject(err);
|
73 | resolve(stream);
|
74 | });
|
75 | });
|
76 | }
|
77 | /**
|
78 | * Resize an exec instance
|
79 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/exec-resize
|
80 | * @param {Object} opts Query params in the request (optional)
|
81 | * @return {Promise} Promise return the result
|
82 | */
|
83 | resize(opts) {
|
84 | const call = {
|
85 | path: `/exec/${this.id}/resize?`,
|
86 | method: 'POST',
|
87 | options: opts,
|
88 | statusCodes: {
|
89 | 201: true,
|
90 | 404: 'no such exec instance'
|
91 | }
|
92 | };
|
93 | return new Promise((resolve, reject) => {
|
94 | this.modem.dial(call, (err) => {
|
95 | if (err)
|
96 | return reject(err);
|
97 | resolve();
|
98 | });
|
99 | });
|
100 | }
|
101 | /**
|
102 | * Get status of an exec instance
|
103 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/exec-inspect
|
104 | * The reason why this module isn't called inspect is because that interferes with the inspect utility of node.
|
105 | * @param {Object} opts Query params in the request (optional)
|
106 | * @return {Promise} Promise return the exec instance
|
107 | */
|
108 | status(opts) {
|
109 | const call = {
|
110 | path: `/exec/${this.id}/json?`,
|
111 | method: 'GET',
|
112 | options: opts,
|
113 | statusCodes: {
|
114 | 200: true,
|
115 | 404: 'no such exec instance',
|
116 | 500: 'server error'
|
117 | }
|
118 | };
|
119 | return new Promise((resolve, reject) => {
|
120 | this.modem.dial(call, (err, conf) => {
|
121 | if (err)
|
122 | return reject(err);
|
123 | const exec = new Exec(this.modem, this.container, conf.Id);
|
124 | exec.data = conf;
|
125 | resolve(exec);
|
126 | });
|
127 | });
|
128 | }
|
129 | }
|
130 | exports.Exec = Exec;
|
131 | /**
|
132 | * Class representing container execution management
|
133 | */
|
134 | class ExecManager {
|
135 | /**
|
136 | * Create an execution
|
137 | * @param {Modem} modem Modem to connect to the remote service
|
138 | * @param {Container} container Container that owns the execution (optional)
|
139 | * @param {string} id Id of the execution
|
140 | */
|
141 | constructor(modem, container) {
|
142 | this.modem = modem;
|
143 | this.container = container;
|
144 | }
|
145 | /**
|
146 | * Get a Exec object
|
147 | * @param {id} string ID of the exec
|
148 | * @return {Exec}
|
149 | */
|
150 | get(id) {
|
151 | return new Exec(this.modem, this.container, id);
|
152 | }
|
153 | /**
|
154 | * Create an exec instance in a container
|
155 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-container
|
156 | * @param {Object} opts Query params in the request (optional)
|
157 | * @return {Promise} Promise return the new exec instance
|
158 | */
|
159 | create(opts) {
|
160 | const call = {
|
161 | path: `/containers/${this.container.id}/exec?`,
|
162 | method: 'POST',
|
163 | options: opts,
|
164 | statusCodes: {
|
165 | 200: true,
|
166 | 201: true,
|
167 | 404: 'no such container',
|
168 | 409: 'container is paused',
|
169 | 500: 'server error'
|
170 | }
|
171 | };
|
172 | return new Promise((resolve, reject) => {
|
173 | this.modem.dial(call, (err, conf) => {
|
174 | if (err)
|
175 | return reject(err);
|
176 | const exec = new Exec(this.modem, this.container, conf.Id);
|
177 | exec.data = conf;
|
178 | resolve(exec);
|
179 | });
|
180 | });
|
181 | }
|
182 | }
|
183 | exports.ExecManager = ExecManager;
|
184 | /**
|
185 | * Class representing container filesystem
|
186 | */
|
187 | class ContainerFs {
|
188 | /**
|
189 | * Create an container filesystem object
|
190 | * @param {Modem} modem Modem to connect to the remote service
|
191 | * @param {Container} container Container that owns the filesystem (optional)
|
192 | */
|
193 | constructor(modem, container) {
|
194 | this.modem = modem;
|
195 | this.container = container;
|
196 | }
|
197 | /**
|
198 | * Get the info about the filesystem of the container
|
199 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/retrieving-information-about-files-and-folders-in-a-container
|
200 | * @param {Object} opts Query params in the request (optional)
|
201 | * @return {Promise} Promise returning the info about the filesystem
|
202 | */
|
203 | info(opts) {
|
204 | const call = {
|
205 | path: `/containers/${this.container.id}/archive?`,
|
206 | method: 'HEAD',
|
207 | isStream: true,
|
208 | options: opts,
|
209 | statusCodes: {
|
210 | 200: true,
|
211 | 404: 'bad request',
|
212 | 500: 'server error'
|
213 | }
|
214 | };
|
215 | return new Promise((resolve, reject) => {
|
216 | this.modem.dial(call, (err, info) => {
|
217 | if (err)
|
218 | return reject(err);
|
219 | resolve(info);
|
220 | });
|
221 | });
|
222 | }
|
223 | /**
|
224 | * Get a tar archive of a resource in the filesystem of a container
|
225 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/get-an-archive-of-a-filesystem-resource-in-a-container
|
226 | * @param {Object} opts Query params in the request (optional)
|
227 | * @return {Promise} Promise returning the result as a stream to the tar file
|
228 | */
|
229 | get(opts = {}) {
|
230 | const call = {
|
231 | path: `/containers/${this.container.id}/archive?path=${opts.path}&`,
|
232 | method: 'GET',
|
233 | isStream: true,
|
234 | options: opts,
|
235 | statusCodes: {
|
236 | 200: true,
|
237 | 400: 'bad request',
|
238 | 404: 'no such container',
|
239 | 500: 'server error'
|
240 | }
|
241 | };
|
242 | return new Promise((resolve, reject) => {
|
243 | this.modem.dial(call, (err, stream) => {
|
244 | if (err)
|
245 | return reject(err);
|
246 | resolve(stream);
|
247 | });
|
248 | });
|
249 | }
|
250 | /**
|
251 | * Put an extracted tar archive in the filesystem of a container
|
252 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/extract-an-archive-of-files-or-folders-to-a-directory-in-a-container
|
253 | * @param {Object} opts Query params in the request (optional)
|
254 | * @return {Promise} Promise returning the result
|
255 | */
|
256 | put(file, opts) {
|
257 | const call = {
|
258 | path: `/containers/${this.container.id}/archive?`,
|
259 | method: 'PUT',
|
260 | options: opts,
|
261 | isStream: true,
|
262 | file: file,
|
263 | statusCodes: {
|
264 | 200: true,
|
265 | 400: 'bad request',
|
266 | 403: 'permission denied',
|
267 | 404: 'no such container',
|
268 | 500: 'server error'
|
269 | }
|
270 | };
|
271 | return new Promise((resolve, reject) => {
|
272 | this.modem.dial(call, (err, res) => {
|
273 | if (err)
|
274 | return reject(err);
|
275 | resolve(res);
|
276 | });
|
277 | });
|
278 | }
|
279 | }
|
280 | exports.ContainerFs = ContainerFs;
|
281 | /**
|
282 | * Class representing a container
|
283 | */
|
284 | class Container {
|
285 | /**
|
286 | * Create an container object
|
287 | * @param {Modem} modem Modem to connect to the remote service
|
288 | * @param {string} id Container id
|
289 | */
|
290 | constructor(modem, id) {
|
291 | this.Warnings = [];
|
292 | this.data = {};
|
293 | this.modem = modem;
|
294 | this.id = id;
|
295 | this.fs = new ContainerFs(this.modem, this);
|
296 | this.exec = new ExecManager(this.modem, this);
|
297 | }
|
298 | /**
|
299 | * Get low-level information on a container
|
300 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-a-container
|
301 | * The reason why this module isn't called inspect is because that interferes with the inspect utility of node.
|
302 | * @param {Object} opts Query params in the request (optional)
|
303 | * @return {Promise} Promise return the container
|
304 | */
|
305 | status(opts) {
|
306 | const call = {
|
307 | path: `/containers/${this.id}/json?`,
|
308 | method: 'GET',
|
309 | options: opts,
|
310 | statusCodes: {
|
311 | 200: true,
|
312 | 404: 'no such container',
|
313 | 500: 'server error'
|
314 | }
|
315 | };
|
316 | return new Promise((resolve, reject) => {
|
317 | this.modem.dial(call, (err, conf) => {
|
318 | if (err)
|
319 | return reject(err);
|
320 | const container = new Container(this.modem, this.id);
|
321 | container.data = conf;
|
322 | resolve(container);
|
323 | });
|
324 | });
|
325 | }
|
326 | /**
|
327 | * Get list of processes (ps) inside a container. Not supported in Windows.
|
328 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-processes-running-inside-a-container
|
329 | * @param {Object} opts Query params in the request (optional)
|
330 | * @return {Promise} Promise return the list of processes
|
331 | */
|
332 | top(opts) {
|
333 | const call = {
|
334 | path: `/containers/${this.id}/top?`,
|
335 | method: 'GET',
|
336 | options: opts,
|
337 | statusCodes: {
|
338 | 200: true,
|
339 | 404: 'no such container',
|
340 | 500: 'server error'
|
341 | }
|
342 | };
|
343 | return new Promise((resolve, reject) => {
|
344 | this.modem.dial(call, (err, processes) => {
|
345 | if (err)
|
346 | return reject(err);
|
347 | resolve(processes);
|
348 | });
|
349 | });
|
350 | }
|
351 | /**
|
352 | * Get stdout and stderr logs from a container
|
353 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/get-container-logs
|
354 | * @param {Object} opts Query params in the request (optional)
|
355 | * @return {Promise} Promise returning the concatenated logs
|
356 | */
|
357 | logs(opts) {
|
358 | const call = {
|
359 | path: `/containers/${this.id}/logs?`,
|
360 | method: 'GET',
|
361 | options: opts,
|
362 | isStream: true,
|
363 | statusCodes: {
|
364 | 101: true,
|
365 | 200: true,
|
366 | 404: 'no such container',
|
367 | 500: 'server error'
|
368 | }
|
369 | };
|
370 | return new Promise((resolve, reject) => {
|
371 | this.modem.dial(call, (err, logs) => {
|
372 | if (err)
|
373 | return reject(err);
|
374 | resolve(logs);
|
375 | });
|
376 | });
|
377 | }
|
378 | /**
|
379 | * Get changes on a container's filesystem
|
380 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-changes-on-a-container-s-filesystem
|
381 | * @return {Promise} Promise returning the changes
|
382 | */
|
383 | changes() {
|
384 | const call = {
|
385 | path: `/containers/${this.id}/changes?`,
|
386 | method: 'GET',
|
387 | options: {},
|
388 | statusCodes: {
|
389 | 200: true,
|
390 | 404: 'no such container',
|
391 | 500: 'server error'
|
392 | }
|
393 | };
|
394 | return new Promise((resolve, reject) => {
|
395 | this.modem.dial(call, (err, changes) => {
|
396 | if (err)
|
397 | return reject(err);
|
398 | resolve(changes);
|
399 | });
|
400 | });
|
401 | }
|
402 | /**
|
403 | * Export the content of a container
|
404 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/export-a-container
|
405 | * @param {Object} opts Query params in the request (optional)
|
406 | * @return {Promise} Promise returning the content of the tar file as a stream or as a string
|
407 | */
|
408 | export(opts = {}) {
|
409 | const call = {
|
410 | path: `/containers/${this.id}/export?`,
|
411 | method: 'GET',
|
412 | options: opts,
|
413 | isStream: !!opts.stream,
|
414 | statusCodes: {
|
415 | 200: true,
|
416 | 404: 'no such container',
|
417 | 500: 'server error'
|
418 | }
|
419 | };
|
420 | return new Promise((resolve, reject) => {
|
421 | this.modem.dial(call, (err, tarStream) => {
|
422 | if (err)
|
423 | return reject(err);
|
424 | if (!opts.stream)
|
425 | return resolve(tarStream);
|
426 | const res = [];
|
427 | tarStream.on('data', (chunk) => {
|
428 | res.push(chunk.toString());
|
429 | });
|
430 | tarStream.on('end', () => {
|
431 | resolve(res.join(''));
|
432 | });
|
433 | });
|
434 | });
|
435 | }
|
436 | /**
|
437 | * Get the stats of a container, either by a live stream or the current state
|
438 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/export-a-container
|
439 | * @param {Object} opts Query params in the request (optional)
|
440 | * @return {Promise} Promise returning the stats, in a stream or string
|
441 | */
|
442 | stats(opts) {
|
443 | const call = {
|
444 | path: `/containers/${this.id}/stats?`,
|
445 | method: 'GET',
|
446 | options: opts,
|
447 | isStream: true,
|
448 | statusCodes: {
|
449 | 200: true,
|
450 | 404: 'no such container',
|
451 | 500: 'server error'
|
452 | }
|
453 | };
|
454 | return new Promise((resolve, reject) => {
|
455 | this.modem.dial(call, (err, stats) => {
|
456 | if (err)
|
457 | return reject(err);
|
458 | resolve(stats);
|
459 | });
|
460 | });
|
461 | }
|
462 | /**
|
463 | * Resize the TTY for a container. You must restart the container to make the resize take effect.
|
464 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/resize-a-container-tty
|
465 | * @param {Object} opts Query params in the request (optional)
|
466 | * @return {Promise} Promise returning the response
|
467 | */
|
468 | resize(opts) {
|
469 | const call = {
|
470 | path: `/containers/${this.id}/resize?`,
|
471 | method: 'GET',
|
472 | options: opts,
|
473 | statusCodes: {
|
474 | 200: true,
|
475 | 404: 'no such container',
|
476 | 500: 'server error'
|
477 | }
|
478 | };
|
479 | return new Promise((resolve, reject) => {
|
480 | this.modem.dial(call, (err, res) => {
|
481 | if (err)
|
482 | return reject(err);
|
483 | resolve(res);
|
484 | });
|
485 | });
|
486 | }
|
487 | /**
|
488 | * Start a container
|
489 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/start-a-container
|
490 | * @param {Object} opts Query params in the request (optional)
|
491 | * @return {Promise} Promise returning the container
|
492 | */
|
493 | start(opts = {}) {
|
494 | const call = {
|
495 | path: `/containers/${this.id}/start?`,
|
496 | method: 'POST',
|
497 | options: opts,
|
498 | statusCodes: {
|
499 | 204: true,
|
500 | 304: true,
|
501 | 404: 'no such container',
|
502 | 500: 'server error'
|
503 | }
|
504 | };
|
505 | return new Promise((resolve, reject) => {
|
506 | this.modem.dial(call, (err) => {
|
507 | if (err)
|
508 | return reject(err);
|
509 | resolve(this);
|
510 | });
|
511 | });
|
512 | }
|
513 | /**
|
514 | * Stop a container
|
515 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/stop-a-container
|
516 | * @param {Object} opts Query params in the request (optional)
|
517 | * @return {Promise} Promise returning the container
|
518 | */
|
519 | stop(opts) {
|
520 | const call = {
|
521 | path: `/containers/${this.id}/stop?`,
|
522 | method: 'POST',
|
523 | options: opts,
|
524 | statusCodes: {
|
525 | 204: true,
|
526 | 304: true,
|
527 | 404: 'no such container',
|
528 | 500: 'server error'
|
529 | }
|
530 | };
|
531 | return new Promise((resolve, reject) => {
|
532 | this.modem.dial(call, (err) => {
|
533 | if (err)
|
534 | return reject(err);
|
535 | resolve(this);
|
536 | });
|
537 | });
|
538 | }
|
539 | /**
|
540 | * Restart a container
|
541 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/restart-a-container
|
542 | * @param {Object} opts Query params in the request (optional)
|
543 | * @return {Promise} Promise returning the container
|
544 | */
|
545 | restart(opts) {
|
546 | const call = {
|
547 | path: `/containers/${this.id}/restart?`,
|
548 | method: 'POST',
|
549 | options: opts,
|
550 | statusCodes: {
|
551 | 204: true,
|
552 | 404: 'no such container',
|
553 | 500: 'server error'
|
554 | }
|
555 | };
|
556 | return new Promise((resolve, reject) => {
|
557 | this.modem.dial(call, (err) => {
|
558 | if (err)
|
559 | return reject(err);
|
560 | resolve(this);
|
561 | });
|
562 | });
|
563 | }
|
564 | /**
|
565 | * Kill a container
|
566 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/kill-a-container
|
567 | * @param {Object} opts Query params in the request (optional)
|
568 | * @return {Promise} Promise returning the container
|
569 | */
|
570 | kill(opts) {
|
571 | const call = {
|
572 | path: `/containers/${this.id}/kill?`,
|
573 | method: 'POST',
|
574 | options: opts,
|
575 | statusCodes: {
|
576 | 204: true,
|
577 | 404: 'no such container',
|
578 | 500: 'server error'
|
579 | }
|
580 | };
|
581 | return new Promise((resolve, reject) => {
|
582 | this.modem.dial(call, (err) => {
|
583 | if (err)
|
584 | return reject(err);
|
585 | resolve(this);
|
586 | });
|
587 | });
|
588 | }
|
589 | /**
|
590 | * Update configuration a container.
|
591 | * Docs says you can do it for more than one, but doesn't exaplin how, so let's leave it in only one
|
592 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/update-a-container
|
593 | * @param {Object} opts Query params in the request (optional)
|
594 | * @return {Promise} Promise returning the container
|
595 | */
|
596 | update(opts) {
|
597 | const call = {
|
598 | path: `/containers/${this.id}/update?`,
|
599 | method: 'POST',
|
600 | options: opts,
|
601 | statusCodes: {
|
602 | 200: true,
|
603 | 400: 'bad request',
|
604 | 404: 'no such container',
|
605 | 500: 'server error'
|
606 | }
|
607 | };
|
608 | return new Promise((resolve, reject) => {
|
609 | this.modem.dial(call, (err, warnings) => {
|
610 | const container = new Container(this.modem, this.id);
|
611 | container.Warnings = warnings;
|
612 | if (err)
|
613 | return reject(err);
|
614 | resolve(container);
|
615 | });
|
616 | });
|
617 | }
|
618 | /**
|
619 | * Rename a container.
|
620 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/rename-a-container
|
621 | * @param {Object} opts Query params in the request (optional)
|
622 | * @return {Promise} Promise returning the container
|
623 | */
|
624 | rename(opts) {
|
625 | const call = {
|
626 | path: `/containers/${this.id}/rename?`,
|
627 | method: 'POST',
|
628 | options: opts,
|
629 | statusCodes: {
|
630 | 204: true,
|
631 | 404: 'no such container',
|
632 | 409: 'name already taken',
|
633 | 500: 'server error'
|
634 | }
|
635 | };
|
636 | return new Promise((resolve, reject) => {
|
637 | this.modem.dial(call, (err) => {
|
638 | if (err)
|
639 | return reject(err);
|
640 | resolve(this);
|
641 | });
|
642 | });
|
643 | }
|
644 | /**
|
645 | * Pause a container.
|
646 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/pause-a-container
|
647 | * @return {Promise} Promise returning the container
|
648 | */
|
649 | pause() {
|
650 | const call = {
|
651 | path: `/containers/${this.id}/pause?`,
|
652 | method: 'POST',
|
653 | options: {},
|
654 | statusCodes: {
|
655 | 204: true,
|
656 | 404: 'no such container',
|
657 | 500: 'server error'
|
658 | }
|
659 | };
|
660 | return new Promise((resolve, reject) => {
|
661 | this.modem.dial(call, (err) => {
|
662 | if (err)
|
663 | return reject(err);
|
664 | resolve(this);
|
665 | });
|
666 | });
|
667 | }
|
668 | /**
|
669 | * Unpause a container.
|
670 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/unpause-a-container
|
671 | * @return {Promise} Promise returning the container
|
672 | */
|
673 | unpause() {
|
674 | const call = {
|
675 | path: `/containers/${this.id}/unpause?`,
|
676 | method: 'POST',
|
677 | options: {},
|
678 | statusCodes: {
|
679 | 204: true,
|
680 | 404: 'no such container',
|
681 | 500: 'server error'
|
682 | }
|
683 | };
|
684 | return new Promise((resolve, reject) => {
|
685 | this.modem.dial(call, (err) => {
|
686 | if (err)
|
687 | return reject(err);
|
688 | resolve(this);
|
689 | });
|
690 | });
|
691 | }
|
692 | /**
|
693 | * Attach to a container.
|
694 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container
|
695 | * @param {Object} opts Query params in the request (optional)
|
696 | * @return {Promise} Promise returning the container
|
697 | */
|
698 | attach(opts = {}) {
|
699 | const call = {
|
700 | path: `/containers/${this.id}/attach?`,
|
701 | method: 'POST',
|
702 | isStream: true,
|
703 | openStdin: opts.stdin,
|
704 | options: opts,
|
705 | statusCodes: {
|
706 | 101: true,
|
707 | 200: true,
|
708 | 400: 'bad request',
|
709 | 404: 'no such container',
|
710 | 500: 'server error'
|
711 | }
|
712 | };
|
713 | return new Promise((resolve, reject) => {
|
714 | this.modem.dial(call, (err, stream) => {
|
715 | if (err)
|
716 | return reject(err);
|
717 | resolve([stream, this]);
|
718 | });
|
719 | });
|
720 | }
|
721 | /**
|
722 | * Attach to a container using websocket.
|
723 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container-websocket
|
724 | * @param {Object} opts Query params in the request (optional)
|
725 | * @return {Promise} Promise returning the stream and the container
|
726 | */
|
727 | wsattach(opts) {
|
728 | const call = {
|
729 | path: `/containers/${this.id}/attach/ws?`,
|
730 | method: 'GET',
|
731 | options: opts,
|
732 | statusCodes: {
|
733 | 200: true,
|
734 | 400: 'bad request',
|
735 | 404: 'no such container',
|
736 | 500: 'server error'
|
737 | }
|
738 | };
|
739 | return new Promise((resolve, reject) => {
|
740 | this.modem.dial(call, (err, stream) => {
|
741 | if (err)
|
742 | return reject(err);
|
743 | resolve([stream, this]);
|
744 | });
|
745 | });
|
746 | }
|
747 | /**
|
748 | * Block until a container stops, returning exit code
|
749 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/wait-a-container
|
750 | * @return {Promise} Promise returning the exit code
|
751 | */
|
752 | wait() {
|
753 | const call = {
|
754 | path: `/containers/${this.id}/wait?`,
|
755 | method: 'POST',
|
756 | options: {},
|
757 | statusCodes: {
|
758 | 200: true,
|
759 | 404: 'no such container',
|
760 | 500: 'server error'
|
761 | }
|
762 | };
|
763 | return new Promise((resolve, reject) => {
|
764 | this.modem.dial(call, (err, code) => {
|
765 | if (err)
|
766 | return reject(err);
|
767 | resolve(code);
|
768 | });
|
769 | });
|
770 | }
|
771 | /**
|
772 | * Remove a container.
|
773 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/remove-a-container
|
774 | * @param {Object} opts Query params in the request (optional)
|
775 | * @return {Promise} Promise returning nothing
|
776 | */
|
777 | delete(opts) {
|
778 | const call = {
|
779 | path: `/containers/${this.id}?`,
|
780 | method: 'DELETE',
|
781 | options: opts,
|
782 | statusCodes: {
|
783 | 204: true,
|
784 | 400: 'bad request',
|
785 | 404: 'no such container',
|
786 | 500: 'server error'
|
787 | }
|
788 | };
|
789 | return new Promise((resolve, reject) => {
|
790 | this.modem.dial(call, (err) => {
|
791 | if (err)
|
792 | return reject(err);
|
793 | resolve();
|
794 | });
|
795 | });
|
796 | }
|
797 | /**
|
798 | * Commit container into an image
|
799 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-new-image-from-a-container-s-changes
|
800 | * @param {Object} opts Query params in the request (optional)
|
801 | * @return {Promise} Promise returning the new image
|
802 | */
|
803 | commit(opts = {}) {
|
804 | opts.container = this.id;
|
805 | const call = {
|
806 | path: `/commit?`,
|
807 | method: 'POST',
|
808 | options: opts,
|
809 | statusCodes: {
|
810 | 201: true,
|
811 | 404: 'no such container',
|
812 | 500: 'server error'
|
813 | }
|
814 | };
|
815 | return new Promise((resolve, reject) => {
|
816 | this.modem.dial(call, (err, res) => {
|
817 | if (err)
|
818 | return reject(err);
|
819 | resolve(new image_1.Image(this.modem, res.Id.replace('sha256:', '')));
|
820 | });
|
821 | });
|
822 | }
|
823 | }
|
824 | exports.Container = Container;
|
825 | class default_1 {
|
826 | constructor(modem) {
|
827 | this.modem = modem;
|
828 | }
|
829 | /**
|
830 | * Get a Container object
|
831 | * @param {id} string ID of the container
|
832 | * @return {Container}
|
833 | */
|
834 | get(id) {
|
835 | return new Container(this.modem, id);
|
836 | }
|
837 | /**
|
838 | * Get the list of containers
|
839 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-containers
|
840 | * @param {Object} opts Query params in the request (optional)
|
841 | * @return {Promise} Promise returning the result as a list of containers
|
842 | */
|
843 | list(opts) {
|
844 | const call = {
|
845 | path: '/containers/json?',
|
846 | method: 'GET',
|
847 | options: opts,
|
848 | statusCodes: {
|
849 | 200: true,
|
850 | 400: 'bad request',
|
851 | 500: 'server error'
|
852 | }
|
853 | };
|
854 | return new Promise((resolve, reject) => {
|
855 | this.modem.dial(call, (err, containers) => {
|
856 | if (err)
|
857 | return reject(err);
|
858 | resolve(containers.map((conf) => {
|
859 | const container = new Container(this.modem, conf.Id);
|
860 | container.data = conf;
|
861 | return container;
|
862 | }));
|
863 | });
|
864 | });
|
865 | }
|
866 | /**
|
867 | * Create a container
|
868 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-container
|
869 | * @param {Object} opts Query params in the request (optional)
|
870 | * @return {Promise} Promise return the new container
|
871 | */
|
872 | create(opts) {
|
873 | const call = {
|
874 | path: '/containers/create?',
|
875 | method: 'POST',
|
876 | options: opts,
|
877 | statusCodes: {
|
878 | 200: true,
|
879 | 201: true,
|
880 | 400: 'bad request',
|
881 | 404: 'no such image',
|
882 | 406: 'impossible to attach',
|
883 | 500: 'server error'
|
884 | }
|
885 | };
|
886 | return new Promise((resolve, reject) => {
|
887 | this.modem.dial(call, (err, conf) => {
|
888 | if (err)
|
889 | return reject(err);
|
890 | const container = new Container(this.modem, conf.Id);
|
891 | container.data = conf;
|
892 | resolve(container);
|
893 | });
|
894 | });
|
895 | }
|
896 | /**
|
897 | * Prune a container
|
898 | * https://docs.docker.com/engine/api/v1.25/#operation/ContainerPrune
|
899 | * @param {Object} opts Query params in the request (optional)
|
900 | * @return {Promise} Promise returning the container
|
901 | */
|
902 | prune(opts) {
|
903 | const call = {
|
904 | path: `/containers/prune`,
|
905 | method: 'POST',
|
906 | options: opts,
|
907 | statusCodes: {
|
908 | 200: true,
|
909 | 500: 'server error'
|
910 | }
|
911 | };
|
912 | return new Promise((resolve, reject) => {
|
913 | this.modem.dial(call, (err, res) => {
|
914 | if (err)
|
915 | return reject(err);
|
916 | resolve(res);
|
917 | });
|
918 | });
|
919 | }
|
920 | }
|
921 | exports.default = default_1;
|
922 | //# sourceMappingURL=container.js.map |
\ | No newline at end of file |