microsandbox_db/entity/sandbox.rs
1//! Entity definition for the `sandboxes` table.
2
3use sea_orm::entity::prelude::*;
4
5//--------------------------------------------------------------------------------------------------
6// Types
7//--------------------------------------------------------------------------------------------------
8
9/// The status of a sandbox.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
11#[sea_orm(rs_type = "String", db_type = "Text")]
12pub enum SandboxStatus {
13 /// The sandbox has been created but not yet started.
14 ///
15 /// Cloud-only today: msb-cloud's create-without-start state. Local create
16 /// requests begin in `Starting` because they boot immediately.
17 #[sea_orm(string_value = "Created")]
18 Created,
19
20 /// A start request has been submitted but the sandbox is not yet running.
21 ///
22 /// Covers the gap between accepting a create/start request and the runtime
23 /// reporting the VM and guest agent as ready.
24 #[sea_orm(string_value = "Starting")]
25 Starting,
26
27 /// The sandbox is running.
28 #[sea_orm(string_value = "Running")]
29 Running,
30
31 /// The sandbox is draining (shutting down gracefully).
32 #[sea_orm(string_value = "Draining")]
33 Draining,
34
35 /// The sandbox is paused.
36 #[sea_orm(string_value = "Paused")]
37 Paused,
38
39 /// The sandbox is stopped.
40 #[sea_orm(string_value = "Stopped")]
41 Stopped,
42
43 /// The sandbox crashed.
44 #[sea_orm(string_value = "Crashed")]
45 Crashed,
46}
47
48//--------------------------------------------------------------------------------------------------
49// Methods
50//--------------------------------------------------------------------------------------------------
51
52impl SandboxStatus {
53 /// Whether this status represents a live runtime or an in-flight runtime transition.
54 pub fn has_active_runtime_state(self) -> bool {
55 match self {
56 Self::Starting | Self::Running | Self::Draining | Self::Paused => true,
57 Self::Created | Self::Stopped | Self::Crashed => false,
58 }
59 }
60}
61
62/// The sandbox entity model.
63#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
64#[sea_orm(table_name = "sandbox")]
65pub struct Model {
66 #[sea_orm(primary_key)]
67 pub id: i32,
68 #[sea_orm(unique)]
69 pub name: String,
70 /// Desired sandbox configuration used for the next start.
71 pub config: String,
72 /// Configuration actually used by the currently running VM, when active.
73 pub active_config: Option<String>,
74 pub status: SandboxStatus,
75 /// Network address-pool slot leased to this sandbox's current run (#1390).
76 /// Inactive rows should not retain a slot; maintenance repairs stale leases.
77 pub network_slot: Option<u16>,
78
79 /// Denormalized copy of `config.policy.ephemeral`.
80 ///
81 /// Indexed alongside `status` so host-runtime lifecycle maintenance can
82 /// find terminal ephemeral cleanup candidates without scanning and
83 /// parsing every stopped sandbox's serialized config.
84 pub ephemeral: bool,
85 pub created_at: Option<DateTime>,
86 pub updated_at: Option<DateTime>,
87}
88
89//--------------------------------------------------------------------------------------------------
90// Types: Relations
91//--------------------------------------------------------------------------------------------------
92
93/// Relations for the sandbox entity.
94#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
95pub enum Relation {
96 /// A sandbox has many runs.
97 #[sea_orm(has_many = "super::run::Entity")]
98 Run,
99}
100
101impl Related<super::run::Entity> for Entity {
102 fn to() -> RelationDef {
103 Relation::Run.def()
104 }
105}
106
107//--------------------------------------------------------------------------------------------------
108// Trait Implementations
109//--------------------------------------------------------------------------------------------------
110
111impl ActiveModelBehavior for ActiveModel {}