Skip to content

Commit ed81ed9

Browse files
committed
refactor(server): unify policy persistence in objects table
Signed-off-by: John Myers <9696606+johntmyers@users.noreply.github.com>
1 parent 55b0266 commit ed81ed9

15 files changed

Lines changed: 1347 additions & 681 deletions
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
CREATE TABLE IF NOT EXISTS objects (
2-
object_type TEXT NOT NULL,
3-
id TEXT NOT NULL,
4-
name TEXT NOT NULL,
5-
payload BYTEA NOT NULL,
2+
id TEXT PRIMARY KEY,
3+
object_type TEXT NOT NULL,
4+
name TEXT,
5+
scope TEXT,
6+
version BIGINT,
7+
status TEXT,
8+
dedup_key TEXT,
9+
hit_count BIGINT NOT NULL DEFAULT 0,
10+
payload BYTEA NOT NULL,
611
created_at_ms BIGINT NOT NULL,
7-
updated_at_ms BIGINT NOT NULL,
8-
PRIMARY KEY (id),
9-
UNIQUE (object_type, name)
12+
updated_at_ms BIGINT NOT NULL
1013
);
14+
15+
CREATE UNIQUE INDEX IF NOT EXISTS objects_name_uq
16+
ON objects (object_type, name)
17+
WHERE name IS NOT NULL;
18+
19+
CREATE UNIQUE INDEX IF NOT EXISTS objects_version_uq
20+
ON objects (object_type, scope, version)
21+
WHERE scope IS NOT NULL AND version IS NOT NULL;
22+
23+
CREATE INDEX IF NOT EXISTS objects_scope_status_idx
24+
ON objects (object_type, scope, status, version)
25+
WHERE scope IS NOT NULL;
26+
27+
CREATE UNIQUE INDEX IF NOT EXISTS objects_dedup_uq
28+
ON objects (object_type, scope, dedup_key)
29+
WHERE dedup_key IS NOT NULL;

crates/openshell-server/migrations/postgres/002_create_sandbox_policies.sql

Lines changed: 0 additions & 15 deletions
This file was deleted.

crates/openshell-server/migrations/postgres/003_create_policy_recommendations.sql

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
CREATE TABLE IF NOT EXISTS objects (
2-
object_type TEXT NOT NULL,
3-
id TEXT NOT NULL,
4-
name TEXT NOT NULL,
5-
payload BLOB NOT NULL,
2+
id TEXT PRIMARY KEY,
3+
object_type TEXT NOT NULL,
4+
name TEXT,
5+
scope TEXT,
6+
version INTEGER,
7+
status TEXT,
8+
dedup_key TEXT,
9+
hit_count INTEGER NOT NULL DEFAULT 0,
10+
payload BLOB NOT NULL,
611
created_at_ms INTEGER NOT NULL,
7-
updated_at_ms INTEGER NOT NULL,
8-
PRIMARY KEY (id),
9-
UNIQUE (object_type, name)
12+
updated_at_ms INTEGER NOT NULL
1013
);
14+
15+
CREATE UNIQUE INDEX IF NOT EXISTS objects_name_uq
16+
ON objects (object_type, name)
17+
WHERE name IS NOT NULL;
18+
19+
CREATE UNIQUE INDEX IF NOT EXISTS objects_version_uq
20+
ON objects (object_type, scope, version)
21+
WHERE scope IS NOT NULL AND version IS NOT NULL;
22+
23+
CREATE INDEX IF NOT EXISTS objects_scope_status_idx
24+
ON objects (object_type, scope, status, version)
25+
WHERE scope IS NOT NULL;
26+
27+
CREATE UNIQUE INDEX IF NOT EXISTS objects_dedup_uq
28+
ON objects (object_type, scope, dedup_key)
29+
WHERE dedup_key IS NOT NULL;

crates/openshell-server/migrations/sqlite/002_create_sandbox_policies.sql

Lines changed: 0 additions & 15 deletions
This file was deleted.

crates/openshell-server/migrations/sqlite/003_create_policy_recommendations.sql

Lines changed: 0 additions & 35 deletions
This file was deleted.

crates/openshell-server/src/compute/mod.rs

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod vm;
77

88
pub use vm::VmComputeConfig;
99

