foundation_deployment_platform 0.1.1

Foundation deployment platform — VM/container orchestration, Docker runtime, guest infrastructure
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! QEMU-only CLI commands (snapshot, resize-disk, mount, adopt, refresh-network, export).
//! These require direct QEMU access (monitor socket, qcow2 manipulation).

use std::path::Path;

use clap::ArgMatches;
use crate::config::{get_profile, DisplayMode};

type BoxedError = Box<dyn std::error::Error + Send + Sync>;

fn resolve_profile(name: &str) -> std::result::Result<crate::config::VmProfile, BoxedError> {
    get_profile(name).map(|p| p.clone()).map_err(|e| Box::new(e) as BoxedError)
}

/// Attach to an already-running VM via its state file + monitor socket.
fn attach_running(profile: &crate::config::VmProfile) -> std::result::Result<crate::qemu::QemuVm, BoxedError> {
    let vm_state = crate::state::load(&profile.name).map_err(|e| Box::new(e) as BoxedError)?;
    let pid = vm_state.pid.ok_or_else(|| format!("VM '{}' is not running", profile.name))?;
    let monitor = std::path::PathBuf::from(&vm_state.monitor_socket);

    let resolved = crate::qemu::ResolvedPorts {
        ssh_port: vm_state.ssh_port,
        winrm_port: vm_state.winrm_port,
        rdp_port: vm_state.rdp_port,
        vnc_port: vm_state.vnc_port,
    };

    let vm = crate::qemu::QemuVm::adopt_running(
        profile.clone(),
        std::path::PathBuf::from(&vm_state.disk_path),
        pid,
        &monitor,
        resolved,
    )?;

    Ok(vm)
}

pub fn cmd_snapshot(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    match args.subcommand() {
        Some(("save", m)) => {
            let name = m.get_one::<String>("profile").unwrap();
            let snap_name = m.get_one::<String>("name").unwrap();
            let profile = resolve_profile(name)?;
            let mut vm = attach_running(&profile)?;
            crate::qemu::snapshot::save(&mut vm, snap_name)?;
            println!("Snapshot '{}' saved for VM '{}'", snap_name, name);
        }
        Some(("load", m)) => {
            let name = m.get_one::<String>("profile").unwrap();
            let snap_name = m.get_one::<String>("name").unwrap();
            let profile = resolve_profile(name)?;
            let mut vm = attach_running(&profile)?;
            crate::qemu::snapshot::load(&mut vm, snap_name)?;
            println!("Restored VM '{}' to snapshot '{}'", name, snap_name);
        }
        Some(("delete", m)) => {
            let name = m.get_one::<String>("profile").unwrap();
            let snap_name = m.get_one::<String>("name").unwrap();
            let profile = resolve_profile(name)?;
            let mut vm = attach_running(&profile)?;
            crate::qemu::snapshot::delete(&mut vm, snap_name)?;
            println!("Snapshot '{}' deleted for VM '{}'", snap_name, name);
        }
        Some(("list", m)) => {
            let name = m.get_one::<String>("profile").unwrap();
            let profile = resolve_profile(name)?;
            let mut vm = attach_running(&profile)?;
            let snapshots = crate::qemu::snapshot::list(&mut vm)?;
            if snapshots.is_empty() {
                println!("No snapshots found for VM '{}'", name);
            } else {
                println!("{:<8} {:<20} {:<12} {:<22}", "ID", "TAG", "SIZE", "DATE");
                println!("{}", "-".repeat(65));
                for snap in &snapshots {
                    let size = if snap.vm_clock_size > 1_073_741_824 {
                        format!("{:.1} GB", snap.vm_clock_size as f64 / 1_073_741_824.0)
                    } else {
                        format!("{:.0} MB", snap.vm_clock_size as f64 / 1_048_576.0)
                    };
                    println!("{:<8} {:<20} {:<12} {:<22}", snap.id, snap.tag, size, snap.date);
                }
            }
        }
        _ => {}
    }
    Ok(())
}

pub fn cmd_resize_disk(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    let name = args.get_one::<String>("profile").unwrap();
    let plus_gb = *args.get_one::<u32>("plus_gb").unwrap();
    let profile = resolve_profile(name)?;

    let disk_path = profile.image_cache_path();
    crate::qemu::disk::resize(&disk_path, plus_gb)?;

    println!("Resized {} by +{}GB", disk_path.display(), plus_gb);
    Ok(())
}

