boxlite 0.9.4

Embeddable virtual machine runtime for secure, isolated code execution
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
//! Box storage operations using JSON blob pattern.
//!
//! Follows Podman's design:
//! - BoxConfig: Immutable configuration (stored once at creation)
//! - BoxState: Mutable state (updated during lifecycle)
//!
//! Each table has queryable columns for filtering + JSON blob for full struct.

use chrono::Utc;
use rusqlite::{OptionalExtension, params};

use crate::litebox::config::BoxConfig;
use crate::runtime::id::BoxID;
use crate::runtime::types::BoxState;
use boxlite_shared::errors::{BoxliteError, BoxliteResult};

use super::{Database, db_err};

/// Box storage wrapping Database.
///
/// Manages BoxConfig (immutable) and BoxState (mutable) tables.
/// Uses JSON blob pattern for flexibility with queryable columns for performance.
#[derive(Clone)]
pub struct BoxStore {
    db: Database,
}

impl BoxStore {
    /// Create a new BoxStore from a Database.
    pub fn new(db: Database) -> Self {
        Self { db }
    }

    /// Get a reference to the underlying database.
    #[allow(dead_code)] // Used by snapshots (temporarily disabled)
    pub(crate) fn db(&self) -> Database {
        self.db.clone()
    }

    // ========================================================================
    // BoxConfig operations (immutable after creation)
    // ========================================================================

    /// Load box configuration by ID.
    #[allow(dead_code)] // API symmetry with load_state
    pub fn load_config(&self, box_id: &str) -> BoxliteResult<Option<BoxConfig>> {
        let conn = self.db.conn();

        let json: Option<String> = db_err!(
            conn.query_row(
                "SELECT json FROM box_config WHERE id = ?1",
                params![box_id],
                |row| row.get(0),
            )
            .optional()
        )?;

        match json {
            Some(j) => {
                let config: BoxConfig = serde_json::from_str(&j).map_err(|e| {
                    BoxliteError::Database(format!("Failed to deserialize config: {}", e))
                })?;
                Ok(Some(config))
            }
            None => Ok(None),
        }
    }

    /// Delete box configuration (and state via CASCADE).
    pub fn delete(&self, box_id: &str) -> BoxliteResult<bool> {
        let conn = self.db.conn();
        let rows_affected =
            db_err!(conn.execute("DELETE FROM box_config WHERE id = ?1", params![box_id],))?;
        Ok(rows_affected > 0)
    }

    // ========================================================================
    // BoxState operations (mutable)
    // ========================================================================

    /// Load box state by ID.
    pub fn load_state(&self, box_id: &str) -> BoxliteResult<Option<BoxState>> {
        let conn = self.db.conn();

        let json: Option<String> = db_err!(
            conn.query_row(
                "SELECT json FROM box_state WHERE id = ?1",
                params![box_id],
                |row| row.get(0),
            )
            .optional()
        )?;

        match json {
            Some(j) => {
                let state: BoxState = serde_json::from_str(&j).map_err(|e| {
                    BoxliteError::Database(format!("Failed to deserialize state: {}", e))
                })?;
                Ok(Some(state))
            }
            None => Ok(None),
        }
    }

    /// Update box state.
    ///
    /// Updates both queryable columns and JSON blob.
    /// Returns error if box doesn't exist (Podman pattern: verify RowsAffected).
    pub fn update_state(&self, box_id: &str, state: &BoxState) -> BoxliteResult<()> {
        let conn = self.db.conn();

        let json = serde_json::to_string(state)
            .map_err(|e| BoxliteError::Database(format!("Failed to serialize state: {}", e)))?;

        let rows_affected = db_err!(conn.execute(
            "UPDATE box_state SET status = ?1, pid = ?2, json = ?3 WHERE id = ?4",
            params![state.status.as_str(), state.pid, json, box_id],
        ))?;

        // Podman pattern: verify rows were actually updated
        if rows_affected == 0 {
            return Err(BoxliteError::NotFound(box_id.to_string()));
        }

        Ok(())
    }

    // ========================================================================
    // Combined operations
    // ========================================================================

