Prepare, configure, and manage Firecracker microVMs in seconds!
virtualization linux microvm firecracker
at main 82 lines 2.4 kB view raw
1use anyhow::Error; 2 3use crate::date::{format_duration_ago, format_status}; 4 5pub async fn list_all_instances(all: bool) -> Result<(), Error> { 6 let pool = firecracker_state::create_connection_pool().await?; 7 let mut vms = firecracker_state::repo::virtual_machine::all(&pool).await?; 8 if !all { 9 vms = vms 10 .into_iter() 11 .filter(|vm| vm.status == "RUNNING") 12 .collect::<Vec<_>>(); 13 } 14 15 if vms.is_empty() { 16 println!("No Firecracker MicroVM instances found."); 17 return Ok(()); 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; 62 63 println!( 64 "{:<name_length$} {:<distro_length$} {:<vcpu_length$} {:<memory_length$} {:<status_length$} {:<pid_length$} {:<ip_length$} {:<created_length$}", 65 "NAME", "IMAGE", "VCPU", "MEMORY", "STATUS", "PID", "IP", "CREATED" 66 ); 67 for vm in vms { 68 println!( 69 "{:<name_length$} {:<distro_length$} {:<vcpu_length$} {:<memory_length$} {:<status_length$} {:<pid_length$} {:<ip_length$} {:<created_length$}", 70 vm.name, 71 vm.distro, 72 vm.vcpu, 73 format!("{} MiB", vm.memory), 74 format_status(&vm.status, vm.updated_at), 75 vm.pid.unwrap_or(0), 76 vm.ip_address.unwrap_or_default(), 77 format_duration_ago(vm.created_at), 78 ); 79 } 80 81 Ok(()) 82}