tart stop: throw RuntimeError.VMNotRunning consistently and use enumeration instead of strings (#784)

* Use enumeration instead of just strings for VMDirectory state

* tart stop: throw RuntimeError.VMNotRunning consistently
This commit is contained in:
Nikolay Edigaryev 2024-04-10 18:53:04 +04:00 committed by GitHub
parent 13e7794bfc
commit 97b7ffef52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 20 additions and 15 deletions

View File

@ -63,7 +63,7 @@ struct Clone: AsyncParsableCommand {
try lock.lock()
let generateMAC = try localStorage.hasVMsWithMACAddress(macAddress: sourceVM.macAddress())
&& sourceVM.state() != "suspended"
&& sourceVM.state() != .Suspended
try sourceVM.clone(to: tmpVMDir, generateMAC: generateMAC)
try localStorage.move(newName, from: tmpVMDir)

View File

@ -26,8 +26,7 @@ struct Get: AsyncParsableCommand {
let vmConfig = try VMConfig(fromURL: vmDir.configURL)
let memorySizeInMb = vmConfig.memorySize / 1024 / 1024
let info = VMInfo(OS: vmConfig.os, CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: try vmDir.sizeGB(), Size: String(format: "%.3f", Float(try vmDir.allocatedSizeBytes()) / 1000 / 1000 / 1000),
Display: vmConfig.display.description, Running: try vmDir.running(), State: try vmDir.state())
let info = VMInfo(OS: vmConfig.os, CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: try vmDir.sizeGB(), Size: String(format: "%.3f", Float(try vmDir.allocatedSizeBytes()) / 1000 / 1000 / 1000), Display: vmConfig.display.description, Running: try vmDir.running(), State: try vmDir.state().rawValue)
print(format.renderSingle(info))
}
}

View File

@ -38,13 +38,13 @@ struct List: AsyncParsableCommand {
if source == nil || source == "local" {
infos += sortedInfos(try VMStorageLocal().list().map { (name, vmDir) in
try VMInfo(Source: "local", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state())
try VMInfo(Source: "local", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state().rawValue)
})
}
if source == nil || source == "oci" {
infos += sortedInfos(try VMStorageOCI().list().map { (name, vmDir, _) in
try VMInfo(Source: "oci", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state())
try VMInfo(Source: "oci", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state().rawValue)
})
}

View File

@ -159,7 +159,7 @@ struct Run: AsyncParsableCommand {
let localStorage = VMStorageLocal()
let vmDir = try localStorage.open(name)
if try vmDir.state() == "suspended" {
if try vmDir.state() == .Suspended {
suspendable = true
}
@ -182,7 +182,7 @@ struct Run: AsyncParsableCommand {
let vmDir = try localStorage.open(name)
let storageLock = try FileLock(lockURL: Config().tartHomeDir)
if try vmDir.state() == "suspended" {
if try vmDir.state() == .Suspended {
try storageLock.lock() // lock before checking
let needToGenerateNewMac = try localStorage.list().contains {
// check if there is a running VM with the same MAC but different name

View File

@ -15,12 +15,12 @@ struct Stop: AsyncParsableCommand {
func run() async throws {
let vmDir = try VMStorageLocal().open(name)
switch try vmDir.state() {
case "suspended":
case .Suspended:
try stopSuspended(vmDir)
case "running":
case .Running:
try await stopRunning(vmDir)
default:
return
case .Stopped:
throw RuntimeError.VMNotRunning(name)
}
}

View File

@ -3,6 +3,12 @@ import Virtualization
import CryptoKit
struct VMDirectory: Prunable {
enum State: String {
case Running = "running"
case Suspended = "suspended"
case Stopped = "stopped"
}
var baseURL: URL
var configURL: URL {
@ -47,13 +53,13 @@ struct VMDirectory: Prunable {
return try lock.pid() != 0
}
func state() throws -> String {
func state() throws -> State {
if try running() {
return "running"
return State.Running
} else if FileManager.default.fileExists(atPath: stateURL.path) {
return "suspended"
return State.Suspended
} else {
return "stopped"
return State.Stopped
}
}