Files
container/Sources/Services/ContainerAPIService/Server/Containers/ContainersHarness.swift
T
J LoganandGitHub 356c8d2f88 Reorganize client libraries. (#1020)
- Closes #461.
- Extract core types into ContainerResources target.
- Extract ContainerNetworkServiceClient from ContainerNetworkService.
- Relocate sandbox client from ContainerClient to
ContainerSandboxServiceClient.
- Relocate ContainerClient to ContainerAPIServiceClient.
- Common structure from services and clients under Source/Services.

Updated project hierarchy:

```
Sources/CAuditToken - audit token access wrapper
Sources/CLI - CLI executable
Sources/ContainerBuild - builder
Sources/ContainerCommands - CLI command implementations
Sources/ContainerLog - logging helpers
Sources/ContainerPersistence - persistent data and system property helpers
Sources/ContainerPlugin - plugin system
Sources/ContainerResource - resource (container, image, volume, network) types
Sources/ContainerVersion - version helpers
Sources/ContainerXPC - XPC helpers
Sources/CVersion - injected project version
Sources/DNSServer - container DNS resolver
Sources/Helpers - service executables
Sources/Services/*/Client - service clients
Sources/Services/*/Server - service implementations
Sources/SocketForwarder - port forwarding
Sources/TerminalProgress - progress bar
```

## Type of Change
- [ ] Bug fix
- [ ] New feature  
- [x] Breaking change
- [ ] Documentation update

## Motivation and Context
The ContainerClient library was a bit of a grab bag. This refactor
applies a more sensible project and library structure for resource data
types, services, and clients.

## Testing
- [x] Tested locally
- [x] Added/updated tests
- [ ] Added/updated docs
2026-01-06 08:27:14 -08:00

290 lines
9.2 KiB
Swift

//===----------------------------------------------------------------------===//
// 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 ContainerizationOS
import Foundation
import Logging
public struct ContainersHarness: Sendable {
let log: Logging.Logger
let service: ContainersService
public init(service: ContainersService, log: Logging.Logger) {
self.log = log
self.service = service
}
@Sendable
public func list(_ message: XPCMessage) async throws -> XPCMessage {
let containers = try await service.list()
let data = try JSONEncoder().encode(containers)
let reply = message.reply()
reply.set(key: .containers, value: data)
return reply
}
@Sendable
public func bootstrap(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let stdio = message.stdio()
try await service.bootstrap(id: id, stdio: stdio)
return message.reply()
}
@Sendable
public func stop(_ message: XPCMessage) async throws -> XPCMessage {
let stopOptions = try message.stopOptions()
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
try await service.stop(id: id, options: stopOptions)
return message.reply()
}
@Sendable
public func dial(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let port = message.uint64(key: .port)
let fh = try await service.dial(id: id, port: UInt32(port))
let reply = message.reply()
reply.setFileHandle(fh)
return reply
}
@Sendable
public func wait(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let processID = message.string(key: .processIdentifier)
guard let processID else {
throw ContainerizationError(
.invalidArgument,
message: "process ID cannot be empty"
)
}
let exitStatus = try await service.wait(id: id, processID: processID)
let reply = message.reply()
reply.set(key: .exitCode, value: Int64(exitStatus.exitCode))
reply.set(key: .exitedAt, value: exitStatus.exitedAt)
return reply
}
@Sendable
public func resize(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let processID = message.string(key: .processIdentifier)
guard let processID else {
throw ContainerizationError(
.invalidArgument,
message: "process ID cannot be empty"
)
}
let width = message.uint64(key: .width)
let height = message.uint64(key: .height)
try await service.resize(
id: id,
processID: processID,
size: Terminal.Size(width: UInt16(width), height: UInt16(height))
)
return message.reply()
}
@Sendable
public func kill(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let processID = message.string(key: .processIdentifier)
guard let processID else {
throw ContainerizationError(
.invalidArgument,
message: "process ID cannot be empty"
)
}
try await service.kill(
id: id,
processID: processID,
signal: try message.signal()
)
return message.reply()
}
@Sendable
public func create(_ message: XPCMessage) async throws -> XPCMessage {
let data = message.dataNoCopy(key: .containerConfig)
guard let data else {
throw ContainerizationError(
.invalidArgument,
message: "container configuration cannot be empty"
)
}
let kdata = message.dataNoCopy(key: .kernel)
guard let kdata else {
throw ContainerizationError(
.invalidArgument,
message: "kernel cannot be empty"
)
}
let odata = message.dataNoCopy(key: .containerOptions)
var options: ContainerCreateOptions = .default
if let odata {
options = try JSONDecoder().decode(ContainerCreateOptions.self, from: odata)
}
let config = try JSONDecoder().decode(ContainerConfiguration.self, from: data)
let kernel = try JSONDecoder().decode(Kernel.self, from: kdata)
try await service.create(configuration: config, kernel: kernel, options: options)
return message.reply()
}
@Sendable
public func createProcess(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let processID = message.string(key: .processIdentifier)
guard let processID else {
throw ContainerizationError(
.invalidArgument,
message: "process ID cannot be empty"
)
}
let config = try message.processConfig()
let stdio = message.stdio()
try await service.createProcess(
id: id,
processID: processID,
config: config,
stdio: stdio
)
return message.reply()
}
@Sendable
public func startProcess(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let processID = message.string(key: .processIdentifier)
guard let processID else {
throw ContainerizationError(
.invalidArgument,
message: "process ID cannot be empty"
)
}
try await service.startProcess(
id: id,
processID: processID,
)
return message.reply()
}
@Sendable
public func delete(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(.invalidArgument, message: "id cannot be empty")
}
let forceDelete = message.bool(key: .forceDelete)
try await service.delete(id: id, force: forceDelete)
return message.reply()
}
@Sendable
public func logs(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let fds = try await service.logs(id: id)
let reply = message.reply()
try reply.set(key: .logs, value: fds)
return reply
}
@Sendable
public func stats(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
guard let id else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
let stats = try await service.stats(id: id)
let data = try JSONEncoder().encode(stats)
let reply = message.reply()
reply.set(key: .statistics, value: data)
return reply
}
}