Skip to content

Commit 70dc281

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

15 files changed

Lines changed: 1318 additions & 700 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
@@ -8,7 +8,7 @@ pub mod vm;
88
pub use openshell_driver_docker::DockerComputeConfig;
99
pub use vm::VmComputeConfig;
1010

11-
use crate::grpc::policy::{SANDBOX_SETTINGS_OBJECT_TYPE, sandbox_settings_id};
11+
use crate::grpc::policy::SANDBOX_SETTINGS_OBJECT_TYPE;
1212
use crate::persistence::{ObjectId, ObjectName, ObjectRecord, ObjectType, Store};
1313
use crate::sandbox_index::SandboxIndex;
1414
use crate::sandbox_watch::SandboxWatchBus;
@@ -501,7 +501,7 @@ impl ComputeRuntime {
501501

502502
if let Err(e) = self
503503
.store
504-
.delete(SANDBOX_SETTINGS_OBJECT_TYPE, &sandbox_settings_id(&id))
504+
.delete_by_name(SANDBOX_SETTINGS_OBJECT_TYPE, sandbox.object_name())
505505
.await
506506
{
507507
warn!(
@@ -1479,6 +1479,115 @@ fn is_terminal_failure_reason(reason: &str) -> bool {
14791479
!transient_reasons.contains(&reason.as_str())
14801480
}
14811481

1482+
#[cfg(test)]
1483+
#[derive(Debug, Default)]
1484+
pub(crate) struct NoopTestDriver;
1485+
1486+
#[cfg(test)]
1487+
#[tonic::async_trait]
1488+
impl ComputeDriver for NoopTestDriver {
1489+
type WatchSandboxesStream = DriverWatchStream;
1490+
1491+
async fn get_capabilities(
1492+
&self,
1493+
_request: Request<GetCapabilitiesRequest>,
1494+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::GetCapabilitiesResponse>, Status>
1495+
{
1496+
Ok(tonic::Response::new(
1497+
openshell_core::proto::compute::v1::GetCapabilitiesResponse {
1498+
driver_name: "noop-test-driver".to_string(),
1499+
driver_version: "test".to_string(),
1500+
default_image: "openshell/sandbox:test".to_string(),
1501+
supports_gpu: false,
1502+
},
1503+
))
1504+
}
1505+
1506+
async fn validate_sandbox_create(
1507+
&self,
1508+
_request: Request<ValidateSandboxCreateRequest>,
1509+
) -> Result<
1510+
tonic::Response<openshell_core::proto::compute::v1::ValidateSandboxCreateResponse>,
1511+
Status,
1512+
> {
1513+
Ok(tonic::Response::new(
1514+
openshell_core::proto::compute::v1::ValidateSandboxCreateResponse {},
1515+
))
1516+
}
1517+
1518+
async fn get_sandbox(
1519+
&self,
1520+
_request: Request<GetSandboxRequest>,
1521+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::GetSandboxResponse>, Status>
1522+
{
1523+
Err(Status::not_found("sandbox not found"))
1524+
}
1525+
1526+
async fn list_sandboxes(
1527+
&self,
1528+
_request: Request<ListSandboxesRequest>,
1529+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::ListSandboxesResponse>, Status>
1530+
{
1531+
Ok(tonic::Response::new(
1532+
openshell_core::proto::compute::v1::ListSandboxesResponse {
1533+
sandboxes: Vec::new(),
1534+
},
1535+
))
1536+
}
1537+
1538+
async fn create_sandbox(
1539+
&self,
1540+
_request: Request<CreateSandboxRequest>,
1541+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::CreateSandboxResponse>, Status>
1542+
{
1543+
Ok(tonic::Response::new(
1544+
openshell_core::proto::compute::v1::CreateSandboxResponse {},
1545+
))
1546+
}
1547+
1548+
async fn stop_sandbox(
1549+
&self,
1550+
_request: Request<openshell_core::proto::compute::v1::StopSandboxRequest>,
1551+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::StopSandboxResponse>, Status>
1552+
{
1553+
Ok(tonic::Response::new(
1554+
openshell_core::proto::compute::v1::StopSandboxResponse {},
1555+
))
1556+
}
1557+
1558+
async fn delete_sandbox(
1559+
&self,
1560+
_request: Request<DeleteSandboxRequest>,
1561+
) -> Result<tonic::Response<openshell_core::proto::compute::v1::DeleteSandboxResponse>, Status>
1562+
{
1563+
Ok(tonic::Response::new(
1564+
openshell_core::proto::compute::v1::DeleteSandboxResponse { deleted: true },
1565+
))
1566+
}
1567+
1568+
async fn watch_sandboxes(
1569+
&self,
1570+
_request: Request<WatchSandboxesRequest>,
1571+
) -> Result<tonic::Response<Self::WatchSandboxesStream>, Status> {
1572+
Ok(tonic::Response::new(Box::pin(futures::stream::empty())))
1573+
}
1574+
}
1575+
1576+
#[cfg(test)]
1577+
pub(crate) async fn new_test_runtime(store: Arc<Store>) -> ComputeRuntime {
1578+
ComputeRuntime {
1579+
driver: Arc::new(NoopTestDriver),
1580+
_driver_process: None,
1581+
default_image: "openshell/sandbox:test".to_string(),
1582+
store,
1583+
sandbox_index: SandboxIndex::new(),
1584+
sandbox_watch_bus: SandboxWatchBus::new(),
1585+
tracing_log_bus: TracingLogBus::new(),
1586+
supervisor_sessions: Arc::new(SupervisorSessionRegistry::new()),
1587+
sync_lock: Arc::new(Mutex::new(())),
1588+
}
1589+
}
1590+
14821591
#[cfg(test)]
14831592
mod tests {
14841593
use super::*;

0 commit comments

Comments
 (0)