ciab_core/traits/
runtime.rs1use std::collections::HashMap;
2
3use async_trait::async_trait;
4use tokio::sync::mpsc;
5use uuid::Uuid;
6
7use crate::error::CiabResult;
8use crate::types::sandbox::{
9 ExecRequest, ExecResult, FileInfo, LogOptions, ResourceStats, SandboxInfo, SandboxSpec,
10 SandboxState,
11};
12
13#[async_trait]
14pub trait SandboxRuntime: Send + Sync {
15 async fn create_sandbox(&self, spec: &SandboxSpec) -> CiabResult<SandboxInfo>;
17
18 async fn get_sandbox(&self, id: &Uuid) -> CiabResult<SandboxInfo>;
20
21 async fn list_sandboxes(
23 &self,
24 state: Option<SandboxState>,
25 provider: Option<&str>,
26 labels: &HashMap<String, String>,
27 ) -> CiabResult<Vec<SandboxInfo>>;
28
29 async fn start_sandbox(&self, id: &Uuid) -> CiabResult<()>;
31
32 async fn stop_sandbox(&self, id: &Uuid) -> CiabResult<()>;
34
35 async fn pause_sandbox(&self, id: &Uuid) -> CiabResult<()>;
37
38 async fn resume_sandbox(&self, id: &Uuid) -> CiabResult<()>;
40
41 async fn terminate_sandbox(&self, id: &Uuid) -> CiabResult<()>;
43
44 async fn exec(&self, id: &Uuid, request: &ExecRequest) -> CiabResult<ExecResult>;
46
47 async fn exec_streaming(
52 &self,
53 id: &Uuid,
54 request: &ExecRequest,
55 ) -> CiabResult<(
56 mpsc::Receiver<String>,
57 tokio::task::JoinHandle<CiabResult<ExecResult>>,
58 )> {
59 let result = self.exec(id, request).await?;
60 let (tx, rx) = mpsc::channel::<String>(256);
61 let stdout = result.stdout.clone();
62 let handle = tokio::spawn(async move {
63 for line in stdout.lines() {
64 let _ = tx.send(line.to_string()).await;
65 }
66 Ok(result)
67 });
68 Ok((rx, handle))
69 }
70
71 async fn exec_streaming_interactive(
75 &self,
76 id: &Uuid,
77 request: &ExecRequest,
78 ) -> CiabResult<(
79 mpsc::Receiver<String>,
80 mpsc::Sender<String>,
81 tokio::task::JoinHandle<CiabResult<ExecResult>>,
82 )> {
83 let (rx, handle) = self.exec_streaming(id, request).await?;
85 let (stdin_tx, _stdin_rx) = mpsc::channel::<String>(16);
86 Ok((rx, stdin_tx, handle))
87 }
88
89 async fn read_file(&self, id: &Uuid, path: &str) -> CiabResult<Vec<u8>>;
91
92 async fn write_file(&self, id: &Uuid, path: &str, content: &[u8]) -> CiabResult<()>;
94
95 async fn list_files(&self, id: &Uuid, path: &str) -> CiabResult<Vec<FileInfo>>;
97
98 async fn get_stats(&self, id: &Uuid) -> CiabResult<ResourceStats>;
100
101 async fn stream_logs(
103 &self,
104 id: &Uuid,
105 options: &LogOptions,
106 ) -> CiabResult<mpsc::Receiver<String>>;
107
108 async fn kill_exec(&self, id: &Uuid) -> CiabResult<()> {
110 let _ = id;
111 Ok(())
112 }
113}