Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import PackageDescription

let releaseVersion = ProcessInfo.processInfo.environment["RELEASE_VERSION"] ?? "0.0.0"
let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
let scVersion = "0.4.1"
let scVersion = "0.5.0"
let builderShimVersion = "0.6.0"

let package = Package(
Expand Down
2 changes: 1 addition & 1 deletion Sources/CLI/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ struct Application: AsyncParsableCommand {
return -1
}

try await process.start(io.stdio)
try await process.start()
defer {
try? io.close()
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/CLI/Builder/BuilderStart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ extension ClientContainer {
detach: true
)
defer { try? io.close() }
let process = try await bootstrap()
_ = try await process.start(io.stdio)

let process = try await bootstrap(stdio: io.stdio)
_ = try await process.start()
await taskManager?.finish()
try io.closeAfterStart()

log.debug("starting BuildKit and BuildKit-shim")
} catch {
try? await stop()
Expand Down
4 changes: 3 additions & 1 deletion Sources/CLI/Container/ContainerExec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ extension Application {

let process = try await container.createProcess(
id: UUID().uuidString.lowercased(),
configuration: config)
configuration: config,
stdio: io.stdio
)

exitCode = try await Application.handleProcess(io: io, process: process)
} catch {
Expand Down
10 changes: 5 additions & 5 deletions Sources/CLI/Container/ContainerStart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ extension Application {
progress.start()

let container = try await ClientContainer.get(id: containerID)
let process = try await container.bootstrap()

progress.set(description: "Starting init process")
let detach = !self.attach && !self.interactive
do {
let detach = !self.attach && !self.interactive
let io = try ProcessIO.create(
tty: container.configuration.initProcess.terminal,
interactive: self.interactive,
detach: detach
)

let process = try await container.bootstrap(stdio: io.stdio)
progress.finish()

if detach {
try await process.start(io.stdio)
try await process.start()
defer {
try? io.close()
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/CLI/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ extension Application {

let detach = self.managementFlags.detach

let process = try await container.bootstrap()
progress.finish()

do {
let io = try ProcessIO.create(
tty: self.processFlags.tty,
interactive: self.processFlags.interactive,
detach: detach
)

let process = try await container.bootstrap(stdio: io.stdio)
progress.finish()

if !self.managementFlags.cidfile.isEmpty {
let path = self.managementFlags.cidfile
let data = id.data(using: .utf8)
Expand All @@ -137,7 +137,7 @@ extension Application {
}

if detach {
try await process.start(io.stdio)
try await process.start()
defer {
try? io.close()
}
Expand Down
12 changes: 8 additions & 4 deletions Sources/ContainerClient/Core/ClientContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ extension ClientContainer {
}

extension ClientContainer {
public func bootstrap() async throws -> ClientProcess {
public func bootstrap(stdio: [FileHandle?]) async throws -> ClientProcess {
let client = self.sandboxClient
try await client.bootstrap()
try await client.bootstrap(stdio: stdio)
return ClientProcessImpl(containerId: self.id, client: self.sandboxClient)
}

Expand Down Expand Up @@ -183,10 +183,14 @@ extension ClientContainer {

extension ClientContainer {
/// Execute a new process inside a running container.
public func createProcess(id: String, configuration: ProcessConfiguration) async throws -> ClientProcess {
public func createProcess(
id: String,
configuration: ProcessConfiguration,
stdio: [FileHandle?]
) async throws -> ClientProcess {
do {
let client = self.sandboxClient
try await client.createProcess(id, config: configuration)
try await client.createProcess(id, config: configuration, stdio: stdio)
return ClientProcessImpl(containerId: self.id, processId: id, client: client)
} catch {
throw ContainerizationError(
Expand Down
6 changes: 3 additions & 3 deletions Sources/ContainerClient/Core/ClientProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public protocol ClientProcess: Sendable {
var id: String { get }

/// Start the underlying process inside of the container.
func start(_ stdio: [FileHandle?]) async throws
func start() async throws
/// Send a terminal resize request to the process `id`.
func resize(_ size: Terminal.Size) async throws
/// Send or "kill" a signal to the process `id`.
Expand Down Expand Up @@ -66,10 +66,10 @@ struct ClientProcessImpl: ClientProcess, Sendable {
}

/// Start the container and return the initial process.
public func start(_ stdio: [FileHandle?]) async throws {
public func start() async throws {
do {
let client = self.client
try await client.startProcess(self.id, stdio: stdio)
try await client.startProcess(self.id)
} catch {
throw ContainerizationError(
.internalError,
Expand Down
35 changes: 26 additions & 9 deletions Sources/ContainerClient/SandboxClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,27 @@ public struct SandboxClient: Sendable, Codable {

// Runtime Methods
extension SandboxClient {
public func bootstrap() async throws {
public func bootstrap(stdio: [FileHandle?]) async throws {
let request = XPCMessage(route: SandboxRoutes.bootstrap.rawValue)
let client = createClient()
defer { client.close() }

for (i, h) in stdio.enumerated() {
let key: XPCKeys = {
switch i {
case 0: .stdin
case 1: .stdout
case 2: .stderr
default:
fatalError("invalid fd \(i)")
}
}()

if let h {
request.set(key: key, value: h)
}
}

try await client.send(request)
}

Expand All @@ -61,19 +77,12 @@ extension SandboxClient {
return try response.sandboxSnapshot()
}

public func createProcess(_ id: String, config: ProcessConfiguration) async throws {
public func createProcess(_ id: String, config: ProcessConfiguration, stdio: [FileHandle?]) async throws {
let request = XPCMessage(route: SandboxRoutes.createProcess.rawValue)
request.set(key: .id, value: id)
let data = try JSONEncoder().encode(config)
request.set(key: .processConfig, value: data)

let client = createClient()
defer { client.close() }
try await client.send(request)
}

public func startProcess(_ id: String, stdio: [FileHandle?]) async throws {
let request = XPCMessage(route: SandboxRoutes.start.rawValue)
for (i, h) in stdio.enumerated() {
let key: XPCKeys = {
switch i {
Expand All @@ -89,6 +98,14 @@ extension SandboxClient {
request.set(key: key, value: h)
}
}

let client = createClient()
defer { client.close() }
try await client.send(request)
}

public func startProcess(_ id: String) async throws {
let request = XPCMessage(route: SandboxRoutes.start.rawValue)
request.set(key: .id, value: id)

let client = createClient()
Expand Down
Loading