Skip to content

Commit 5017835

Browse files
committed
Rename snapshots and attempt to make them more tree like
1 parent a482e7e commit 5017835

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Sources/gitlab-fusion/Stages/Cleanup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct Cleanup: ParsableCommand {
4848
let base = VirtualMachine(image: baseVMPath, executable: options.vmwareFusion)
4949

5050
/// The name of VMware Fusion guest created by the clone operation
51-
let clonedGuestName = "\(base.name)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
51+
let clonedGuestName = "\(base.name)-\(ciServerHost)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
5252

5353
/// The path of the VMware Fusion guest created by the clone operation
5454
let clonedGuestPath = options.vmImagesPath

Sources/gitlab-fusion/Stages/Prepare.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,26 @@ struct Prepare: ParsableCommand {
7979
os_log("The base VMware Fusion guest is %{public}@", log: log, type: .debug, baseVMPath.string)
8080
let base = VirtualMachine(image: baseVMPath, executable: options.vmwareFusion)
8181

82-
/// The name of VMware Fusion guest created by the clone operation
83-
let clonedGuestName = "\(base.name)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
82+
// Check if the snapshot exists (creating it if necessary)
83+
let rootSnapshotname = subsystem
84+
if !base.snapshots.contains(rootSnapshotname) {
85+
FileHandle.standardOutput
86+
.write(line: "Creating snapshot \"\(rootSnapshotname)\" in base guest \"\(base.name)\"...")
87+
try base.snapshot(rootSnapshotname)
88+
}
8489

8590
// Check if the snapshot exists (creating it if necessary)
86-
let baseVMSnapshotName = "base-snapshot-\(clonedGuestName)"
87-
if !base.snapshots.contains(baseVMSnapshotName) {
91+
let cloneBaseSnapshotname = "\(ciServerHost)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
92+
if !base.snapshots.contains(cloneBaseSnapshotname) {
8893
FileHandle.standardOutput
89-
.write(line: "Creating snapshot \"\(baseVMSnapshotName)\" in base guest \"\(base.name)\"...")
90-
try base.snapshot(baseVMSnapshotName)
94+
.write(line: "Creating snapshot \"\(cloneBaseSnapshotname)\" in base guest \"\(base.name)\"...")
95+
// Ensure that the common base snapshot is used
96+
try base.revert(to: rootSnapshotname)
97+
try base.snapshot(cloneBaseSnapshotname)
9198
}
9299

93100
/// The path of the VMware Fusion guest created by the clone operation
101+
let clonedGuestName = "\(base.name)-\(ciServerHost)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
94102
let clonedGuestPath = options.vmImagesPath
95103
.join("\(clonedGuestName).vmwarevm")
96104
.join("\(clonedGuestName).vmx")
@@ -99,8 +107,8 @@ struct Prepare: ParsableCommand {
99107
let clone: VirtualMachine
100108
if !clonedGuestPath.exists {
101109
FileHandle.standardOutput
102-
.write(line: "Cloning from snapshot \"\(baseVMSnapshotName)\" in base guest \"\(base.name)\" to \"\(clonedGuestName)\"...")
103-
clone = try base.clone(to: clonedGuestPath, named: clonedGuestName, linkedTo: baseVMSnapshotName)
110+
.write(line: "Cloning from snapshot \"\(cloneBaseSnapshotname)\" in base guest \"\(base.name)\" to \"\(clonedGuestName)\"...")
111+
clone = try base.clone(to: clonedGuestPath, named: clonedGuestName, linkedTo: cloneBaseSnapshotname)
104112
} else {
105113
clone = VirtualMachine(image: clonedGuestPath, executable: options.vmwareFusion)
106114
}

Sources/gitlab-fusion/Stages/Run.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct Run: ParsableCommand {
6565
let base = VirtualMachine(image: baseVMPath, executable: options.vmwareFusion)
6666

6767
/// The name of VMware Fusion guest created by the clone operation
68-
let clonedGuestName = "\(base.name)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
68+
let clonedGuestName = "\(base.name)-\(ciServerHost)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)"
6969

7070
/// The path of the VMware Fusion guest created by the clone operation
7171
let clonedGuestPath = options.vmImagesPath

Sources/gitlab-fusion/main.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ let ciConcurrentProjectId = Environment.CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID ?? 0
6767
/// - SeeAlso: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
6868
let ciProjectPath = Environment.CUSTOM_ENV_CI_PROJECT_PATH ?? ""
6969

70+
/// Host component of the GitLab instance URL, without protocol and port
71+
/// (like `gitlab.example.com`).
72+
///
73+
/// This is pulled from the environment variable
74+
/// `CUSTOM_ENV_CI_SERVER_HOST`. If that variable is unset then the value
75+
/// defaults to `me.lovelett.gitlab-fusion`.
76+
///
77+
/// The `CUSTOM_ENV_CI_SERVER_HOST` is a variation on the typical GitLab
78+
/// predefined environment variable `CI_SERVER_HOST`. The environment variables
79+
/// provided by GitLab are prefixed with `CUSTOM_ENV_` to prevent conflicts with
80+
/// system environment variables.
81+
///
82+
/// - SeeAlso: https://docs.gitlab.com/runner/executors/custom.html#stages
83+
/// - SeeAlso: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
84+
let ciServerHost = Environment.CUSTOM_ENV_CI_SERVER_HOST ?? subsystem
85+
7086
/// Collects the command-line options that were passed to `gitlab-fusion` and
7187
/// dispatches them to the appropriate subcommands (executor stages).
7288
struct GitlabFusion: ParsableCommand {

0 commit comments

Comments
 (0)