Skip to main content

mvm_core/
build_env.rs

1use anyhow::Result;
2
3use crate::instance::InstanceNet;
4use crate::pool::{BuildRevision, PoolSpec};
5use crate::tenant::{TenantConfig, TenantNet};
6
7/// Minimal shell execution abstraction used by dev-mode builds.
8///
9/// This trait provides just shell execution and logging — enough for
10/// `dev_build()` which runs `nix build` directly in the Lima VM.
11pub trait ShellEnvironment: Send + Sync {
12    /// Execute a shell script in the VM.
13    fn shell_exec(&self, script: &str) -> Result<()>;
14
15    /// Execute a shell script in the VM and capture stdout.
16    fn shell_exec_stdout(&self, script: &str) -> Result<String>;
17
18    /// Execute a shell script with visible output.
19    fn shell_exec_visible(&self, script: &str) -> Result<()>;
20
21    /// Log an informational message.
22    fn log_info(&self, msg: &str);
23
24    /// Log a success message.
25    fn log_success(&self, msg: &str);
26
27    /// Log a warning (optional; default no-op for test fakes).
28    fn log_warn(&self, _msg: &str) {
29        // default implementation: no-op
30    }
31
32    /// Execute a shell script, capturing both stdout and stderr.
33    ///
34    /// Returns `(stdout, stderr)` on success. On failure, returns the exit code and
35    /// captured stderr in the error. Used by the build pipeline to capture nix build
36    /// errors for structured reporting.
37    ///
38    /// Default: falls back to `shell_exec_stdout` (stderr not captured).
39    fn shell_exec_capture(&self, script: &str) -> Result<(String, String)> {
40        let stdout = self.shell_exec_stdout(script)?;
41        Ok((stdout, String::new()))
42    }
43}
44
45/// Full build environment for orchestrated pool builds.
46///
47/// Extends [`ShellEnvironment`] with tenant/pool/network operations needed
48/// by the pool build pipeline (ephemeral FC builder VMs, artifact recording).
49/// mvm-build depends on mvm-core only. At runtime, the orchestrator provides
50/// a concrete implementation that delegates to the runtime modules.
51pub trait BuildEnvironment: ShellEnvironment {
52    /// Load a pool spec from the filesystem.
53    fn load_pool_spec(&self, tenant_id: &str, pool_id: &str) -> Result<PoolSpec>;
54
55    /// Load a tenant config from the filesystem.
56    fn load_tenant_config(&self, tenant_id: &str) -> Result<TenantConfig>;
57
58    /// Ensure the tenant network bridge is up.
59    fn ensure_bridge(&self, net: &TenantNet) -> Result<()>;
60
61    /// Create and attach a TAP device for a VM.
62    fn setup_tap(&self, net: &InstanceNet, bridge_name: &str) -> Result<()>;
63
64    /// Remove a TAP device.
65    fn teardown_tap(&self, tap_dev: &str) -> Result<()>;
66
67    /// Record a build revision and update the current symlink.
68    fn record_revision(
69        &self,
70        tenant_id: &str,
71        pool_id: &str,
72        revision: &BuildRevision,
73    ) -> Result<()>;
74}