Repository navigation
Expand file tree
/
Copy pathcontainer.mbt
More file actions
738 lines (724 loc) · 18.3 KB
/
Copy pathcontainer.mbt
File metadata and controls
738 lines (724 loc) · 18.3 KB
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
///|
/// Turning one container description into the argument list `podman` sees.
/// This is the heart of the tool: every Compose field that maps onto a podman
/// flag is translated here.
///|
/// A parsed volume/mount description.
struct Mount {
kind : String
source : String?
target : String
options : Array[String]
}
///|
/// The argument list for `podman create`/`run`, following the reference
/// implementation's ordering closely enough that the output is comparable.
pub fn container_to_args(
compose : Compose,
cnt : @yaml.Yaml,
detached : Bool,
no_deps : Bool,
) -> Array[String] raise Error {
let args : Array[String] = []
let name = y_str_or(y_get(cnt, "name").unwrap_or(y_null()), "")
args.push("--name=" + name)
if detached {
args.push("-d")
}
match y_get(cnt, "pod") {
Some(pod) =>
match y_str(pod) {
Some(p) if p.length() > 0 => args.push("--pod=" + p)
_ => ()
}
None => ()
}
if !no_deps {
let dep_names = y_list_str(y_get(cnt, "_deps").unwrap_or(y_null()))
if dep_names.length() > 0 {
let csv : Array[String] = []
for d in dep_names {
match compose.container_names_by_service.get(d) {
Some(names) =>
for n in names {
csv.push(n)
}
None => ()
}
}
if csv.length() > 0 {
args.push("--requires=" + csv.join(","))
}
}
}
for item in y_list_str(y_get(cnt, "security_opt").unwrap_or(y_null())) {
args.push("--security-opt")
args.push(item)
}
for item in y_list_str(y_get(cnt, "annotations").unwrap_or(y_null())) {
args.push("--annotation")
args.push(item)
}
if y_bool(y_get(cnt, "read_only").unwrap_or(y_null())).unwrap_or(false) {
args.push("--read-only")
}
match y_get(cnt, "http_proxy") {
Some(v) => if y_bool(v) is Some(false) { args.push("--http-proxy=false") }
None => ()
}
for label in y_list_str(y_get(cnt, "labels").unwrap_or(y_null())) {
args.push("--label")
args.push(label)
}
push_pairs(
args,
"--cap-add",
y_list_str(y_get(cnt, "cap_add").unwrap_or(y_null())),
)
push_pairs(
args,
"--cap-drop",
y_list_str(y_get(cnt, "cap_drop").unwrap_or(y_null())),
)
push_pairs(
args,
"--group-add",
y_list_str(y_get(cnt, "group_add").unwrap_or(y_null())),
)
push_pairs(
args,
"--device",
y_list_str(y_get(cnt, "devices").unwrap_or(y_null())),
)
push_pairs(
args,
"--device-cgroup-rule",
y_list_str(y_get(cnt, "device_cgroup_rules").unwrap_or(y_null())),
)
push_pairs(args, "--dns", y_list_str(y_get(cnt, "dns").unwrap_or(y_null())))
push_pairs(
args,
"--dns-opt",
y_list_str(y_get(cnt, "dns_opt").unwrap_or(y_null())),
)
push_pairs(
args,
"--dns-search",
y_list_str(y_get(cnt, "dns_search").unwrap_or(y_null())),
)
// env_file entries are read and expanded inline.
for env_file in y_list(y_get(cnt, "env_file").unwrap_or(y_null())) {
let (path, required) = env_file_path(env_file)
if path.length() == 0 {
continue
}
let full = path_abspath(compose.dirname, path)
if !@fs.path_exists(full) {
if required {
raise fail("env file at \{full} does not exist")
}
continue
}
for entry in compose_env_file(compose, full) {
args.push("-e")
args.push(entry.0 + "=" + entry.1)
}
}
for item in y_env_list(y_get(cnt, "environment").unwrap_or(y_null())) {
if item.contains_char('=') {
args.push("-e")
args.push(item)
} else {
match compose.environ.get(item) {
Some(value) => {
args.push("-e")
args.push(item + "=" + value)
}
None => ()
}
}
}
for item in y_list_str(y_get(cnt, "tmpfs").unwrap_or(y_null())) {
args.push("--tmpfs")
args.push(item)
}
for mount in container_mounts(compose, cnt) {
for arg in mount_arg(compose, cnt, mount) {
args.push(arg)
}
}
for arg in network_args(compose, cnt) {
args.push(arg)
}
match y_get(cnt, "logging") {
Some(logging) =>
match y_map(logging) {
Some(lm) => {
let driver = match lm.get("driver") {
Some(d) => y_str_or(d, "k8s-file")
None => "k8s-file"
}
args.push("--log-driver=" + driver)
match lm.get("options") {
Some(options) =>
match y_map(options) {
Some(om) =>
for entry in om {
args.push(
"--log-opt=" + entry.0 + "=" + y_str_or(entry.1, ""),
)
}
None => ()
}
None => ()
}
}
None => ()
}
None => ()
}
for secret in y_list(y_get(cnt, "secrets").unwrap_or(y_null())) {
for arg in secret_args(secret) {
args.push(arg)
}
}
push_pairs(
args,
"--add-host",
y_list_str(y_get(cnt, "extra_hosts").unwrap_or(y_null())),
)
push_pairs(
args,
"--expose",
y_list_str(y_get(cnt, "expose").unwrap_or(y_null())),
)
if y_bool(y_get(cnt, "publishall").unwrap_or(y_null())).unwrap_or(false) {
args.push("-P")
}
for port in y_list(y_get(cnt, "ports").unwrap_or(y_null())) {
match port_to_str(port) {
Some(spec) => {
args.push("-p")
args.push(spec)
}
None => ()
}
}
match y_get(cnt, "userns_mode") {
Some(v) =>
match y_str(v) {
Some(s) => {
args.push("--userns")
args.push(s)
}
None => ()
}
None => ()
}
match y_get(cnt, "user") {
Some(v) =>
match y_str(v) {
Some(s) => {
args.push("-u")
args.push(s)
}
None => ()
}
None => ()
}
match y_get(cnt, "working_dir") {
Some(v) =>
match y_str(v) {
Some(s) => {
args.push("-w")
args.push(s)
}
None => ()
}
None => ()
}
push_opt(args, "--hostname", y_get(cnt, "hostname"))
push_opt(args, "--shm-size", y_get(cnt, "shm_size"))
if y_bool(y_get(cnt, "stdin_open").unwrap_or(y_null())).unwrap_or(false) {
args.push("-i")
}
push_opt(args, "--stop-signal", y_get(cnt, "stop_signal"))
match y_get(cnt, "stop_grace_period") {
Some(v) =>
match y_str(v) {
Some(s) =>
match str_to_seconds(s) {
Some(seconds) => {
args.push("--stop-timeout")
args.push(seconds.to_string())
}
None => ()
}
None => ()
}
None => ()
}
match y_get(cnt, "sysctls") {
Some(v) =>
match v {
@yaml.Yaml::Map(m) =>
for entry in m {
args.push("--sysctl")
args.push(entry.0 + "=" + y_str_or(entry.1, ""))
}
@yaml.Yaml::Array(_) => push_pairs(args, "--sysctl", y_list_str(v))
_ => ()
}
None => ()
}
if y_bool(y_get(cnt, "tty").unwrap_or(y_null())).unwrap_or(false) {
args.push("--tty")
}
if y_bool(y_get(cnt, "privileged").unwrap_or(y_null())).unwrap_or(false) {
args.push("--privileged")
}
push_opt(args, "--pid", y_get(cnt, "pid"))
match y_get(cnt, "pull_policy") {
Some(v) =>
match y_str(v) {
Some(policy) => if policy != "build" { args.push("--pull=" + policy) }
None => ()
}
None => ()
}
push_opt(args, "--restart", y_get(cnt, "restart"))
for ulimit in ulimit_args(y_get(cnt, "ulimits").unwrap_or(y_null())) {
args.push("--ulimit")
args.push(ulimit)
}
for arg in resource_args(cnt) {
args.push(arg)
}
if y_bool(y_get(cnt, "init").unwrap_or(y_null())).unwrap_or(false) {
args.push("--init")
}
push_opt(args, "--init-path", y_get(cnt, "init-path"))
push_opt(args, "--ipc", y_get(cnt, "ipc"))
match y_get(cnt, "entrypoint") {
Some(v) =>
match v {
@yaml.Yaml::String(s) => {
args.push("--entrypoint")
args.push(json_string_array(shell_split(s)))
}
@yaml.Yaml::Array(_) => {
args.push("--entrypoint")
args.push(json_string_array(y_list_str(v)))
}
_ => ()
}
None => ()
}
push_opt(args, "--platform", y_get(cnt, "platform"))
push_opt(args, "--runtime", y_get(cnt, "runtime"))
push_opt(args, "--cpuset-cpus", y_get(cnt, "cpuset"))
for arg in healthcheck_args(y_get(cnt, "healthcheck")) {
args.push(arg)
}
for uidmap in y_list_str(y_get(cnt, "x-podman.uidmaps").unwrap_or(y_null())) {
args.push("--uidmap")
args.push(uidmap)
}
for gidmap in y_list_str(y_get(cnt, "x-podman.gidmaps").unwrap_or(y_null())) {
args.push("--gidmap")
args.push(gidmap)
}
if y_bool(y_get(cnt, "x-podman.no_hosts").unwrap_or(y_null())).unwrap_or(
false,
) {
args.push("--no-hosts")
}
match y_get(cnt, "x-podman.passwd") {
Some(passwd) => {
// `--passwd` defaults to true, so only an explicit false turns it off.
let enabled = y_bool(passwd).unwrap_or(true)
let flag = if enabled { "true" } else { "false" }
args.push("--passwd=" + flag)
}
None => ()
}
// The image, then the command.
match y_get(cnt, "x-podman.rootfs") {
Some(rootfs) =>
match y_str(rootfs) {
Some(s) => {
args.push("--rootfs")
args.push(s)
}
None => ()
}
None =>
match y_get(cnt, "image") {
Some(image) => args.push(y_str_or(image, ""))
None => ()
}
}
match y_get(cnt, "command") {
Some(v) =>
match v {
@yaml.Yaml::String(s) =>
for part in shell_split(s) {
args.push(part)
}
_ =>
for part in y_list_str(v) {
args.push(part)
}
}
None => ()
}
args
}
///|
/// Append `flag value` for every value in `values`.
fn push_pairs(
args : Array[String],
flag : String,
values : Array[String],
) -> Unit {
for v in values {
args.push(flag)
args.push(v)
}
}
///|
/// Append `flag value` when a scalar is present.
fn push_opt(args : Array[String], flag : String, value : @yaml.Yaml?) -> Unit {
match value {
Some(v) =>
match y_str(v) {
Some(s) => {
args.push(flag)
args.push(s)
}
None => ()
}
None => ()
}
}
///|
/// Split an `env_file` entry into its path and whether it is required.
fn env_file_path(entry : @yaml.Yaml) -> (String, Bool) {
match entry {
@yaml.Yaml::String(s) => (s, true)
@yaml.Yaml::Map(m) => {
let path = match m.get("path") {
Some(p) => y_str_or(p, "")
None => ""
}
let required = match m.get("required") {
Some(r) => y_bool(r).unwrap_or(true)
None => true
}
(path, required)
}
_ => ("", true)
}
}
///|
/// Render a `ports` entry as `[[ip:]published:]target[/proto]`.
fn port_to_str(port : @yaml.Yaml) -> String? {
match port {
@yaml.Yaml::String(s) => Some(s)
@yaml.Yaml::Integer(i) => Some(i.to_string())
@yaml.Yaml::Map(m) => {
let target = match m.get("target") {
Some(t) => y_str_or(t, "")
None => ""
}
if target.length() == 0 {
return None
}
let protocol = match m.get("protocol") {
Some(p) => y_str_or(p, "tcp")
None => "tcp"
}
let out = StringBuilder()
match m.get("host_ip") {
Some(ip) =>
match y_str(ip) {
Some(s) if s.length() > 0 => {
out.write_string(s)
out.write_char(':')
}
_ => ()
}
None => ()
}
match m.get("published") {
Some(published) =>
match y_str(published) {
Some(s) if s.length() > 0 => {
out.write_string(s)
out.write_char(':')
}
_ => ()
}
None => ()
}
out.write_string(target)
out.write_char('/')
out.write_string(protocol)
Some(out.to_string())
}
_ => None
}
}
///|
/// Secret references become `--secret` flags.
fn secret_args(secret : @yaml.Yaml) -> Array[String] {
let args : Array[String] = []
match secret {
@yaml.Yaml::String(s) => {
args.push("--secret")
args.push(s)
}
@yaml.Yaml::Map(m) => {
let source = match m.get("source") {
Some(s) => y_str_or(s, "")
None => ""
}
if source.length() == 0 {
return args
}
let spec = StringBuilder()
spec.write_string(source)
match m.get("target") {
Some(t) =>
match y_str(t) {
Some(target) if target.length() > 0 =>
spec.write_string(",target=" + target)
_ => ()
}
None => ()
}
args.push("--secret")
args.push(spec.to_string())
}
_ => ()
}
args
}
///|
/// `ulimits` in either the scalar, list or mapping form.
fn ulimit_args(v : @yaml.Yaml) -> Array[String] {
let out : Array[String] = []
match v {
@yaml.Yaml::Null | @yaml.Yaml::BadValue => ()
@yaml.Yaml::String(s) => out.push(s)
@yaml.Yaml::Map(m) =>
for entry in m {
out.push(entry.0 + "=" + norm_ulimit(entry.1))
}
@yaml.Yaml::Array(_) =>
for entry in y_dict(v) {
out.push(entry.0 + "=" + norm_ulimit(entry.1))
}
_ => ()
}
out
}
///|
fn norm_ulimit(v : @yaml.Yaml) -> String {
match v {
@yaml.Yaml::Map(m) => {
let soft = match m.get("soft") {
Some(s) => y_str_or(s, "")
None => ""
}
let hard = match m.get("hard") {
Some(h) => y_str_or(h, "")
None => soft
}
let soft = if soft.length() == 0 { hard } else { soft }
soft + ":" + hard
}
_ => y_str_or(v, "")
}
}
///|
/// CPU and memory limits from `deploy.resources.limits`.
fn resource_args(cnt : @yaml.Yaml) -> Array[String] {
let out : Array[String] = []
match y_get(cnt, "deploy") {
Some(deploy) =>
match y_get(deploy, "resources") {
Some(resources) =>
match y_get(resources, "limits") {
Some(limits) => {
match y_get(limits, "cpus") {
Some(cpus) =>
match y_str(cpus) {
Some(s) => {
out.push("--cpus")
out.push(s)
}
None => ()
}
None => ()
}
match y_get(limits, "memory") {
Some(memory) =>
match y_str(memory) {
Some(s) => {
out.push("--memory")
out.push(s)
}
None => ()
}
None => ()
}
}
None => ()
}
None => ()
}
None => ()
}
out
}
///|
/// The `healthcheck` block.
fn healthcheck_args(healthcheck : @yaml.Yaml?) -> Array[String] {
let out : Array[String] = []
let hm = match healthcheck.bind(y_map) {
Some(m) => m
None => return out
}
let disable = match hm.get("disable") {
Some(d) => y_bool(d).unwrap_or(false)
None => false
}
if disable {
out.push("--no-healthcheck")
} else {
match hm.get("test") {
Some(hc) => apply_healthcheck_test(out, hc)
None => ()
}
}
push_opt(out, "--health-interval", hm.get("interval"))
push_opt(out, "--health-timeout", hm.get("timeout"))
push_opt(out, "--health-start-period", hm.get("start_period"))
push_opt(out, "--health-startup-interval", hm.get("start_interval"))
match hm.get("retries") {
Some(r) =>
match y_str(r) {
Some(s) => {
out.push("--health-retries")
out.push(s)
}
None => ()
}
None => ()
}
out
}
///|
/// The `test:` of a healthcheck: a shell string, or a `CMD`/`CMD-SHELL` list.
fn apply_healthcheck_test(out : Array[String], hc : @yaml.Yaml) -> Unit {
match hc {
@yaml.Yaml::String(s) => {
out.push("--health-cmd")
out.push(json_string_array(["CMD-SHELL", s]))
}
@yaml.Yaml::Array(_) => {
let parts = y_list_str(hc)
if parts.length() > 0 {
let kind = parts[0]
let rest : Array[String] = []
for i in 1..<parts.length() {
rest.push(parts[i])
}
if kind == "NONE" {
out.push("--no-healthcheck")
} else if kind == "CMD" || kind == "CMD-SHELL" {
out.push("--health-cmd")
out.push(json_string_array(rest))
}
}
}
_ => ()
}
}
///|
/// Convert `"90s"`, `"1m30s"`, `"500ms"` or a bare number to seconds.
pub fn str_to_seconds(text : String) -> Int32? {
let s = text.trim().to_owned()
if s.length() == 0 {
return None
}
let mut total = 0
let number = StringBuilder()
let mut i = 0
let chars = s.to_array()
let mut saw_unit = false
while i < chars.length() {
let c = chars[i]
if (c >= '0' && c <= '9') || c == '.' {
number.write_char(c)
i += 1
continue
}
let value = @string.parse_double(number.to_string()) catch {
_ => return None
}
number.reset()
saw_unit = true
match c {
'h' => total = total + (value * 3600.0).to_int()
'm' =>
// `ms` is milliseconds; a lone `m` is minutes.
if i + 1 < chars.length() && chars[i + 1] == 's' {
i += 1
} else {
total = total + (value * 60.0).to_int()
}
's' => total = total + value.to_int()
_ => return None
}
i += 1
}
if !number.is_empty() {
let value = @string.parse_double(number.to_string()) catch {
_ => return None
}
total = total + value.to_int()
} else if !saw_unit {
return None
}
Some(total)
}
///|
/// A JSON array of strings, for `--entrypoint` and `--health-cmd`, which podman
/// expects in that form.
fn json_string_array(items : Array[String]) -> String {
let out = StringBuilder()
out.write_char('[')
let mut first = true
for item in items {
if !first {
out.write_char(',')
}
first = false
out.write_char('"')
for c in item {
match c {
'"' => out.write_string("\\\"")
'\\' => out.write_string("\\\\")
'\n' => out.write_string("\\n")
'\t' => out.write_string("\\t")
'\r' => out.write_string("\\r")
_ => out.write_char(c)
}
}
out.write_char('"')
}
out.write_char(']')
out.to_string()
}