Option to run with VNC (#97)

* Option to run with VNC

So copy/paste is working. For example, `tart run --vnc latest`.

Related to 14

* Add VNC task first

* Indicate the wait
This commit is contained in:
Fedor Korotkov
2022-05-23 16:49:15 +03:00
committed by GitHub
parent afb23a3e3a
commit 088cdc51a3
2 changed files with 29 additions and 3 deletions
+2 -2
View File
@@ -17,7 +17,7 @@ struct IP: AsyncParsableCommand {
let vmDir = try VMStorageLocal().open(name)
let vmConfig = try VMConfig.init(fromURL: vmDir.configURL)
guard let ip = try await resolveIP(vmConfig, secondsToWait: wait) else {
guard let ip = try await IP.resolveIP(vmConfig, secondsToWait: wait) else {
print("no IP address found, is your VM running?")
Foundation.exit(1)
@@ -33,7 +33,7 @@ struct IP: AsyncParsableCommand {
}
}
private func resolveIP(_ config: VMConfig, secondsToWait: UInt16) async throws -> IPv4Address? {
static public func resolveIP(_ config: VMConfig, secondsToWait: UInt16) async throws -> IPv4Address? {
let waitUntil = Calendar.current.date(byAdding: .second, value: Int(secondsToWait), to: Date.now)!
let vmMacAddress = MACAddress(fromString: config.macAddress.string)!
+27 -1
View File
@@ -5,6 +5,9 @@ import Virtualization
var vm: VM?
struct IPNotFound: Error {
}
struct Run: AsyncParsableCommand {
static var configuration = CommandConfiguration(abstract: "Run a VM")
@@ -14,13 +17,36 @@ struct Run: AsyncParsableCommand {
@Flag var noGraphics: Bool = false
@Flag var recovery: Bool = false
@Flag var vnc: Bool = false
@MainActor
func run() async throws {
if recovery && vnc {
print("You can't run in recovery and use VNC!")
Foundation.exit(1)
}
let vmDir = try VMStorageLocal().open(name)
vm = try VM(vmDir: vmDir)
await withThrowingTaskGroup(of: Void.self) { group in
if vnc {
group.addTask(operation: {
do {
print("Waiting for the VM to boot...")
let resolvedIP = try await IP.resolveIP(vm!.config, secondsToWait: 60)
guard let ip = resolvedIP else {
throw IPNotFound()
}
let url = URL(string: "vnc://\(ip)")!
print("Opening \(url)")
NSWorkspace.shared.open(url)
} catch {
print("Failed to get an IP for screen sharing: \(error)")
}
})
}
group.addTask {
do {
try await vm!.run(recovery)
@@ -33,7 +59,7 @@ struct Run: AsyncParsableCommand {
}
}
if noGraphics {
if noGraphics || vnc {
dispatchMain()
} else {
runUI()