Files
VirtualBuddy/VirtualUI/Source/Installer/VMInstallationViewModel.swift
Guilherme Rambo d84911d433 Fix duplicate name during setup overwriting existing VM (#776)
* Fix duplicate name during setup overwriting existing VM

* Move VirtualCore tests into a dedicated target
2026-10-02 13:20:13 -03:00

816 lines
27 KiB
Swift

//
// VMInstallationViewModel.swift
// VirtualBuddy
//
// Created by Guilherme Rambo on 07/06/22.
//
import Foundation
import UniformTypeIdentifiers
import Combine
import Virtualization
import VirtualCore
import BuddyUI
public enum VMInstallationStep: Int, Hashable, Codable {
case systemType
case restoreImageInput
case restoreImageSelection
case name
case configuration
case download
case install
case done
}
@MainActor
final class VMInstallationViewModel: ObservableObject, @unchecked Sendable {
struct RestorableState: Codable {
var data: VMInstallData
var step: Step
}
typealias Step = VMInstallationStep
enum State: Hashable {
case idle
case loading(_ progress: Double?, _ info: String?)
case error(_ message: String)
}
private var restorableState: RestorableState {
RestorableState(
data: self.data,
step: self.step
)
}
@Published var machine: VBVirtualMachine?
@Published var data = VMInstallData() {
didSet {
guard data != oldValue else { return }
validate()
}
}
@Published private(set) var state = State.idle
@Published var step = Step.systemType {
didSet {
guard step != oldValue else { return }
performActions(for: step)
writeRestorationData()
}
}
var canGoBack: Bool {
switch step {
case .systemType:
false
case .restoreImageInput, .restoreImageSelection:
/// Only allow going back from restore image selection if a VM bundle has not been created yet.
/// It's possible for this condition to be reached when user goes back after the name step,
/// and we don't want the user to change the guest type after that.
machine == nil
case .name, .configuration, .done:
true
case .download:
if case .failed = downloadState {
true
} else {
false
}
case .install:
if case .error = state {
true
} else {
false
}
}
}
@Published private(set) var buttonTitle = "Continue"
@Published private(set) var showNextButton = true
@Published var disableNextButton = false
@Published private(set) var nameValidationError: String?
private let library: VMLibraryController
init(library: VMLibraryController, restoring restoreVM: VBVirtualMachine?) {
self.library = library
/// Skip OS selection if there's only a single supported OS.
step = VBGuestType.supportedByHost.count > 1 ? .systemType : .restoreImageSelection
if let restoreVM {
restoreInstallation(with: restoreVM)
}
validate()
}
init(library: VMLibraryController, restoringAt restoreURL: URL?, initialStep: Step? = nil) {
self.library = library
/// Skip OS selection if there's only a single supported OS.
step = initialStep ?? (VBGuestType.supportedByHost.count > 1 ? .systemType : .restoreImageSelection)
if let restoreURL {
restoreInstallation(with: restoreURL)
}
validate()
}
private func restoreInstallation(with url: URL) {
do {
let vm = try VBVirtualMachine(bundleURL: url)
restoreInstallation(with: vm)
} catch {
assertionFailure("Couldn't restore install: \(error)")
NSAlert(error: error).runModal()
}
}
private func restoreInstallation(with model: VBVirtualMachine) {
do {
guard let restoreData = model.installRestoreData else {
throw CocoaError(.coderInvalidValue, userInfo: [NSLocalizedDescriptionKey: "VM is missing install restore data"])
}
var restoredState = try PropertyListDecoder.virtualBuddy.decode(RestorableState.self, from: restoreData)
try restoredState.data.resolveCatalogImageIfNeeded(with: model)
self.data = restoredState.data
self.machine = model
self.step = restoredState.step
} catch {
assertionFailure("Couldn't restore install: \(error)")
NSAlert(error: error).runModal()
}
}
private var preventTerminationAssertion: PreventTerminationAssertion?
private func startPreventingAppTermination(forReason reason: String) {
stopPreventingAppTermination()
preventTerminationAssertion = NSApp.preventTermination(reason: reason)
}
private func stopPreventingAppTermination() {
preventTerminationAssertion?.invalidate()
preventTerminationAssertion = nil
}
private func writeRestorationData() {
guard var machine else { return }
machine.metadata.updateRestoreImageURLs(with: data)
do {
let restoreData = try PropertyListEncoder.virtualBuddy.encode(restorableState)
machine.installRestoreData = restoreData
try machine.saveMetadata()
self.machine = machine
} catch {
assertionFailure("Failed to save install restore data: \(error)")
}
}
@Published private(set) var downloader: DownloadBackend?
@SubjectPublisher private(set) var downloadState: DownloadState = .idle
private func validate() {
nameValidationError = nil
if step == .name, machine == nil, FileManager.default.fileExists(atPath: newMachineURL.path) {
nameValidationError = VBVirtualMachine.BundleAlreadyExistsError().localizedDescription
}
disableNextButton = !data.canContinue(from: step) || nameValidationError != nil
}
private var newMachineURL: URL {
library.libraryURL
.appendingPathComponent(data.name)
.appendingPathExtension(VBVirtualMachine.bundleExtension)
}
func next() {
/// It's possible for the user to go back in case of a failed download/install,
/// in which case the naming step will be skipped because the VM has already been named.
func goNextAfterRestoreImageSelection() {
if machine == nil {
step = .name
} else {
step = .configuration
}
}
switch step {
case .systemType:
step = .restoreImageSelection
case .restoreImageInput:
commitCustomRestoreImageURL()
goNextAfterRestoreImageSelection()
case .restoreImageSelection:
commitSelectedRestoreImage()
goNextAfterRestoreImageSelection()
case .name:
validate()
guard !disableNextButton else { return }
do {
try prepareModel()
state = .idle
step = .configuration
} catch let error as VBVirtualMachine.BundleAlreadyExistsError {
nameValidationError = error.localizedDescription
disableNextButton = true
} catch {
state = .error("Failed to prepare VM model: \(error.localizedDescription)")
}
case .configuration:
step = data.needsDownload ? .download : .install
case .download:
step = .install
case .install:
step = .done
case .done:
break
}
}
func back() {
guard canGoBack else { return }
switch step {
case .systemType, .done:
break
case .restoreImageInput:
selectInstallMethod(.remoteOptions)
case .restoreImageSelection:
data.backgroundHash = .virtualBuddyBackground
step = .systemType
case .download, .install:
/// Always nuke restore image selection when backing out of download/install.
/// This ensures that the new selection will be respected when moving forward again.
data.resetRestoreImageSelection()
state = .idle
step = .restoreImageSelection
case .name, .configuration:
/// **NOTE:** When going back from configuration step, all configuration is lost due to how configuration saving is currently tied
/// to the specific "continue" button in the configuration sheet view. This will be addressed alongside other configuration UI changes soon...
/// Re-trigger any UI prompts associated with the install method, such as entering remote URL or selecting local file.
selectInstallMethod(data.installMethod)
}
}
private func performActions(for step: Step) {
defer { validate() }
switch step {
case .systemType:
showNextButton = true
case .restoreImageInput:
showNextButton = true
validateCustomRemoteURL()
case .restoreImageSelection:
showNextButton = true
case .name:
showNextButton = true
case .configuration:
showNextButton = true
do {
try prepareModel()
} catch {
state = .error("Failed to prepare VM model: \(error.localizedDescription)")
}
case .download:
showNextButton = false
DispatchQueue.main.async { self.setupDownload() }
case .install:
Task { await startInstallation() }
showNextButton = false
case .done:
showNextButton = true
buttonTitle = "Back to Library"
}
}
func selectInstallMethod(_ method: InstallMethod) {
UILog("\(#function) \(method)")
data.resetInstallMethodSelectionIfNeeded(selectedMethod: method)
switch method {
case .localFile:
selectInstallFile()
case .remoteOptions:
step = .restoreImageSelection
case .remoteManual:
step = .restoreImageInput
}
}
private func commitCustomRestoreImageURL() {
do {
try data.commitCustomRestoreImageURL()
} catch {
state = .error("\(error)")
}
}
private func commitSelectedRestoreImage() {
guard data.installMethod != .localFile, data.installMethod != .remoteManual else { return }
do {
try data.commitSelectedRestoreImage()
} catch {
state = .error("\(error)")
}
}
private func createDownloadBackend(cookie: String?) -> DownloadBackend {
let Backend: DownloadBackend.Type
#if DEBUG
if UserDefaults.standard.bool(forKey: "VBSimulateDownload") || ProcessInfo.isSwiftUIPreview {
Backend = SimulatedDownloadBackend.self
} else {
Backend = URLSessionDownloadBackend.self
}
#else
Backend = URLSessionDownloadBackend.self
#endif
return Backend.init(library: library, cookie: cookie)
}
private var downloadURL: URL? {
#if DEBUG
guard let url = data.downloadURL else {
if ProcessInfo.isSwiftUIPreview {
return .catalogPlaceholder
} else {
assertionFailure("Expected download URL to be available for download")
return nil
}
}
return url
#else
return data.downloadURL
#endif
}
private func setupDownload() {
guard let url = downloadURL else {
return
}
/// Run TSS check before download to prevent user from spending the time/bandwidth to download a build that will not install successfully.
if data.systemType == .mac, VBSettings.current.enableTSSCheck {
UILog("Requesting TSS check before download.")
Task {
downloadState = .preCheck("Checking Signing Status")
let status = await VBAPIClient.shared.signingStatus(for: url)
switch status {
case .signed:
UILog("TSS check signed, proceeding with download.")
startDownload(with: url)
case .unsigned(let message):
UILog("TSS check UNSIGNED, failing now.")
downloadState = .failed(message)
stopPreventingAppTermination()
case .checkFailed:
/// Performing the check itself failed is ignored to avoid server-side issues preventing users from downloading/installing macOS.
UILog("Ignoring check failed TSS status.")
startDownload(with: url)
}
}
} else {
startDownload(with: url)
}
}
private func startDownload(with url: URL) {
let backend = createDownloadBackend(cookie: data.cookie)
backend.statePublisher.sink { [weak self] state in
self?.downloadState = state
}
.store(in: &cancellables)
self.downloader = backend
backend.statePublisher
.receive(on: DispatchQueue.main)
.sink
{ [weak self] state in
guard let self = self else { return }
switch state {
case .done(let localURL):
stopPreventingAppTermination()
self.handleDownloadCompleted(with: localURL)
case .failed:
downloader = nil
stopPreventingAppTermination()
case .idle, .preCheck, .downloading:
break
}
}.store(in: &cancellables)
startPreventingAppTermination(forReason: "downloading operating system image")
backend.startDownload(with: url)
}
func handleDownloadCompleted(with fileURL: URL) {
downloader = nil
do {
/// Attach metadata to the file so that VirtualBuddy can match it with the restore image in the software catalog
/// even if the user renames the file or moves it within the same volume.
if data.systemType == .mac, let restoreImage = data.restoreImage {
UILog("Attaching metadata for \(restoreImage.name.quoted) to downloaded file \(fileURL.lastPathComponent.quoted)")
fileURL.vb_softwareCatalogData = .init(restoreImage)
}
try updateModelInstallerURL(with: fileURL)
next()
} catch {
state = .error("Failed to update the virtual machine settings after downloading the installer. \(error)")
}
}
private lazy var cancellables = Set<AnyCancellable>()
private var installer: RestoreBackend?
private var progressObservation: NSKeyValueObservation?
private func prepareModel() throws {
/// It's possible for the model to already be available in case installation failed and the user navigated back to restore image selection.
guard machine == nil else {
return
}
let vmURL = newMachineURL
var model: VBVirtualMachine
switch data.systemType {
case .mac:
model = try VBVirtualMachine(bundleURL: vmURL, isNewInstall: true)
case .linux:
model = try VBVirtualMachine(creatingLinuxMachineAt: vmURL)
}
model.metadata.backgroundHash = data.backgroundHash
self.machine = model
writeRestorationData()
}
private func updateModelInstallerURL(with newURL: URL) throws {
assert(machine != nil || ProcessInfo.isSwiftUIPreview, "This method requires the VM model to be available")
assert(newURL.isFileURL || ProcessInfo.isSwiftUIPreview, "This method should be updating the installer URL with a local file URL, not a remote one!")
guard var machine else { return }
data.commitLocalRestoreImageURL(newURL)
machine.metadata.updateRestoreImageURLs(with: data)
try machine.saveMetadata()
}
private func startInstallation() async {
installationStartTime = .now
switch machine?.configuration.systemType {
case .mac:
startMacInstallation()
case .linux:
await startLinuxInstallation()
case .none:
state = .error("Missing VM model or system type")
}
}
@available(macOS 13, *)
private func startLinuxInstallation() async {
guard let installURL = data.localRestoreImageURL else {
state = .error("Missing install image URL")
return
}
guard let model = machine else {
state = .error("Missing VM model")
return
}
do {
let config = try await VMInstance.makeConfiguration(for: model, installImageURL: installURL)
try config.validate()
step = .done
} catch let error as VZError {
handleVirtualMachineValidationError(error)
} catch {
state = .error(error.localizedDescription)
}
}
private func handleVirtualMachineValidationError(_ error: VZError) {
UILog("Validation failed with error \(error)")
let baseMessage = """
Virtualization error \(error.code.rawValue). \(error.localizedDescription)
"""
switch error.code {
case .invalidDiskImage, .invalidRestoreImage, .restoreImageLoadFailed:
let imageType = switch data.systemType {
case .mac: "restore image"
case .linux: "iso image"
}
let detail = """
The \(imageType) on your computer may be corrupted from a faulty download.
Please delete the existing download and try again, or use a different \(imageType).
"""
state = .error(baseMessage + "\n" + detail)
default:
state = .error(baseMessage)
}
}
private func createRestoreBackend(for model: VBVirtualMachine, restoreURL: URL, forceVirtualInstallation: Bool) -> RestoreBackend {
let Backend: RestoreBackend.Type
#if DEBUG
if VirtualInstallationRestoreBackend.isFailureSimulationEnabled {
UILog("Using VirtualInstallation restore backend to simulate an install failure")
Backend = VirtualInstallationRestoreBackend.self
} else if UserDefaults.standard.bool(forKey: "VBSimulateInstall") || ProcessInfo.isSwiftUIPreview {
Backend = SimulatedRestoreBackend.self
} else if restoreURL == SimulatedDownloadBackend.localFileURL {
UILog("⚠️ Using simulated installer because the download was also simulated.")
Backend = SimulatedRestoreBackend.self
} else if forceVirtualInstallation {
Backend = VirtualInstallationRestoreBackend.self
} else {
Backend = VirtualizationRestoreBackend.self
}
#else
if forceVirtualInstallation {
Backend = VirtualInstallationRestoreBackend.self
} else {
Backend = VirtualizationRestoreBackend.self
}
#endif
return Backend.init(model: model, restoringFromImageAt: restoreURL)
}
@Published private(set) var virtualMachine: VZVirtualMachine? = nil
@Published private(set) var consolePredicate: LogStreamer.Predicate? = ProcessInfo.isSwiftUIPreview ? .process("Xcode") : nil
@Published private(set) var installationStartTime = Date.now
@Published private(set) var installationLogFiles = [URL]()
private var installationTask: Task<Void, Never>?
private func startMacInstallation() {
installationTask = Task { await _runMacInstallation() }
}
private func _runMacInstallation() async {
installationLogFiles = []
guard let restoreURL = data.localRestoreImageURL else {
state = .error("Missing local restore image URL")
return
}
guard let model = machine else {
state = .error("Missing VM model")
return
}
state = .loading(nil, "Preparing Installation\nThis may take a moment")
let forceVirtualInstallation: Bool
if data.systemType == .mac {
if UserDefaults.standard.bool(forKey: "VBForceVirtualInstallationBackend") {
UILog("Will use VirtualInstallation restore backend (VBForceVirtualInstallationBackend in user defaults)")
forceVirtualInstallation = true
} else if let resolvedRestoreImage = data.resolvedRestoreImage {
if resolvedRestoreImage.requirements.shouldForceVirtualInstallationBackend {
UILog("Resolved restore image requirement set requires VirtualInstallation backend, enforcing it")
forceVirtualInstallation = true
} else {
forceVirtualInstallation = false
}
} else {
UILog("⚠️ No resolved restore image, so can't determine restore backend to use")
forceVirtualInstallation = false
}
} else {
forceVirtualInstallation = false
}
let backend = createRestoreBackend(for: model, restoreURL: restoreURL, forceVirtualInstallation: forceVirtualInstallation)
installer = backend
consolePredicate = backend.consolePredicate
if let vmProvidingBackend = backend as? VirtualMachineProvidingRestoreBackend {
vmProvidingBackend.virtualMachine.assign(to: &$virtualMachine)
}
progressObservation = backend.progress.observe(\.completedUnitCount) { [weak self] progress, _ in
guard let self = self else { return }
DispatchQueue.main.async {
let percent = Double(progress.completedUnitCount) / Double(progress.totalUnitCount)
self.state = .loading(percent, nil)
}
}
@Sendable func cleanup() {
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.stopPreventingAppTermination()
self.library.loadMachines()
self.installationTask = nil
self.cleanupInstallerArtifacts()
}
}
defer { cleanup() }
await withTaskCancellationHandler {
do {
startPreventingAppTermination(forReason: "restoring virtual machine")
try await backend.install()
try Task.checkCancellation()
self.machine?.metadata.backgroundHash = self.data.backgroundHash
self.machine?.metadata.installFinished = true
self.step = .done
UILog("Installation task finished successfully")
} catch is CancellationError {
} catch let error as RestoreFailure {
UILog("Installation task finished with restore error \(error)")
installationLogFiles = error.diagnosticFileURLs
state = .error(error.localizedDescription)
} catch let error as VZError {
handleVirtualMachineValidationError(error)
} catch {
UILog("Installation task finished with error \(error)")
self.state = .error(error.localizedDescription)
}
} onCancel: {
UILog("Installation task cancelled")
cleanup()
}
}
func validateCustomRemoteURL() {
let isValid = data.validateCustomRestoreImageRemoteURL()
disableNextButton = !isValid
}
func selectInstallFile() {
guard let url = NSOpenPanel.run(accepting: data.systemType.supportedRestoreImageTypes, defaultDirectoryKey: "restoreImage") else {
return
}
data.commitCustomRestoreImageLocalFile(path: url.path)
next()
}
private func cleanupInstallerArtifacts() {
UILog(#function)
progressObservation?.invalidate()
progressObservation = nil
virtualMachine = nil
installer = nil
}
var confirmBeforeClosing: () async -> Bool {
{ [weak self] in
guard let self else { return true }
guard self.needsConfirmationBeforeClosing else { return true }
let confirmed = await NSAlert.runConfirmationAlert(
title: "Cancel Installation?",
message: "If you close the window now, the virtual machine will not be ready for use. You can continue the installation later.",
continueButtonTitle: "Cancel Installation",
cancelButtonTitle: "Continue"
)
guard confirmed else { return false }
await cancelInstallation()
return true
}
}
func cancelInstallation() async {
UILog(#function)
downloader?.cancelDownload()
downloader = nil
await installer?.cancel()
installationTask?.cancel()
}
func exportInstallationLogs() {
let existingLogFiles = installationLogFiles.filter {
FileManager.default.fileExists(atPath: $0.path)
}
guard !existingLogFiles.isEmpty else {
NSAlert(error: CocoaError(
.fileNoSuchFile,
userInfo: [NSLocalizedDescriptionKey: "The MobileDevice log files are no longer available."]
)).runModal()
return
}
guard let parentDirectoryURL = NSOpenPanel.run(
accepting: [.folder],
defaultDirectoryKey: "mobileDeviceLogs",
prompt: String(localized: "Export", bundle: #bundle, comment: "Button in a folder picker that exports MobileDevice restore logs.")
) else { return }
do {
let exportDirectoryURL = uniqueLogExportDirectory(in: parentDirectoryURL)
try FileManager.default.createDirectory(at: exportDirectoryURL, withIntermediateDirectories: false)
for sourceURL in existingLogFiles {
let destinationURL = exportDirectoryURL.appending(component: sourceURL.lastPathComponent)
try FileManager.default.copyItem(at: sourceURL, to: destinationURL)
}
NSWorkspace.shared.activateFileViewerSelecting([exportDirectoryURL])
} catch {
NSAlert(error: error).runModal()
}
}
private func uniqueLogExportDirectory(in parentDirectoryURL: URL) -> URL {
let baseName = String(
localized: "VirtualBuddy MobileDevice Logs",
bundle: #bundle,
comment: "Name of a folder containing exported MobileDevice restore logs."
)
var candidateURL = parentDirectoryURL.appending(path: baseName, directoryHint: .isDirectory)
var suffix = 2
while FileManager.default.fileExists(atPath: candidateURL.path) {
candidateURL = parentDirectoryURL.appending(path: "\(baseName) \(suffix)", directoryHint: .isDirectory)
suffix += 1
}
return candidateURL
}
private var needsConfirmationBeforeClosing: Bool {
/// Require confirmation as soon as a VM bundle has been created for this install and it's not finished.
step != .done && machine != nil
}
}