forked from apple/container
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlags.swift
More file actions
409 lines (342 loc) · 13.4 KB
/
Copy pathFlags.swift
File metadata and controls
409 lines (342 loc) · 13.4 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
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the container project authors.
//
// 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
//
// https://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.
//===----------------------------------------------------------------------===//
import ArgumentParser
import ContainerizationError
import Foundation
public struct Flags {
public struct Logging: ParsableArguments {
public init() {}
public init(debug: Bool) {
self.debug = debug
}
@Flag(name: .long, help: "Enable debug output [environment: CONTAINER_DEBUG]")
public var debug = false
}
public struct Process: ParsableArguments {
public init() {}
public init(
cwd: String?,
env: [String],
envFile: [String],
gid: UInt32?,
interactive: Bool,
tty: Bool,
uid: UInt32?,
ulimits: [String],
user: String?
) {
self.cwd = cwd
self.env = env
self.envFile = envFile
self.gid = gid
self.interactive = interactive
self.tty = tty
self.uid = uid
self.ulimits = ulimits
self.user = user
}
@Option(name: .shortAndLong, help: "Set environment variables (key=value, or just key to inherit from host)")
public var env: [String] = []
@Option(
name: .long,
help: "Read in a file of environment variables (key=value format, ignores # comments and blank lines)"
)
public var envFile: [String] = []
@Option(name: .long, help: "Set the group ID for the process")
public var gid: UInt32?
@Flag(name: .shortAndLong, help: "Keep the standard input open even if not attached")
public var interactive = false
@Flag(name: .shortAndLong, help: "Open a TTY with the process")
public var tty = false
@Option(name: .shortAndLong, help: "Set the user for the process (format: name|uid[:gid])")
public var user: String?
@Option(name: .long, help: "Set the user ID for the process")
public var uid: UInt32?
@Option(
name: [.customShort("w"), .customLong("workdir"), .long],
help: .init(
"Set the initial working directory inside the container",
valueName: "dir"
)
)
public var cwd: String?
@Option(
name: .customLong("ulimit"),
help: .init(
"Set resource limits (format: <type>=<soft>[:<hard>])",
valueName: "limit"
)
)
public var ulimits: [String] = []
}
public struct Resource: ParsableArguments {
public init() {}
public init(
cpus: Int64?,
memory: String?
) {
self.cpus = cpus
self.memory = memory
}
@Option(name: .shortAndLong, help: "Number of CPUs to allocate to the container")
public var cpus: Int64?
@Option(
name: .shortAndLong,
help: "Amount of memory (1MiByte granularity), with optional K, M, G, T, or P suffix"
)
public var memory: String?
}
public struct DNS: ParsableArguments {
public init() {}
public init(domain: String?, nameservers: [String], options: [String], searchDomains: [String]) {
self.domain = domain
self.nameservers = nameservers
self.options = options
self.searchDomains = searchDomains
}
@Option(
name: .customLong("dns"),
help: .init("DNS nameserver IP address", valueName: "ip")
)
public var nameservers: [String] = []
@Option(
name: .customLong("dns-domain"),
help: .init("Default DNS domain", valueName: "domain")
)
public var domain: String? = nil
@Option(
name: .customLong("dns-option"),
help: .init("DNS options", valueName: "option")
)
public var options: [String] = []
@Option(
name: .customLong("dns-search"),
help: .init("DNS search domains", valueName: "domain")
)
public var searchDomains: [String] = []
}
public struct Registry: ParsableArguments {
public init() {}
public init(scheme: String) {
self.scheme = scheme
}
@Option(help: "Scheme to use when connecting to the container registry. One of (http, https, auto)")
public var scheme: String = "auto"
}
public struct Management: ParsableArguments {
public init() {}
public init(
arch: String,
capAdd: [String],
capDrop: [String],
cidfile: String,
detach: Bool,
dns: Flags.DNS,
dnsDisabled: Bool,
entrypoint: String?,
initImage: String?,
kernel: String?,
labels: [String],
mounts: [String],
name: String?,
networks: [String],
os: String,
platform: String?,
publishPorts: [String],
publishSockets: [String],
readOnly: Bool,
remove: Bool,
rosetta: Bool,
runtime: String?,
ssh: Bool,
shmSize: String?,
blkio: [String] = [],
tmpFs: [String],
useInit: Bool,
virtualization: Bool,
volumes: [String]
) {
self.arch = arch
self.capAdd = capAdd
self.capDrop = capDrop
self.cidfile = cidfile
self.detach = detach
self.dns = dns
self.dnsDisabled = dnsDisabled
self.entrypoint = entrypoint
self.initImage = initImage
self.kernel = kernel
self.labels = labels
self.mounts = mounts
self.name = name
self.networks = networks
self.os = os
self.platform = platform
self.publishPorts = publishPorts
self.publishSockets = publishSockets
self.readOnly = readOnly
self.remove = remove
self.rosetta = rosetta
self.runtime = runtime
self.ssh = ssh
self.shmSize = shmSize
self.blkio = blkio
self.tmpFs = tmpFs
self.useInit = useInit
self.virtualization = virtualization
self.volumes = volumes
}
@Option(name: .shortAndLong, help: "Set arch if image can target multiple architectures")
public var arch: String = Arch.hostArchitecture().rawValue
@Option(
name: .customLong("cap-add"),
help: .init("Add a Linux capability (e.g. CAP_NET_RAW, or ALL)", valueName: "cap")
)
public var capAdd: [String] = []
@Option(
name: .customLong("cap-drop"),
help: .init("Drop a Linux capability (e.g. CAP_NET_RAW, or ALL)", valueName: "cap")
)
public var capDrop: [String] = []
@Option(name: .long, help: "Write the container ID to the path provided")
public var cidfile = ""
@Flag(name: .shortAndLong, help: "Run the container and detach from the process")
public var detach = false
@OptionGroup
public var dns: Flags.DNS
@Option(
name: .long,
help: .init(
"Override the entrypoint of the image",
valueName: "cmd"
)
)
public var entrypoint: String?
@Flag(name: .customLong("init"), help: "Run an init process inside the container that forwards signals and reaps processes")
public var useInit = false
@Option(
name: .long,
help: .init("Use a custom init image instead of the default", valueName: "image")
)
public var initImage: String?
@Option(
name: .shortAndLong,
help: .init("Set a custom kernel path", valueName: "path"),
completion: .file(),
transform: { str in
URL(fileURLWithPath: str, relativeTo: .currentDirectory()).absoluteURL.path(percentEncoded: false)
}
)
public var kernel: String?
@Option(name: [.short, .customLong("label")], help: "Add a key=value label to the container")
public var labels: [String] = []
@Option(name: .customLong("mount"), help: "Add a mount to the container (format: type=<>,source=<>,target=<>,readonly)")
public var mounts: [String] = []
@Option(name: .long, help: "Use the specified name as the container ID")
public var name: String?
@Option(name: [.customLong("network")], help: "Attach the container to a network (format: <name>[,mac=XX:XX:XX:XX:XX:XX][,mtu=VALUE])")
public var networks: [String] = []
@Flag(name: [.customLong("no-dns")], help: "Do not configure DNS in the container")
public var dnsDisabled = false
@Option(name: .long, help: "Set OS if image can target multiple operating systems")
public var os = "linux"
@Option(
name: [.customShort("p"), .customLong("publish")],
help: .init(
"Publish a port from container to host (format: [host-ip:]host-port:container-port[/protocol])",
valueName: "spec"
)
)
public var publishPorts: [String] = []
@Option(name: .long, help: "Platform for the image if it's multi-platform. This takes precedence over --os and --arch [environment: CONTAINER_DEFAULT_PLATFORM]")
public var platform: String?
@Option(
name: .customLong("publish-socket"),
help: .init(
"Publish a socket from container to host (format: host_path:container_path)",
valueName: "spec"
)
)
public var publishSockets: [String] = []
@Flag(name: .long, help: "Mount the container's root filesystem as read-only")
public var readOnly = false
@Flag(name: [.customLong("rm"), .long], help: "Remove the container after it stops")
public var remove = false
@Flag(name: .long, help: "Enable Rosetta in the container")
public var rosetta = false
@Option(name: .long, help: "Set the runtime handler for the container (default: container-runtime-linux)")
public var runtime: String?
@Flag(name: .long, help: "Forward SSH agent socket to container")
public var ssh = false
@Option(name: .customLong("shm-size"), help: "Size of /dev/shm (e.g. 64M, 1G)")
public var shmSize: String?
@Option(
name: .customLong("blkio"),
help: .init(
"Block I/O cgroup tuning options (experimental: see command reference for the supported keys)",
valueName: "option"
)
)
public var blkio: [String] = []
@Option(name: .customLong("tmpfs"), help: "Add a tmpfs mount to the container at the given path")
public var tmpFs: [String] = []
@Flag(
name: .long,
help:
"Expose virtualization capabilities to the container (requires host and guest support)"
)
public var virtualization: Bool = false
@Option(name: [.customLong("volume"), .short], help: "Bind mount a volume into the container")
public var volumes: [String] = []
public func validate() throws {
if dnsDisabled {
let hasDNSConfig =
!dns.nameservers.isEmpty
|| dns.domain != nil
|| !dns.options.isEmpty
|| !dns.searchDomains.isEmpty
if hasDNSConfig {
throw ValidationError(
"`--no-dns` cannot be used with DNS configuration flags (`--dns`, `--dns-domain`, `--dns-option`, `--dns-search`)"
)
}
}
}
}
public struct Progress: ParsableArguments {
public init() {}
public init(progress: ProgressType) {
self.progress = progress
}
public enum ProgressType: String, ExpressibleByArgument {
case auto
case none
case ansi
case plain
case color
}
@Option(name: .long, help: ArgumentHelp("Progress type (format: auto|none|ansi|plain|color)", valueName: "type"))
public var progress: ProgressType = .auto
}
public struct ImageFetch: ParsableArguments {
public init() {}
public init(maxConcurrentDownloads: Int) {
self.maxConcurrentDownloads = maxConcurrentDownloads
}
@Option(name: .long, help: "Maximum number of concurrent downloads")
public var maxConcurrentDownloads: Int = 3
}
}