|
| 1 | +#!/bin/sh |
| 2 | +# Installs Rack Dashboard for the person running this script. It needs no root and puts nothing |
| 3 | +# outside that person's home folder. |
| 4 | +# |
| 5 | +# curl -fsSL https://raw.githubusercontent.com/nprev-dev/Docker-UI-app/main/install.sh | sh |
| 6 | +# |
| 7 | +# To pass options, put them after "sh -s --": |
| 8 | +# |
| 9 | +# --lan let other machines on the network open the page (the default is this machine only) |
| 10 | +# --port N listen on port N (default 8787) |
| 11 | +# --no-service install the command only; do not set it up to start at boot |
| 12 | +# --uninstall remove the service and the command (settings and saved data stay) |
| 13 | +# --ref NAME install a branch, tag or commit other than main |
| 14 | +# |
| 15 | +# What it does: fetches uv (https://docs.astral.sh/uv/) if it is not there, which brings its own |
| 16 | +# Python; has uv install the dashboard from GitHub; and, on Linux with systemd, registers a service |
| 17 | +# for this user so that the dashboard starts when the machine does. |
| 18 | + |
| 19 | +set -eu |
| 20 | + |
| 21 | +REPO="https://github.com/nprev-dev/Docker-UI-app" |
| 22 | +NAME="rack-dashboard" |
| 23 | + |
| 24 | +say() { printf '%s\n' "$*"; } |
| 25 | +fail() { printf 'install.sh: %s\n' "$*" >&2; exit 1; } |
| 26 | + |
| 27 | +usage() { |
| 28 | + cat <<EOF |
| 29 | +Installs Rack Dashboard for the person running it. No root needed. |
| 30 | +
|
| 31 | + --lan let other machines on the network open the page (default: this machine only) |
| 32 | + --port N listen on port N (default 8787) |
| 33 | + --no-service install the command only; do not set it up to start at boot |
| 34 | + --uninstall remove the service and the command (settings and saved data stay) |
| 35 | + --ref NAME install a branch, tag or commit other than main |
| 36 | +EOF |
| 37 | +} |
| 38 | + |
| 39 | +# Sets a KEY=value line in the settings file, replacing an earlier one for the same key. |
| 40 | +set_setting() { |
| 41 | + key="$1"; value="$2"; file="$3" |
| 42 | + if grep -q "^$key=" "$file" 2>/dev/null; then |
| 43 | + scratch="$file.tmp" |
| 44 | + sed "s|^$key=.*|$key=$value|" "$file" > "$scratch" && mv "$scratch" "$file" |
| 45 | + else |
| 46 | + printf '%s=%s\n' "$key" "$value" >> "$file" |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +find_uv() { |
| 51 | + if command -v uv >/dev/null 2>&1; then |
| 52 | + command -v uv |
| 53 | + elif [ -x "$HOME/.local/bin/uv" ]; then |
| 54 | + printf '%s\n' "$HOME/.local/bin/uv" |
| 55 | + else |
| 56 | + return 1 |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +fetch() { |
| 61 | + if command -v curl >/dev/null 2>&1; then |
| 62 | + curl -LsSf "$1" |
| 63 | + elif command -v wget >/dev/null 2>&1; then |
| 64 | + wget -qO- "$1" |
| 65 | + else |
| 66 | + fail "need curl or wget to download things" |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +have_user_services() { |
| 71 | + [ "$(uname -s)" = "Linux" ] && command -v systemctl >/dev/null 2>&1 && systemctl --user show-environment >/dev/null 2>&1 |
| 72 | +} |
| 73 | + |
| 74 | +uninstall() { |
| 75 | + config="${XDG_CONFIG_HOME:-$HOME/.config}" |
| 76 | + unit="$config/systemd/user/$NAME.service" |
| 77 | + if have_user_services && [ -f "$unit" ]; then |
| 78 | + systemctl --user disable --now "$NAME.service" >/dev/null 2>&1 || true |
| 79 | + rm -f "$unit" |
| 80 | + systemctl --user daemon-reload || true |
| 81 | + say "Removed the service." |
| 82 | + fi |
| 83 | + # Starting at boot without anyone logged in was switched on by this script; switch it back off. |
| 84 | + if [ -f "$config/$NAME/.linger-set-by-installer" ]; then |
| 85 | + loginctl disable-linger "$(id -un)" >/dev/null 2>&1 || true |
| 86 | + rm -f "$config/$NAME/.linger-set-by-installer" |
| 87 | + fi |
| 88 | + if uv_bin="$(find_uv)"; then |
| 89 | + "$uv_bin" tool uninstall "$NAME" >/dev/null 2>&1 && say "Removed the $NAME command." || say "The $NAME command was not installed." |
| 90 | + fi |
| 91 | + say "Settings in $config/$NAME and saved data in ${XDG_DATA_HOME:-$HOME/.local/share}/$NAME were left alone." |
| 92 | +} |
| 93 | + |
| 94 | +main() { |
| 95 | + lan=0; port=""; service=1; ref="main"; source=""; remove=0 |
| 96 | + while [ $# -gt 0 ]; do |
| 97 | + case "$1" in |
| 98 | + --lan) lan=1 ;; |
| 99 | + --port) [ $# -ge 2 ] || fail "--port needs a number"; port="$2"; shift ;; |
| 100 | + --no-service) service=0 ;; |
| 101 | + --uninstall) remove=1 ;; |
| 102 | + --ref) [ $# -ge 2 ] || fail "--ref needs a name"; ref="$2"; shift ;; |
| 103 | + # For trying out a copy that is not on GitHub: a folder, a built package or an address. |
| 104 | + --source) [ $# -ge 2 ] || fail "--source needs a path or address"; source="$2"; shift ;; |
| 105 | + -h|--help) usage; exit 0 ;; |
| 106 | + *) fail "unknown option: $1 (see --help)" ;; |
| 107 | + esac |
| 108 | + shift |
| 109 | + done |
| 110 | + case "$port" in |
| 111 | + ""|*[!0-9]*) [ -z "$port" ] || fail "--port needs a number from 1 to 65535" ;; |
| 112 | + esac |
| 113 | + if [ -n "$port" ] && { [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; }; then |
| 114 | + fail "--port needs a number from 1 to 65535" |
| 115 | + fi |
| 116 | + |
| 117 | + system="$(uname -s)" |
| 118 | + case "$system" in |
| 119 | + Linux|Darwin) ;; |
| 120 | + *) fail "this script is for Linux and macOS; on Windows use install.ps1 (see the readme)" ;; |
| 121 | + esac |
| 122 | + [ -n "${HOME:-}" ] || fail "HOME is not set" |
| 123 | + |
| 124 | + if [ "$remove" -eq 1 ]; then |
| 125 | + uninstall |
| 126 | + exit 0 |
| 127 | + fi |
| 128 | + |
| 129 | + # 1. uv, which installs the dashboard into its own corner and supplies Python if need be. |
| 130 | + if ! uv_bin="$(find_uv)"; then |
| 131 | + say "Fetching uv (the installer tool) from astral.sh ..." |
| 132 | + scratch="$(mktemp)" |
| 133 | + fetch https://astral.sh/uv/install.sh > "$scratch" || { rm -f "$scratch"; fail "could not download uv from astral.sh"; } |
| 134 | + sh "$scratch" >/dev/null 2>&1 || { rm -f "$scratch"; fail "could not install uv; see https://docs.astral.sh/uv/getting-started/installation/"; } |
| 135 | + rm -f "$scratch" |
| 136 | + uv_bin="$(find_uv)" || fail "uv was installed but cannot be found; open a new terminal and run this again" |
| 137 | + fi |
| 138 | + |
| 139 | + # 2. The dashboard itself. |
| 140 | + [ -n "$source" ] || source="$NAME @ $REPO/archive/$ref.tar.gz" |
| 141 | + say "Installing $NAME ..." |
| 142 | + "$uv_bin" tool install --force --quiet "$source" || fail "could not install $NAME" |
| 143 | + bin_dir="$("$uv_bin" tool dir --bin)" |
| 144 | + command_path="$bin_dir/$NAME" |
| 145 | + [ -x "$command_path" ] || fail "the $NAME command did not appear in $bin_dir" |
| 146 | + say "Installed $("$command_path" --version) in $bin_dir" |
| 147 | + |
| 148 | + # 3. Settings: one file of KEY=value lines, read by the service. An earlier one is kept. |
| 149 | + config="${XDG_CONFIG_HOME:-$HOME/.config}" |
| 150 | + settings="$config/$NAME/env" |
| 151 | + mkdir -p "$config/$NAME" |
| 152 | + if [ ! -f "$settings" ]; then |
| 153 | + cat > "$settings" <<EOF |
| 154 | +# Settings for Rack Dashboard, one KEY=value per line. The readme lists them all. |
| 155 | +# After a change: systemctl --user restart $NAME |
| 156 | +EOF |
| 157 | + fi |
| 158 | + [ "$lan" -eq 0 ] || set_setting DASH_HOST 0.0.0.0 "$settings" |
| 159 | + [ -z "$port" ] || set_setting DASH_PORT "$port" "$settings" |
| 160 | + listening_port="$(sed -n 's/^DASH_PORT=//p' "$settings" | tail -n 1)" |
| 161 | + listening_port="${listening_port:-8787}" |
| 162 | + |
| 163 | + if [ "$service" -eq 0 ]; then |
| 164 | + say "Not set up as a service. Start it with: $command_path" |
| 165 | + say "(In a new terminal, plain \"$NAME\" works too.)" |
| 166 | + exit 0 |
| 167 | + fi |
| 168 | + if ! have_user_services; then |
| 169 | + say "This system has no systemd user services, so it was not set up to start at boot." |
| 170 | + say "Start it with: $command_path" |
| 171 | + exit 0 |
| 172 | + fi |
| 173 | + |
| 174 | + # 4. A service for this user. |
| 175 | + unit_dir="$config/systemd/user" |
| 176 | + mkdir -p "$unit_dir" |
| 177 | + cat > "$unit_dir/$NAME.service" <<EOF |
| 178 | +[Unit] |
| 179 | +Description=Rack Dashboard |
| 180 | +Documentation=$REPO |
| 181 | +
|
| 182 | +[Service] |
| 183 | +ExecStart="$command_path" |
| 184 | +EnvironmentFile=-$settings |
| 185 | +# The dashboard calls a few system tools (ss, nvidia-smi, upower); make sure they are found. |
| 186 | +Environment=PATH=$bin_dir:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
| 187 | +Restart=on-failure |
| 188 | +RestartSec=5 |
| 189 | +
|
| 190 | +[Install] |
| 191 | +WantedBy=default.target |
| 192 | +EOF |
| 193 | + systemctl --user daemon-reload |
| 194 | + systemctl --user enable "$NAME.service" >/dev/null 2>&1 || fail "could not enable the service" |
| 195 | + systemctl --user restart "$NAME.service" || fail "could not start the service; see: journalctl --user -u $NAME" |
| 196 | + |
| 197 | + # Without this a user's services only run while that user is logged in. |
| 198 | + user="$(id -un)" |
| 199 | + if [ "$(loginctl show-user "$user" -p Linger 2>/dev/null)" != "Linger=yes" ]; then |
| 200 | + if loginctl enable-linger "$user" >/dev/null 2>&1; then |
| 201 | + : > "$config/$NAME/.linger-set-by-installer" |
| 202 | + else |
| 203 | + say "Note: it will start when you log in. To have it start at boot with nobody logged in, run:" |
| 204 | + say " sudo loginctl enable-linger $user" |
| 205 | + fi |
| 206 | + fi |
| 207 | + |
| 208 | + # 5. Is it answering? |
| 209 | + tries=0 |
| 210 | + until fetch "http://127.0.0.1:$listening_port/api/snapshot" >/dev/null 2>&1; do |
| 211 | + tries=$((tries + 1)) |
| 212 | + if [ "$tries" -ge 20 ]; then |
| 213 | + if journalctl --user -u "$NAME" -n 20 --no-pager 2>/dev/null | grep -qi "address already in use"; then |
| 214 | + say "Port $listening_port is already used by another program. Choose another: run this again with --port N" |
| 215 | + else |
| 216 | + say "The service was started but is not answering. Look at: journalctl --user -u $NAME" |
| 217 | + fi |
| 218 | + exit 1 |
| 219 | + fi |
| 220 | + sleep 1 |
| 221 | + done |
| 222 | + say "" |
| 223 | + say "Rack Dashboard is running: http://127.0.0.1:$listening_port" |
| 224 | + if grep -q '^DASH_HOST=0\.0\.0\.0' "$settings"; then |
| 225 | + address="$(hostname -I 2>/dev/null | awk '{print $1}')" |
| 226 | + [ -z "$address" ] || say "From other machines: http://$address:$listening_port" |
| 227 | + else |
| 228 | + say "Only this machine can open it. Run the installer again with --lan to change that." |
| 229 | + fi |
| 230 | + say "Settings: $settings Logs: journalctl --user -u $NAME" |
| 231 | + if command -v docker >/dev/null 2>&1 && ! [ -r /var/run/docker.sock ] && ! [ -S "$HOME/.docker/desktop/docker.sock" ]; then |
| 232 | + say "Note: $user may not use Docker, so the containers section will be empty." |
| 233 | + say " The usual fix: sudo usermod -aG docker $user (then log out and back in)" |
| 234 | + fi |
| 235 | +} |
| 236 | + |
| 237 | +# Everything runs from here, so that a download cut short runs nothing at all. |
| 238 | +main "$@" |
0 commit comments