|
| 1 | +#!/usr/bin/env bash |
| 2 | +# web-shell feature installer. |
| 3 | +# https://github.com/SoureCode/web-shell |
| 4 | +# |
| 5 | +# Installs the release tarball globally via npm against the Node toolchain |
| 6 | +# provided by the `nvm` feature (required via `dependsOn`). The binary ends up |
| 7 | +# inside the nvm prefix, so we also symlink it into /usr/local/bin for a stable |
| 8 | +# path that systemd, sudo, and non-login shells can resolve without sourcing |
| 9 | +# nvm. |
| 10 | +# |
| 11 | +# Supervision: a real systemd unit if PID 1 is systemd (sysbox workspaces), a |
| 12 | +# /etc/profile.d login-shell fallback otherwise. Starting is never done here — |
| 13 | +# systemd isn't up during image build, and fallback starts happen on user |
| 14 | +# login. |
| 15 | +set -e |
| 16 | + |
| 17 | +WS_VERSION_OPT="${VERSION:-latest}" |
| 18 | +WS_PORT="${PORT:-4000}" |
| 19 | +WS_HOST="${HOST:-127.0.0.1}" |
| 20 | +WS_AUTH_TOKEN="${AUTHTOKEN:-}" |
| 21 | + |
| 22 | +# 1. OS deps: tmux for the terminal multiplexer, build-essential + python3 |
| 23 | +# because node-pty compiles native bindings, plus curl/jq for release lookup. |
| 24 | +APT_PKGS="" |
| 25 | +for pkg in tmux build-essential python3 ca-certificates curl jq; do |
| 26 | + if ! dpkg -s "$pkg" >/dev/null 2>&1; then |
| 27 | + APT_PKGS="$APT_PKGS $pkg" |
| 28 | + fi |
| 29 | +done |
| 30 | +if [ -n "$APT_PKGS" ]; then |
| 31 | + apt-get update |
| 32 | + # shellcheck disable=SC2086 |
| 33 | + apt-get install -y --no-install-recommends $APT_PKGS |
| 34 | + rm -rf /var/lib/apt/lists/* |
| 35 | +fi |
| 36 | + |
| 37 | +# 2. Activate nvm so `npm` and `npm config get prefix` resolve against the |
| 38 | +# default Node alias the nvm feature set up. |
| 39 | +export NVM_DIR="${NVM_DIR:-/usr/local/share/nvm}" |
| 40 | +if [ -s "$NVM_DIR/nvm.sh" ]; then |
| 41 | + # shellcheck disable=SC1091 |
| 42 | + . "$NVM_DIR/nvm.sh" |
| 43 | + nvm use default >/dev/null |
| 44 | +fi |
| 45 | + |
| 46 | +if ! command -v npm >/dev/null 2>&1; then |
| 47 | + echo "web-shell feature: npm not on PATH. Add ghcr.io/sourecode/devcontainer-features/nvm:2 to your features." >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# 3. Resolve target version. `latest` → newest tag via GitHub API. Otherwise |
| 52 | +# normalize `vX.Y.Z` / `X.Y.Z` → `X.Y.Z`. |
| 53 | +if [ "$WS_VERSION_OPT" = "latest" ]; then |
| 54 | + WS_VERSION="$(curl -fsSL https://api.github.com/repos/SoureCode/web-shell/releases/latest | jq -r .tag_name)" |
| 55 | +else |
| 56 | + WS_VERSION="$WS_VERSION_OPT" |
| 57 | +fi |
| 58 | +WS_VERSION="${WS_VERSION#v}" |
| 59 | +if [ -z "$WS_VERSION" ] || [ "$WS_VERSION" = "null" ]; then |
| 60 | + echo "web-shell feature: failed to resolve release version (got '$WS_VERSION_OPT')." >&2 |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +# 4. Download + global install. |
| 65 | +TMPDIR="$(mktemp -d)" |
| 66 | +trap 'rm -rf "$TMPDIR"' EXIT |
| 67 | +TARBALL_URL="https://github.com/SoureCode/web-shell/releases/download/v${WS_VERSION}/web-shell-${WS_VERSION}.tgz" |
| 68 | +curl -fsSL -o "$TMPDIR/web-shell.tgz" "$TARBALL_URL" |
| 69 | + |
| 70 | +npm install -g "$TMPDIR/web-shell.tgz" |
| 71 | + |
| 72 | +# 5. Stable symlink at /usr/local/bin/web-shell — the nvm prefix isn't on the |
| 73 | +# systemd service PATH. |
| 74 | +NPM_PREFIX="$(npm config get prefix)" |
| 75 | +WS_BIN="$NPM_PREFIX/bin/web-shell" |
| 76 | +if [ ! -x "$WS_BIN" ]; then |
| 77 | + echo "web-shell feature: $WS_BIN missing after npm install." >&2 |
| 78 | + exit 1 |
| 79 | +fi |
| 80 | +if [ "$WS_BIN" != "/usr/local/bin/web-shell" ]; then |
| 81 | + ln -sf "$WS_BIN" /usr/local/bin/web-shell |
| 82 | +fi |
| 83 | + |
| 84 | +# 6. Systemd unit. We always write it — even when systemd isn't PID 1 right |
| 85 | +# now, a later rebase onto a systemd base won't need to reinstall. |
| 86 | +install -d -m 0755 /etc/systemd/system |
| 87 | +cat >/etc/systemd/system/web-shell.service <<EOF |
| 88 | +[Unit] |
| 89 | +Description=web-shell |
| 90 | +After=network.target |
| 91 | +
|
| 92 | +[Service] |
| 93 | +Type=simple |
| 94 | +Environment=HOST=${WS_HOST} |
| 95 | +Environment=PORT=${WS_PORT} |
| 96 | +Environment=AUTH_TOKEN=${WS_AUTH_TOKEN} |
| 97 | +ExecStart=/usr/local/bin/web-shell |
| 98 | +Restart=on-failure |
| 99 | +RestartSec=1 |
| 100 | +
|
| 101 | +[Install] |
| 102 | +WantedBy=multi-user.target |
| 103 | +EOF |
| 104 | +chmod 0644 /etc/systemd/system/web-shell.service |
| 105 | + |
| 106 | +INIT_COMM="$(ps -p 1 -o comm= 2>/dev/null | tr -d '[:space:]' || true)" |
| 107 | +if [ "$INIT_COMM" = "systemd" ]; then |
| 108 | + systemctl daemon-reload |
| 109 | + systemctl enable web-shell.service |
| 110 | +else |
| 111 | + if command -v systemctl >/dev/null 2>&1; then |
| 112 | + systemctl enable web-shell.service >/dev/null 2>&1 || true |
| 113 | + fi |
| 114 | + |
| 115 | + # Login-shell fallback for non-systemd bases. pgrep guard avoids spawning |
| 116 | + # duplicate supervisors on repeat logins. The inner while-loop restarts |
| 117 | + # web-shell if it exits, mirroring `Restart=on-failure`. |
| 118 | + cat >/etc/profile.d/web-shell.sh <<EOF |
| 119 | +# web-shell feature fallback: systemd wasn't PID 1 at feature install time, |
| 120 | +# so a login-shell supervisor is used instead. |
| 121 | +if ! pgrep -u "\$(id -u)" -f '/usr/local/bin/web-shell' >/dev/null 2>&1; then |
| 122 | + HOST='${WS_HOST}' PORT='${WS_PORT}' AUTH_TOKEN='${WS_AUTH_TOKEN}' \\ |
| 123 | + nohup sh -c 'while true; do /usr/local/bin/web-shell; sleep 1; done' \\ |
| 124 | + > /tmp/web-shell.log 2>&1 & |
| 125 | + disown >/dev/null 2>&1 || true |
| 126 | +fi |
| 127 | +EOF |
| 128 | + chmod 0644 /etc/profile.d/web-shell.sh |
| 129 | + |
| 130 | + echo "web-shell feature: PID 1 is '${INIT_COMM:-unknown}' (not systemd). Installed /etc/profile.d/web-shell.sh as a login-shell fallback; use a systemd-enabled base (e.g. Coder + sysbox) for proper supervision." >&2 |
| 131 | +fi |
0 commit comments