This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
/
serviceflags.go
557 lines (452 loc) · 12.9 KB
/
serviceflags.go
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
// Copyright © 2017 Max Goltzsche
//
// 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.
package cmd
import (
"path/filepath"
"strings"
"github.com/mgoltzsche/ctnr/model"
"github.com/mgoltzsche/ctnr/net"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
var flagsBundle = bundleFlags{}
type bundleFlags struct {
netCfg
update bool
stdin bool
tty bool
readonly bool
privileged bool
noPivot bool
noNewKeyring bool
proot bool
app *model.Service
}
func (c *bundleFlags) InitContainerFlags(f *pflag.FlagSet) {
f.Var((*cName)(c), "name", "container name. Also used as hostname when hostname is not set explicitly")
f.BoolVar(&c.update, "update", false, "update an existing bundle's configuration and rootfs if changed")
f.BoolVar(&c.update, "rm", false, "alias for update")
f.MarkHidden("rm") // docker compatibility
f.VarP((*cBundle)(c), "bundle", "b", "bundle name or directory")
c.InitProcessFlags(f)
f.Var((*cSeccomp)(c), "seccomp", "seccomp profile file or 'default' or 'unconfined'")
f.Var((*cMountCgroups)(c), "mount-cgroups", "mounts the host's cgroups with the given option: ro|rw|no")
f.Var((*cMount)(c), "mount", "mounts a volume: type=T,src=S,dst=D,opt=O")
f.VarP((*cVolumeMount)(c), "volume", "v", "bind mounts a volume: TARGET|SOURCE:TARGET[:OPTIONS]")
f.MarkHidden("volume")
f.Var((*cExpose)(c), "expose", "container ports to be exposed")
f.BoolVar(&c.readonly, "readonly", false, "mounts the root file system in read only mode")
f.BoolVar(&c.privileged, "privileged", false, "give extended privileges to the container")
f.BoolVar(&c.proot, "proot", false, "enables PRoot")
initNetConfFlags(f, &c.netCfg)
}
func (c *bundleFlags) InitRunFlags(f *pflag.FlagSet) {
f.BoolVarP(&c.stdin, "stdin", "i", false, "binds stdin to the container")
f.BoolVar(&c.noNewKeyring, "no-new-keyring", false, "do not create a new session keyring for the container. This will cause the container to inherit the calling processes session key")
f.BoolVar(&c.noPivot, "no-pivot", false, "do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk")
}
func (c *bundleFlags) InitProcessFlags(f *pflag.FlagSet) {
f.Var((*cEntrypoint)(c), "entrypoint", "container entrypoint")
f.VarP((*cWorkingDir)(c), "workdir", "w", "container entrypoint")
f.VarP((*cEnvironment)(c), "env", "e", "container environment variables")
f.VarP((*cUser)(c), "user", "u", "process user (requires root): UID[:GID]")
f.Var((*cAdditionalGroups)(c), "additional-groups", "additional groups")
f.BoolVarP(&c.tty, "tty", "t", false, "binds a terminal to the container")
f.Var((*cCapAdd)(c), "cap-add", "add process capability ('all' adds all)")
f.Var((*cCapDrop)(c), "cap-drop", "drop process capability ('all' drops all)")
// Stop parsing after first non flag argument (either <IMAGE> <CMD> or <CONTAINERID> <CMD>)
f.SetInterspersed(false)
}
func initNetConfFlags(f *pflag.FlagSet, c *netCfg) {
f.Var((*cHostname)(c), "hostname", "container hostname")
f.Var((*cDomainname)(c), "domainname", "container domainname")
f.Var((*cDns)(c), "dns", "DNS nameservers to write in container's /etc/resolv.conf")
f.Var((*cDnsSearch)(c), "dns-search", "DNS search domains to write in container's /etc/resolv.conf")
f.Var((*cDnsOptions)(c), "dns-opts", "DNS search options to write in container's /etc/resolv.conf")
f.Var((*cExtraHosts)(c), "hosts-entry", "additional entries to write in container's /etc/hosts")
f.VarP((*cPortBinding)(c), "publish", "p", "container ports to be published on the host: [[HOSTIP:]HOSTPORT:]PORT[/PROT]")
f.Var((*cNetworks)(c), "network", "add CNI network to container's network namespace")
}
func (c *bundleFlags) curr() *model.Service {
if c.app == nil {
s := model.NewService("")
c.app = &s
}
return c.app
}
func (c *bundleFlags) Read() (*model.Service, error) {
if c.app == nil {
return nil, usageError("No service defined")
}
if c.proot && flagPRootPath == "" {
return nil, usageError("--proot enabled but no --proot-path specified")
}
s := c.app
s.BundleUpdate = c.update
s.NetConf = c.net
s.Tty = c.tty
s.StdinOpen = c.stdin
s.Privileged = c.privileged
s.ReadOnly = c.readonly
s.NoPivot = c.noPivot
s.NoNewKeyring = c.noNewKeyring
s.PRoot = c.proot
c.app = nil
c.net = model.NetConf{}
return s, nil
}
func (c *bundleFlags) SetBundleArgs(ca []string) error {
if len(ca) == 0 {
return usageError("No image arg specified")
}
last := c.curr()
last.Image = ca[0]
if len(ca) > 1 {
last.Command = ca[1:]
}
return nil
}
type cName bundleFlags
func (c *cName) Set(s string) error {
(*bundleFlags)(c).curr().Name = s
return nil
}
func (c *cName) Type() string {
return "string"
}
func (c *cName) String() string {
return (*bundleFlags)(c).curr().Name
}
type cStopSignal bundleFlags
func (c *cStopSignal) Set(s string) error {
(*bundleFlags)(c).curr().StopSignal = s
return nil
}
func (c *cStopSignal) Type() string {
return "string"
}
func (c *cStopSignal) String() string {
return (*bundleFlags)(c).curr().StopSignal
}
type cBundle bundleFlags
func (c *cBundle) Set(bundle string) error {
(*bundleFlags)(c).curr().Bundle = bundle
return nil
}
func (c *cBundle) Type() string {
return "string"
}
func (c *cBundle) String() string {
return (*bundleFlags)(c).curr().Bundle
}
type cEntrypoint bundleFlags
func (c *cEntrypoint) Set(s string) error {
(*bundleFlags)(c).curr().Entrypoint = nil
return addStringEntries(s, &(*bundleFlags)(c).curr().Entrypoint)
}
func (c *cEntrypoint) Type() string {
return "cmd"
}
func (c *cEntrypoint) String() string {
return entriesToString((*bundleFlags)(c).curr().Entrypoint)
}
type cWorkingDir bundleFlags
func (c *cWorkingDir) Set(s string) error {
(*bundleFlags)(c).curr().Cwd = s
return nil
}
func (c *cWorkingDir) Type() string {
return "dir"
}
func (c *cWorkingDir) String() string {
return (*bundleFlags)(c).curr().Cwd
}
type cEnvironment bundleFlags
func (c *cEnvironment) Set(s string) error {
return addMapEntries(s, &(*bundleFlags)(c).curr().Environment)
}
func (c *cEnvironment) Type() string {
return "NAME=VALUE..."
}
func (c *cEnvironment) String() string {
return mapToString((*bundleFlags)(c).curr().Environment)
}
type cUser bundleFlags
func (c *cUser) Set(s string) error {
u := &(*bundleFlags)(c).curr().User
if *u == nil {
*u = &model.User{}
}
ug := strings.SplitN(s, ":", 2)
(*u).User = ug[0]
if len(ug) == 2 {
(*u).Group = ug[1]
}
return nil
}
func (c *cUser) Type() string {
return "string"
}
func (c *cUser) String() string {
u := (*bundleFlags)(c).curr().User
if u == nil || u.User == "" {
return ""
} else if u.Group == "" {
return u.User
} else {
return u.User + ":" + u.Group
}
}
type cAdditionalGroups bundleFlags
func (c *cAdditionalGroups) Set(s string) error {
u := &(*bundleFlags)(c).curr().User
if *u == nil {
*u = &model.User{}
}
return addStringEntries(s, &(*u).AdditionalGroups)
}
func (c *cAdditionalGroups) Type() string {
return "string..."
}
func (c *cAdditionalGroups) String() string {
u := &(*bundleFlags)(c).curr().User
if *u == nil {
return ""
}
return entriesToString((*u).AdditionalGroups)
}
type cCapAdd bundleFlags
func (c *cCapAdd) Set(s string) error {
return addStringEntries(s, &(*bundleFlags)(c).curr().CapAdd)
}
func (c *cCapAdd) Type() string {
return "string..."
}
func (c *cCapAdd) String() string {
return entriesToString((*bundleFlags)(c).curr().CapAdd)
}
type cCapDrop bundleFlags
func (c *cCapDrop) Set(s string) error {
if strings.ToUpper(s) == "ALL" {
(*bundleFlags)(c).curr().CapAdd = nil
return nil
} else {
return addStringEntries(s, &(*bundleFlags)(c).curr().CapDrop)
}
}
func (c *cCapDrop) Type() string {
return "string..."
}
func (c *cCapDrop) String() string {
return entriesToString((*bundleFlags)(c).curr().CapDrop)
}
type cSeccomp bundleFlags
func (c *cSeccomp) Set(s string) error {
(*bundleFlags)(c).curr().Seccomp = s
return nil
}
func (c *cSeccomp) Type() string {
return "string"
}
func (c *cSeccomp) String() string {
return (*bundleFlags)(c).curr().Seccomp
}
type cMountCgroups bundleFlags
func (c *cMountCgroups) Set(opt string) error {
(*bundleFlags)(c).curr().MountCgroups = opt
return nil
}
func (c *cMountCgroups) Type() string {
return "string"
}
func (c *cMountCgroups) String() string {
return (*bundleFlags)(c).curr().MountCgroups
}
type cExpose bundleFlags
func (c *cExpose) Set(s string) (err error) {
return addStringEntries(s, &(*bundleFlags)(c).curr().Expose)
}
func (c *cExpose) Type() string {
return "port..."
}
func (c *cExpose) String() string {
return entriesToString((*bundleFlags)(c).curr().Entrypoint)
}
type cMount bundleFlags
func (c *cMount) Set(s string) (err error) {
v, err := ParseMount(s)
if err != nil {
return
}
v.Source, err = filepath.Abs(v.Source)
if err != nil {
err = errors.New(err.Error())
}
r := &(*bundleFlags)(c).curr().Volumes
*r = append(*r, v)
return
}
func (c *cMount) Type() string {
return "string..."
}
func (c *cMount) String() string {
s := ""
for _, v := range (*bundleFlags)(c).curr().Volumes {
s += strings.Trim(" "+v.String(), " ")
}
return s
}
type cVolumeMount bundleFlags
func (c *cVolumeMount) Set(s string) (err error) {
v, err := ParseBindMount(s)
if err != nil {
return
}
v.Source, err = filepath.Abs(v.Source)
if err != nil {
err = errors.New(err.Error())
}
r := &(*bundleFlags)(c).curr().Volumes
*r = append(*r, v)
return
}
func (c *cVolumeMount) Type() string {
return "string..."
}
func (c *cVolumeMount) String() string {
s := ""
for _, v := range (*bundleFlags)(c).curr().Volumes {
s += strings.Trim(" "+v.String(), " ")
}
return s
}
type netCfg struct {
net model.NetConf
}
type cHostname netCfg
func (c *cHostname) Set(s string) error {
(*netCfg)(c).net.Hostname = s
return nil
}
func (c *cHostname) Type() string {
return "string"
}
func (c *cHostname) String() string {
return (*netCfg)(c).net.Hostname
}
type cDomainname netCfg
func (c *cDomainname) Set(s string) error {
(*netCfg)(c).net.Domainname = s
return nil
}
func (c *cDomainname) Type() string {
return "string"
}
func (c *cDomainname) String() string {
return (*netCfg)(c).net.Domainname
}
type cDns netCfg
func (c *cDns) Set(s string) error {
return addStringEntries(s, &(*netCfg)(c).net.Dns)
}
func (c *cDns) Type() string {
return "string..."
}
func (c *cDns) String() string {
return entriesToString((*netCfg)(c).net.Dns)
}
type cDnsSearch netCfg
func (c *cDnsSearch) Set(s string) error {
return addStringEntries(s, &(*netCfg)(c).net.DnsSearch)
}
func (c *cDnsSearch) Type() string {
return "string..."
}
func (c *cDnsSearch) String() string {
return entriesToString((*netCfg)(c).net.DnsSearch)
}
type cDnsOptions netCfg
func (c *cDnsOptions) Set(s string) error {
return addStringEntries(s, &(*netCfg)(c).net.DnsOptions)
}
func (c *cDnsOptions) Type() string {
return "string..."
}
func (c *cDnsOptions) String() string {
return entriesToString((*netCfg)(c).net.DnsOptions)
}
type cExtraHosts netCfg
func (c *cExtraHosts) Set(v string) error {
s := strings.SplitN(v, "=", 2)
k := strings.Trim(s[0], " ")
if len(s) != 2 || k == "" || strings.Trim(s[1], " ") == "" {
return errors.New("Expected option value format: NAME=IP")
}
(*c).net.ExtraHosts = append((*c).net.ExtraHosts, model.ExtraHost{k, strings.Trim(s[1], " ")})
return nil
}
func (c *cExtraHosts) Type() string {
return "NAME=IP..."
}
func (c *cExtraHosts) String() string {
s := ""
for _, e := range (*c).net.ExtraHosts {
s += strings.Trim(" "+e.Name+"="+e.Ip, " ")
}
return s
}
type cPortBinding netCfg
func (c *cPortBinding) Set(s string) (err error) {
ports := make([]net.PortMapEntry, 0, 1)
if err = net.ParsePortMapping(s, &ports); err != nil {
return
}
for _, p := range ports {
(*netCfg)(c).net.Ports = append((*netCfg)(c).net.Ports, model.PortBinding{
Published: p.HostPort,
Target: p.ContainerPort,
Protocol: p.Protocol,
IP: p.HostIP,
})
}
return
}
func (c *cPortBinding) Type() string {
return "port..."
}
func (c *cPortBinding) String() string {
s := ""
p := (*netCfg)(c).net.Ports
if p != nil {
for _, p := range p {
s += strings.Trim(" "+p.String(), " ")
}
}
return s
}
type cNetworks netCfg
func (c *cNetworks) Set(s string) error {
if s == "" {
return errors.New("no network names provided")
}
n := &(*netCfg)(c).net.Networks
*n = append(*n, strings.Split(s, ",")...)
return nil
}
func (c *cNetworks) Type() string {
return "string..."
}
func (c *cNetworks) String() string {
return strings.Join((*netCfg)(c).net.Networks, ",")
}