    /// Save both config and initial state atomically.
    ///
    /// Uses a transaction to ensure both inserts succeed or neither does.
    /// Follows Podman pattern of explicit transactions for multi-statement operations.
    pub fn save(&self, config: &BoxConfig, state: &BoxState) -> BoxliteResult<()> {
        let mut conn = self.db.conn();
        let tx = db_err!(conn.transaction())?;

        // Serialize config
        let config_json = serde_json::to_string(config)
            .map_err(|e| BoxliteError::Database(format!("Failed to serialize config: {}", e)))?;

        // Serialize state
        let state_json = serde_json::to_string(state)
            .map_err(|e| BoxliteError::Database(format!("Failed to serialize state: {}", e)))?;

        // Insert config (name has UNIQUE constraint, will fail on duplicate)
        db_err!(tx.execute(
            "INSERT INTO box_config (id, name, created_at, json) VALUES (?1, ?2, ?3, ?4)",
            params![
                config.id,
                config.name.as_deref(),
                config.created_at.timestamp(),
                config_json
            ],
        ))?;

        // Insert state
        db_err!(tx.execute(
            "INSERT INTO box_state (id, status, pid, json) VALUES (?1, ?2, ?3, ?4)",
            params![config.id, state.status.as_str(), state.pid, state_json],
        ))?;

        // Commit transaction
        db_err!(tx.commit())?;

        Ok(())
    }

    /// Load both config and state for a box.
    #[allow(dead_code)] // API symmetry - currently unused but part of designed API
    pub fn load(&self, box_id: &str) -> BoxliteResult<Option<(BoxConfig, BoxState)>> {
        let config = self.load_config(box_id)?;
        let state = self.load_state(box_id)?;

        match (config, state) {
            (Some(c), Some(s)) => Ok(Some((c, s))),
            _ => Ok(None),
        }
    }

    /// List all boxes as (config, state) pairs.
    ///
    /// Returns boxes sorted by creation time (newest first).
    pub fn list_all(&self) -> BoxliteResult<Vec<(BoxConfig, BoxState)>> {
        let conn = self.db.conn();

        let mut stmt = db_err!(conn.prepare(
            r#"
            SELECT c.json as config_json, s.json as state_json
            FROM box_config c
            JOIN box_state s ON c.id = s.id
            ORDER BY c.created_at DESC
            "#
        ))?;

        let rows = db_err!(stmt.query_map([], |row| {
            let config_json: String = row.get(0)?;
            let state_json: String = row.get(1)?;
            Ok((config_json, state_json))
        }))?;

        let mut result = Vec::new();
        for row in rows {
            let (config_json, state_json) = db_err!(row)?;
            let config: BoxConfig = serde_json::from_str(&config_json).map_err(|e| {
                BoxliteError::Database(format!("Failed to deserialize config: {}", e))
            })?;
            let state: BoxState = serde_json::from_str(&state_json).map_err(|e| {
                BoxliteError::Database(format!("Failed to deserialize state: {}", e))
            })?;
            result.push((config, state));
        }

        Ok(result)
    }

    /// List active boxes (Starting, Running, Detached).
    pub fn list_active(&self) -> BoxliteResult<Vec<(BoxConfig, BoxState)>> {
        let conn = self.db.conn();

        let mut stmt = db_err!(conn.prepare(
            r#"
            SELECT c.json as config_json, s.json as state_json
            FROM box_config c
            JOIN box_state s ON c.id = s.id
            WHERE s.status IN ('starting', 'running', 'detached')
            ORDER BY c.created_at DESC
            "#
        ))?;

        let rows = db_err!(stmt.query_map([], |row| {
            let config_json: String = row.get(0)?;
            let state_json: String = row.get(1)?;
            Ok((config_json, state_json))
        }))?;

        let mut result = Vec::new();
        for row in rows {
            let (config_json, state_json) = db_err!(row)?;
            let config: BoxConfig = serde_json::from_str(&config_json).map_err(|e| {
                BoxliteError::Database(format!("Failed to deserialize config: {}", e))
            })?;
            let state: BoxState = serde_json::from_str(&state_json).map_err(|e| {
                BoxliteError::Database(format!("Failed to deserialize state: {}", e))
            })?;
            result.push((config, state));
        }

        Ok(result)
    }

