Prepare, configure, and manage Firecracker microVMs in seconds!
virtualization linux microvm firecracker

Merge pull request #7 from tsirysndr/fix/table-rendering

fix: update virtual machine status formatting and add updated timestamp in database

authored by tsiry-sandratraina.com and committed by

GitHub 34943993 2109a1c1

+57 -5
+2 -1
crates/firecracker-state/src/repo/virtual_machine.rs
··· 120 120 distro = ?, 121 121 vmlinux = ?, 122 122 rootfs = ?, 123 - bootargs = ? 123 + bootargs = ?, 124 + updated_at = CURRENT_TIMESTAMP 124 125 WHERE id = ?"#, 125 126 ) 126 127 .bind(&vm.project_dir)
+47 -4
crates/firecracker-up/src/cmd/ps.rs
··· 1 1 use anyhow::Error; 2 2 3 - use crate::date::format_duration_ago; 3 + use crate::date::{format_duration_ago, format_status}; 4 4 5 5 pub async fn list_all_instances(all: bool) -> Result<(), Error> { 6 6 let pool = firecracker_state::create_connection_pool().await?; ··· 16 16 println!("No Firecracker MicroVM instances found."); 17 17 return Ok(()); 18 18 } 19 + let distro_length = vms.iter().map(|vm| vm.distro.len()).max().unwrap_or(10) + 2; 20 + let name_length = vms 21 + .iter() 22 + .map(|vm| vm.name.len()) 23 + .max() 24 + .unwrap_or(10) 25 + .max(10) 26 + + 2; 27 + let vcpu_length = vms 28 + .iter() 29 + .map(|vm| vm.vcpu.to_string().len()) 30 + .max() 31 + .unwrap_or(10) 32 + + 2; 33 + let memory_length = vms 34 + .iter() 35 + .map(|vm| format!("{} MiB", vm.memory).len()) 36 + .max() 37 + .unwrap_or(10) 38 + + 2; 39 + let status_length = vms 40 + .iter() 41 + .map(|vm| format_status(&vm.status, vm.updated_at).len()) 42 + .max() 43 + .unwrap_or(10); 44 + let pid_length = vms 45 + .iter() 46 + .map(|vm| vm.pid.unwrap_or(0).to_string().len()) 47 + .max() 48 + .unwrap_or(10) 49 + + 2; 50 + let ip_length = vms 51 + .iter() 52 + .map(|vm| vm.ip_address.clone().unwrap_or_default().len()) 53 + .max() 54 + .unwrap_or(10) 55 + + 2; 56 + let created_length = vms 57 + .iter() 58 + .map(|vm| format_duration_ago(vm.created_at).len()) 59 + .max() 60 + .unwrap_or(10) 61 + + 2; 19 62 20 63 println!( 21 - "{:<20} {:<10} {:<5} {:<10} {:<15} {:<10} {:<15} {:<10}", 64 + "{:<name_length$} {:<distro_length$} {:<vcpu_length$} {:<memory_length$} {:<status_length$} {:<pid_length$} {:<ip_length$} {:<created_length$}", 22 65 "NAME", "DISTRO", "VCPU", "MEMORY", "STATUS", "PID", "IP", "CREATED" 23 66 ); 24 67 for vm in vms { 25 68 println!( 26 - "{:<20} {:<10} {:<5} {:<10} {:<15} {:<10} {:<15} {:<10}", 69 + "{:<name_length$} {:<distro_length$} {:<vcpu_length$} {:<memory_length$} {:<status_length$} {:<pid_length$} {:<ip_length$} {:<created_length$}", 27 70 vm.name, 28 71 vm.distro, 29 72 vm.vcpu, 30 73 format!("{} MiB", vm.memory), 31 - vm.status, 74 + format_status(&vm.status, vm.updated_at), 32 75 vm.pid.unwrap_or(0), 33 76 vm.ip_address.unwrap_or_default(), 34 77 format_duration_ago(vm.created_at),
+8
crates/firecracker-up/src/date.rs
··· 36 36 ) 37 37 } 38 38 } 39 + 40 + pub fn format_status(status: &str, date: DateTime<Utc>) -> String { 41 + if status == "RUNNING" { 42 + format!("Up {}", format_duration_ago(date)) 43 + } else { 44 + status.to_string() 45 + } 46 + }