|
3 | 3 |
|
4 | 4 | //! Utility helpers shared across compute-driver crates. |
5 | 5 |
|
6 | | -use crate::proto::compute::v1::DriverSandbox; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +use crate::proto::compute::v1::{DriverSandbox, GetCapabilitiesResponse}; |
7 | 9 |
|
8 | 10 | // --------------------------------------------------------------------------- |
9 | 11 | // Sandbox container/pod label keys (openshell.ai/ namespace) |
@@ -34,6 +36,50 @@ pub const LABEL_SANDBOX_NAMESPACE: &str = "openshell.ai/sandbox-namespace"; |
34 | 36 | /// path used when building the `openshell-sandbox` image layer. |
35 | 37 | pub const SUPERVISOR_IMAGE_BINARY_PATH: &str = "/openshell-sandbox"; |
36 | 38 |
|
| 39 | +/// Return the XDG state path for a driver's sandbox JWT token file. |
| 40 | +/// |
| 41 | +/// The resulting path is `$XDG_STATE_HOME/openshell/<driver_subdir>[/<namespace>]/<sandbox_id>/sandbox.jwt`. |
| 42 | +/// |
| 43 | +/// `driver_subdir` is driver-specific, e.g. `"docker-sandbox-tokens"` or |
| 44 | +/// `"podman-sandbox-tokens"`. When `namespace` is `Some`, it is appended as |
| 45 | +/// an additional path component (with `/` and `\` replaced by `-`). |
| 46 | +/// |
| 47 | +/// # Errors |
| 48 | +/// Returns an error if the XDG state directory cannot be resolved. |
| 49 | +pub fn sandbox_token_path( |
| 50 | + driver_subdir: &str, |
| 51 | + namespace: Option<&str>, |
| 52 | + sandbox_id: &str, |
| 53 | +) -> miette::Result<PathBuf> { |
| 54 | + let mut path = crate::paths::xdg_state_dir()? |
| 55 | + .join("openshell") |
| 56 | + .join(driver_subdir); |
| 57 | + if let Some(ns) = namespace { |
| 58 | + path = path.join(ns.replace(['/', '\\'], "-")); |
| 59 | + } |
| 60 | + Ok(path.join(sandbox_id).join("sandbox.jwt")) |
| 61 | +} |
| 62 | + |
| 63 | +/// Build a [`GetCapabilitiesResponse`] from the common driver capability fields. |
| 64 | +/// |
| 65 | +/// Every compute driver constructs this response with the same fields. Shared |
| 66 | +/// here to avoid repeating the struct literal (and the always-zero `gpu_count` |
| 67 | +/// default) in each driver crate. |
| 68 | +pub fn build_capabilities_response( |
| 69 | + driver_name: &str, |
| 70 | + driver_version: impl Into<String>, |
| 71 | + default_image: impl Into<String>, |
| 72 | + supports_gpu: bool, |
| 73 | +) -> GetCapabilitiesResponse { |
| 74 | + GetCapabilitiesResponse { |
| 75 | + driver_name: driver_name.to_string(), |
| 76 | + driver_version: driver_version.into(), |
| 77 | + default_image: default_image.into(), |
| 78 | + supports_gpu, |
| 79 | + gpu_count: 0, |
| 80 | + } |
| 81 | +} |
| 82 | + |
37 | 83 | /// Return the effective log level for a sandbox. |
38 | 84 | /// |
39 | 85 | /// Uses the level from the sandbox spec when non-empty, falling back to |
|
0 commit comments