|
| 1 | +// |
| 2 | +// Prepare.swift |
| 3 | +// gitlab-fusion |
| 4 | +// |
| 5 | +// Created by Ryan Lovelett on 9/27/20. |
| 6 | +// |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import Environment |
| 10 | +import Foundation |
| 11 | +import os.log |
| 12 | +import Path |
| 13 | +import Shout |
| 14 | + |
| 15 | +private let log = OSLog(subsystem: subsystem, category: "prepare") |
| 16 | + |
| 17 | +private let discussion = """ |
| 18 | +The prepare subcommand is responsible for creating the clean and isolated build |
| 19 | +environment that the job will use. |
| 20 | +
|
| 21 | +To achieve the goal of a clean and isolated build environment this command must |
| 22 | +be provided the path to a base VMware Guest. The prepare subcommand will then |
| 23 | +create a snapshot on base VMware Guest (if necessary) and then make a linked |
| 24 | +clone of the snapshot (if necessary). |
| 25 | +
|
| 26 | +The linked clone will also have a snapshot created. This snapshots will |
| 27 | +represent the clean base state of any job. Finally, the subcommand will restore |
| 28 | +from the snapshot and start the cloned VMware Guest. |
| 29 | +
|
| 30 | +Once the guest is started. The subcommand will wait for the guest to boot and |
| 31 | +provide its IP address via the VMware Guest Additions. Before signaling that |
| 32 | +the guest is working the prepare subcommand will also ensure that the SSH |
| 33 | +server is responding and that the supplied credentials work. |
| 34 | +
|
| 35 | +https://docs.gitlab.com/runner/executors/custom.html#prepare |
| 36 | +""" |
| 37 | + |
| 38 | +/// The prepare stage is responsible for creating the clean and isolated build |
| 39 | +/// environment that the job will use. |
| 40 | +/// |
| 41 | +/// - SeeAlso: https://docs.gitlab.com/runner/executors/custom.html#prepare |
| 42 | +struct Prepare: ParsableCommand { |
| 43 | + static let configuration = CommandConfiguration( |
| 44 | + abstract: "This subcommand should be called by the prepare_exec stage.", |
| 45 | + discussion: discussion |
| 46 | + ) |
| 47 | + |
| 48 | + @OptionGroup() |
| 49 | + var options: StageOptions |
| 50 | + |
| 51 | + // MARK: - Virtual Machine runtime specific arguments |
| 52 | + |
| 53 | + @Argument(help: "Fully qualified path to the base VMware Fusion guest.") |
| 54 | + var baseVMPath: Path |
| 55 | + |
| 56 | + @Flag(help: "Determines if the VMware Fusion guest is started interactively.") |
| 57 | + var isGUI = false |
| 58 | + |
| 59 | + // MARK: - Secure Shell (SSH) specific arguments |
| 60 | + |
| 61 | + @Option(help: "User used to authenticate as over SSH to the VMware Fusion guest.") |
| 62 | + var sshUsername = "buildbot" |
| 63 | + |
| 64 | + @Option(help: "Password used to authenticate as over SSH to the VMware Fusion guest.") |
| 65 | + var sshPassword = "Time2Build" |
| 66 | + |
| 67 | + // MARK: - Validating the command-line input |
| 68 | + |
| 69 | + func validate() throws { |
| 70 | + guard options.vmrunPath.isExecutable else { |
| 71 | + os_log("%{public}@ is not executable.", log: log, type: .error, options.vmrunPath.string) |
| 72 | + throw GitlabRunnerError.systemFailure |
| 73 | + } |
| 74 | + |
| 75 | + guard options.vmImagesPath.exists, options.vmImagesPath.isWritable else { |
| 76 | + os_log("%{public}@ does not exist.", log: log, type: .error, options.vmImagesPath.string) |
| 77 | + throw GitlabRunnerError.systemFailure |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // MARK: - Prepare steps |
| 82 | + |
| 83 | + func run() throws { |
| 84 | + os_log("Prepare stage is starting.", log: log, type: .info) |
| 85 | + |
| 86 | + os_log("The base VMware Fusion guest is %{public}@", log: log, type: .debug, baseVMPath.string) |
| 87 | + let base = VirtualMachine(image: baseVMPath, executable: options.vmrunPath) |
| 88 | + |
| 89 | + /// The name of VMware Fusion guest created by the clone operation |
| 90 | + let clonedGuestName = "\(base.name)-runner-\(ciRunnerId)-concurrent-\(ciConcurrentProjectId)" |
| 91 | + |
| 92 | + // Check if the snapshot exists (creating it if necessary) |
| 93 | + let baseVMSnapshotName = "base-snapshot-\(clonedGuestName)" |
| 94 | + if !base.snapshots.contains(baseVMSnapshotName) { |
| 95 | + FileHandle.standardOutput |
| 96 | + .write(line: "Creating snapshot \"\(baseVMSnapshotName)\" in base guest \"\(base.name)\"...") |
| 97 | + try base.snapshot(baseVMSnapshotName) |
| 98 | + } |
| 99 | + |
| 100 | + /// The path of the VMware Fusion guest created by the clone operation |
| 101 | + let clonedGuestPath = options.vmImagesPath |
| 102 | + .join("\(clonedGuestName).vmwarevm") |
| 103 | + .join("\(clonedGuestName).vmx") |
| 104 | + |
| 105 | + // Check if the VM image exists |
| 106 | + let clone: VirtualMachine |
| 107 | + if !clonedGuestPath.exists { |
| 108 | + FileHandle.standardOutput |
| 109 | + .write(line: "Cloning from snapshot \"\(baseVMSnapshotName)\" in base guest \"\(base.name)\" to \"\(clonedGuestName)\"...") |
| 110 | + clone = try base.clone(to: clonedGuestPath, named: clonedGuestName, linkedTo: baseVMSnapshotName) |
| 111 | + } else { |
| 112 | + clone = VirtualMachine(image: clonedGuestPath, executable: options.vmrunPath) |
| 113 | + } |
| 114 | + |
| 115 | + /// The name of the snapshot to create on linked clone |
| 116 | + let cloneGuestSnapshotName = clonedGuestName |
| 117 | + |
| 118 | + // Check if the snapshot exists |
| 119 | + if clone.snapshots.contains(cloneGuestSnapshotName) { |
| 120 | + FileHandle.standardOutput |
| 121 | + .write(line: "Restoring guest \"\(clonedGuestName)\" from snapshot \"\(cloneGuestSnapshotName)\"...") |
| 122 | + try clone.revert(to: cloneGuestSnapshotName) |
| 123 | + } else { |
| 124 | + FileHandle.standardOutput |
| 125 | + .write(line: "Creating snapshot \"\(cloneGuestSnapshotName)\" in guest \"\(clonedGuestName)\"...") |
| 126 | + try clone.snapshot(cloneGuestSnapshotName) |
| 127 | + } |
| 128 | + |
| 129 | + FileHandle.standardOutput.write(line: "Starting guest \"\(clonedGuestName)\"...") |
| 130 | + try clone.start(hasGUI: isGUI) |
| 131 | + |
| 132 | + FileHandle.standardOutput.write(line: "Waiting for guest \"\(clonedGuestName)\" to become responsive...") |
| 133 | + guard let ip = clone.ip else { |
| 134 | + throw GitlabRunnerError.systemFailure |
| 135 | + } |
| 136 | + |
| 137 | + // Wait for ssh to become available |
| 138 | + for i in 1...60 { |
| 139 | + guard i != 60 else { |
| 140 | + // 'Waited 60 seconds for sshd to start, exiting...' |
| 141 | + throw GitlabRunnerError.systemFailure |
| 142 | + } |
| 143 | + |
| 144 | + // TODO: Encapsulate this for timeout purposes |
| 145 | + let ssh = try SSH(host: ip) |
| 146 | + try ssh.authenticate(username: sshUsername, password: sshPassword) |
| 147 | + let exitCode = try ssh.execute("echo -n 2>&1") |
| 148 | + |
| 149 | + if exitCode == 0 { |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + sleep(60) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments