Skip to content

Commit 4d839f2

Browse files
Merge pull request NVIDIA#23 from markatcanonical/deploy-k8s
Don't force the OPENSHELL_GATEWAY in snap environment variables, instead infer it from SNAP_COMMON
2 parents 1d4cf7f + 9d08f73 commit 4d839f2

3 files changed

Lines changed: 62 additions & 20 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,45 @@ fn resolve_gateway(
7777
});
7878
}
7979

80-
let name = gateway_flag
80+
let name_opt = gateway_flag
8181
.clone()
8282
.or_else(|| {
8383
std::env::var("OPENSHELL_GATEWAY")
8484
.ok()
8585
.filter(|v| !v.trim().is_empty())
8686
})
87-
.or_else(load_active_gateway)
88-
.ok_or_else(|| {
87+
.or_else(load_active_gateway);
88+
89+
if let Some(name) = name_opt {
90+
let metadata = load_gateway_metadata(&name).map_err(|_| {
8991
miette::miette!(
90-
"No active gateway.\n\
91-
Set one with: openshell gateway select <name>\n\
92-
Or deploy a new gateway: openshell gateway start"
92+
"Unknown gateway '{name}'.\n\
93+
Deploy it first: openshell gateway start --name {name}\n\
94+
Or list available gateways: openshell gateway select"
9395
)
9496
})?;
9597

96-
let metadata = load_gateway_metadata(&name).map_err(|_| {
97-
miette::miette!(
98-
"Unknown gateway '{name}'.\n\
99-
Deploy it first: openshell gateway start --name {name}\n\
100-
Or list available gateways: openshell gateway select"
101-
)
102-
})?;
98+
return Ok(GatewayContext {
99+
name: metadata.name,
100+
endpoint: metadata.gateway_endpoint,
101+
});
102+
}
103103

104-
Ok(GatewayContext {
105-
name: metadata.name,
106-
endpoint: metadata.gateway_endpoint,
107-
})
104+
if let Ok(snap_common) = std::env::var("SNAP_COMMON") {
105+
let sock_path = format!("{}/run/gateway.sock", snap_common);
106+
if std::path::Path::new(&sock_path).exists() {
107+
return Ok(GatewayContext {
108+
name: "local-vm".to_string(),
109+
endpoint: format!("unix://{}", sock_path),
110+
});
111+
}
112+
}
113+
114+
Err(miette::miette!(
115+
"No active gateway.\n\
116+
Set one with: openshell gateway select <name>\n\
117+
Or deploy a new gateway: openshell gateway start"
118+
))
108119
}
109120

110121
/// Resolve only the gateway name (without requiring metadata to exist).
@@ -120,6 +131,15 @@ fn resolve_gateway_name(gateway_flag: &Option<String>) -> Option<String> {
120131
.filter(|v| !v.trim().is_empty())
121132
})
122133
.or_else(load_active_gateway)
134+
.or_else(|| {
135+
if let Ok(snap_common) = std::env::var("SNAP_COMMON") {
136+
let sock_path = format!("{}/run/gateway.sock", snap_common);
137+
if std::path::Path::new(&sock_path).exists() {
138+
return Some("local-vm".to_string());
139+
}
140+
}
141+
None
142+
})
123143
}
124144

125145
/// Apply edge authentication token from local storage when the gateway uses edge auth.
@@ -1771,7 +1791,12 @@ async fn main() -> Result<()> {
17711791
}) => match command {
17721792
ClusterCommands::Init { namespace, registry, registry_username, registry_token, registry_authfile, kubeconfig } => {
17731793
let gateway_name = match cli.gateway {
1774-
Some(name) => name,
1794+
Some(name) => {
1795+
if name == "local-vm" {
1796+
return Err(miette::miette!("The gateway name 'local-vm' is reserved."));
1797+
}
1798+
name
1799+
},
17751800
None => return Err(miette::miette!("ERROR: --gateway is required to name the new K8s gateway")),
17761801
};
17771802

@@ -1888,6 +1913,10 @@ async fn main() -> Result<()> {
18881913
registry_token,
18891914
gpu,
18901915
} => {
1916+
if name == "local-vm" {
1917+
return Err(miette::miette!("The gateway name 'local-vm' is reserved."));
1918+
}
1919+
18911920
let gpu = if gpu {
18921921
vec!["auto".to_string()]
18931922
} else {
@@ -1935,6 +1964,12 @@ async fn main() -> Result<()> {
19351964
ssh_key,
19361965
local,
19371966
} => {
1967+
if let Some(n) = &name {
1968+
if n == "local-vm" {
1969+
return Err(miette::miette!("The gateway name 'local-vm' is reserved."));
1970+
}
1971+
}
1972+
19381973
run::gateway_add(
19391974
&endpoint,
19401975
name.as_deref(),

‎rockcraft-openshell-core.yaml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ parts:
2828
- libssl3t64
2929
- libz3-4
3030
- ca-certificates
31+
- busybox-static
3132
rust-features: []
3233
rust-path:
3334
- "crates/openshell-server"
@@ -38,7 +39,14 @@ parts:
3839
override-pull: |
3940
craftctl default
4041
# Set version to git describe or short commit hash
41-
craftctl set version=$(git describe --tags --abbrev=10 || git rev-parse --short HEAD)
42+
craftctl set version=$(git branch --show-current)-g$(git rev-parse --short HEAD)
43+
44+
override-build: |
45+
craftctl default
46+
mkdir -p $CRAFT_PART_INSTALL/bin
47+
for cmd in sh cp sleep nsenter; do
48+
ln -s busybox $CRAFT_PART_INSTALL/bin/$cmd
49+
done
4250
4351
4452
migrations:

‎snap/snapcraft.yaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ apps:
2222
command: bin/openshell
2323
environment:
2424
KUBECONFIG: $SNAP_REAL_HOME/.kube/config
25-
OPENSHELL_GATEWAY_ENDPOINT: unix://$SNAP_COMMON/run/gateway.sock
2625
plugs:
2726
- network
2827
- network-bind

0 commit comments

Comments
 (0)