mvm-cli 0.11.0

CLI commands, UI, and bootstrap for mvm
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
use std::collections::BTreeMap;
use std::path::PathBuf;

use anyhow::{Context, Result};
use serde::Deserialize;

/// Project-level fleet configuration loaded from `mvm.toml`.
///
/// Defines a set of named VMs that share a Nix flake reference.
/// Each VM can override resource defaults (cpus, memory, profile).
#[derive(Debug, Deserialize)]
pub struct FleetConfig {
    /// Nix flake reference, shared across all VMs.
    pub flake: String,

    /// Default resource settings applied to all VMs unless overridden.
    #[serde(default)]
    pub defaults: FleetDefaults,

    /// Named VM definitions. BTreeMap for deterministic ordering.
    #[serde(default)]
    pub vms: BTreeMap<String, VmConfig>,
}

#[derive(Debug, Deserialize, Default)]
pub struct FleetDefaults {
    #[serde(default)]
    pub cpus: Option<u32>,

    #[serde(default)]
    pub memory: Option<u32>,

    #[serde(default)]
    pub profile: Option<String>,

    /// Default port mappings applied to all VMs (format: "HOST:GUEST" or "PORT").
    #[serde(default)]
    pub ports: Vec<String>,

    /// Default environment variables injected into all VMs (format: "KEY=VALUE").
    #[serde(default)]
    pub env: Vec<String>,
}

#[derive(Debug, Deserialize, Default)]
pub struct VmConfig {
    #[serde(default)]
    pub profile: Option<String>,

    #[serde(default)]
    pub cpus: Option<u32>,

    #[serde(default)]
    pub memory: Option<u32>,

    #[serde(default)]
    pub volumes: Vec<String>,

    /// Port mappings (format: "HOST:GUEST" or "PORT"). Replaces defaults if non-empty.
    #[serde(default)]
    pub ports: Vec<String>,

    /// Environment variables (format: "KEY=VALUE"). Replaces defaults if non-empty.
    #[serde(default)]
    pub env: Vec<String>,
}

const DEFAULT_CPUS: u32 = 2;
const DEFAULT_MEM: u32 = 1024;

/// Resolved VM configuration after merging VM-level > defaults > hardcoded.
pub struct ResolvedVm {
    pub name: String,
    pub profile: Option<String>,
    pub cpus: u32,
    pub memory: u32,
    pub volumes: Vec<String>,
    /// Merged port mappings (VM-specific replaces defaults).
    pub ports: Vec<String>,
    /// Merged environment variables (VM-specific replaces defaults).
    pub env: Vec<String>,
}

/// Search for `mvm.toml` starting from cwd, walking up the directory tree.
///
/// Returns `(config, directory_containing_mvm_toml)` so flake paths can be
/// resolved relative to the config file location.
pub fn find_fleet_config() -> Result<Option<(FleetConfig, PathBuf)>> {
    let mut dir = std::env::current_dir()?;
    loop {
        let candidate = dir.join("mvm.toml");
        if candidate.is_file() {
            let content = std::fs::read_to_string(&candidate)
                .with_context(|| format!("Failed to read {}", candidate.display()))?;
            let config: FleetConfig = toml::from_str(&content)
                .with_context(|| format!("Failed to parse {}", candidate.display()))?;
            return Ok(Some((config, dir)));
        }
        if !dir.pop() {
            return Ok(None);
        }
    }
}

/// Parse a fleet config from a TOML string.
pub fn parse_fleet_config(content: &str) -> Result<FleetConfig> {
    toml::from_str(content).context("Failed to parse fleet config")
}

