#!/bin/sh # wagent installer. # curl -fsSL https://wagent.faqihhakim.tech/install.sh | sh # # Installs Docker if missing, downloads wagent to $WAGENT_DIR, generates secrets, # starts the stack and prints (and copies) the link that opens the dashboard # already pointed at this server. Safe to re-run: it upgrades and keeps .env. # # Options (environment variables): # WAGENT_DIR install directory (default /opt/wagent) # WAGENT_PORT local port of the UI/API (default 48723, also the Tailscale HTTPS port) # WAGENT_DOMAIN public domain pointing here; enables Caddy with Let's Encrypt # WAGENT_CONVERT 1 = add the Office converter for /convert (LibreOffice, ~1 GB disk) # WAGENT_REF git branch or tag (default main; a vX.Y.Z tag pulls images X.Y.Z) # WAGENT_BUILD 1 = build images locally instead of pulling them from GHCR set -eu UI=https://wagent.faqihhakim.tech REPO=https://github.com/Fqih/wagent say() { printf '\033[1;32m==>\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m!!\033[0m %s\n' "$*" >&2; } die() { printf '\033[1;31mxx\033[0m %s\n' "$*" >&2; exit 1; } need() { command -v "$1" >/dev/null 2>&1 || die "$1 is required"; } env_get() { sed -n "s/^$1=//p" .env | tail -n 1; } env_set() { if grep -q "^$1=" .env; then sed -i "s|^$1=.*|$1=$2|" .env else printf '%s=%s\n' "$1" "$2" >> .env fi } ensure_docker() { if ! command -v docker >/dev/null 2>&1; then say "Installing Docker (get.docker.com)" curl -fsSL https://get.docker.com | $SUDO sh fi DOCKER=docker docker info >/dev/null 2>&1 || DOCKER="$SUDO docker" $DOCKER compose version >/dev/null 2>&1 || die "Docker Compose v2 plugin is missing (package docker-compose-plugin)" } fetch() { say "Downloading wagent ($REF) to $DIR" $SUDO mkdir -p "$DIR" $SUDO chown "$(id -u):$(id -g)" "$DIR" curl -fsSL "$REPO/archive/$REF.tar.gz" | tar -xz -C "$DIR" --strip-components=1 cd "$DIR" } configure() { FRESH= if [ ! -e .env ]; then sh scripts/init-env.sh >/dev/null FRESH=1 fi cors=$(env_get CORS_ORIGINS) case ",$cors," in *",$UI,"*) ;; ,,) env_set CORS_ORIGINS "$UI" ;; *) env_set CORS_ORIGINS "$cors,$UI" ;; esac [ -n "${WAGENT_PORT:-}" ] && env_set WAGENT_PORT "$WAGENT_PORT" [ -n "${WAGENT_DOMAIN:-}" ] && env_set WAGENT_DOMAIN "$WAGENT_DOMAIN" [ -n "${WAGENT_CONVERT:-}" ] && env_set WAGENT_CONVERT "$WAGENT_CONVERT" profiles= [ -n "$(env_get WAGENT_DOMAIN)" ] && profiles=https [ "$(env_get WAGENT_CONVERT)" = 1 ] && profiles="${profiles:+$profiles,}convert" env_set COMPOSE_PROFILES "$profiles" PORT=$(env_get WAGENT_PORT) PORT=${PORT:-48723} } start() { WAGENT_VERSION=$(printf '%s' "${WAGENT_REF:-}" | sed -n 's/^v\([0-9].*\)/\1/p') export WAGENT_VERSION # prebuilt images from GHCR; build locally when asked (WAGENT_BUILD=1) or when the pull fails if [ "${WAGENT_BUILD:-}" = 1 ] || ! { say "Downloading images"; $DOCKER compose pull --quiet; }; then say "Building images (first run takes a few minutes)" $DOCKER compose build --quiet fi # gateway and agent share the files volume as uid 1000 (older installs used another uid) $DOCKER compose run --rm --no-deps --user 0 --entrypoint chown agent -R 1000:1000 /data/files /data/backups $DOCKER compose up -d --remove-orphans for _ in $(seq 60); do curl -fsS "http://127.0.0.1:$PORT/health" >/dev/null 2>&1 && return sleep 2 done die "wagent did not become healthy; check: cd $DIR && $DOCKER compose logs" } # Prints the HTTPS URL the browser should use. The dashboard is served over # HTTPS, so browsers refuse plain http://IP:port; localhost is the exception. public_url() { domain=$(env_get WAGENT_DOMAIN) if [ -n "$domain" ]; then echo "https://$domain" return fi if command -v tailscale >/dev/null 2>&1 && tailscale status >/dev/null 2>&1; then host=$(tailscale status --self --peers=false --json | sed -n 's/.*"DNSName": *"\([^"]*\)\.".*/\1/p' | head -n 1) # Same rarely used port on the tailnet side, so it never collides with other tailscale serve entries on 443. if [ -n "$host" ] && $SUDO tailscale serve --bg --https="$PORT" "http://127.0.0.1:$PORT" >/dev/null 2>&1; then echo "https://$host:$PORT" return fi warn "tailscale serve failed (enable HTTPS certificates in the Tailscale admin console); falling back to an SSH tunnel" fi echo "http://localhost:$PORT" } # OSC 52: asks the terminal (also over SSH) to put the text on the clipboard. copy() { [ -w /dev/tty ] || return 0 printf '\033]52;c;%s\a' "$(printf %s "$1" | base64 | tr -d '\n')" 2>/dev/null >/dev/tty || true } report() { url=$1 link="$UI/app/#server=$url" copy "$link" echo say "wagent is running" echo echo " Open this link (copied to your clipboard if your terminal allows it):" echo printf ' \033[1m%s\033[0m\n' "$link" echo if [ -n "$FRESH" ]; then echo " Admin password: $(env_get ADMIN_PASSWORD) (change it under Account)" else echo " Admin password: unchanged" fi case $url in http://localhost*) ip=$(curl -fsS4 --max-time 5 https://api.ipify.org 2>/dev/null || hostname -I 2>/dev/null | cut -d' ' -f1) echo echo " No Tailscale or domain found, so reach the server through an SSH tunnel." echo " Keep this running on your own computer while you use the dashboard:" echo echo " ssh -N -L $PORT:127.0.0.1:$PORT $(id -un)@${ip:-YOUR_SERVER_IP}" echo echo " Or re-run with a domain: curl -fsSL $UI/install.sh | WAGENT_DOMAIN=wa.example.com sh" ;; esac echo } main() { need curl need tar SUDO= if [ "$(id -u)" -ne 0 ]; then need sudo SUDO=sudo fi DIR=${WAGENT_DIR:-/opt/wagent} REF=${WAGENT_REF:-main} ensure_docker fetch configure start report "$(public_url)" } main "$@"