tart list: show if the VM is running or not (#456)

* tart list: show if the VM is running or not

* Boolean "running" field instead of "state", similarly to "tart get"

* Re-use VMDirectory.running() in "tart get"
This commit is contained in:
Nikolay Edigaryev 2023-03-29 16:51:53 +04:00 committed by GitHub
parent 33a51b7344
commit e72fcd9b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View File

@ -23,9 +23,9 @@ struct Get: AsyncParsableCommand {
let vmConfig = try VMConfig(fromURL: vmDir.configURL)
let diskSizeInGb = try vmDir.sizeGB()
let memorySizeInMb = vmConfig.memorySize / 1024 / 1024
let running = try PIDLock(lockURL: vmDir.configURL).pid() > 0
let info = VMInfo(CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: diskSizeInGb, Display: vmConfig.display.description, Running: running)
let info = VMInfo(CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: diskSizeInGb,
Display: vmConfig.display.description, Running: try vmDir.running())
print(format.renderSingle(info))
}
}

View File

@ -6,6 +6,7 @@ fileprivate struct VMInfo: Encodable {
let Source: String
let Name: String
let Size: Int
let Running: Bool
}
struct List: AsyncParsableCommand {
@ -32,17 +33,19 @@ struct List: AsyncParsableCommand {
func run() async throws {
var infos: [VMInfo] = []
if source == nil || source == "local" {
infos += sortedInfos(try VMStorageLocal().list().map { (name, vmDir) in
try VMInfo(Source: "local", Name: name, Size: vmDir.sizeGB())
try VMInfo(Source: "local", Name: name, Size: vmDir.sizeGB(), Running: vmDir.running())
})
}
if source == nil || source == "oci" {
infos += sortedInfos(try VMStorageOCI().list().map { (name, vmDir, _) in
try VMInfo(Source: "oci", Name: name, Size: vmDir.sizeGB())
try VMInfo(Source: "oci", Name: name, Size: vmDir.sizeGB(), Running: vmDir.running())
})
}
if (quiet) {
for info in infos {
print(info.Name)

View File

@ -26,6 +26,10 @@ struct VMDirectory: Prunable {
baseURL
}
func running() throws -> Bool {
try PIDLock(lockURL: configURL).pid() != 0
}
static func temporary() throws -> VMDirectory {
let tmpDir = try Config().tartTmpDir.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: false)