arcbox-core 0.4.9

Core orchestration layer for ArcBox
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
//! Machine configuration persistence.
//!
//! Stores machine configurations to disk so they survive process restarts.

use crate::error::{CoreError, Result};
use crate::machine::{MachineInfo, MachineState};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;
use std::path::PathBuf;

/// Persisted machine data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistedMachine {
    /// Machine name.
    pub name: String,
    /// Number of CPUs.
    pub cpus: u32,
    /// Memory in MB.
    pub memory_mb: u64,
    /// Disk size in GB.
    pub disk_gb: u64,
    /// Kernel path.
    #[serde(default)]
    pub kernel: Option<String>,
    /// Kernel command line.
    #[serde(default)]
    pub cmdline: Option<String>,
    /// Block devices (e.g., EROFS rootfs, Btrfs data disk).
    #[serde(default)]
    pub block_devices: Vec<crate::vm::BlockDeviceConfig>,
    /// Distribution name.
    #[serde(default)]
    pub distro: Option<String>,
    /// Distribution version.
    #[serde(default)]
    pub distro_version: Option<String>,
    /// Path to the disk image.
    #[serde(default)]
    pub disk_path: Option<String>,
    /// Path to the SSH private key.
    #[serde(default)]
    pub ssh_key_path: Option<String>,
    /// Last known IP address.
    #[serde(default)]
    pub ip_address: Option<String>,
    /// Last known state.
    pub state: PersistedState,
    /// VM ID (for correlation).
    pub vm_id: String,
    /// Creation timestamp.
    #[serde(default = "default_created_at")]
    pub created_at: DateTime<Utc>,
}

/// Default creation time for backward compatibility with old configs.
fn default_created_at() -> DateTime<Utc> {
    Utc::now()
}

/// Persisted machine state.
///
/// Records the last known state when the daemon wrote the config.
/// On reload, [`MachineState::from`] maps this to the *current* state
/// (e.g. `Running` → `Stopped` because the VM process is gone).
/// Use [`Self::needs_recovery`] to distinguish a clean stop from a
/// crash/daemon restart.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum PersistedState {
    #[default]
    Created,
    Running,
    Stopped,
}

impl PersistedState {
    /// Returns `true` if the machine was running when the daemon last
    /// persisted state — meaning it was interrupted and may need recovery
    /// (e.g. networking re-registration, container reconciliation).
    #[must_use]
    pub const fn needs_recovery(self) -> bool {
        matches!(self, Self::Running)
    }
}

impl From<MachineState> for PersistedState {
    fn from(state: MachineState) -> Self {
        match state {
            MachineState::Created => Self::Created,
            MachineState::Starting | MachineState::Running => Self::Running,
            MachineState::Stopping | MachineState::Stopped => Self::Stopped,
        }
    }
}

impl From<PersistedState> for MachineState {
    /// Maps persisted state to current machine state on reload.
    ///
    /// A machine persisted as `Running` is mapped to `Stopped` because
    /// the VM process is no longer alive after a daemon restart. Use
    /// [`PersistedState::needs_recovery`] to detect this case and
    /// trigger recovery logic.
    fn from(state: PersistedState) -> Self {
        match state {
            PersistedState::Created => Self::Created,
            PersistedState::Running => Self::Stopped,
            PersistedState::Stopped => Self::Stopped,
        }
    }
}

impl From<&MachineInfo> for PersistedMachine {
    fn from(info: &MachineInfo) -> Self {
        Self {
            name: info.name.clone(),
            cpus: info.cpus,
            memory_mb: info.memory_mb,
            disk_gb: info.disk_gb,
            kernel: info.kernel.clone(),
            cmdline: info.cmdline.clone(),
            block_devices: info.block_devices.clone(),
            distro: info.distro.clone(),
            distro_version: info.distro_version.clone(),
            disk_path: info
                .disk_path
                .as_ref()
                .map(|p| p.to_string_lossy().to_string()),
            ssh_key_path: info
                .ssh_key_path
                .as_ref()
                .map(|p| p.to_string_lossy().to_string()),
            ip_address: info.ip_address.clone(),
            state: info.state.into(),
            vm_id: info.vm_id.to_string(),
            created_at: info.created_at,
        }
    }
}

/// Writes `data` to `path` atomically via write-to-temp-then-rename.
///
/// A process crash or panic during the write leaves either the old file
/// or the new file intact — never a truncated/partial file.  This does
/// **not** call `fsync`; durability across power loss is not guaranteed.
fn atomic_write(path: &std::path::Path, data: &[u8]) -> Result<()> {
    let dir = path
        .parent()
        .ok_or_else(|| CoreError::config("config path has no parent directory"))?;

    let mut tmp = tempfile::NamedTempFile::new_in(dir)?;
    tmp.write_all(data)?;
    tmp.flush()?;
    tmp.persist(path).map_err(|e| {
        std::io::Error::new(
            e.error.kind(),
            format!("failed to persist to '{}': {}", path.display(), e.error),
        )
    })?;

    Ok(())
}