10-
use crate::grpc::policy::{SANDBOX_SETTINGS_OBJECT_TYPE, sandbox_settings_id};
10+
use crate::grpc::policy::SANDBOX_SETTINGS_OBJECT_TYPE;
1111
use crate::persistence::{ObjectId, ObjectName, ObjectRecord, ObjectType, Store};
1212
use crate::sandbox_index::SandboxIndex;
1313
use crate::sandbox_watch::SandboxWatchBus;
@@ -406,7 +406,7 @@ impl ComputeRuntime {
406406

407407
if let Err(e) = self
408408
.store
409-
.delete(SANDBOX_SETTINGS_OBJECT_TYPE, &sandbox_settings_id(&id))
409+
.delete_by_name(SANDBOX_SETTINGS_OBJECT_TYPE, &sandbox.name)
410410
.await
411411
{
412412
warn!(
@@ -1183,6 +1183,115 @@ fn is_terminal_failure_reason(reason: &str) -> bool {
11831183
!transient_reasons.contains(&reason.as_str())
11841184
}
11851185

1186+
#[cfg(test)]
1187+
#[derive(Debug, Default)]
1188+
pub(crate) struct NoopTestDriver;
1189+
1190+
#[cfg(test)]
1191+
#[tonic::async_trait]
1192+
impl ComputeDriver for NoopTestDriver {
1193+
type WatchSandboxesStream = DriverWatchStream;
1194+
1195+
async fn get_capabilities(
1196+
&self,
1197+
_request: Request<GetCapabilitiesRequest>,
1198+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::GetCapabilitiesResponse>, Status>
1199+
{
1200+
Ok(tonic::Response::new(
1201+
openshell_core::proto::compute::v1::GetCapabilitiesResponse {
1202+
driver_name: "noop-test-driver".to_string(),
1203+
driver_version: "test".to_string(),
1204+
default_image: "openshell/sandbox:test".to_string(),
1205+
supports_gpu: false,
1206+
},
1207+
))
1208+
}
1209+
1210+
async fn validate_sandbox_create(
1211+
&self,
1212+
_request: Request<ValidateSandboxCreateRequest>,
1213+
) -> Result<
1214+
tonic::Response<openshell_core::proto::compute::v1::ValidateSandboxCreateResponse>,
1215+
Status,
1216+
> {
1217+
Ok(tonic::Response::new(
1218+
openshell_core::proto::compute::v1::ValidateSandboxCreateResponse {},
1219+
))
1220+
}
1221+
1222+
async fn get_sandbox(
1223+
&self,
1224+
_request: Request<GetSandboxRequest>,
1225+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::GetSandboxResponse>, Status>
1226+
{
1227+
Err(Status::not_found("sandbox not found"))
1228+
}
1229+
1230+
async fn list_sandboxes(
1231+
&self,
1232+
_request: Request<ListSandboxesRequest>,
1233+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::ListSandboxesResponse>, Status>
1234+
{
1235+
Ok(tonic::Response::new(
1236+
openshell_core::proto::compute::v1::ListSandboxesResponse {
1237+
sandboxes: Vec::new(),
1238+
},
1239+
))
1240+
}
1241+
1242+
async fn create_sandbox(
1243+
&self,
1244+
_request: Request<CreateSandboxRequest>,
1245+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::CreateSandboxResponse>, Status>
1246+
{
1247+
Ok(tonic::Response::new(
1248+
openshell_core::proto::compute::v1::CreateSandboxResponse {},
1249+
))
1250+
}
1251+
1252+
async fn stop_sandbox(
1253+
&self,
1254+
_request: Request<openshell_core::proto::compute::v1::StopSandboxRequest>,
1255+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::StopSandboxResponse>, Status>
1256+
{
1257+
Ok(tonic::Response::new(
1258+
openshell_core::proto::compute::v1::StopSandboxResponse {},
1259+
))
1260+
}
1261+
1262+
async fn delete_sandbox(
1263+
&self,
1264+
_request: Request<DeleteSandboxRequest>,
1265+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::DeleteSandboxResponse>, Status>
1266+
{
1267+
Ok(tonic::Response::new(
1268+
openshell_core::proto::compute::v1::DeleteSandboxResponse { deleted: true },
1269+
))
1270+
}
1271+
1272+
async fn watch_sandboxes(
1273+
&self,
1274+
_request: Request<WatchSandboxesRequest>,
1275+
) -> Result<tonic::Response<Self::WatchSandboxesStream>, Status> {
1276+
Ok(tonic::Response::new(Box::pin(futures::stream::empty())))
1277+
}
1278+
}
1279+
1280+
#[cfg(test)]
1281+
pub(crate) async fn new_test_runtime(store: Arc<Store>) -> ComputeRuntime {
1282+
ComputeRuntime {
1283+
driver: Arc::new(NoopTestDriver),
1284+
_driver_process: None,
1285+
default_image: "openshell/sandbox:test".to_string(),
1286+
store,
1287+
sandbox_index: SandboxIndex::new(),
1288+
sandbox_watch_bus: SandboxWatchBus::new(),
1289+
tracing_log_bus: TracingLogBus::new(),
1290+
supervisor_sessions: Arc::new(SupervisorSessionRegistry::new()),
1291+
sync_lock: Arc::new(Mutex::new(())),
1292+
}
1293+
}
1294+
11861295
#[cfg(test)]
11871296
mod tests {
11881297
use super::*;

0 commit comments

Comments
 (0)