Skip to content

Commit 8d6bb82

Browse files
committed
feat(install): prefer Snap on Linux, add preflight checks and tests
Add snap install support: resolve_linux_install_method auto-selects snap when snapd is available, with OPENSHELL_INSTALL_METHOD=classic fallback. Add preflight checks for Docker conflicts and interface connections. Update test suite to cover has_snapd, resolve_snap_channel, and resolve_linux_install_method functions. Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
1 parent 3a4463e commit 8d6bb82

2 files changed

Lines changed: 336 additions & 23 deletions

File tree

‎install.sh‎

Lines changed: 205 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,40 @@
44
#
55
# Install OpenShell from a GitHub release.
66
#
7-
# Linux installs either the Debian or RPM packages from the selected release.
8-
# Apple Silicon macOS installs the generated Homebrew formula, so Homebrew owns
9-
# the binary layout and launchd service lifecycle.
7+
# Linux prefers the Snap path when snapd is available. Otherwise Linux installs
8+
# the Debian or RPM packages from the selected release. Apple Silicon macOS
9+
# installs the generated Homebrew formula, so Homebrew owns the binary layout
10+
# and launchd service lifecycle.
1011
#
1112
set -e
1213

14+
# Ensure snap binaries are on PATH. On some systems (notably Fedora) the
15+
# snapd package is installed via the native package manager and does not
16+
# configure /var/lib/snapd/snap/bin in /etc/profile.d/. Additionally,
17+
# spread injects its own PATH that may omit this directory.
18+
case "${PATH}" in
19+
*/var/lib/snapd/snap/bin*) ;;
20+
*) export PATH="/var/lib/snapd/snap/bin:${PATH}" ;;
21+
esac
22+
1323
APP_NAME="openshell"
1424
REPO="NVIDIA/OpenShell"
1525
GITHUB_URL="https://github.com/${REPO}"
1626
RELEASE_TAG="${OPENSHELL_VERSION:-}"
1727
CHECKSUMS_NAME="openshell-checksums-sha256.txt"
1828
LOCAL_GATEWAY_PORT="17670"
29+
LOCAL_GATEWAY_PROTOCOL="https"
1930
HOMEBREW_TAP="nvidia/openshell"
2031
HOMEBREW_FORMULA_NAME="openshell"
2132
BREAKING_RELEASE_VERSION="0.0.37"
2233
LINUX_PACKAGE_GLIBC_MIN_VERSION="2.31"
2334
UPGRADE_NOTICE_ACK="${OPENSHELL_ACK_BREAKING_UPGRADE:-}"
35+
SNAP_PACKAGE_NAME="openshell"
36+
SNAP_DOCKER_SLOT="docker:docker-daemon"
37+
SNAP_DEFAULT_CHANNEL="latest/stable"
38+
SNAP_DEV_CHANNEL="latest/edge"
39+
LINUX_INSTALL_METHOD_SNAP="snap"
40+
LINUX_INSTALL_METHOD_CLASSIC="classic"
2441

2542
info() {
2643
printf '%s: %s\n' "$APP_NAME" "$*" >&2
@@ -54,13 +71,27 @@ ENVIRONMENT VARIABLES:
5471
OPENSHELL_ACK_BREAKING_UPGRADE
5572
Set to 1 only after backing up and cleaning up a
5673
pre-v0.0.37 installation.
74+
OPENSHELL_INSTALL_METHOD
75+
Linux only. Selects snap or classic. Accepted values:
76+
snap install from the Snap Store
77+
classic install via dpkg or rpm
78+
When unset, snap is used when snapd is available and
79+
running, otherwise the classic package manager is used.
80+
Set OPENSHELL_INSTALL_METHOD=classic on distros where
81+
snapd is not desired.
5782
5883
NOTES:
5984
When OPENSHELL_VERSION is unset, this resolves the latest tagged release
6085
from ${GITHUB_URL}/releases/latest.
6186
62-
Linux installs the Debian package on amd64/arm64 or the RPM packages on
63-
x86_64/aarch64, depending on the host package manager.
87+
Linux prefers the snap method when snapd is available. The Snap channel
88+
follows OPENSHELL_VERSION: 'dev' selects ${SNAP_DEV_CHANNEL:-latest/edge},
89+
tagged releases select ${SNAP_DEFAULT_CHANNEL:-latest/stable}. snapd
90+
manages the gateway as the 'openshell.gateway' service.
91+
92+
On systems without snapd, Linux installs the Debian package on
93+
amd64/arm64 or the RPM packages on x86_64/aarch64, depending on the
94+
host package manager.
6495
macOS installs the release Homebrew formula on Apple Silicon and starts a
6596
brew services-backed local gateway.
6697
EOF
@@ -477,6 +508,75 @@ linux_package_method() {
477508
fi
478509
}
479510

511+
has_snapd() {
512+
if [ "${OPENSHELL_INSTALL_SH_TEST:-0}" = "1" ]; then
513+
case "${OPENSHELL_TEST_SNAPD_AVAILABLE:-0}" in
514+
1) return 0 ;;
515+
0) return 1 ;;
516+
esac
517+
fi
518+
519+
command -v snap >/dev/null 2>&1 || return 1
520+
# Check if snapd.socket is enabled via systemd. If so, snapd can be
521+
# started and we should use the snap install path. Fall back to
522+
# checking the socket file for systems where snapd is already running.
523+
systemctl is-enabled snapd.socket >/dev/null 2>&1 && return 0
524+
[ -S /run/snapd.socket ] || [ -S /var/run/snapd.socket ] || return 1
525+
return 0
526+
}
527+
528+
host_supports_native_package() {
529+
has_cmd dpkg || has_cmd rpm
530+
}
531+
532+
has_native_docker() {
533+
if [ "${OPENSHELL_INSTALL_SH_TEST:-0}" = "1" ] && [ -n "${OPENSHELL_TEST_NATIVE_DOCKER+x}" ]; then
534+
case "${OPENSHELL_TEST_NATIVE_DOCKER}" in
535+
1) return 0 ;;
536+
*) return 1 ;;
537+
esac
538+
fi
539+
540+
command -v docker >/dev/null 2>&1 || return 1
541+
case "$(command -v docker)" in
542+
*/snap/bin/docker | */var/lib/snapd/snap/bin/docker)
543+
return 1
544+
;;
545+
esac
546+
return 0
547+
}
548+
549+
resolve_linux_install_method() {
550+
_method="${OPENSHELL_INSTALL_METHOD:-}"
551+
if [ -n "$_method" ]; then
552+
case "$_method" in
553+
"$LINUX_INSTALL_METHOD_SNAP" | "$LINUX_INSTALL_METHOD_CLASSIC")
554+
echo "$_method"
555+
return 0
556+
;;
557+
*)
558+
error "OPENSHELL_INSTALL_METHOD must be '${LINUX_INSTALL_METHOD_SNAP}' or '${LINUX_INSTALL_METHOD_CLASSIC}' (got: ${_method})"
559+
;;
560+
esac
561+
fi
562+
563+
if has_snapd && ! has_native_docker; then
564+
echo "$LINUX_INSTALL_METHOD_SNAP"
565+
elif host_supports_native_package; then
566+
echo "$LINUX_INSTALL_METHOD_CLASSIC"
567+
else
568+
error "OpenShell Linux installs require either snapd or dpkg/rpm"
569+
fi
570+
}
571+
572+
resolve_snap_channel() {
573+
if [ "${RELEASE_TAG:-}" = "dev" ]; then
574+
echo "$SNAP_DEV_CHANNEL"
575+
else
576+
echo "$SNAP_DEFAULT_CHANNEL"
577+
fi
578+
}
579+
480580
set_linux_target_runtime_dir() {
481581
if [ "$(id -u)" -eq "$TARGET_UID" ] && [ -n "${XDG_RUNTIME_DIR:-}" ]; then
482582
TARGET_RUNTIME_DIR="$XDG_RUNTIME_DIR"
@@ -688,7 +788,7 @@ start_user_gateway() {
688788
if ! as_target_user systemctl --user daemon-reload; then
689789
info "could not reach the user systemd manager for ${TARGET_USER}"
690790
info "restart the gateway later with: systemctl --user enable openshell-gateway && systemctl --user restart openshell-gateway"
691-
info "then register it with: openshell gateway add https://127.0.0.1:17670 --local --name openshell"
791+
info "then register it with: openshell gateway add ${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT} --local --name openshell"
692792
return 0
693793
fi
694794

@@ -764,14 +864,19 @@ wait_for_local_gateway_listener() {
764864
_timeout="${OPENSHELL_INSTALL_GATEWAY_TIMEOUT:-30}"
765865
_elapsed=0
766866
_last_output=""
767-
_probe_url="https://127.0.0.1:${LOCAL_GATEWAY_PORT}/"
867+
_probe_url="${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT}/"
768868
_mtls_dir="${TARGET_HOME}/.config/openshell/gateways/openshell/mtls"
769869

770870
info "waiting for local gateway listener to become reachable..."
771871
while [ "$_elapsed" -lt "$_timeout" ]; do
772-
if [ ! -f "${_mtls_dir}/ca.crt" ] || [ ! -f "${_mtls_dir}/tls.crt" ] || [ ! -f "${_mtls_dir}/tls.key" ]; then
773-
_last_output="mTLS client bundle is not ready under ${_mtls_dir}"
774-
elif _last_output="$(as_target_user curl -sS --max-time 2 --cacert "${_mtls_dir}/ca.crt" --cert "${_mtls_dir}/tls.crt" --key "${_mtls_dir}/tls.key" -o /dev/null "$_probe_url" 2>&1)"; then
872+
if [ "$LOCAL_GATEWAY_PROTOCOL" = "https" ]; then
873+
if [ ! -f "${_mtls_dir}/ca.crt" ] || [ ! -f "${_mtls_dir}/tls.crt" ] || [ ! -f "${_mtls_dir}/tls.key" ]; then
874+
_last_output="mTLS client bundle is not ready under ${_mtls_dir}"
875+
elif _last_output="$(as_target_user curl -sS --max-time 2 --cacert "${_mtls_dir}/ca.crt" --cert "${_mtls_dir}/tls.crt" --key "${_mtls_dir}/tls.key" -o /dev/null "$_probe_url" 2>&1)"; then
876+
info "local gateway listener is reachable"
877+
return 0
878+
fi
879+
elif _last_output="$(as_target_user curl -sS --max-time 2 -o /dev/null "$_probe_url" 2>&1)"; then
775880
info "local gateway listener is reachable"
776881
return 0
777882
fi
@@ -836,7 +941,7 @@ remove_local_gateway_registration() {
836941
register_local_gateway() {
837942
_register_bin="${OPENSHELL_REGISTER_BIN:-openshell}"
838943

839-
if _add_output="$(as_target_user "$_register_bin" gateway add "https://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell 2>&1)"; then
944+
if _add_output="$(as_target_user "$_register_bin" gateway add "${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell 2>&1)"; then
840945
[ -z "$_add_output" ] || print_gateway_add_output "$_add_output"
841946
return 0
842947
else
@@ -847,7 +952,7 @@ register_local_gateway() {
847952
*"already exists"*)
848953
info "local gateway already exists; removing and re-adding it..."
849954
remove_local_gateway_registration
850-
as_target_user "$_register_bin" gateway add "https://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell
955+
as_target_user "$_register_bin" gateway add "${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell
851956
;;
852957
*)
853958
printf '%s\n' "$_add_output" >&2
@@ -859,13 +964,83 @@ register_local_gateway() {
859964
print_gateway_add_output() {
860965
printf '%s\n' "$1" | while IFS= read -r _line; do
861966
case "$_line" in
862-
*"Gateway is not reachable at https://127.0.0.1:${LOCAL_GATEWAY_PORT}"*) ;;
967+
*"Gateway is not reachable at ${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT}"*) ;;
863968
*"Verify the gateway is running and the endpoint is correct."*) ;;
864969
*) printf '%s\n' "$_line" >&2 ;;
865970
esac
866971
done
867972
}
868973

974+
start_snap_gateway() {
975+
info "waiting for the OpenShell gateway (snap.openshell.gateway) to become ready..."
976+
977+
_timeout="${OPENSHELL_INSTALL_GATEWAY_TIMEOUT:-60}"
978+
_elapsed=0
979+
980+
while [ "$_elapsed" -lt "$_timeout" ]; do
981+
if systemctl is-active --quiet snap.openshell.gateway 2>/dev/null; then
982+
info "openshell.gateway service is running"
983+
break
984+
fi
985+
sleep 1
986+
_elapsed=$((_elapsed + 1))
987+
done
988+
989+
if [ "$_elapsed" -ge "$_timeout" ]; then
990+
info "snap.openshell.gateway did not become ready within ${_timeout}s"
991+
info "check its status with: systemctl status snap.openshell.gateway"
992+
info "then register the gateway with:"
993+
info " openshell gateway add ${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT} --local --name openshell"
994+
return 1
995+
fi
996+
997+
info "registering local gateway as ${TARGET_USER}..."
998+
register_local_gateway
999+
wait_for_local_gateway_listener
1000+
wait_for_local_gateway_status
1001+
}
1002+
1003+
install_linux_snap() {
1004+
_channel="$(resolve_snap_channel)"
1005+
local _snap_installed=false
1006+
1007+
_snap_clean() {
1008+
if [ "$_snap_installed" = true ]; then
1009+
warn "removing ${APP_NAME} snap due to interrupted install..."
1010+
as_root snap remove --purge "$SNAP_PACKAGE_NAME" || \
1011+
warn "could not remove ${APP_NAME} snap; please remove it manually"
1012+
fi
1013+
}
1014+
trap '_snap_clean' EXIT INT TERM
1015+
1016+
info "pre-installing the Docker snap..."
1017+
as_root snap install docker
1018+
1019+
info "installing ${APP_NAME} from the Snap Store (channel: ${_channel})..."
1020+
as_root snap install "$SNAP_PACKAGE_NAME" --channel="$_channel"
1021+
_snap_installed=true
1022+
1023+
info "connecting required interfaces..."
1024+
as_root snap connect "${SNAP_PACKAGE_NAME}:docker" "$SNAP_DOCKER_SLOT" || \
1025+
warn "could not connect ${SNAP_PACKAGE_NAME}:docker to ${SNAP_DOCKER_SLOT}; the Docker driver will not work until this is fixed"
1026+
for _plug in log-observe system-observe ssh-keys; do
1027+
as_root snap connect "${SNAP_PACKAGE_NAME}:${_plug}" || \
1028+
warn "could not connect ${SNAP_PACKAGE_NAME}:${_plug}; some features may be limited"
1029+
done
1030+
1031+
info "restarting OpenShell gateway (snap.openshell.gateway) after interface connections"
1032+
systemctl stop snap.openshell.gateway
1033+
systemctl start snap.openshell.gateway
1034+
1035+
trap - EXIT INT TERM
1036+
info "installed ${APP_NAME} from the Snap Store (${_channel})"
1037+
info "snapd manages the OpenShell gateway as the 'snap.openshell.gateway' service."
1038+
1039+
set_linux_target_runtime_dir
1040+
LOCAL_GATEWAY_PROTOCOL="http"
1041+
start_snap_gateway || return $?
1042+
}
1043+
8691044
install_linux_deb() {
8701045
check_linux_deb_platform
8711046
set_linux_target_runtime_dir
@@ -992,7 +1167,7 @@ install_macos_homebrew() {
9921167
if ! as_target_user brew services restart "$_formula_ref"; then
9931168
warn "could not restart the OpenShell Homebrew service"
9941169
info "restart it later with: brew services restart ${_formula_ref}"
995-
info "then register it with: openshell gateway add https://127.0.0.1:${LOCAL_GATEWAY_PORT} --local --name openshell"
1170+
info "then register it with: openshell gateway add ${LOCAL_GATEWAY_PROTOCOL}://127.0.0.1:${LOCAL_GATEWAY_PORT} --local --name openshell"
9961171
return 0
9971172
fi
9981173

@@ -1033,16 +1208,23 @@ main() {
10331208

10341209
case "$PLATFORM" in
10351210
linux)
1036-
require_linux_package_glibc
1037-
case "$(linux_package_method)" in
1038-
deb)
1039-
install_linux_deb
1040-
;;
1041-
rpm)
1042-
install_linux_rpm
1211+
case "$(resolve_linux_install_method)" in
1212+
"$LINUX_INSTALL_METHOD_SNAP")
1213+
install_linux_snap
10431214
;;
1044-
*)
1045-
error "unsupported Linux package method"
1215+
"$LINUX_INSTALL_METHOD_CLASSIC")
1216+
require_linux_package_glibc
1217+
case "$(linux_package_method)" in
1218+
deb)
1219+
install_linux_deb
1220+
;;
1221+
rpm)
1222+
install_linux_rpm
1223+
;;
1224+
*)
1225+
error "unsupported Linux package method"
1226+
;;
1227+
esac
10461228
;;
10471229
esac
10481230
;;

0 commit comments

Comments
 (0)