Skip to content

Commit 6b686ec

Browse files
committed
Keep CVersion in the vminitd package
1 parent 485a1a3 commit 6b686ec

4 files changed

Lines changed: 32 additions & 23 deletions

File tree

Package.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import CompilerPluginSupport
2121
import Foundation
2222
import PackageDescription
2323

24-
let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
25-
let gitTag = ProcessInfo.processInfo.environment["GIT_TAG"] ?? ""
26-
let buildTime = ProcessInfo.processInfo.environment["BUILD_TIME"] ?? "unspecified"
27-
2824
let package = Package(
2925
name: "containerization",
3026
platforms: [.macOS("15.0")],
@@ -264,15 +260,6 @@ let package = Package(
264260
.target(
265261
name: "CShim"
266262
),
267-
.target(
268-
name: "CVersion",
269-
path: "vminitd/Sources/CVersion",
270-
cSettings: [
271-
.define("GIT_COMMIT", to: "\"\(gitCommit)\""),
272-
.define("GIT_TAG", to: "\"\(gitTag)\""),
273-
.define("BUILD_TIME", to: "\"\(buildTime)\""),
274-
]
275-
),
276263
.target(
277264
name: "LCShim",
278265
path: "vminitd/Sources/LCShim"
@@ -303,7 +290,6 @@ let package = Package(
303290
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"),
304291
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
305292
"LCShim",
306-
"CVersion",
307293
"Cgroup",
308294
],
309295
path: "vminitd/Sources/VminitdCore"

vminitd/Package.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717

1818
// The swift-tools-version declares the minimum version of Swift required to build this package.
1919

20+
import Foundation
2021
import PackageDescription
2122

23+
let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
24+
let gitTag = ProcessInfo.processInfo.environment["GIT_TAG"] ?? ""
25+
let buildTime = ProcessInfo.processInfo.environment["BUILD_TIME"] ?? "unspecified"
26+
2227
let package = Package(
2328
name: "swift-vminitd",
2429
platforms: [.macOS("15")],
@@ -33,13 +38,22 @@ let package = Package(
3338
.package(name: "containerization", path: "../"),
3439
],
3540
targets: [
41+
.target(
42+
name: "CVersion",
43+
cSettings: [
44+
.define("GIT_COMMIT", to: "\"\(gitCommit)\""),
45+
.define("GIT_TAG", to: "\"\(gitTag)\""),
46+
.define("BUILD_TIME", to: "\"\(buildTime)\""),
47+
]
48+
),
3649
.executableTarget(
3750
name: "vminitd",
3851
dependencies: [
3952
.product(name: "ArgumentParser", package: "swift-argument-parser"),
4053
.product(name: "ContainerizationOS", package: "containerization"),
4154
.product(name: "Logging", package: "swift-log"),
4255
.product(name: "VminitdCore", package: "containerization"),
56+
"CVersion",
4357
]
4458
),
4559
.executableTarget(

vminitd/Sources/VminitdCore/AgentCommand.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#if os(Linux)
1818

1919
import ArgumentParser
20-
import CVersion
2120
import Cgroup
2221
import Containerization
2322
import ContainerizationError
@@ -27,6 +26,7 @@ import GRPCCore
2726
import Logging
2827
import NIOCore
2928
import NIOPosix
29+
import Synchronization
3030

3131
#if os(Linux)
3232
#if canImport(Musl)
@@ -46,6 +46,8 @@ public struct AgentCommand: AsyncParsableCommand {
4646
private static let foregroundEnvVar = "FOREGROUND"
4747
public static let vsockPort = 1024
4848

49+
public static let versionMetadata = Mutex<Logger.Metadata>([:])
50+
4951
@OptionGroup var options: LogLevelOption
5052

5153
public init() {}
@@ -79,14 +81,7 @@ public struct AgentCommand: AsyncParsableCommand {
7981

8082
signal(SIGPIPE, SIG_IGN)
8183

82-
let gitCommit = String(cString: CZ_get_git_commit())
83-
let gitTag = String(cString: CZ_get_git_tag())
84-
let buildTime = String(cString: CZ_get_build_time())
85-
var metadata: Logger.Metadata = ["commit": "\(gitCommit)", "built": "\(buildTime)"]
86-
if !gitTag.isEmpty {
87-
metadata["tag"] = "\(gitTag)"
88-
}
89-
log.info("vminitd booting", metadata: metadata)
84+
log.info("vminitd booting", metadata: Self.versionMetadata.withLock { $0 })
9085

9186
// Set of mounts necessary to be mounted prior to taking any RPCs.
9287
// 1. /proc as the sysctl rpc wouldn't make sense if it wasn't there (NOTE: This is done before this method

vminitd/Sources/vminitd/Application.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ArgumentParser
18+
import CVersion
1819
import ContainerizationOS
1920
import Foundation
2021
import Logging
@@ -35,6 +36,8 @@ struct Application: AsyncParsableCommand {
3536
)
3637

3738
static func main() async throws {
39+
AgentCommand.versionMetadata.withLock { $0 = Self.versionMetadata() }
40+
3841
// Busybox-style: if invoked as .cz-init, run init mode directly.
3942
let invoked = CommandLine.arguments.first?.split(separator: "/").last.map(String.init) ?? ""
4043
if invoked == ".cz-init" {
@@ -57,6 +60,17 @@ struct Application: AsyncParsableCommand {
5760
}
5861
}
5962

63+
private static func versionMetadata() -> Logger.Metadata {
64+
let gitCommit = String(cString: CZ_get_git_commit())
65+
let gitTag = String(cString: CZ_get_git_tag())
66+
let buildTime = String(cString: CZ_get_build_time())
67+
var metadata: Logger.Metadata = ["commit": "\(gitCommit)", "built": "\(buildTime)"]
68+
if !gitTag.isEmpty {
69+
metadata["tag"] = "\(gitTag)"
70+
}
71+
return metadata
72+
}
73+
6074
private static func mountProc() throws {
6175
if isProcMounted() {
6276
return

0 commit comments

Comments
 (0)