pub fn cmd_mount(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    match args.subcommand() {
        Some(("status", m)) => {
            let name = m.get_one::<String>("profile").unwrap();
            let profile = resolve_profile(name)?;

            let disk_path = profile.image_cache_path();
            let vm_state = crate::state::load(name).ok();

            println!("Mount configuration for VM '{}':", profile.name);
            println!("  OS: {:?}", profile.os);
            println!("  Disk: {}", disk_path.display());
            if let Some(ref s) = vm_state {
                println!("  PID: {:?}", s.pid);
                println!("  Running: {}", s.pid.is_some());
            } else {
                println!("  Running: no");
            }

            println!("  Host path (project): . (current directory)");
            println!("  Guest tag: {}", crate::qemu::mount::DEFAULT_TAG);
            println!("  Guest path: {}", crate::qemu::mount::DEFAULT_GUEST_PATH);
            println!("  Protocol: 9p/virtio");
        }
        Some(("verify", m)) => {
            let name = m.get_one::<String>("profile").unwrap();
            let profile = resolve_profile(name)?;

            let mut session = crate::ssh::connect(&profile)?;
            let verify_cmd = crate::qemu::mount::verify_mount_command(
                crate::qemu::mount::DEFAULT_GUEST_PATH,
            );
            let output = crate::ssh::exec(&mut session, &verify_cmd)?;
            if output.contains("OK") {
                println!("Project mount is active in VM '{}'", profile.name);
            } else {
                println!("Project mount is NOT active in VM '{}'", profile.name);
                println!("  Ensure the VM was started with project mount enabled.");
            }
        }
        _ => {}
    }
    Ok(())
}

/// Wait for VM SSH to be ready, with timeout.
/// Returns the SSH session once connected.
fn wait_for_ssh_ready(
    profile: &crate::config::VmProfile,
    timeout_secs: u64,
) -> Result<crate::ssh::VmSession, BoxedError> {
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(timeout_secs);

    while std::time::Instant::now() < deadline {
        match crate::ssh::connect(profile) {
            Ok(session) => {
                return Ok(session);
            }
            Err(e) => {
                if e.to_string().contains("authentication") {
                    return Err(format!("SSH auth failed: {}", e).into());
                }
                std::thread::sleep(std::time::Duration::from_secs(2));
            }
        }
    }

    Err("Timeout waiting for SSH".into())
}