/// Machine persistence manager.
pub struct MachinePersistence {
    /// Base directory for machine configs.
    base_dir: PathBuf,
}

impl MachinePersistence {
    /// Creates a new persistence manager.
    pub fn new(base_dir: impl Into<PathBuf>) -> Self {
        Self {
            base_dir: base_dir.into(),
        }
    }

    /// Returns the config file path for a machine.
    fn config_path(&self, name: &str) -> PathBuf {
        self.base_dir.join(name).join("config.toml")
    }

    /// Returns the machine directory path.
    fn machine_dir(&self, name: &str) -> PathBuf {
        self.base_dir.join(name)
    }

    /// Saves a machine configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration cannot be saved.
    pub fn save(&self, machine: &MachineInfo) -> Result<()> {
        let dir = self.machine_dir(&machine.name);
        fs::create_dir_all(&dir)?;

        let persisted = PersistedMachine::from(machine);
        let content = toml::to_string_pretty(&persisted)
            .map_err(|e| CoreError::config(format!("failed to serialize config: {e}")))?;

        atomic_write(&self.config_path(&machine.name), content.as_bytes())?;

        tracing::debug!("Saved machine config: {}", machine.name);
        Ok(())
    }

    /// Loads a machine configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration cannot be loaded.
    pub fn load(&self, name: &str) -> Result<PersistedMachine> {
        let path = self.config_path(name);
        let content = fs::read_to_string(&path)
            .map_err(|e| CoreError::not_found(format!("Machine config not found: {e}")))?;

        toml::from_str(&content).map_err(CoreError::from)
    }

    /// Lists all saved machines.
    #[must_use]
    pub fn list(&self) -> Vec<String> {
        let Ok(entries) = fs::read_dir(&self.base_dir) else {
            return Vec::new();
        };

        entries
            .filter_map(std::result::Result::ok)
            .filter(|e| e.path().is_dir())
            .filter(|e| e.path().join("config.toml").exists())
            .filter_map(|e| e.file_name().into_string().ok())
            .collect()
    }

    /// Loads all saved machines.
    #[must_use]
    pub fn load_all(&self) -> Vec<PersistedMachine> {
        self.list()
            .iter()
            .filter_map(|name| self.load(name).ok())
            .collect()
    }

    /// Removes a machine configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration cannot be removed.
    pub fn remove(&self, name: &str) -> Result<()> {
        let dir = self.machine_dir(name);
        if dir.exists() {
            fs::remove_dir_all(&dir)?;
            tracing::debug!("Removed machine config: {}", name);
        }
        Ok(())
    }

    /// Updates the state of a persisted machine.
    ///
    /// # Errors
    ///
    /// Returns an error if the state cannot be updated.
    pub fn update_state(&self, name: &str, state: MachineState) -> Result<()> {
        self.update(name, |machine| {
            machine.state = state.into();
        })
    }

    /// Updates the IP address of a persisted machine.
    ///
    /// # Errors
    ///
    /// Returns an error if the IP cannot be updated.
    pub fn update_ip(&self, name: &str, ip: Option<&str>) -> Result<()> {
        self.update(name, |machine| {
            machine.ip_address = ip.map(ToString::to_string);
        })
    }

