Skip to content

Commit 5a44137

Browse files
committed
feat(gateway): read driver_dir from [openshell.gateway]
The gateway owns driver_dir. A driver_dir in a [openshell.drivers.<name>] table is deprecated: it is stripped with a warning and used only when the gateway section sets none. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 85afe7f commit 5a44137

9 files changed

Lines changed: 101 additions & 29 deletions

File tree

‎crates/openshell-gateway/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl openshell_server::ComputeDriverFactory for PodmanFactory {
512512
fn podman_launch_plan(
513513
context: openshell_server::ComputeDriverConfigContext<'_>,
514514
) -> openshell_core::Result<podman::PodmanLaunchPlan> {
515-
podman::PodmanLaunchPlan::from_table(context.driver_table()?)
515+
podman::PodmanLaunchPlan::from_table(context.driver_table()?, context.driver_dir())
516516
}
517517

518518
#[cfg(all(not(target_os = "windows"), feature = "compute-driver-vm"))]
@@ -572,7 +572,7 @@ impl openshell_server::ComputeDriverFactory for VmFactory {
572572
fn vm_launch_plan(
573573
context: openshell_server::ComputeDriverConfigContext<'_>,
574574
) -> openshell_core::Result<vm::VmLaunchPlan> {
575-
vm::VmLaunchPlan::from_table(context.driver_table()?)
575+
vm::VmLaunchPlan::from_table(context.driver_table()?, context.driver_dir())
576576
}
577577

578578
#[cfg(all(

‎crates/openshell-gateway/src/podman.rs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,28 @@ pub struct PodmanLaunchPlan {
3333

3434
impl PodmanLaunchPlan {
3535
/// Split gateway-owned keys from the table the driver will parse.
36-
pub fn from_table(mut table: toml::map::Map<String, toml::Value>) -> Result<Self> {
37-
let driver_dir = table
36+
///
37+
/// `gateway_driver_dir` is `driver_dir` from `[openshell.gateway]`. A
38+
/// `driver_dir` in the driver table is deprecated: it is removed with a
39+
/// warning and used only when the gateway section sets none.
40+
pub fn from_table(
41+
mut table: toml::map::Map<String, toml::Value>,
42+
gateway_driver_dir: Option<&Path>,
43+
) -> Result<Self> {
44+
let legacy_driver_dir = table
3845
.remove("driver_dir")
3946
.map(|value| {
47+
tracing::warn!(
48+
"[openshell.drivers.podman].driver_dir is deprecated; set driver_dir in [openshell.gateway]"
49+
);
4050
value.as_str().map(PathBuf::from).ok_or_else(|| {
4151
Error::config("[openshell.drivers.podman].driver_dir must be a string")
4252
})
4353
})
4454
.transpose()?;
55+
let driver_dir = gateway_driver_dir
56+
.map(Path::to_path_buf)
57+
.or(legacy_driver_dir);
4558
let table = toml::to_string(&table)
4659
.map_err(|error| Error::config(format!("invalid podman driver table: {error}")))?;
4760
Ok(Self { driver_dir, table })
@@ -82,7 +95,7 @@ pub fn detect(context: &DriverDetectionContext<'_>) -> DriverDetection {
8295
}
8396

8497
fn detect_in(context: &DriverDetectionContext<'_>, socket_dir: &Path) -> DriverDetection {
85-
let plan = match PodmanLaunchPlan::from_table(context.table.clone()) {
98+
let plan = match PodmanLaunchPlan::from_table(context.table.clone(), context.driver_dir) {
8699
Ok(plan) => plan,
87100
Err(error) => return DriverDetection::InvalidConfig(error.to_string()),
88101
};

‎crates/openshell-gateway/src/podman/tests.rs‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ fn driver(dir: &Path, body: &str) {
3030
fn detect_with(body: &str, extra: &str) -> DriverDetection {
3131
let dir = tempfile::tempdir().unwrap();
3232
driver(dir.path(), body);
33-
let table = table(&format!(
34-
"driver_dir = \"{}\"\n{extra}",
35-
dir.path().display()
36-
));
33+
let table = table(extra);
3734
detect_in(
3835
&DriverDetectionContext {
3936
table: &table,
37+
driver_dir: Some(dir.path()),
4038
gateway_log_level: "info",
4139
},
4240
&dir.path().join("run"),
@@ -45,17 +43,20 @@ fn detect_with(body: &str, extra: &str) -> DriverDetection {
4543

4644
#[test]
4745
fn gateway_owned_keys_are_removed_and_the_rest_is_forwarded() {
48-
let plan = PodmanLaunchPlan::from_table(table(
49-
"driver_dir = \"/opt/drivers\"\nsocket_path = \"/run/podman.sock\"\nnetwork_name = \"n\"",
50-
))
46+
let plan = PodmanLaunchPlan::from_table(
47+
table(
48+
"driver_dir = \"/opt/drivers\"\nsocket_path = \"/run/podman.sock\"\nnetwork_name = \"n\"",
49+
),
50+
None,
51+
)
5152
.unwrap();
5253

5354
assert_eq!(plan.driver_dir, Some(PathBuf::from("/opt/drivers")));
5455
let forwarded = table(&plan.table);
5556
assert!(!forwarded.contains_key("driver_dir"));
5657
assert_eq!(forwarded["socket_path"].as_str(), Some("/run/podman.sock"));
5758
assert_eq!(forwarded["network_name"].as_str(), Some("n"));
58-
assert!(PodmanLaunchPlan::from_table(table("driver_dir = 3")).is_err());
59+
assert!(PodmanLaunchPlan::from_table(table("driver_dir = 3"), None).is_err());
5960
}
6061

6162
#[test]
@@ -100,9 +101,10 @@ fn detection_keeps_the_driver_it_started_and_stops_it_if_unused() {
100101
pid_file.display()
101102
),
102103
);
103-
let table = table(&format!("driver_dir = \"{}\"", bin.display()));
104+
let table = table("");
104105
let context = DriverDetectionContext {
105106
table: &table,
107+
driver_dir: Some(&bin),
106108
gateway_log_level: "info",
107109
};
108110

@@ -134,10 +136,11 @@ fn detection_keeps_the_driver_it_started_and_stops_it_if_unused() {
134136
#[test]
135137
fn a_missing_driver_binary_is_unavailable() {
136138
let dir = tempfile::tempdir().unwrap();
137-
let table = table(&format!("driver_dir = \"{}\"", dir.path().display()));
139+
let table = table("");
138140
let result = detect_in(
139141
&DriverDetectionContext {
140142
table: &table,
143+
driver_dir: Some(dir.path()),
141144
gateway_log_level: "info",
142145
},
143146
&dir.path().join("run"),

‎crates/openshell-gateway/src/vm.rs‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ pub struct VmLaunchPlan {
3535

3636
impl VmLaunchPlan {
3737
/// Split gateway-owned keys from the table the driver will parse.
38-
pub fn from_table(mut table: toml::map::Map<String, toml::Value>) -> Result<Self> {
38+
///
39+
/// `gateway_driver_dir` is `driver_dir` from `[openshell.gateway]`. A
40+
/// `driver_dir` in the driver table is deprecated: it is removed with a
41+
/// warning and used only when the gateway section sets none.
42+
pub fn from_table(
43+
mut table: toml::map::Map<String, toml::Value>,
44+
gateway_driver_dir: Option<&Path>,
45+
) -> Result<Self> {
3946
let path_key = |table: &toml::map::Map<String, toml::Value>, key: &str| {
4047
table
4148
.get(key)
@@ -46,8 +53,15 @@ impl VmLaunchPlan {
4653
})
4754
.transpose()
4855
};
49-
let driver_dir = path_key(&table, "driver_dir")?;
50-
table.remove("driver_dir");
56+
let legacy_driver_dir = path_key(&table, "driver_dir")?;
57+
if table.remove("driver_dir").is_some() {
58+
tracing::warn!(
59+
"[openshell.drivers.vm].driver_dir is deprecated; set driver_dir in [openshell.gateway]"
60+
);
61+
}
62+
let driver_dir = gateway_driver_dir
63+
.map(Path::to_path_buf)
64+
.or(legacy_driver_dir);
5165
let state_dir = path_key(&table, "state_dir")?
5266
.filter(|dir| !dir.as_os_str().is_empty())
5367
.unwrap_or_else(Self::default_state_dir);

‎crates/openshell-gateway/src/vm/tests.rs‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ fn table(text: &str) -> toml::map::Map<String, toml::Value> {
1313

1414
#[test]
1515
fn gateway_owned_keys_are_removed_and_the_rest_is_forwarded() {
16-
let plan = VmLaunchPlan::from_table(table(
17-
"driver_dir = \"/opt/drivers\"\nstate_dir = \"/var/lib/vm\"\nvcpus = 4\nunknown_to_the_gateway = true",
18-
))
16+
let plan = VmLaunchPlan::from_table(
17+
table(
18+
"driver_dir = \"/opt/drivers\"\nstate_dir = \"/var/lib/vm\"\nvcpus = 4\nunknown_to_the_gateway = true",
19+
),
20+
None,
21+
)
1922
.unwrap();
2023

2124
assert_eq!(plan.driver_dir, Some(PathBuf::from("/opt/drivers")));
@@ -29,23 +32,24 @@ fn gateway_owned_keys_are_removed_and_the_rest_is_forwarded() {
2932

3033
#[test]
3134
fn an_empty_table_selects_the_default_state_dir() {
32-
let plan = VmLaunchPlan::from_table(table("")).unwrap();
35+
let plan = VmLaunchPlan::from_table(table(""), None).unwrap();
3336
assert_eq!(plan.state_dir, VmLaunchPlan::default_state_dir());
3437
assert_eq!(plan.driver_dir, None);
3538
assert!(plan.table.trim().is_empty());
3639

37-
let empty = VmLaunchPlan::from_table(table("state_dir = \"\"")).unwrap();
40+
let empty = VmLaunchPlan::from_table(table("state_dir = \"\""), None).unwrap();
3841
assert_eq!(empty.state_dir, VmLaunchPlan::default_state_dir());
3942
}
4043

4144
#[test]
4245
fn a_non_string_gateway_key_is_rejected() {
43-
assert!(VmLaunchPlan::from_table(table("driver_dir = 3")).is_err());
46+
assert!(VmLaunchPlan::from_table(table("driver_dir = 3"), None).is_err());
4447
}
4548

4649
#[test]
4750
fn socket_dir_is_the_private_run_dir_under_the_state_dir() {
48-
let plan = VmLaunchPlan::from_table(table("state_dir = \"/tmp/openshell-vm-state\"")).unwrap();
51+
let plan =
52+
VmLaunchPlan::from_table(table("state_dir = \"/tmp/openshell-vm-state\""), None).unwrap();
4953
assert_eq!(
5054
plan.socket_dir(),
5155
PathBuf::from("/tmp/openshell-vm-state/run")
@@ -124,3 +128,21 @@ fn prepare_vm_state_dir_rejects_symlinks_and_the_wrong_owner() {
124128
.to_string();
125129
assert!(error.contains("is owned by uid"), "{error}");
126130
}
131+
132+
#[test]
133+
fn driver_dir_comes_from_the_gateway_section_and_the_legacy_key_is_stripped() {
134+
let from_gateway = VmLaunchPlan::from_table(
135+
table("driver_dir = \"/legacy\"\nvcpus = 2"),
136+
Some(Path::new("/gateway")),
137+
)
138+
.unwrap();
139+
assert_eq!(from_gateway.driver_dir, Some(PathBuf::from("/gateway")));
140+
assert!(!from_gateway.table.contains("driver_dir"));
141+
142+
let legacy_only = VmLaunchPlan::from_table(table("driver_dir = \"/legacy\""), None).unwrap();
143+
assert_eq!(legacy_only.driver_dir, Some(PathBuf::from("/legacy")));
144+
assert!(!legacy_only.table.contains("driver_dir"));
145+
146+
let gateway_only = VmLaunchPlan::from_table(table(""), Some(Path::new("/gateway"))).unwrap();
147+
assert_eq!(gateway_only.driver_dir, Some(PathBuf::from("/gateway")));
148+
}

‎crates/openshell-gateway/tests/config_preflight.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl Fixture {
5353
.expect("serialize VM state directory");
5454
let driver_dir = toml::Value::try_from(&self.tools).expect("serialize driver directory");
5555
fs::write(&self.config, format!(
56-
"[openshell]\nversion = 2\n[openshell.gateway]\ndisable_tls = true\n{selector}[openshell.drivers.vm]\ndriver_dir = {driver_dir}\nbootstrap_image = 'unreachable.invalid/vm:must-not-pull'\nstate_dir = {state_dir}\n"
56+
"[openshell]\nversion = 2\n[openshell.gateway]\ndisable_tls = true\ndriver_dir = {driver_dir}\n{selector}[openshell.drivers.vm]\nbootstrap_image = 'unreachable.invalid/vm:must-not-pull'\nstate_dir = {state_dir}\n"
5757
)).expect("gateway configuration");
5858
}
5959

‎crates/openshell-server/src/config_file.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ pub struct GatewayFileSection {
214214
#[serde(default)]
215215
pub guest_tls_ca: Option<PathBuf>,
216216

217+
// ── Launched compute drivers ─────────────────────────────────────────
218+
/// Directory searched first for launched compute-driver binaries such as
219+
/// `openshell-driver-vm`. The gateway owns it; a `driver_dir` in a
220+
/// `[openshell.drivers.<name>]` table is deprecated.
221+
#[serde(default)]
222+
pub driver_dir: Option<PathBuf>,
223+
217224
// ── TLS toggle ───────────────────────────────────────────────────────
218225
/// When `true`, the gateway listens on plaintext HTTP and ignores any
219226
/// `[openshell.gateway.tls]` table. Mirrors `--disable-tls`.

‎crates/openshell-server/src/lib.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,8 @@ pub enum DriverDetection {
19251925
pub struct DriverDetectionContext<'a> {
19261926
/// The driver's merged `[openshell.drivers.<name>]` table.
19271927
pub table: &'a toml::map::Map<String, toml::Value>,
1928+
/// `driver_dir` from `[openshell.gateway]`.
1929+
pub driver_dir: Option<&'a Path>,
19281930
pub gateway_log_level: &'a str,
19291931
}
19301932

@@ -2159,6 +2161,8 @@ impl ComputeDriverRegistry {
21592161
.unwrap_or_default();
21602162
let context = DriverDetectionContext {
21612163
table: &table,
2164+
driver_dir: file
2165+
.and_then(|file| file.openshell.gateway.driver_dir.as_deref()),
21622166
gateway_log_level,
21632167
};
21642168
match probe(&context) {
@@ -2289,6 +2293,15 @@ impl ComputeDriverConfigContext<'_> {
22892293
self.driver_startup.gateway_tls_enabled
22902294
}
22912295

2296+
/// `driver_dir` from `[openshell.gateway]`: where launched compute-driver
2297+
/// binaries are searched for first.
2298+
#[must_use]
2299+
pub fn driver_dir(&self) -> Option<&Path> {
2300+
self.driver_startup
2301+
.file
2302+
.and_then(|file| file.openshell.gateway.driver_dir.as_deref())
2303+
}
2304+
22922305
/// The selected driver's merged TOML table, undecoded. A launched driver
22932306
/// parses and validates it itself.
22942307
pub fn driver_table(&self) -> Result<toml::map::Map<String, toml::Value>> {

‎docs/how-it-works/gateways/configuration.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ The gateway starts `openshell-driver-<name> serve` and passes the table as text
15471547

15481548
A driver exits with status 69 when its backend is not reachable and 78 when its table is invalid. It writes one JSON line describing the reason to standard output first. Standard error stays for logs.
15491549

1550-
The gateway removes `driver_dir` from the table before forwarding it. It looks for the driver binary in `driver_dir`, then in `~/.local/libexec/openshell`, `/usr/libexec/openshell`, `/usr/local/libexec/openshell`, and `/usr/local/libexec`, then next to the gateway binary. The binary must be owned by root or the gateway user and must not be writable by group or others.
1550+
Set `driver_dir` in `[openshell.gateway]`. A `driver_dir` in a driver table is deprecated: the gateway removes it with a warning and uses it only when `[openshell.gateway]` sets none. The gateway looks for the driver binary in `driver_dir`, then in `~/.local/libexec/openshell`, `/usr/libexec/openshell`, `/usr/local/libexec/openshell`, and `/usr/local/libexec`, then next to the gateway binary. The binary must be owned by root or the gateway user and must not be writable by group or others.
15511551

15521552
When no driver is selected, the gateway tries Kubernetes, then Podman, then Docker. For Podman it starts the driver with the operator's table, so a configured `socket_path` is honored. The first driver that binds its socket is kept and configured, so detection never starts a driver it then stops. A Podman table that is invalid, or a socket that is not reachable, moves detection to the next candidate and logs the reason. If a driver is selected by name, any such failure stops startup.
15531553

@@ -2118,6 +2118,8 @@ log_level = "info"
21182118
compute_driver = "vm"
21192119
# Gateway-owned CA injected into the selected local driver.
21202120
guest_tls_ca = "/var/lib/openshell/guest-tls/ca.pem"
2121+
# Where the gateway looks for the openshell-driver-vm subprocess binary.
2122+
driver_dir = "/usr/local/libexec/openshell"
21212123

21222124
# Sandbox launch signing is separate from listener and guest TLS.
21232125
[openshell.gateway.gateway_jwt]
@@ -2128,8 +2130,6 @@ gateway_id = "openshell"
21282130

21292131
[openshell.drivers.vm]
21302132
state_dir = "/var/lib/openshell/vm"
2131-
# Where the gateway looks for the openshell-driver-vm subprocess binary.
2132-
driver_dir = "/usr/local/libexec/openshell"
21332133
default_image = "nvcr.io/nvidia/base/ubuntu:24.04"
21342134
# Optional override. When omitted, the gateway derives
21352135
# https://host.openshell.internal:<gateway-port> for the VM driver.

0 commit comments

Comments
 (0)