#!/usr/bin/env herodo
// Production VM with Network Access
// This script starts an Ubuntu VM with internet connectivity and SSH access

// ============================================================================
// SETUP REQUIRED: Edit these paths for your environment
// ============================================================================
let cloud_dir = "/path/to/vm-images";  // EDIT: Directory with VM images
// ============================================================================

print("╔════════════════════════════════════════════════════════════╗");
print("║  Starting Ubuntu VM with Network Access                    ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

print("🔧 Initializing Cloud Hypervisor Runtime...");
let runtime = RuntimeBuilder()
    .ch_path("/usr/local/bin/cloud-hypervisor")
    .api_socket("/tmp/ch-vm.sock")
    .seccomp(true)
    .landlock(true);

if !init_runtime(runtime) {
    print("   ❌ Failed to initialize runtime. Exiting.");
    return;
}
print("   ✓ Runtime initialized");
print("");

print("🔧 Building VM Configuration...");

let vm_spec = VMBuilder()
    .kernel(cloud_dir + "/vmlinuz-cloud")
    .initramfs(cloud_dir + "/initrd-cloud.img")
    .cmdline("console=ttyS0 root=/dev/vda1 rootfstype=ext4 rw")
    .cpus(2)
    .memory(2048)
    .virtio_blk(cloud_dir + "/ubuntu.raw", false)
    .virtio_blk(cloud_dir + "/cloud-init-network.iso", true)
    .virtio_net("tap-ch0")
    .serial("tty")
    .console("off")
    .build();

print("   ✓ Kernel: vmlinuz-cloud (5.15.0-164-generic)");
print("   ✓ Initramfs: initrd-cloud.img");
print("   ✓ CPUs: 2");
print("   ✓ Memory: 2 GB");
print("   ✓ Disk: ubuntu.raw (10 GB)");
print("   ✓ Cloud-init: cloud-init-network.iso");
print("   ✓ Network: tap-ch0");
print("");

print("🎯 Creating VM...");
let vm = create_vm(vm_spec);
print("   ✓ VM created: " + vm.id);
print("");

print("🚀 Starting VM...");
start_vm(vm);
print("   ✓ VM started!");
print("");

print("╔════════════════════════════════════════════════════════════╗");
print("║  VM IS RUNNING                                             ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

print("📊 VM Information:");
print("   Hostname: ubuntu-vm");
print("   IP: 192.168.100.10");
print("   Gateway: 192.168.100.1");
print("   DNS: 8.8.8.8");
print("");

print("🔐 SSH Access:");
print("   ssh -p 2222 ubuntu@localhost");
print("   Password: password123");
print("");

print("🔌 API Access:");
print("   Socket: /tmp/ch-vm.sock");
print("   Example: curl --unix-socket /tmp/ch-vm.sock http://localhost/api/v1/vm.info");
print("");

print("⏳ Waiting for VM to be ready...");
let max_wait = 120;  // Maximum 120 seconds
let elapsed = 0;
let ready = false;

while elapsed < max_wait {
    let status = get_vm_status();
    print("   Status: " + status + " (" + elapsed + "s)");
    
    if status == "Running" {
        ready = true;
        break;
    }
    
    sleep(5000);  // Check every 5 seconds
    elapsed += 5;
}

if ready {
    print("   ✓ VM is running!");
    print("");
    print("⏳ Waiting for cloud-init to complete (30 more seconds)...");
    sleep(30000);
    print("   ✓ Cloud-init should be complete");
} else {
    print("   ⚠ VM did not reach Running state within " + max_wait + " seconds");
}
print("");

print("✅ VM is ready! Press Ctrl+C to stop.");
print("");

// Keep VM running indefinitely
loop {
    sleep(60000);  // Sleep for 60 seconds at a time
}
