From 33ca96e1a0494ed98bcd9c882cc7fb891b6dae9f Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Mon, 17 Jul 2023 12:51:43 +0400 Subject: [PATCH] Retrieve VM's IP for use in VNC after the VM is started #2 (#562) --- Sources/tart/Commands/Run.swift | 26 ++++++++++++++------------ Sources/tart/VM.swift | 4 +++- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Sources/tart/Commands/Run.swift b/Sources/tart/Commands/Run.swift index dd3a547..07d68be 100644 --- a/Sources/tart/Commands/Run.swift +++ b/Sources/tart/Commands/Run.swift @@ -220,17 +220,6 @@ struct Run: AsyncParsableCommand { let task = Task { do { - if let vncImpl = vncImpl { - let vncURL = try await vncImpl.waitForURL() - - if noGraphics || ProcessInfo.processInfo.environment["CI"] != nil { - print("VNC server is running at \(vncURL)") - } else { - print("Opening \(vncURL)...") - NSWorkspace.shared.open(vncURL) - } - } - var resume = false if #available(macOS 14, *) { @@ -243,7 +232,20 @@ struct Run: AsyncParsableCommand { } } - try await vm!.run(recovery: recovery, resume: resume) + try await vm!.start(recovery: recovery, resume: resume) + + if let vncImpl = vncImpl { + let vncURL = try await vncImpl.waitForURL() + + if noGraphics || ProcessInfo.processInfo.environment["CI"] != nil { + print("VNC server is running at \(vncURL)") + } else { + print("Opening \(vncURL)...") + NSWorkspace.shared.open(vncURL) + } + } + + try await vm!.run() if let vncImpl = vncImpl { try vncImpl.stop() diff --git a/Sources/tart/VM.swift b/Sources/tart/VM.swift index d93d875..a97ee2c 100644 --- a/Sources/tart/VM.swift +++ b/Sources/tart/VM.swift @@ -225,7 +225,7 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject { return try VM(vmDir: vmDir) } - func run(recovery: Bool, resume shouldResume: Bool) async throws { + func start(recovery: Bool, resume shouldResume: Bool) async throws { try network.run(sema) if shouldResume { @@ -233,7 +233,9 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject { } else { try await start(recovery) } + } + func run() async throws { await withTaskCancellationHandler(operation: { // Wait for the VM to finish running // or for the exit condition