Files
wireguard-ui-multi/scripts/install.sh
T
sysopsandClaude Sonnet 5 6eeea65ede Fix "Text file busy" on reinstall while service is running
cp truncates the destination in place, which fails with ETXTBSY when
the binary is currently executing (e.g. running update.sh while the
systemd service is active). Copy to a temp file and mv it into place
instead - mv is a rename within the same filesystem, which the
kernel allows even for a running binary.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-10 18:23:20 +02:00

75 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Idempotent installer for wireguard-ui-multi. Expects the binary to already be
# built at ./wireguard-ui-multi (run `go build ./cmd/wireguard-ui-multi` first).
set -euo pipefail
export PATH="/usr/local/go/bin:/usr/local/bin:$PATH"
BIN_SRC="./wireguard-ui-multi"
BIN_DST="/usr/local/bin/wireguard-ui-multi"
CONFIG_DIR="/etc/wireguard-ui-multi"
DATA_DIR="/var/lib/wireguard-ui-multi"
HOOKS_DIR="/etc/wireguard-manager/hooks"
UI_SRC="internal/ui"
UI_DST="/usr/local/share/wireguard-ui-multi/ui"
SERVICE_SRC="systemd/wireguard-ui-multi.service"
SERVICE_DST="/etc/systemd/system/wireguard-ui-multi.service"
if [[ "$(id -u)" -ne 0 ]]; then
echo "This installer must be run as root (it writes to /usr/local/bin, /etc, /var/lib)." >&2
exit 1
fi
if [[ ! -f "$BIN_SRC" ]]; then
if [[ -d "./cmd/wireguard-ui-multi" ]] && command -v go >/dev/null 2>&1; then
echo "Binary not found, building from source with 'go build'..."
go mod tidy
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o "$BIN_SRC" ./cmd/wireguard-ui-multi
else
echo "Binary not found at $BIN_SRC and cannot build (need Go toolchain + source). Build it first, e.g.:" >&2
echo " go build -o wireguard-ui-multi ./cmd/wireguard-ui-multi" >&2
exit 1
fi
fi
if [[ -d /usr/local/go/bin ]] && [[ ! -f /etc/profile.d/go-path.sh ]]; then
echo "Persisting Go on PATH for future shells (/etc/profile.d/go-path.sh)..."
echo 'export PATH="/usr/local/go/bin:$PATH"' > /etc/profile.d/go-path.sh
chmod 0644 /etc/profile.d/go-path.sh
fi
echo "Creating directories..."
mkdir -p /usr/local/bin
mkdir -p "$CONFIG_DIR"
mkdir -p "$DATA_DIR"
mkdir -p "$HOOKS_DIR"
echo "Installing binary to $BIN_DST..."
cp "$BIN_SRC" "${BIN_DST}.new"
chmod 0755 "${BIN_DST}.new"
mv -f "${BIN_DST}.new" "$BIN_DST"
echo "Installing UI assets to $UI_DST..."
mkdir -p "$(dirname "$UI_DST")"
rm -rf "$UI_DST"
cp -r "$UI_SRC" "$UI_DST"
echo "Installing systemd unit to $SERVICE_DST..."
cp "$SERVICE_SRC" "$SERVICE_DST"
systemctl daemon-reload
# The database holds bcrypt password hashes; keep the directory private.
echo "Restricting permissions on $DATA_DIR (0700)..."
chmod 0700 "$DATA_DIR"
echo
echo "Installation complete."
echo "This script does NOT start the service automatically. To enable and start it, run:"
echo
echo " systemctl enable --now wireguard-ui-multi.service"
echo
echo "Then check status with:"
echo
echo " systemctl status wireguard-ui-multi.service"
echo " journalctl -u wireguard-ui-multi.service -f"