Skip to content

Commit

Permalink
tart run: provide a hint with names of other running VMs (#900)
Browse files Browse the repository at this point in the history
When VM limit gets exceeded.
  • Loading branch information
edigaryev authored Sep 9, 2024
1 parent 7046886 commit 3da91e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Sources/tart/Commands/Run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,35 @@ struct Run: AsyncParsableCommand {
}
#endif

try await vm!.start(recovery: recovery, resume: resume)
do {
try await vm!.start(recovery: recovery, resume: resume)
} catch let error as VZError {
if error.code == .virtualMachineLimitExceeded {
var hint = ""

do {
let runningVMs: [String] = try localStorage.list().compactMap { (name, vmDir) in
if try !vmDir.running() {
return nil
}

return name
}

if !runningVMs.isEmpty {
let runningVMsJoined = runningVMs.joined(separator: ", ")

hint = " (other running VMs: \(runningVMsJoined))"
}
} catch {
// we can't provide any hint
}

throw RuntimeError.VirtualMachineLimitExceeded(hint)
}

throw error
}

if let vncImpl = vncImpl {
let vncURL = try await vncImpl.waitForURL(netBridged: !netBridged.isEmpty)
Expand Down
3 changes: 3 additions & 0 deletions Sources/tart/VMStorageHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ enum RuntimeError : Error {
case OCIUnsupportedDiskFormat(_ format: String)
case SuspendFailed(_ message: String)
case PullFailed(_ message: String)
case VirtualMachineLimitExceeded(_ hint: String)
}

protocol HasExitCode {
Expand Down Expand Up @@ -128,6 +129,8 @@ extension RuntimeError : CustomStringConvertible {
return "Failed to suspend the VM: \(message)"
case .PullFailed(let message):
return message
case .VirtualMachineLimitExceeded(let hint):
return "The number of VMs exceeds the system limit\(hint)"
}
}
}
Expand Down

0 comments on commit 3da91e6

Please sign in to comment.