#!/bin/sh # wxch agent installer (safe to re-run; re-running upgrades the agent). # usage: curl -fsSL @@BASE@@/install.sh | sudo WXCH_TOKEN=xxxx sh # upgrade with the token already on this machine: # sudo sh -c '. /etc/wxch-agent.env; curl -fsSL @@BASE@@/install.sh | WXCH_TOKEN=$WXCH_TOKEN sh' set -e BASE="${WXCH_BASE:-@@BASE@@}" [ -n "$WXCH_TOKEN" ] || { echo "WXCH_TOKEN is required"; exit 1; } [ "$(id -u)" = 0 ] || { echo "run with sudo"; exit 1; } case "$(uname -m)" in aarch64|arm64) A=arm64 ;; x86_64|amd64) A=amd64 ;; *) echo "unsupported arch $(uname -m)"; exit 1 ;; esac RUNUSER="${SUDO_USER:-root}" DIR=/opt/wxch-agent # the agent user owns its own directory so the agent can update itself from the web UI mkdir -p "$DIR" curl -fsSL "$BASE/dl/wxch-agent-$A" -o "$DIR/wxch-agent.new" chmod 755 "$DIR/wxch-agent.new" "$DIR/wxch-agent.new" -version >/dev/null || { echo "downloaded agent does not run"; rm -f "$DIR/wxch-agent.new"; exit 1; } mv "$DIR/wxch-agent.new" "$DIR/wxch-agent" chown -R "$RUNUSER" "$DIR" rm -f /usr/local/bin/wxch-agent # old install location umask 077 echo "WXCH_TOKEN=$WXCH_TOKEN" > /etc/wxch-agent.env SERVER_URL="$(echo "$BASE" | sed 's#^https#wss#; s#^http#ws#')/ws/agent" cat > /etc/systemd/system/wxch-agent.service </dev/null 2>&1 systemctl restart wxch-agent sleep 2 systemctl is-active wxch-agent && echo "installed $("$DIR/wxch-agent" -version); agent runs as $RUNUSER"