/// Resolve a single VM's effective configuration by merging:
/// VM-specific > [defaults] > hardcoded defaults.
pub fn resolve_vm(fleet: &FleetConfig, name: &str) -> Result<ResolvedVm> {
    let vm = fleet
        .vms
        .get(name)
        .ok_or_else(|| anyhow::anyhow!("VM '{}' not defined in fleet config", name))?;

    let profile = vm
        .profile
        .clone()
        .or_else(|| fleet.defaults.profile.clone());

    let cpus = vm.cpus.or(fleet.defaults.cpus).unwrap_or(DEFAULT_CPUS);

    let memory = vm.memory.or(fleet.defaults.memory).unwrap_or(DEFAULT_MEM);

    // Ports: VM-specific replaces defaults entirely (fall through if empty)
    let ports = if vm.ports.is_empty() {
        fleet.defaults.ports.clone()
    } else {
        vm.ports.clone()
    };

    // Env: VM-specific replaces defaults entirely (fall through if empty)
    let env = if vm.env.is_empty() {
        fleet.defaults.env.clone()
    } else {
        vm.env.clone()
    };

    Ok(ResolvedVm {
        name: name.to_string(),
        profile,
        cpus,
        memory,
        volumes: vm.volumes.clone(),
        ports,
        env,
    })
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_full_config() {
        let toml = r#"
            flake = "./nix/examples/openclaw/"

            [defaults]
            cpus = 2
            memory = 1024

            [vms.gw]
            profile = "gateway"

            [vms.w1]
            profile = "worker"

            [vms.w2]
            profile = "worker"
            cpus = 4
            memory = 2048
            volumes = ["./data:/mnt/data:2G"]
        "#;

        let config = parse_fleet_config(toml).unwrap();
        assert_eq!(config.flake, "./nix/examples/openclaw/");
        assert_eq!(config.defaults.cpus, Some(2));
        assert_eq!(config.defaults.memory, Some(1024));
        assert_eq!(config.vms.len(), 3);

        let gw = &config.vms["gw"];
        assert_eq!(gw.profile.as_deref(), Some("gateway"));
        assert_eq!(gw.cpus, None);

        let w2 = &config.vms["w2"];
        assert_eq!(w2.cpus, Some(4));
        assert_eq!(w2.memory, Some(2048));
        assert_eq!(w2.volumes, vec!["./data:/mnt/data:2G"]);
    }

    #[test]
    fn test_parse_config_with_ports_and_env() {
        let toml = r#"
            flake = "."

            [defaults]
            ports = ["3333:3000", "3334:3002"]
            env = ["NODE_ENV=production"]

            [vms.oc]
            profile = "gateway"
            ports = ["8080:3000"]
            env = ["OPENCLAW_EXTERNAL_PORT=8080"]

            [vms.worker]
            profile = "worker"
        "#;

        let config = parse_fleet_config(toml).unwrap();
        assert_eq!(config.defaults.ports, vec!["3333:3000", "3334:3002"]);
        assert_eq!(config.defaults.env, vec!["NODE_ENV=production"]);

        let oc = &config.vms["oc"];
        assert_eq!(oc.ports, vec!["8080:3000"]);
        assert_eq!(oc.env, vec!["OPENCLAW_EXTERNAL_PORT=8080"]);

        let worker = &config.vms["worker"];
        assert!(worker.ports.is_empty());
        assert!(worker.env.is_empty());
    }

    #[test]
    fn test_parse_minimal_config() {
        let toml = r#"
            flake = "."

            [vms.dev]
            profile = "worker"
        "#;

        let config = parse_fleet_config(toml).unwrap();
        assert_eq!(config.flake, ".");
        assert_eq!(config.defaults.cpus, None);
        assert_eq!(config.defaults.memory, None);
        assert!(config.defaults.ports.is_empty());
        assert!(config.defaults.env.is_empty());
        assert_eq!(config.vms.len(), 1);
    }

    #[test]
    fn test_parse_no_vms() {
        let toml = r#"flake = ".""#;

        let config = parse_fleet_config(toml).unwrap();
        assert!(config.vms.is_empty());
    }

    #[test]
    fn test_parse_requires_flake() {
        let toml = r#"
            [vms.dev]
            profile = "worker"
        "#;

        let result = parse_fleet_config(toml);
        assert!(result.is_err());
    }

    #[test]
    fn test_resolve_vm_uses_vm_level_overrides() {
        let config = parse_fleet_config(
            r#"
            flake = "."
            [defaults]
            cpus = 2
            memory = 1024

            [vms.big]
            profile = "worker"
            cpus = 8
            memory = 4096
        "#,
        )
        .unwrap();

        let resolved = resolve_vm(&config, "big").unwrap();
        assert_eq!(resolved.cpus, 8);
        assert_eq!(resolved.memory, 4096);
        assert_eq!(resolved.profile.as_deref(), Some("worker"));
    }

    #[test]
    fn test_resolve_vm_falls_through_to_defaults() {
        let config = parse_fleet_config(
            r#"
            flake = "."
            [defaults]
            cpus = 4
            memory = 2048
            profile = "worker"

            [vms.small]
        "#,
        )
        .unwrap();

        let resolved = resolve_vm(&config, "small").unwrap();
        assert_eq!(resolved.cpus, 4);
        assert_eq!(resolved.memory, 2048);
        assert_eq!(resolved.profile.as_deref(), Some("worker"));
    }

    #[test]
    fn test_resolve_vm_falls_through_to_hardcoded() {
        let config = parse_fleet_config(
            r#"
            flake = "."
            [vms.bare]
        "#,
        )
        .unwrap();

        let resolved = resolve_vm(&config, "bare").unwrap();
        assert_eq!(resolved.cpus, DEFAULT_CPUS);
        assert_eq!(resolved.memory, DEFAULT_MEM);
        assert!(resolved.profile.is_none());
        assert!(resolved.ports.is_empty());
        assert!(resolved.env.is_empty());
    }

    #[test]
    fn test_resolve_vm_not_found() {
        let config = parse_fleet_config(r#"flake = ".""#).unwrap();
        let result = resolve_vm(&config, "missing");
        assert!(result.is_err());
    }

    #[test]
    fn test_vm_ordering_is_deterministic() {
        let config = parse_fleet_config(
            r#"
            flake = "."
            [vms.charlie]
            [vms.alpha]
            [vms.bravo]
        "#,
        )
        .unwrap();

        let names: Vec<&str> = config.vms.keys().map(|s| s.as_str()).collect();
        assert_eq!(names, vec!["alpha", "bravo", "charlie"]);
    }

    #[test]
    fn test_resolve_profile_priority() {
        // VM profile beats defaults profile
        let config = parse_fleet_config(
            r#"
            flake = "."
            [defaults]
            profile = "worker"

            [vms.gw]
            profile = "gateway"

            [vms.w1]
        "#,
        )
        .unwrap();

        let gw = resolve_vm(&config, "gw").unwrap();
        assert_eq!(gw.profile.as_deref(), Some("gateway"));

        let w1 = resolve_vm(&config, "w1").unwrap();
        assert_eq!(w1.profile.as_deref(), Some("worker"));
    }

    #[test]
    fn test_resolve_ports_vm_overrides_defaults() {
        let config = parse_fleet_config(
            r#"
            flake = "."
            [defaults]
            ports = ["3333:3000", "3334:3002"]

            [vms.oc]
            ports = ["8080:3000"]

            [vms.worker]
        "#,
        )
        .unwrap();

        // VM-level ports replace defaults entirely
        let oc = resolve_vm(&config, "oc").unwrap();
        assert_eq!(oc.ports, vec!["8080:3000"]);

        // No VM-level ports → fall through to defaults
        let worker = resolve_vm(&config, "worker").unwrap();
        assert_eq!(worker.ports, vec!["3333:3000", "3334:3002"]);
    }

    #[test]
    fn test_resolve_env_vm_overrides_defaults() {
        let config = parse_fleet_config(
            r#"
            flake = "."
            [defaults]
            env = ["NODE_ENV=production"]

            [vms.oc]
            env = ["NODE_ENV=development", "DEBUG=true"]

            [vms.worker]
        "#,
        )
        .unwrap();

        let oc = resolve_vm(&config, "oc").unwrap();
        assert_eq!(oc.env, vec!["NODE_ENV=development", "DEBUG=true"]);

        let worker = resolve_vm(&config, "worker").unwrap();
        assert_eq!(worker.env, vec!["NODE_ENV=production"]);
    }
}