This commit is contained in:
Maxime Van Hees
2025-09-09 10:31:07 +02:00
parent f5670f20be
commit 182b0edeb7
10 changed files with 115 additions and 294 deletions

View File

@@ -171,6 +171,13 @@ pub fn cloudhv_vm_info(id: &str) -> Result<Map, Box<EvalAltResult>> {
}
pub fn cloudhv_discover_ipv4_from_leases(lease_path: &str, mac_lower: &str, timeout_secs: i64) -> Dynamic {
// Check verbosity from environment variable, default to verbose
let verbose = std::env::var("VIRT_VERBOSE").unwrap_or_else(|_| "1".to_string()) == "1";
if verbose {
println!("🔍 Discovering VM network addresses...");
}
match crate::cloudhv::net::discover_ipv4_from_leases(lease_path, mac_lower, timeout_secs as u64) {
Some(ip) => ip.into(),
None => Dynamic::UNIT,
@@ -184,6 +191,45 @@ pub fn cloudhv_discover_ipv6_on_bridge(bridge_name: &str, mac_lower: &str) -> Dy
}
}
pub fn cloudhv_display_network_info(vm_id: &str, ipv4: Dynamic, ipv6: Dynamic) {
// Check verbosity from environment variable, default to verbose
let verbose = std::env::var("VIRT_VERBOSE").unwrap_or_else(|_| "1".to_string()) == "1";
if !verbose {
return;
}
println!("✅ VM {} is ready!", vm_id);
println!("");
println!("🌐 Network Information:");
if ipv4.is_string() && !ipv4.clone().cast::<String>().is_empty() {
println!(" IPv4: {}", ipv4.clone().cast::<String>());
} else {
println!(" IPv4: Not assigned yet (VM may still be configuring)");
}
if ipv6.is_string() && !ipv6.clone().cast::<String>().is_empty() {
println!(" IPv6: {}", ipv6.clone().cast::<String>());
} else {
println!(" IPv6: Not available");
}
println!("");
println!("💡 VM is running in the background. To connect:");
let ssh_addr = if ipv4.is_string() && !ipv4.clone().cast::<String>().is_empty() {
ipv4.cast::<String>()
} else {
"<IPv4>".to_string()
};
println!(" SSH: ssh ubuntu@{}", ssh_addr);
println!("");
println!("🛑 To stop the VM later:");
println!(" cloudhv_vm_stop(\"{}\", false);", vm_id);
println!(" cloudhv_vm_delete(\"{}\", true);", vm_id);
}
// Module registration
pub fn register_cloudhv_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
@@ -195,5 +241,6 @@ pub fn register_cloudhv_module(engine: &mut Engine) -> Result<(), Box<EvalAltRes
engine.register_fn("cloudhv_vm_info", cloudhv_vm_info);
engine.register_fn("cloudhv_discover_ipv4_from_leases", cloudhv_discover_ipv4_from_leases);
engine.register_fn("cloudhv_discover_ipv6_on_bridge", cloudhv_discover_ipv6_on_bridge);
engine.register_fn("cloudhv_display_network_info", cloudhv_display_network_info);
Ok(())
}