    /// Applies a mutation to a persisted machine config in a single
    /// load→mutate→write cycle.
    ///
    /// # Errors
    ///
    /// Returns an error if the config cannot be loaded or written.
    pub fn update(&self, name: &str, mutate: impl FnOnce(&mut PersistedMachine)) -> Result<()> {
        let mut machine = self.load(name)?;
        mutate(&mut machine);

        let content = toml::to_string_pretty(&machine)
            .map_err(|e| CoreError::config(format!("failed to serialize config: {e}")))?;

        atomic_write(&self.config_path(name), content.as_bytes())?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vm::VmId;
    use tempfile::TempDir;

    #[test]
    fn test_save_and_load() {
        let temp = TempDir::new().unwrap();
        let persistence = MachinePersistence::new(temp.path());

        let created_at = Utc::now();
        let info = MachineInfo {
            name: "test-vm".to_string(),
            state: MachineState::Created,
            vm_id: VmId::new(),
            cpus: 4,
            memory_mb: 4096,
            disk_gb: 50,
            kernel: Some("/path/to/kernel".to_string()),
            cmdline: Some("console=ttyS0".to_string()),
            block_devices: Vec::new(),
            distro: None,
            distro_version: None,
            disk_path: None,
            ssh_key_path: None,
            ip_address: None,
            cid: None,
            created_at,
        };

        persistence.save(&info).unwrap();

        let loaded = persistence.load("test-vm").unwrap();
        assert_eq!(loaded.name, "test-vm");
        assert_eq!(loaded.cpus, 4);
        assert_eq!(loaded.memory_mb, 4096);
        assert_eq!(loaded.kernel, Some("/path/to/kernel".to_string()));
        assert_eq!(loaded.cmdline, Some("console=ttyS0".to_string()));
        // Check created_at is preserved (within 1 second tolerance)
        assert!((loaded.created_at - created_at).num_seconds().abs() < 1);
    }

    #[test]
    fn test_list() {
        let temp = TempDir::new().unwrap();
        let persistence = MachinePersistence::new(temp.path());

        // Create multiple machines
        for name in ["vm1", "vm2", "vm3"] {
            let info = MachineInfo {
                name: name.to_string(),
                state: MachineState::Created,
                vm_id: VmId::new(),
                cpus: 2,
                memory_mb: 2048,
                disk_gb: 20,
                kernel: None,
                cmdline: None,
                block_devices: Vec::new(),
                distro: None,
                distro_version: None,
                disk_path: None,
                ssh_key_path: None,
                ip_address: None,
                cid: None,
                created_at: Utc::now(),
            };
            persistence.save(&info).unwrap();
        }

        let machines = persistence.list();
        assert_eq!(machines.len(), 3);
        assert!(machines.contains(&"vm1".to_string()));
        assert!(machines.contains(&"vm2".to_string()));
        assert!(machines.contains(&"vm3".to_string()));
    }

    #[test]
    fn test_remove() {
        let temp = TempDir::new().unwrap();
        let persistence = MachinePersistence::new(temp.path());

        let info = MachineInfo {
            name: "test-vm".to_string(),
            state: MachineState::Created,
            vm_id: VmId::new(),
            cpus: 2,
            memory_mb: 2048,
            disk_gb: 20,
            kernel: None,
            cmdline: None,
            block_devices: Vec::new(),
            distro: None,
            distro_version: None,
            disk_path: None,
            ssh_key_path: None,
            ip_address: None,
            cid: None,
            created_at: Utc::now(),
        };

        persistence.save(&info).unwrap();
        assert!(persistence.load("test-vm").is_ok());

        persistence.remove("test-vm").unwrap();
        assert!(persistence.load("test-vm").is_err());
    }

    #[test]
    fn test_update_batches_mutations() {
        let temp = TempDir::new().unwrap();
        let persistence = MachinePersistence::new(temp.path());

        let info = MachineInfo {
            name: "test-vm".to_string(),
            state: MachineState::Created,
            vm_id: VmId::new(),
            cpus: 2,
            memory_mb: 2048,
            disk_gb: 20,
            kernel: None,
            cmdline: None,
            block_devices: Vec::new(),
            distro: None,
            distro_version: None,
            disk_path: None,
            ssh_key_path: None,
            ip_address: None,
            cid: None,
            created_at: Utc::now(),
        };

        persistence.save(&info).unwrap();

        // Single update call sets both state and IP.
        persistence
            .update("test-vm", |m| {
                m.state = PersistedState::Running;
                m.ip_address = Some("10.0.2.15".to_string());
            })
            .unwrap();

        let loaded = persistence.load("test-vm").unwrap();
        assert_eq!(loaded.state, PersistedState::Running);
        assert_eq!(loaded.ip_address.as_deref(), Some("10.0.2.15"));
    }

    #[test]
    fn test_needs_recovery_detects_interrupted_running() {
        assert!(PersistedState::Running.needs_recovery());
        assert!(!PersistedState::Stopped.needs_recovery());
        assert!(!PersistedState::Created.needs_recovery());
    }

    #[test]
    fn test_persisted_running_maps_to_stopped_on_reload() {
        let temp = TempDir::new().unwrap();
        let persistence = MachinePersistence::new(temp.path());

        let info = MachineInfo {
            name: "crash-vm".to_string(),
            state: MachineState::Running,
            vm_id: VmId::new(),
            cpus: 2,
            memory_mb: 2048,
            disk_gb: 20,
            kernel: None,
            cmdline: None,
            block_devices: Vec::new(),
            distro: None,
            distro_version: None,
            disk_path: None,
            ssh_key_path: None,
            ip_address: Some("10.0.2.15".to_string()),
            cid: None,
            created_at: Utc::now(),
        };
        persistence.save(&info).unwrap();

        // Simulate daemon restart: load and check recovery flag.
        let loaded = persistence.load("crash-vm").unwrap();
        assert_eq!(loaded.state, PersistedState::Running);
        assert!(loaded.state.needs_recovery());

        // After recovery, MachineState maps Running → Stopped.
        let state: MachineState = loaded.state.into();
        assert_eq!(state, MachineState::Stopped);

        // Correcting the persisted state should stick.
        persistence
            .update_state("crash-vm", MachineState::Stopped)
            .unwrap();
        let reloaded = persistence.load("crash-vm").unwrap();
        assert_eq!(reloaded.state, PersistedState::Stopped);
        assert!(!reloaded.state.needs_recovery());
    }
}