/// Run mount verification and return results.
fn verify_mount(
    session: &mut crate::ssh::VmSession,
    mount_type: &str,
    guest_path: &str,
    profile: &crate::config::VmProfile,
) -> std::result::Result<(bool, String), BoxedError> {
    let mut success = false;
    let mut diagnosis = String::new();

    match mount_type {
        "virtiofs" => {
            diagnosis.push_str("  Checking virtiofs...\n");

            // Check if virtiofs.exe is available — FAST PATH first
            let check = crate::ssh::exec(session,
                "$p1='C:\\Program Files\\Virtio-Win\\VioFS\\virtiofs.exe'; $p2='C:\\Program Files (x86)\\Virtio-Win\\VioFS\\virtiofs.exe'; if (Test-Path $p1) { 'VIRTIOFS_OK' } elseif (Test-Path $p2) { 'VIRTIOFS_OK_X86' } else { 'VIRTIOFS_MISSING' }");
            match check {
                Ok(output) if output.contains("VIRTIOFS_OK") => {
                    diagnosis.push_str("  ✓ virtiofs.exe found\n");
                }
                _ => {
                    // FALLBACK: Check registry
                    let reg_check = crate::ssh::exec(session,
                        "$reg=Get-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Virtio-win-driver-installer' -ErrorAction SilentlyContinue; if ($reg -and $reg.InstallLocation -and (Test-Path (Join-Path $reg.InstallLocation 'VioFS\\virtiofs.exe'))) { 'VIRTIOFS_OK_REG' } else { 'VIRTIOFS_MISSING' }");
                    match reg_check {
                        Ok(output) if output.contains("VIRTIOFS_OK") => {
                            diagnosis.push_str("  ✓ virtiofs.exe found via registry\n");
                        }
                        _ => {
                            diagnosis.push_str("  ✗ virtiofs.exe not found\n");
                            diagnosis.push_str(&format!("    Fix: Run 'testbed bootstrap {}'\n", profile.name));
                        }
                    }
                }
            }

            // Check mount point
            let mount_check = crate::ssh::exec(session,
                &format!("if (Test-Path '{}') {{ 'MOUNT_OK' }} else {{ 'MOUNT_MISSING' }}", guest_path));
            match mount_check {
                Ok(output) if output.contains("MOUNT_OK") => {
                    // Try to read a file to verify it's working
                    let file_check = crate::ssh::exec(session,
                        &format!("Test-Path (Join-Path '{}' 'Cargo.toml')", guest_path));
                    match file_check {
                        Ok(_) => {
                            diagnosis.push_str(&format!("  ✓ Mount accessible: {}\n", guest_path));
                            success = true;
                        }
                        Err(_) => {
                            diagnosis.push_str(&format!("  ⚠ Mount exists but files not readable\n"));
                        }
                    }
                }
                _ => {
                    diagnosis.push_str(&format!("  ✗ Mount point missing: {}\n", guest_path));
                    diagnosis.push_str("    The scheduled task may not have run.\n");
                    diagnosis.push_str(&format!("    Check: testbed exec {} --method winrm 'Get-ScheduledTask -TaskName FoundationTestbed*'\n", profile.name));
                }
            }
        }
        "smb" => {
            diagnosis.push_str("  Checking SMB...\n");

            // Check if SMB is already mounted
            let smb_check = crate::ssh::exec(session, "if (Test-Path 'Z:\\') { 'SMB_OK' } else { 'SMB_MISSING' }");
            match smb_check {
                Ok(output) if output.contains("SMB_OK") => {
                    diagnosis.push_str("  ✓ SMB mounted at Z:\\\n");
                    success = true;
                }
                _ => {
                    diagnosis.push_str("  → SMB not mounted\n");
                    diagnosis.push_str("    To mount inside VM: net use Z: \\\\\\10.0.2.4\\\\qemu\n");
                    diagnosis.push_str(&format!("    Or: testbed exec {} --method winrm \"net use Z: \\\\\\\\\\10.0.2.4\\\\qemu\"\n", profile.name));
                }
            }
        }
        "9p" => {
            diagnosis.push_str("  Checking 9p mount...\n");
            let mount_check = crate::ssh::exec(session,
                &format!("mount | grep '{}' || echo 'NOT_MOUNTED'", guest_path));
            match mount_check {
                Ok(output) if output.contains("9p") => {
                    diagnosis.push_str(&format!("  ✓ 9p mount active at {}\n", guest_path));
                    success = true;
                }
                _ => {
                    diagnosis.push_str(&format!("  → 9p not mounted, attempting auto-mount...\n"));
                    let mount_cmd = format!("sudo mkdir -p {} && sudo mount -t 9p -o trans=virtio,version=9p2000.L project {}",
                        guest_path, guest_path);
                    match crate::ssh::exec(session, &mount_cmd) {
                        Ok(_) => {
                            diagnosis.push_str("  ✓ Auto-mount successful\n");
                            success = true;
                        }
                        Err(e) => {
                            diagnosis.push_str(&format!("  ✗ Auto-mount failed: {}\n", e));
                            diagnosis.push_str(&format!("    Try manual: testbed shell {}\n", profile.name));
                        }
                    }
                }
            }
        }
        "none" => {
            diagnosis.push_str("  No mount configured (--type=none)\n");
            success = true;
        }
        _ => {
            diagnosis.push_str(&format!("  Unknown mount type: {}\n", mount_type));
        }
    }

    Ok((success, diagnosis))
}

