forked from apple/containerization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunCommand.swift
More file actions
177 lines (149 loc) · 6.38 KB
/
RunCommand.swift
File metadata and controls
177 lines (149 loc) · 6.38 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
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the Containerization project authors.
// All rights reserved.
//
// 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 Containerization
import ContainerizationError
import ContainerizationOCI
import ContainerizationOS
import Foundation
extension Application {
struct Run: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "run",
abstract: "Run a container"
)
@Option(name: [.customLong("image"), .customShort("i")], help: "image reference to base the container on")
var imageReference: String = "docker.io/library/alpine:3.16"
@Option(name: .long, help: "id for the container")
var id: String = "cctl"
@Option(name: [.customLong("cpus"), .customShort("c")], help: "Number of CPUs to allocate to the container")
var cpus: Int = 2
@Option(name: [.customLong("memory"), .customShort("m")], help: "Amount of memory in megabytes")
var memory: UInt64 = 1024
@Option(name: .customLong("fs-size"), help: "The size to create the block filesystem as")
var fsSizeInMB: UInt64 = 2048
@Option(name: .customLong("mount"), help: "directory to share into the container (Example: /foo:/bar)")
var mounts: [String] = []
@Option(name: .long, help: "ip address with subnet")
var ip: String?
@Option(name: .long, help: "gateway address")
var gateway: String?
@Option(name: .customLong("ns"), help: "nameserver addresses")
var nameservers: [String] = []
@Option(
name: [.customLong("kernel"), .customShort("k")], help: "Kernel binary path", completion: .file(),
transform: { str in
URL(fileURLWithPath: str, relativeTo: .currentDirectory()).absoluteURL.path(percentEncoded: false)
})
public var kernel: String
@Option(
name: .customLong("init"), help: "Init block path", completion: .file(),
transform: { str in
URL(fileURLWithPath: str, relativeTo: .currentDirectory()).absoluteURL.path(percentEncoded: false)
})
public var initBlock: String
@Option(name: .long, help: "Current working directory")
var cwd: String = "/"
@Argument var arguments: [String] = ["/bin/sh"]
func run() async throws {
let kernel = Kernel(
path: URL(fileURLWithPath: kernel),
platform: .linuxArm
)
let store = try await ContainerStore(
root: Self.appRoot,
kernel: kernel,
initPath: .init(filePath: initBlock)
)
let sigwinch = setupSigwinchHandler()
let current = try Terminal.current
try current.setraw()
defer { current.tryReset() }
let container = try await store.create(
id: id,
reference: imageReference,
fsSizeInBytes: fsSizeInMB.mib()
)
container.cpus = cpus
container.memoryInBytes = memory.mib()
container.terminalDevice = current
container.arguments = arguments
container.environment.append(contentsOf: [
"HOME=/",
"TERM=xterm",
])
container.workingDirectory = cwd
for mount in self.mounts {
let paths = mount.split(separator: ":")
if paths.count != 2 {
throw ContainerizationError(
.invalidArgument,
message: "incorrect mount format detected: \(mount)"
)
}
let host = String(paths[0])
let guest = String(paths[1])
let czMount = Containerization.Mount.share(
source: host,
destination: guest
)
container.mounts.append(czMount)
}
if let ip {
guard let gateway else {
throw ContainerizationError(.invalidArgument, message: "gateway must be specified")
}
container.interfaces.append(NATInterface(address: ip, gateway: gateway))
container.dns = .init(nameservers: [gateway])
if nameservers.count > 0 {
container.dns = .init(nameservers: nameservers)
}
}
try await container.create()
try await container.start()
// Resize the containers pty to the current terminal window.
try? await container.resize(to: try current.size)
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
for await _ in sigwinch {
try await container.resize(to: try current.size)
}
}
try await container.wait()
group.cancelAll()
try await container.stop()
}
}
private func setupSigwinchHandler() -> AsyncStream<Void> {
let sigwinch = DispatchSource.makeSignalSource(signal: SIGWINCH)
let stream = AsyncStream<Void> { cont in
sigwinch.setEventHandler {
cont.yield()
}
}
sigwinch.resume()
return stream
}
private static let appRoot: URL = {
FileManager.default.urls(
for: .applicationSupportDirectory,
in: .userDomainMask
).first!
.appendingPathComponent("com.apple.containerization")
}()
}
}