Skip to content

Commit e14f64d

Browse files
refactor(scripts): rewrite install method detection
- Rewrites the logic for detecting the installation method - Removes duplicated and outdated detection code. - Adds missing sudo usage to `docker run` - Enhances output messages - Improved WSL detection
1 parent 516ed10 commit e14f64d

File tree

1 file changed

+80
-51
lines changed

1 file changed

+80
-51
lines changed

install.sh

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ podman_install() {
4646

4747
CONTAINER_NAME="${CONTAINER_NAME:-$DEFAULT_CONTAINER_NAME}"
4848

49-
if ! $SUDO systemctl is-active --quiet podman.socket; then
50-
echo "❌ Podman is not running as a systemd unit service. Please start it and try again."
51-
exit 1
52-
fi
53-
5449
$SUDO podman run -d \
5550
--name=$CONTAINER_NAME \
5651
--replace \
@@ -120,7 +115,7 @@ docker_install() {
120115

121116
CONTAINER_NAME="${CONTAINER_NAME:-$DEFAULT_CONTAINER_NAME}"
122117

123-
docker run -d \
118+
$SUDO docker run -d \
124119
--name=$CONTAINER_NAME \
125120
--restart=on-failure \
126121
--privileged \
@@ -140,7 +135,6 @@ docker_install() {
140135
$ARGS \
141136
shellhubio/agent:$AGENT_VERSION \
142137
$MODE
143-
144138
}
145139

146140
snap_install() {
@@ -344,47 +338,6 @@ RUNC_ARCH=$RUNC_ARCH
344338
INSTALL_DIR="${INSTALL_DIR:-/opt/shellhub}"
345339
TMP_DIR="${TMP_DIR:-$(mktemp -d -t shellhub-installer-XXXXXX)}"
346340

347-
# Checking for podman first as it can be aliased for docker in some systems
348-
# Always running podman as root as we need to mount system directories
349-
if type podman >/dev/null 2>&1; then
350-
if [ "$(id -u)" -ne 0 ]; then
351-
[ -z "$SUDO" ] && SUDO="sudo" || { SUDO=""; }
352-
fi
353-
if $SUDO podman info >/dev/null 2>&1; then
354-
INSTALL_METHOD="${INSTALL_METHOD:-podman}"
355-
fi
356-
fi
357-
358-
if [ -z "$INSTALL_METHOD" ] && type docker >/dev/null 2>&1; then
359-
while :; do
360-
if $SUDO docker info >/dev/null 2>&1; then
361-
INSTALL_METHOD="${INSTALL_METHOD:-docker}"
362-
break
363-
elif [ "$(id -u)" -ne 0 ]; then
364-
[ -z "$SUDO" ] && SUDO="sudo" || { SUDO="" && break; }
365-
fi
366-
done
367-
fi
368-
369-
if [ -z "$INSTALL_METHOD" ] && type snap >/dev/null 2>&1; then
370-
INSTALL_METHOD="snap"
371-
fi
372-
373-
# Check if running on WSL
374-
if [ -e /proc/sys/fs/binfmt_misc/WSLInterop ]; then
375-
WSL_EXE=$(find /mnt/*/Windows/System32/wsl.exe 2>/dev/null | head -n 1)
376-
WSL_VERSION=$($WSL_EXE -l -v | tr -d '\0' | grep ${WSL_DISTRO_NAME} | awk '{print $NF}' | tr -d -c '0-9')
377-
378-
if [ -z "$WSL_VERSION" ] || [ "$WSL_VERSION" -lt 2 ]; then
379-
echo "❌ ERROR: WSL version 2 is required to run ShellHub."
380-
exit 1
381-
fi
382-
383-
INSTALL_METHOD="wsl"
384-
fi
385-
386-
INSTALL_METHOD="${INSTALL_METHOD:-standalone}"
387-
388341
# Auto detect arch if it has not already been set
389342
if [ -z "$AGENT_ARCH" ]; then
390343
case $(uname -m) in
@@ -407,15 +360,91 @@ if [ -z "$AGENT_ARCH" ]; then
407360
esac
408361
fi
409362

410-
echo "🛠️ Welcome to the ShellHub Agent Installer Script"
363+
echo "🛠️ ShellHub Agent Installer"
411364
echo
412-
echo "📝 Summary of chosen options:"
365+
if [ -z "$INSTALL_METHOD" ]; then
366+
echo "This script will install the ShellHub agent on your system."
367+
echo "It will auto-detect the best available installation method."
368+
echo
369+
echo "Installation methods (priority order):"
370+
echo " 1. Docker - If Docker is installed and accessible in rootful mode"
371+
echo " 2. Podman - If Podman is installed and accessible in rootful mode"
372+
echo " 3. Snap - If Snap package manager is available"
373+
echo " 4. WSL - If running in WSL2 with systemd and mirrored networking"
374+
echo " 5. Standalone - Fallback method using runc and systemd"
375+
echo
376+
fi
377+
378+
echo "⚙️ Detected settings:"
413379
echo "- Server address: $SERVER_ADDRESS"
414380
echo "- Tenant ID: $TENANT_ID"
415-
echo "- Install method: $INSTALL_METHOD"
416381
echo "- Agent version: $AGENT_VERSION"
382+
echo "- Architecture: $AGENT_ARCH"
383+
[ -n "$INSTALL_METHOD" ] && echo "- Install method: $INSTALL_METHOD"
417384
echo
418385

386+
if [ -z "$INSTALL_METHOD" ] && type docker >/dev/null 2>&1; then
387+
echo "🔍 Checking if Docker is available and accessible in rootful mode..."
388+
389+
export DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
390+
391+
for prefix in "" "sudo"; do
392+
if $prefix docker info >/dev/null 2>&1; then
393+
SUDO=$prefix
394+
INSTALL_METHOD="docker"
395+
break
396+
fi
397+
done
398+
399+
[ -z "$INSTALL_METHOD" ] && echo "ℹ️ Docker is not accessible in rootful mode."
400+
fi
401+
402+
if [ -z "$INSTALL_METHOD" ] && type podman >/dev/null 2>&1; then
403+
echo "🔍 Checking if Podman is available and accessible in rootful mode..."
404+
405+
export CONTAINER_HOST="${CONTAINER_HOST:-unix:///var/run/podman/podman.sock}"
406+
407+
for prefix in "" "sudo"; do
408+
if $prefix podman info >/dev/null 2>&1; then
409+
SUDO=$prefix
410+
INSTALL_METHOD="podman"
411+
break
412+
fi
413+
done
414+
415+
[ -z "$INSTALL_METHOD" ] && echo "ℹ️ Podman is not accessible in rootful mode."
416+
fi
417+
418+
if [ -z "$INSTALL_METHOD" ]; then
419+
echo
420+
echo "⚠️ NOTE: No recommended installation method was detected."
421+
echo "⚠️ For best performance, easier updates, and better isolation, it is strongly recommended to use Docker or Podman."
422+
echo "ℹ️ The installer will proceed with an alternative method (Snap, Standalone, or WSL), but these may have limitations."
423+
echo
424+
fi
425+
426+
if [ -z "$INSTALL_METHOD" ] && type snap >/dev/null 2>&1; then
427+
echo "🔍 Detected Snap package manager..."
428+
INSTALL_METHOD="snap"
429+
fi
430+
431+
# Check if running on WSL
432+
if grep -qi Microsoft /proc/version; then
433+
echo "🔍 Detected WSL environment..."
434+
435+
WSL_EXE=$(find /mnt/*/Windows/System32/wsl.exe 2>/dev/null | head -n 1)
436+
WSL_VERSION=$($WSL_EXE -l -v | tr -d '\0' | grep ${WSL_DISTRO_NAME} | awk '{print $NF}' | tr -d -c '0-9')
437+
438+
if [ -z "$WSL_VERSION" ] || [ "$WSL_VERSION" -lt 2 ]; then
439+
echo "❌ ERROR: WSL version 2 is required to run ShellHub."
440+
exit 1
441+
fi
442+
443+
INSTALL_METHOD="wsl"
444+
fi
445+
446+
[ -z "$INSTALL_METHOD" ] && INSTALL_METHOD="standalone"
447+
419448
case "$INSTALL_METHOD" in
420449
podman)
421450
echo "🐳 Installing ShellHub using podman method..."

0 commit comments

Comments
 (0)