/// Start a VM with specific network/share configuration for testing.
///
/// Without --daemonize: Start VM, wait for ready, verify mount, keep running
/// With --daemonize: Start VM in background thread, wait for ready, verify mount,
///                   print diagnosis, stop VM
pub fn cmd_network(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    use crate::config::{DisplayMode, GuestOs};
    use crate::providers::default_provider;
    use crate::state;
    use std::sync::mpsc;

    let name = args.get_one::<String>("profile").unwrap();
    let profile = resolve_profile(name)?;

    let mount_type = args.get_one::<String>("type").map(|s| s.as_str()).unwrap_or("virtiofs");
    let host_dir = args.get_one::<String>("host-dir").map(|s| s.to_string()).unwrap_or_else(|| ".".to_string());
    let guest_dir = args.get_one::<String>("guest-dir").map(|s| s.to_string());
    let headful = args.get_flag("headful");
    let daemonize = args.get_flag("daemonize");

    let display = if headful { DisplayMode::Headful } else { DisplayMode::Headless };

    // Canonicalize the path
    let canonical_host = std::env::current_dir()?
        .join(&host_dir)
        .canonicalize()
        .map_err(|e| format!("Cannot resolve host directory '{}': {}", host_dir, e))?;

    println!("Starting VM '{}' with network mount configuration:", profile.name);
    println!("  Mount type: {}", mount_type);
    println!("  Host path: {}", canonical_host.display());

    // Determine guest path
    let effective_guest_path = match guest_dir {
        Some(path) => path,
        None => {
            match profile.os {
                GuestOs::Windows => r"C:\Users\vagrant\project".to_string(),
                _ => "/mnt/project".to_string(),
            }
        }
    };
    println!("  Guest path: {}", effective_guest_path);

    // Validate mount type for OS
    match mount_type {
        "virtiofs" => {
            if profile.os != GuestOs::Windows {
                println!("  Note: virtiofs is primarily for Windows. Use '9p' for Linux/macOS.");
            }
        }
        "smb" => {
            if profile.os != GuestOs::Windows {
                return Err("SMB mount is only supported for Windows guests".into());
            }
            println!("  SMB share will be available at: \\10.0.2.4\\qemu");
        }
        "9p" => {
            if profile.os == GuestOs::Windows {
                return Err("9p mount is not supported for Windows guests. Use virtiofs or SMB".into());
            }
        }
        "none" => {}
        _ => unreachable!(),
    }

    // Launch the VM using the default provider
    let provider = default_provider()?;
    let handle = provider.launch(&profile, display)?;

    // Save state
    let pid = handle.pid().map(|p| p as u32);
    let vm_state = state::VmState {
        profile_name: profile.name.to_string(),
        disk_path: profile.image_cache_path().to_string_lossy().to_string(),
        pid,
        provider_id: handle.provider_id,
        provider_internal_id: handle.internal_id.clone(),
        monitor_socket: String::new(),
        ssh_port: handle.resolved_ports.ssh_port,
        winrm_port: handle.resolved_ports.winrm_port,
        rdp_port: handle.resolved_ports.rdp_port,
        vnc_port: handle.resolved_ports.vnc_port,
        bootstrapped: false,
        created_at: chrono::Utc::now().to_rfc3339(),
    };
    state::save(&vm_state)?;

    println!("\n✓ VM '{}' started", profile.name);
    println!("  SSH: 127.0.0.1:{}", handle.resolved_ports.ssh_port);
    if let Some(p) = handle.resolved_ports.winrm_port {
        println!("  WinRM: 127.0.0.1:{p}");
    }
    println!("  VNC: 127.0.0.1:{}", handle.resolved_ports.vnc_port);

    if daemonize {
        println!("\n→ Running in daemonize mode - waiting for VM ready in background...");

        // Create channel for SSH ready signal
        let (ready_tx, ready_rx) = mpsc::channel::<Result<crate::ssh::VmSession, String>>();
        let profile_clone = profile.clone();

        // Spawn background thread to wait for SSH
        std::thread::spawn(move || {
            match wait_for_ssh_ready(&profile_clone, 120) {
                Ok(session) => {
                    let _ = ready_tx.send(Ok(session));
                }
                Err(e) => {
                    let _ = ready_tx.send(Err(e.to_string()));
                }
            }
        });

        // Wait for ready signal (blocking)
        let session = match ready_rx.recv() {
            Ok(Ok(session)) => {
                println!("✓ SSH connected");
                session
            }
            Ok(Err(e)) => {
                println!("✗ SSH connection failed: {}", e);
                println!("\n→ Stopping VM...");
                let _ = provider.stop(&handle);
                return Err("VM did not become ready".into());
            }
            Err(_) => {
                println!("✗ Channel error");
                let _ = provider.stop(&handle);
                return Err("Failed to receive ready signal".into());
            }
        };

        // Run mount verification
        println!("\n→ Running mount verification...");
        let mut session = session;
        let (success, diagnosis) = verify_mount(&mut session, mount_type, &effective_guest_path, &profile)?;

        println!("{}", diagnosis);

        if success {
            println!("\n✓ Mount verification PASSED");
        } else {
            println!("\n⚠ Mount verification FAILED - see diagnosis above");
        }

        // Stop the VM
        println!("\n→ Stopping VM...");
        provider.stop(&handle)?;
        println!("✓ VM stopped");
        println!("\nNetwork test complete.");
        return Ok(());
    }

    // Non-daemonize: wait for SSH in main thread
    println!("\n→ Waiting for VM to be ready...");
    let mut session = match wait_for_ssh_ready(&profile, 120) {
        Ok(session) => {
            println!("✓ SSH connected");
            session
        }
        Err(e) => {
            println!("✗ SSH connection failed: {}", e);
            println!("\nDiagnosis:");
            println!("  1. Check if VM is running: testbed doctor {}", profile.name);
            println!("  2. View VM logs: ls ~/.cache/foundation_testbed/logs/");
            return Err("SSH connection failed".into());
        }
    };

    // Run mount verification
    println!("\n→ Running mount verification...");
    let (success, diagnosis) = verify_mount(&mut session, mount_type, &effective_guest_path, &profile)?;
    println!("{}", diagnosis);

    if success {
        println!("\n✓ Mount verification PASSED");
    } else {
        println!("\n⚠ Mount verification FAILED");
    }

    println!("\n✓ VM is running. Press Ctrl+C to stop or run 'testbed stop {}'", profile.name);

    // Keep process alive
    loop {
        std::thread::sleep(std::time::Duration::from_secs(1));
    }
}