    // ========================================================================
    // Reboot detection via alive table
    // ========================================================================

    /// Check if this is a fresh boot (alive record is stale or missing).
    ///
    /// Returns true if reboot detected (need to reset active boxes).
    pub fn check_and_update_boot(&self) -> BoxliteResult<bool> {
        let conn = self.db.conn();
        let current_boot_id = get_boot_id();

        // Check existing alive record
        let existing: Option<String> = db_err!(
            conn.query_row("SELECT boot_id FROM alive WHERE id = 1", [], |row| row
                .get(0))
                .optional()
        )?;

        let is_reboot = match existing {
            None => {
                // First run ever - not a reboot
                false
            }
            Some(stored_boot_id) => {
                // Reboot if boot ID changed
                stored_boot_id != current_boot_id
            }
        };

        // Update alive record
        let now = Utc::now().timestamp();
        db_err!(conn.execute(
            r#"
            INSERT INTO alive (id, boot_id, started_at) VALUES (1, ?1, ?2)
            ON CONFLICT(id) DO UPDATE SET boot_id = ?1, started_at = ?2
            "#,
            params![current_boot_id, now],
        ))?;

        Ok(is_reboot)
    }

    /// Reset all active boxes to stopped state after reboot.
    ///
    /// Called after reboot detection. VM rootfs is preserved, so boxes
    /// become Stopped (not Crashed) and can be restarted.
    pub fn reset_active_boxes_after_reboot(&self) -> BoxliteResult<Vec<BoxID>> {
        let active = self.list_active()?;
        let mut reset_ids = Vec::new();

        for (config, mut state) in active {
            state.reset_for_reboot();
            self.update_state(config.id.as_str(), &state)?;
            reset_ids.push(config.id);
        }

        Ok(reset_ids)
    }
}

