-
Notifications
You must be signed in to change notification settings - Fork 703
Expand file tree
/
Copy pathContainerClient.swift
More file actions
379 lines (339 loc) · 13.2 KB
/
ContainerClient.swift
File metadata and controls
379 lines (339 loc) · 13.2 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
//===----------------------------------------------------------------------===//
// 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 ContainerResource
import ContainerXPC
import Containerization
import ContainerizationError
import ContainerizationOCI
import Foundation
/// A client for interacting with the container API server.
///
/// This client holds a reusable XPC connection and provides methods for
/// container lifecycle operations. All methods that operate on a specific
/// container take an `id` parameter.
public struct ContainerClient: Sendable {
private static let serviceIdentifier = "com.apple.container.apiserver"
private let xpcClient: XPCClient
/// Creates a new container client with a connection to the API server.
public init() {
self.xpcClient = XPCClient(service: Self.serviceIdentifier)
}
@discardableResult
private func xpcSend(
message: XPCMessage,
timeout: Duration? = XPCClient.xpcRegistrationTimeout
) async throws -> XPCMessage {
try await xpcClient.send(message, responseTimeout: timeout)
}
/// Create a new container with the given configuration.
public func create(
configuration: ContainerConfiguration,
options: ContainerCreateOptions = .default,
kernel: Kernel,
initImage: String? = nil
) async throws {
do {
let request = XPCMessage(route: .containerCreate)
let data = try JSONEncoder().encode(configuration)
let kdata = try JSONEncoder().encode(kernel)
let odata = try JSONEncoder().encode(options)
request.set(key: .containerConfig, value: data)
request.set(key: .kernel, value: kdata)
request.set(key: .containerOptions, value: odata)
if let initImage {
request.set(key: .initImage, value: initImage)
}
try await xpcSend(message: request)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to create container",
cause: error
)
}
}
/// List containers matching the given filters.
public func list(filters: ContainerListFilters = .all) async throws -> [ContainerSnapshot] {
do {
let request = XPCMessage(route: .containerList)
let filterData = try JSONEncoder().encode(filters)
request.set(key: .listFilters, value: filterData)
let response = try await xpcSend(
message: request,
timeout: .seconds(10)
)
let data = response.dataNoCopy(key: .containers)
guard let data else {
return []
}
return try JSONDecoder().decode([ContainerSnapshot].self, from: data)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to list containers",
cause: error
)
}
}
/// Get the container for the provided id.
public func get(id: String) async throws -> ContainerSnapshot {
let containers = try await list(filters: ContainerListFilters(ids: [id]))
guard let container = containers.first else {
throw ContainerizationError(
.notFound,
message: "get failed: container \(id) not found"
)
}
return container
}
/// Bootstrap the container's init process.
public func bootstrap(id: String, stdio: [FileHandle?]) async throws -> ClientProcess {
let request = XPCMessage(route: .containerBootstrap)
for (i, h) in stdio.enumerated() {
let key: XPCKeys = try {
switch i {
case 0: .stdin
case 1: .stdout
case 2: .stderr
default:
throw ContainerizationError(.invalidArgument, message: "invalid fd \(i)")
}
}()
if let h {
request.set(key: key, value: h)
}
}
do {
request.set(key: .id, value: id)
try await xpcClient.send(request)
return ClientProcessImpl(containerId: id, xpcClient: xpcClient)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to bootstrap container",
cause: error
)
}
}
/// Send a signal to the container.
public func kill(id: String, signal: Int32) async throws {
do {
let request = XPCMessage(route: .containerKill)
request.set(key: .id, value: id)
request.set(key: .processIdentifier, value: id)
request.set(key: .signal, value: Int64(signal))
try await xpcClient.send(request)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to kill container",
cause: error
)
}
}
/// Stop the container and all processes currently executing inside.
public func stop(id: String, opts: ContainerStopOptions = ContainerStopOptions.default) async throws {
do {
let request = XPCMessage(route: .containerStop)
let data = try JSONEncoder().encode(opts)
request.set(key: .id, value: id)
request.set(key: .stopOptions, value: data)
try await xpcClient.send(request)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to stop container",
cause: error
)
}
}
/// Delete the container along with any resources.
public func delete(id: String, force: Bool = false) async throws {
do {
let request = XPCMessage(route: .containerDelete)
request.set(key: .id, value: id)
request.set(key: .forceDelete, value: force)
try await xpcClient.send(request)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to delete container",
cause: error
)
}
}
/// Get the disk usage for a container.
public func diskUsage(id: String) async throws -> UInt64 {
let request = XPCMessage(route: .containerDiskUsage)
request.set(key: .id, value: id)
let reply = try await xpcClient.send(request)
let size = reply.uint64(key: .containerSize)
return size
}
/// Create a new process inside a running container.
/// The process is in a created state and must still be started.
public func createProcess(
containerId: String,
processId: String,
configuration: ProcessConfiguration,
stdio: [FileHandle?]
) async throws -> ClientProcess {
do {
let request = XPCMessage(route: .containerCreateProcess)
request.set(key: .id, value: containerId)
request.set(key: .processIdentifier, value: processId)
let data = try JSONEncoder().encode(configuration)
request.set(key: .processConfig, value: data)
for (i, h) in stdio.enumerated() {
let key: XPCKeys = try {
switch i {
case 0: .stdin
case 1: .stdout
case 2: .stderr
default:
throw ContainerizationError(.invalidArgument, message: "invalid fd \(i)")
}
}()
if let h {
request.set(key: key, value: h)
}
}
try await xpcClient.send(request)
return ClientProcessImpl(containerId: containerId, processId: processId, xpcClient: xpcClient)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to create process in container",
cause: error
)
}
}
/// Get the log file handles for a container.
public func logs(id: String) async throws -> [FileHandle] {
do {
let request = XPCMessage(route: .containerLogs)
request.set(key: .id, value: id)
let response = try await xpcClient.send(request)
let fds = response.fileHandles(key: .logs)
guard let fds else {
throw ContainerizationError(
.internalError,
message: "no log fds returned"
)
}
return fds
} catch {
throw ContainerizationError(
.internalError,
message: "failed to get logs for container \(id)",
cause: error
)
}
}
/// Dial a port on the container via vsock.
public func dial(id: String, port: UInt32) async throws -> FileHandle {
let request = XPCMessage(route: .containerDial)
request.set(key: .id, value: id)
request.set(key: .port, value: UInt64(port))
let response: XPCMessage
do {
response = try await xpcClient.send(request)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to dial port \(port) on container",
cause: error
)
}
guard let fh = response.fileHandle(key: .fd) else {
throw ContainerizationError(
.internalError,
message: "failed to get fd for vsock port \(port)"
)
}
return fh
}
/// Copy a file or directory from the host into the container.
public func copyIn(id: String, source: URL, destination: URL, mode: UInt32 = 0o644, destinationIsDirectory: Bool = false) async throws {
let request = XPCMessage(route: .containerCopyIn)
request.set(key: .id, value: id)
request.set(key: .sourcePath, value: source.path)
request.set(key: .destinationPath, value: destination.path)
request.set(key: .fileMode, value: UInt64(mode))
request.set(key: .destinationIsDirectory, value: destinationIsDirectory)
do {
try await xpcSend(message: request, timeout: .seconds(300))
} catch {
throw ContainerizationError(
.internalError,
message: "failed to copy into container \(id)",
cause: error
)
}
}
/// Copy a file or directory from the container to the host.
public func copyOut(id: String, source: URL, destination: URL, destinationIsDirectory: Bool = false) async throws {
let request = XPCMessage(route: .containerCopyOut)
request.set(key: .id, value: id)
request.set(key: .sourcePath, value: source.path)
request.set(key: .destinationPath, value: destination.path)
request.set(key: .destinationIsDirectory, value: destinationIsDirectory)
do {
try await xpcSend(message: request, timeout: .seconds(300))
} catch {
throw ContainerizationError(
.internalError,
message: "failed to copy from container \(id)",
cause: error
)
}
}
/// Get resource usage statistics for a container.
public func stats(id: String) async throws -> ContainerStats {
let request = XPCMessage(route: .containerStats)
request.set(key: .id, value: id)
do {
let response = try await xpcClient.send(request)
guard let data = response.dataNoCopy(key: .statistics) else {
throw ContainerizationError(
.internalError,
message: "no statistics data returned"
)
}
return try JSONDecoder().decode(ContainerStats.self, from: data)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to get statistics for container \(id)",
cause: error
)
}
}
public func export(id: String, archive: URL) async throws {
let request = XPCMessage(route: .containerExport)
request.set(key: .id, value: id)
request.set(key: .archive, value: archive.absolutePath())
do {
try await xpcClient.send(request)
} catch {
throw ContainerizationError(
.internalError,
message: "failed to export container",
cause: error
)
}
}
}