|
| 1 | +import ArgumentParser |
| 2 | +import CloudMachineCore |
| 3 | +import Foundation |
| 4 | + |
| 5 | +// The machine layout (Time Machine over SMB to a Lima VM) - see vm/README.md. |
| 6 | +// It runs beside the attached-image commands; none of these touch the image. |
| 7 | + |
| 8 | +struct VMSetupCommand: AsyncParsableCommand { |
| 9 | + static let configuration = CommandConfiguration( |
| 10 | + commandName: "vm-setup", |
| 11 | + abstract: L10n.tr( |
| 12 | + "Creates or starts the CloudMachine machine (Lima), gives it rclone's configuration and an SMB password, and prints the Time Machine command." |
| 13 | + )) |
| 14 | + |
| 15 | + @Flag( |
| 16 | + name: .long, |
| 17 | + help: ArgumentHelp( |
| 18 | + L10n.tr("Copy rclone.conf into the machine even if it already has one."))) |
| 19 | + var replaceRcloneConfig = false |
| 20 | + |
| 21 | + @Flag( |
| 22 | + name: .long, |
| 23 | + help: ArgumentHelp(L10n.tr("Also install the vm-start and vm-guard launchd agents."))) |
| 24 | + var installAgents = false |
| 25 | + |
| 26 | + func run() async throws { |
| 27 | + let result = await VMSetup.run(replaceRcloneConfig: replaceRcloneConfig) |
| 28 | + for line in result.messages { print(line) } |
| 29 | + guard result.succeeded else { throw ExitCode.failure } |
| 30 | + if installAgents { |
| 31 | + let agents = await LaunchdInstaller.installVMAgents() |
| 32 | + print(agents.message) |
| 33 | + if !agents.succeeded { throw ExitCode.failure } |
| 34 | + } |
| 35 | + if let command = result.destinationCommand { |
| 36 | + // Printed, never logged: it holds the password. |
| 37 | + print("") |
| 38 | + print( |
| 39 | + L10n.tr("Point Time Machine at the machine (once, needs an administrator password):")) |
| 40 | + print(" \(command)") |
| 41 | + print( |
| 42 | + L10n.tr( |
| 43 | + "When Time Machine has created its empty image, stop the backup (tmutil stopbackup) and run: cloudmachine-agent vm-prepare-bundle" |
| 44 | + )) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +struct VMPrepareBundle: AsyncParsableCommand { |
| 50 | + static let configuration = CommandConfiguration( |
| 51 | + commandName: "vm-prepare-bundle", |
| 52 | + abstract: L10n.tr( |
| 53 | + "Converts Time Machine's new, empty image in the machine to 32 MB bands and moves the bands to Google Drive." |
| 54 | + )) |
| 55 | + |
| 56 | + @Option( |
| 57 | + name: .long, |
| 58 | + help: ArgumentHelp( |
| 59 | + L10n.tr("Image name in the machine (default: from the computer name)."))) |
| 60 | + var bundle: String? |
| 61 | + |
| 62 | + func run() async throws { |
| 63 | + let outcome = await VMBundlePrep.run(bundleOverride: bundle) |
| 64 | + print(outcome.message) |
| 65 | + if !outcome.succeeded { throw ExitCode.failure } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +struct VMGuardCommand: AsyncParsableCommand { |
| 70 | + static let configuration = CommandConfiguration( |
| 71 | + commandName: "vm-guard", |
| 72 | + abstract: L10n.tr( |
| 73 | + "One check of the machine's upload buffer: pauses Time Machine when it fills, resumes what it paused." |
| 74 | + )) |
| 75 | + |
| 76 | + @Option(name: .long, help: ArgumentHelp(L10n.tr("Pause above this many GiB in the buffer."))) |
| 77 | + var highGiB: Int = VMGuard.Thresholds().highGiB |
| 78 | + |
| 79 | + @Option( |
| 80 | + name: .long, |
| 81 | + help: ArgumentHelp(L10n.tr("Resume below this many GiB (only a backup the guard paused)."))) |
| 82 | + var lowGiB: Int = VMGuard.Thresholds().lowGiB |
| 83 | + |
| 84 | + @Option( |
| 85 | + name: .long, |
| 86 | + help: ArgumentHelp(L10n.tr("Pause when the machine has less free than this, in GiB.")) |
| 87 | + ) |
| 88 | + var minFreeGiB: Int = VMGuard.Thresholds().minFreeGiB |
| 89 | + |
| 90 | + func run() async throws { |
| 91 | + let guardian = VMGuard( |
| 92 | + thresholds: .init(highGiB: highGiB, lowGiB: lowGiB, minFreeGiB: minFreeGiB)) |
| 93 | + await guardian.step() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +struct VMStatusCommand: AsyncParsableCommand { |
| 98 | + static let configuration = CommandConfiguration( |
| 99 | + commandName: "vm-status", |
| 100 | + abstract: L10n.tr( |
| 101 | + "Shows the machine: address, services, upload buffer, free space and the Time Machine destination." |
| 102 | + )) |
| 103 | + |
| 104 | + func run() async throws { |
| 105 | + let (lines, healthy) = await VMStatus.lines() |
| 106 | + for line in lines { print(line) } |
| 107 | + if !healthy { throw ExitCode.failure } |
| 108 | + } |
| 109 | +} |
0 commit comments