mirror of
https://github.com/apple/container.git
synced 2026-07-12 20:47:04 +00:00
4c800db3fd
## Type of Change - [x] Bug fix - [ ] New feature - [ ] Breaking change - [ ] Documentation update ## Motivation and Context - CI build is failing because runners don't have an /etc/resolver directory, causing the directory monitor to fail. This occurs while the install-kernel make target is running, so it appears that kernel download is failing when it's just that the API server is dying. Created #1207 for the issue. - Fixing the initial scan for the directory just moves the problem to the filesystem watch loop, likely because we're not testing the result of `open()` for errors. - Right now the priority is getting CI running and PRs merged, so just commenting out the realhost DNS server setup. - Also seeing that under some conditions it can take quite a while for launchd to start the helper for the default network (8 seconds or more). With the 10 second health check timeout after API server registration, this means that some CI runs can exhibit this failure mode. Added a `--timeout` option to SystemStart and set a 60 second timeout for install-kernel and integration Makefile targets. - Fixed a bug where `--debug` was being placed in the wrong location in the api server startup args. - Disabled all network CLI tests due to container bootstrap errors when trying to run the container immediately after creating the network. The slow network helper launch could be the reason behind the failures that drove us to serialize these tests. Filed #1206 for this issue. ## Testing - [x] Tested locally - [ ] Added/updated tests - [ ] Added/updated docs
192 lines
7.1 KiB
Swift
192 lines
7.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
// Copyright © 2025-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 ContainerAPIClient
|
|
import ContainerPersistence
|
|
import ContainerPlugin
|
|
import ContainerizationError
|
|
import Foundation
|
|
import TerminalProgress
|
|
|
|
extension Application {
|
|
public struct SystemStart: AsyncLoggableCommand {
|
|
public static let configuration = CommandConfiguration(
|
|
commandName: "start",
|
|
abstract: "Start `container` services"
|
|
)
|
|
|
|
@Option(
|
|
name: .shortAndLong,
|
|
help: "Path to the root directory for application data",
|
|
transform: { URL(filePath: $0) })
|
|
var appRoot = ApplicationRoot.defaultURL
|
|
|
|
@Option(
|
|
name: .long,
|
|
help: "Path to the root directory for application executables and plugins",
|
|
transform: { URL(filePath: $0) })
|
|
var installRoot = InstallRoot.defaultURL
|
|
|
|
@Flag(
|
|
name: .long,
|
|
inversion: .prefixedEnableDisable,
|
|
help: "Specify whether the default kernel should be installed or not (default: prompt user)")
|
|
var kernelInstall: Bool?
|
|
|
|
@Option(
|
|
name: .long,
|
|
help: "Number of seconds to wait for API service to become responsive")
|
|
var timeout: Double = 10.0
|
|
|
|
@OptionGroup
|
|
public var logOptions: Flags.Logging
|
|
|
|
public init() {}
|
|
|
|
public func run() async throws {
|
|
// Without the true path to the binary in the plist, `container-apiserver` won't launch properly.
|
|
// TODO: Can we use the plugin loader to bootstrap the API server?
|
|
let executableUrl = CommandLine.executablePathUrl
|
|
.deletingLastPathComponent()
|
|
.appendingPathComponent("container-apiserver")
|
|
.resolvingSymlinksInPath()
|
|
|
|
var args = [executableUrl.absolutePath()]
|
|
|
|
args.append("start")
|
|
if logOptions.debug {
|
|
args.append("--debug")
|
|
}
|
|
|
|
let apiServerDataUrl = appRoot.appending(path: "apiserver")
|
|
try! FileManager.default.createDirectory(at: apiServerDataUrl, withIntermediateDirectories: true)
|
|
|
|
var env = PluginLoader.filterEnvironment()
|
|
env[ApplicationRoot.environmentName] = appRoot.path(percentEncoded: false)
|
|
env[InstallRoot.environmentName] = installRoot.path(percentEncoded: false)
|
|
|
|
let plist = LaunchPlist(
|
|
label: "com.apple.container.apiserver",
|
|
arguments: args,
|
|
environment: env,
|
|
limitLoadToSessionType: [.Aqua, .Background, .System],
|
|
runAtLoad: true,
|
|
machServices: ["com.apple.container.apiserver"]
|
|
)
|
|
|
|
let plistURL = apiServerDataUrl.appending(path: "apiserver.plist")
|
|
let data = try plist.encode()
|
|
try data.write(to: plistURL)
|
|
|
|
print("Registering API server with launchd...")
|
|
try ServiceManager.register(plistPath: plistURL.path)
|
|
|
|
// Now ping our friendly daemon. Fail if we don't get a response.
|
|
do {
|
|
print("Verifying apiserver is running...")
|
|
_ = try await ClientHealthCheck.ping(timeout: .seconds(timeout))
|
|
} catch {
|
|
throw ContainerizationError(
|
|
.internalError,
|
|
message: "failed to get a response from apiserver: \(error)"
|
|
)
|
|
}
|
|
|
|
if await !initImageExists() {
|
|
try? await installInitialFilesystem()
|
|
}
|
|
|
|
guard await !kernelExists() else {
|
|
return
|
|
}
|
|
try await installDefaultKernel()
|
|
}
|
|
|
|
private func installInitialFilesystem() async throws {
|
|
let dep = Dependencies.initFs
|
|
var pullCommand = try ImagePull.parse()
|
|
pullCommand.reference = dep.source
|
|
print("Installing base container filesystem...")
|
|
do {
|
|
try await pullCommand.run()
|
|
} catch {
|
|
log.error("failed to install base container filesystem: \(error)")
|
|
}
|
|
}
|
|
|
|
private func installDefaultKernel() async throws {
|
|
let kernelDependency = Dependencies.kernel
|
|
let defaultKernelURL = kernelDependency.source
|
|
let defaultKernelBinaryPath = DefaultsStore.get(key: .defaultKernelBinaryPath)
|
|
|
|
var shouldInstallKernel = false
|
|
if kernelInstall == nil {
|
|
print("No default kernel configured.")
|
|
print("Install the recommended default kernel from [\(kernelDependency.source)]? [Y/n]: ", terminator: "")
|
|
guard let read = readLine(strippingNewline: true) else {
|
|
throw ContainerizationError(.internalError, message: "failed to read user input")
|
|
}
|
|
guard read.lowercased() == "y" || read.count == 0 else {
|
|
print("Please use the `container system kernel set --recommended` command to configure the default kernel")
|
|
return
|
|
}
|
|
shouldInstallKernel = true
|
|
} else {
|
|
shouldInstallKernel = kernelInstall ?? false
|
|
}
|
|
guard shouldInstallKernel else {
|
|
return
|
|
}
|
|
print("Installing kernel...")
|
|
try await KernelSet.downloadAndInstallWithProgressBar(tarRemoteURL: defaultKernelURL, kernelFilePath: defaultKernelBinaryPath, force: true)
|
|
}
|
|
|
|
private func initImageExists() async -> Bool {
|
|
do {
|
|
let img = try await ClientImage.get(reference: Dependencies.initFs.source)
|
|
let _ = try await img.getSnapshot(platform: .current)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func kernelExists() async -> Bool {
|
|
do {
|
|
try await ClientKernel.getDefaultKernel(for: .current)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
private enum Dependencies: String {
|
|
case kernel
|
|
case initFs
|
|
|
|
var source: String {
|
|
switch self {
|
|
case .initFs:
|
|
return DefaultsStore.get(key: .defaultInitImage)
|
|
case .kernel:
|
|
return DefaultsStore.get(key: .defaultKernelURL)
|
|
}
|
|
}
|
|
}
|
|
}
|