-
Notifications
You must be signed in to change notification settings - Fork 268
/
libpod-dockerode.ts
863 lines (787 loc) · 22.3 KB
/
libpod-dockerode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/**********************************************************************
* Copyright (C) 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { ManifestCreateOptions, ManifestInspectInfo } from '@podman-desktop/api';
import Dockerode from 'dockerode';
import type { ImageInfo, PodmanListImagesOptions } from '../api/image-info.js';
export interface PodContainerInfo {
Id: string;
Names: string;
Status: string;
}
export interface PodInfo {
Cgroup: string;
Containers: PodContainerInfo[];
Created: string;
Id: string;
InfraId: string;
Labels: { [key: string]: string };
Name: string;
Namespace: string;
Networks: string[];
Status: string;
}
export interface PodInspectInfo {
CgroupParent: string;
CgroupPath: string;
Containers: PodContainerInfo[];
Created: string;
Hostname: string;
Id: string;
InfraContainerId: string;
memory_limit: number;
memory_swap: number;
Name: string;
Namespace: string;
NumContainers: number;
security_opt: string[];
SharedNamespaces: string[];
State: string;
volumes_from: string[];
}
export interface PlayKubePodInfo {
ContainerErrors: string[];
Containers: string[];
Id: string;
InitContainers: string[];
Logs: string[];
}
export interface PlayKubeInfo {
Pods: PlayKubePodInfo[];
RmReport: { Err: string; Id: string }[];
Secrets: { CreateReport: { ID: string } }[];
StopReport: { Err: string; Id: string }[];
Volumes: { Name: string }[];
}
export interface PodCreatePortOptions {
host_ip: string;
container_port: number;
host_port: number;
protocol: string;
range: number;
}
export interface PodCreateOptions {
name: string;
portmappings?: PodCreatePortOptions[];
labels?: { [key: string]: string };
}
export interface ContainerCreateMountOption {
Name?: string;
Type: string;
Source: string;
Destination: string;
Driver?: string;
RW: boolean;
Propagation: string;
Options?: string[];
}
export interface ContainerCreateHealthConfigOption {
Test?: string[];
Interval?: number;
Timeout?: number;
StartPeriod?: number;
Retries?: number;
}
export interface ContainerCreatePortMappingOption {
container_port: number;
host_ip?: string;
host_port?: number;
protocol?: string;
range?: number;
}
export interface ContainerCreateNetNSOption {
nsmode: string;
value?: string;
}
export interface ContainerCreateOptions {
command?: string[];
entrypoint?: string | string[];
env?: { [key: string]: string };
pod?: string;
hostname?: string;
image?: string;
name?: string;
mounts?: Array<ContainerCreateMountOption>;
user?: string;
labels?: { [label: string]: string };
work_dir?: string;
portmappings?: Array<ContainerCreatePortMappingOption>;
stop_timeout?: number;
healthconfig?: ContainerCreateHealthConfigOption;
restart_policy?: string;
restart_tries?: number;
remove?: boolean;
seccomp_policy?: string;
seccomp_profile_path?: string;
cap_add?: Array<string>;
cap_drop?: Array<string>;
privileged?: boolean;
netns?: ContainerCreateNetNSOption;
read_only_filesystem?: boolean;
dns_server?: Array<Array<number>>;
hostadd?: Array<string>;
userns?: string;
}
export interface PodRemoveOptions {
force: boolean;
}
export interface PodmanContainerInfo {
Id: string;
Names: string[];
ImageID: string;
Image: string;
Created: string;
State: string;
StartedAt: number;
Command: string[];
Labels: { [label: string]: string };
Ports: { host_ip: string; container_port: number; host_port: number; range?: string; protocol: string }[];
}
export interface Info {
host: Host;
store: Store;
registries: Registries;
plugins: Plugins;
version: Version;
}
export interface Host {
arch: string;
buildahVersion: string;
cgroupManager: string;
cgroupVersion: string;
cgroupControllers: unknown[];
conmon: Conmon;
cpus: number;
cpuUtilization: CpuUtilization;
databaseBackend: string;
distribution: Distribution;
eventLogger: string;
hostname: string;
idMappings: IdMappings;
kernel: string;
logDriver: string;
memFree: number;
memTotal: number;
networkBackend: string;
ociRuntime: OciRuntime;
os: string;
remoteSocket: RemoteSocket;
serviceIsRemote: boolean;
security: Security;
slirp4netns: Slirp4netns;
swapFree: number;
swapTotal: number;
uptime: string;
linkmode: string;
}
export interface Conmon {
package: string;
path: string;
version: string;
}
export interface CpuUtilization {
userPercent: number;
systemPercent: number;
idlePercent: number;
}
export interface Distribution {
distribution: string;
variant: string;
version: string;
}
export interface IdMappings {
gidmap: Gidmap[];
uidmap: Uidmap[];
}
export interface Gidmap {
container_id: number;
host_id: number;
size: number;
}
export interface Uidmap {
container_id: number;
host_id: number;
size: number;
}
export interface OciRuntime {
name: string;
package: string;
path: string;
version: string;
}
export interface RemoteSocket {
path: string;
exists: boolean;
}
export interface Security {
apparmorEnabled: boolean;
capabilities: string;
rootless: boolean;
seccompEnabled: boolean;
seccompProfilePath: string;
selinuxEnabled: boolean;
}
export interface Slirp4netns {
executable: string;
package: string;
version: string;
}
export interface Store {
configFile: string;
containerStore: ContainerStore;
graphDriverName: string;
graphOptions: GraphOptions;
graphRoot: string;
graphRootAllocated: number;
graphRootUsed: number;
graphStatus: GraphStatus;
imageCopyTmpDir: string;
imageStore: ImageStore;
runRoot: string;
volumePath: string;
transientStore: boolean;
}
export interface ContainerStore {
number: number;
paused: number;
running: number;
stopped: number;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface GraphOptions {}
export interface GraphStatus {
'Backing Filesystem': string;
'Native Overlay Diff': string;
'Supports d_type': string;
'Using metacopy': string;
}
export interface ImageStore {
number: number;
}
export interface Registries {
search: string[];
}
export interface Plugins {
volume: string[];
network: string[];
log: string[];
authorization: unknown;
}
export interface Version {
APIVersion: string;
Version: string;
GoVersion: string;
GitCommit: string;
BuiltTime: string;
Built: number;
OsArch: string;
Os: string;
}
export interface GetImagesOptions {
names: string[];
}
// API of libpod that we want to expose on our side
export interface LibPod {
createPod(podOptions: PodCreateOptions): Promise<{ Id: string }>;
createPodmanContainer(containerCreateOptions: ContainerCreateOptions): Promise<{ Id: string; Warnings: string[] }>;
listPods(): Promise<PodInfo[]>;
listPodmanContainers(opts?: { all: boolean }): Promise<PodmanContainerInfo[]>;
prunePods(): Promise<void>;
podmanAttach(containerId: string): Promise<NodeJS.ReadWriteStream>;
getPodInspect(podId: string): Promise<PodInspectInfo>;
startPod(podId: string): Promise<void>;
stopPod(podId: string): Promise<void>;
removePod(podId: string, options?: PodRemoveOptions): Promise<void>;
restartPod(podId: string): Promise<void>;
generateKube(names: string[]): Promise<string>;
playKube(yamlContentFilePath: string): Promise<PlayKubeInfo>;
pruneAllImages(dangling: boolean): Promise<void>;
podmanInfo(): Promise<Info>;
getImages(options: GetImagesOptions): Promise<NodeJS.ReadableStream>;
podmanListImages(options?: PodmanListImagesOptions): Promise<ImageInfo[]>;
podmanCreateManifest(manifestOptions: ManifestCreateOptions): Promise<{ engineId: string; Id: string }>;
podmanInspectManifest(manifestName: string): Promise<ManifestInspectInfo>;
}
// tweak Dockerode by adding the support of libpod API
// WARNING: make sure to not override existing functions
export class LibpodDockerode {
// setup the libpod API
enhancePrototypeWithLibPod(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const prototypeOfDockerode = Dockerode.prototype as any;
// add listPodmanContainers
prototypeOfDockerode.listPodmanContainers = function (opts?: { all: boolean }): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/containers/json?',
method: 'GET',
options: opts,
statusCodes: {
200: true,
400: 'bad parameter',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add createPodmanContainer
prototypeOfDockerode.createPodmanContainer = function (
containerCreateOptions: ContainerCreateOptions,
): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/containers/create',
method: 'POST',
options: containerCreateOptions,
statusCodes: {
201: true,
204: true,
400: 'bad parameter in request',
404: 'no such container',
409: 'status conflict',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add listImages
prototypeOfDockerode.podmanListImages = function (options?: PodmanListImagesOptions): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/images/json',
method: 'GET',
options: options,
statusCodes: {
200: true,
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add listPods
prototypeOfDockerode.listPods = function (): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/pods/json',
method: 'GET',
options: {},
statusCodes: {
200: true,
400: 'bad parameter',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add attach
prototypeOfDockerode.podmanAttach = function (containerId: string): Promise<unknown> {
const optsf = {
path: `/v4.2.0/libpod/containers/${containerId}/attach?stdin=true&stdout=true&stderr=true&`,
method: 'POST',
isStream: true,
openStdin: true,
allowEmpty: true,
statusCodes: {
200: true,
404: 'no such container',
500: 'server error',
},
options: {},
};
// patch the modem to not send any data. By default dockerode send query parameters as JSON payload
// but podman REST API will then echo the response, so send empty data '' instead
const originalBuildRequest = this.modem.buildRequest;
this.modem.buildRequest = function (
options: unknown,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
context: any,
data: unknown,
callback: unknown,
): Promise<unknown> {
if (context.allowEmpty && context.path.includes('/attach?')) {
data = '';
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return originalBuildRequest.call(this, options, context, data, callback);
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, stream: unknown) => {
if (err) {
return reject(err);
}
resolve(stream);
});
});
};
// add pruneAllImages
prototypeOfDockerode.pruneAllImages = function (): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/images/prune?all=true&', // this works
// For some reason the below doesn't work? TODO / help / fixme
// options: {all: 'true'}, // this doesn't work
method: 'POST',
statusCodes: {
200: true,
400: 'bad parameter',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add createPod
prototypeOfDockerode.createPod = function (podOptions: PodCreateOptions): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/pods/create',
method: 'POST',
options: podOptions,
statusCodes: {
201: true,
204: true,
400: 'bad parameter in request',
409: 'status conflict',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add getPodInspect
prototypeOfDockerode.getPodInspect = function (podId: string): Promise<unknown> {
const optsf = {
path: `/v4.2.0/libpod/pods/${podId}/json`,
method: 'GET',
statusCodes: {
200: true,
204: true,
404: 'no such pod',
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add startPod
prototypeOfDockerode.startPod = function (podId: string): Promise<unknown> {
const optsf = {
path: `/v4.2.0/libpod/pods/${podId}/start?`,
method: 'POST',
statusCodes: {
200: true,
204: true,
304: 'pod already stopped',
404: 'no such pod',
409: 'unexpected error',
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.modem.dial(optsf, (err: any, data: unknown) => {
if (err) {
// check that err.json is a JSON
if (err?.statusCode === 409 && err?.json?.Errs) {
return reject(err.json.Errs.join(' '));
}
return reject(err);
}
resolve(data);
});
});
};
// add stopPod
prototypeOfDockerode.stopPod = function (podId: string): Promise<unknown> {
const optsf = {
path: `/v4.2.0/libpod/pods/${podId}/stop?`,
method: 'POST',
statusCodes: {
200: true,
204: true,
304: 'pod already stopped',
404: 'no such pod',
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add restartPod
prototypeOfDockerode.restartPod = function (podId: string): Promise<unknown> {
const optsf = {
path: `/v4.2.0/libpod/pods/${podId}/restart?`,
method: 'POST',
statusCodes: {
200: true,
204: true,
304: 'pod already stopped',
404: 'no such pod',
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add removePod
prototypeOfDockerode.removePod = function (podId: string, options?: { force: boolean }): Promise<unknown> {
const optsf = {
path: `/v4.2.0/libpod/pods/${podId}?`,
method: 'DELETE',
statusCodes: {
200: true,
204: true,
304: 'pod already stopped',
404: 'no such pod',
500: 'server error',
},
options: options || {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add prunePods
prototypeOfDockerode.prunePods = function (): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/pods/prune',
method: 'POST',
statusCodes: {
200: true,
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add generateKube
prototypeOfDockerode.generateKube = function (names: string[]): Promise<unknown> {
// transform array into a list of queries
const queries = names
.map(name => {
return `names=${name}`;
})
.join('&');
const path = `/v4.2.0/libpod/generate/kube?${queries}`;
const optsf = {
path,
method: 'GET',
options: {},
statusCodes: {
200: true,
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
if (Buffer.isBuffer(data)) {
resolve((data as Buffer).toString());
} else {
resolve(data);
}
});
});
};
// add playKube
prototypeOfDockerode.playKube = function (yamlContentFilePath: string): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/play/kube',
method: 'POST',
file: yamlContentFilePath,
statusCodes: {
200: true,
204: true,
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// info
prototypeOfDockerode.podmanInfo = function (): Promise<unknown> {
const optsf = {
path: '/v4.2.0/libpod/info',
method: 'GET',
statusCodes: {
200: true,
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// info
prototypeOfDockerode.getImages = function (options: GetImagesOptions): Promise<NodeJS.ReadableStream> {
// let's create the query using the names list.
// N.B: last ? will be cut by the modem dial call
const query = `names=${options.names.join('&names=')}?`;
const optsf = {
path: `/images/get?${query}`,
method: 'GET',
options: {},
abortSignal: undefined,
isStream: true,
statusCodes: {
200: true,
400: 'bad parameter',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: NodeJS.ReadableStream) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add createManifest
prototypeOfDockerode.podmanCreateManifest = function (manifestOptions: ManifestCreateOptions): Promise<unknown> {
// make sure encodeURI component for the name ex. domain.com/foo/bar:latest
const encodedManifestName = encodeURIComponent(manifestOptions.name);
const optsf = {
path: `/v4.2.0/libpod/manifests/${encodedManifestName}`,
method: 'POST',
options: manifestOptions,
statusCodes: {
201: true,
400: 'bad parameter in request',
404: 'no such image',
500: 'server error',
},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// add inspectManifest
prototypeOfDockerode.podmanInspectManifest = function (manifestName: string): Promise<unknown> {
// make sure encodeURI component for the name ex. domain.com/foo/bar:latest
const encodedManifestName = encodeURIComponent(manifestName);
const optsf = {
path: `/v4.2.0/libpod/manifests/${encodedManifestName}/json`,
method: 'GET',
// Match the status codes from https://docs.podman.io/en/latest/_static/api.html#tag/manifests/operation/ManifestInspectLibpod
statusCodes: {
200: true,
404: 'no such manifest',
500: 'server error',
},
options: {},
};
return new Promise((resolve, reject) => {
this.modem.dial(optsf, (err: unknown, data: unknown) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
}
}