/// Get system boot ID (unique per boot).
///
/// On macOS: Uses kern.bootsessionuuid
/// On Linux: Uses /proc/sys/kernel/random/boot_id
fn get_boot_id() -> String {
    #[cfg(target_os = "macos")]
    {
        use std::process::Command;
        Command::new("sysctl")
            .args(["-n", "kern.bootsessionuuid"])
            .output()
            .ok()
            .and_then(|o| String::from_utf8(o.stdout).ok())
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string())
    }

    #[cfg(target_os = "linux")]
    {
        std::fs::read_to_string("/proc/sys/kernel/random/boot_id")
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|_| uuid::Uuid::new_v4().to_string())
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        uuid::Uuid::new_v4().to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::litebox::config::ContainerRuntimeConfig;
    use crate::runtime::id::BoxID;
    use crate::runtime::types::{BoxStatus, ContainerID};
    use crate::vmm::VmmKind;
    use boxlite_shared::Transport;
    use std::path::PathBuf;
    use tempfile::tempdir;

    fn create_test_db() -> (BoxStore, tempfile::TempDir) {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("test.db");
        let db = Database::open(&db_path).unwrap();
        (BoxStore::new(db), dir)
    }

    fn create_test_config(id: &str) -> BoxConfig {
        use crate::runtime::options::{BoxOptions, RootfsSpec};
        let now = Utc::now();
        BoxConfig {
            id: BoxID::parse(id).unwrap(),
            name: None,
            created_at: now,
            container: ContainerRuntimeConfig {
                id: ContainerID::new(),
            },
            options: BoxOptions {
                rootfs: RootfsSpec::Image("test:latest".to_string()),
                cpus: Some(2),
                memory_mib: Some(512),
                ..Default::default()
            },
            engine_kind: VmmKind::Libkrun,
            transport: Transport::unix(PathBuf::from("/tmp/test.sock")),
            box_home: PathBuf::from("/tmp/boxes/test"),
            ready_socket_path: PathBuf::from("/tmp/ready.sock"),
        }
    }

    // Test IDs (26-char ULID format, accepted by BoxID::parse)
    const TEST_ID_1: &str = "01HJK4TNRPQSXYZ8WM6NCVT9R1";
    const TEST_ID_2: &str = "01HJK4TNRPQSXYZ8WM6NCVT9R2";
    const TEST_ID_3: &str = "01HJK4TNRPQSXYZ8WM6NCVT9R3";

    #[test]
    fn test_save_and_load_config() {
        let (store, _dir) = create_test_db();
        let config = create_test_config(TEST_ID_1);
        let state = BoxState::new();

        store.save(&config, &state).unwrap();

        let loaded = store.load_config(config.id.as_str()).unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap().id, config.id);
    }

    #[test]
    fn test_save_and_load_state() {
        let (store, _dir) = create_test_db();
        let config = create_test_config(TEST_ID_1);
        let state = BoxState::new();

        store.save(&config, &state).unwrap();

        let loaded = store.load_state(config.id.as_str()).unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap().status, BoxStatus::Configured);
    }

    #[test]
    fn test_update_state() {
        let (store, _dir) = create_test_db();
        let config = create_test_config(TEST_ID_1);
        let state = BoxState::new();

        store.save(&config, &state).unwrap();

        // Update to running with PID
        let mut new_state = state.clone();
        new_state.set_status(BoxStatus::Running);
        new_state.set_pid(Some(12345));
        store.update_state(config.id.as_str(), &new_state).unwrap();

        let loaded = store.load_state(config.id.as_str()).unwrap().unwrap();
        assert_eq!(loaded.status, BoxStatus::Running);
        assert_eq!(loaded.pid, Some(12345));
    }

    #[test]
    fn test_delete() {
        let (store, _dir) = create_test_db();
        let config = create_test_config(TEST_ID_1);
        let state = BoxState::new();

        store.save(&config, &state).unwrap();
        assert!(store.load(config.id.as_str()).unwrap().is_some());

        store.delete(config.id.as_str()).unwrap();
        assert!(store.load(config.id.as_str()).unwrap().is_none());
    }

    #[test]
    fn test_list_all() {
        let (store, _dir) = create_test_db();

        // Create multiple boxes
        let ids = [TEST_ID_1, TEST_ID_2, TEST_ID_3];
        for id in ids {
            let config = create_test_config(id);
            let state = BoxState::new();
            store.save(&config, &state).unwrap();
        }

        let all = store.list_all().unwrap();
        assert_eq!(all.len(), 3);
    }

    #[test]
    fn test_list_active() {
        let (store, _dir) = create_test_db();

        // Create running box
        let config1 = create_test_config(TEST_ID_1);
        let mut state1 = BoxState::new();
        state1.set_status(BoxStatus::Running);
        store.save(&config1, &state1).unwrap();

        // Create stopped box
        let config2 = create_test_config(TEST_ID_2);
        let mut state2 = BoxState::new();
        state2.set_status(BoxStatus::Stopped);
        store.save(&config2, &state2).unwrap();

        let active = store.list_active().unwrap();
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].0.id.as_str(), TEST_ID_1);
    }

    #[test]
    fn test_reboot_detection() {
        let (store, _dir) = create_test_db();

        // First call - not a reboot
        let is_reboot = store.check_and_update_boot().unwrap();
        assert!(!is_reboot);

        // Second call with same boot_id - still not a reboot
        let is_reboot = store.check_and_update_boot().unwrap();
        assert!(!is_reboot);
    }

    #[test]
    fn test_reset_active_boxes_after_reboot() {
        let (store, _dir) = create_test_db();

        // Create running box
        let config = create_test_config(TEST_ID_1);
        let mut state = BoxState::new();
        state.set_status(BoxStatus::Running);
        state.set_pid(Some(12345));
        store.save(&config, &state).unwrap();

        // Reset active boxes after reboot
        let reset_ids = store.reset_active_boxes_after_reboot().unwrap();
        assert_eq!(reset_ids.len(), 1);
        assert_eq!(reset_ids[0].as_str(), TEST_ID_1);

        // Verify state changed to Stopped (not Crashed - rootfs preserved)
        let loaded = store.load_state(config.id.as_str()).unwrap().unwrap();
        assert_eq!(loaded.status, BoxStatus::Stopped);
        assert_eq!(loaded.pid, None);
    }
}