pub fn cmd_adopt(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    let name = args.get_one::<String>("profile").unwrap();
    let disk = args.get_one::<String>("disk").unwrap();
    let profile = resolve_profile(name)?;

    let (adopted_profile, disk_path) = crate::qemu::adopt(&profile, Path::new(disk))?;
    println!("Adopted disk {} as VM '{}'", disk_path.display(), adopted_profile.name);
    println!("Start with: testbed start {}", adopted_profile.name);
    Ok(())
}

pub fn cmd_refresh_network(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    let name = args.get_one::<String>("profile").unwrap();
    let profile = resolve_profile(name)?;

    let vm = crate::qemu::refresh_network(&profile, DisplayMode::Headless)?;
    println!("VM '{}' restarted with fresh ports:", profile.name);
    println!("  SSH: 127.0.0.1:{}", vm.resolved_ports.ssh_port);
    if let Some(p) = vm.resolved_ports.winrm_port {
        println!("  WinRM: 127.0.0.1:{p}");
    }
    println!("  VNC: 127.0.0.1:{}", vm.resolved_ports.vnc_port);
    Ok(())
}

pub fn cmd_export(args: &ArgMatches) -> std::result::Result<(), BoxedError> {
    let name = args.get_one::<String>("profile").unwrap();
    let out = args.get_one::<String>("out").map(|s| std::path::PathBuf::from(s));
    let store = args.get_one::<String>("store").map(|s| s.clone());
    let version = args.get_one::<String>("version").map(|s| s.clone())
        .unwrap_or_else(|| "latest".to_string());
    let notes = args.get_one::<String>("notes").map(|s| s.clone())
        .unwrap_or_default();
    let include_bootstrap = args.get_flag("include-bootstrap");
    let clean = args.get_flag("clean");
    let shrink = args.get_flag("shrink");
    let compression = match args.get_one::<String>("compress").map(|s| s.as_str()) {
        Some("xz") | Some("lzma") => crate::export::Compression::Xz,
        Some("gzip") | Some("gz") => crate::export::Compression::Gzip,
        Some("none") => crate::export::Compression::None,
        Some(other) => return Err(format!("Unknown compression type: {other}. Use xz, gzip, or none.").into()),
        None => crate::export::Compression::Xz, // default: best compression
    };

    let opts = crate::export::ExportOptions {
        out,
        store,
        version,
        notes,
        include_bootstrap,
        clean,
        shrink,
        compression,
    };

    crate::export::export_vm(name, &opts)?;
    Ok(())
}