Files
webterm/update.sh
T
izackp 070844d5ac
CI / check (push) Has been cancelled
Add first-time install support to update.sh
Creates systemd service file, enables linger, and starts the service on first
run instead of requiring the service file to already exist.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-20 08:41:08 -04:00

174 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
set -e
cd "$(dirname "$0")"
export PATH="$HOME/.bun/bin:/usr/local/go/bin:$HOME/go/bin:$PATH"
SERVICE_FILE="$HOME/.config/systemd/user/webterm.service"
DEFAULT_USERNAME="izackp"
DEFAULT_TTL_SECONDS="86400"
UPDATE_AUTH_PASSWORD=0
INSTALLING=0
if [ ! -f "$SERVICE_FILE" ]; then
INSTALLING=1
fi
while getopts ":p" opt; do
case "$opt" in
p)
UPDATE_AUTH_PASSWORD=1
;;
*)
echo "Usage: $0 [-p]" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ "$INSTALLING" -eq 1 ]; then
echo "No service file found — running first-time install."
UPDATE_AUTH_PASSWORD=1
fi
if [ "$UPDATE_AUTH_PASSWORD" -eq 1 ]; then
echo "Enter password for webterm login."
read -rsp "Password for $DEFAULT_USERNAME: " WEBTERM_PASSWORD
echo
if [ -z "$WEBTERM_PASSWORD" ]; then
echo "Password cannot be empty." >&2
exit 1
fi
fi
if [ "$INSTALLING" -eq 1 ]; then
mkdir -p "$(dirname "$SERVICE_FILE")"
LOG_DIR="$HOME/.webterm"
mkdir -p "$LOG_DIR"
BINARY_PATH="$HOME/go/bin/webterm"
WORK_DIR="$(pwd)"
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=webterm
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
WorkingDirectory=$WORK_DIR
Environment=HOME=$HOME
Environment=PATH=/usr/local/go/bin:$HOME/go/bin:$HOME/.bun/bin:/usr/local/bin:/usr/bin:/bin
ExecStart=$BINARY_PATH
Restart=on-failure
RestartSec=5
StandardOutput=append:$LOG_DIR/server.log
StandardError=append:$LOG_DIR/server.log
[Install]
WantedBy=default.target
EOF
echo "Created $SERVICE_FILE"
if ! loginctl show-user "$USER" 2>/dev/null | grep -q "Linger=yes"; then
echo "Enabling linger for $USER so the service starts at boot without login..."
loginctl enable-linger "$USER"
fi
fi
CURRENT_SECRET="$(sed -n 's/^Environment=WEBTERM_AUTH_COOKIE_SECRET=//p' "$SERVICE_FILE" | tail -n 1)"
if [ -z "$CURRENT_SECRET" ]; then
CURRENT_SECRET="$(openssl rand -hex 32)"
fi
if [ "$UPDATE_AUTH_PASSWORD" -eq 1 ]; then
TMP_SERVICE="$(mktemp)"
awk '
BEGIN {
skip["Environment=WEBTERM_AUTH_USERNAME"]=1
skip["Environment=WEBTERM_AUTH_PASSWORD"]=1
skip["Environment=WEBTERM_AUTH_COOKIE_SECRET"]=1
skip["Environment=WEBTERM_AUTH_SESSION_TTL_SECONDS"]=1
}
{
for (prefix in skip) {
if (index($0, prefix) == 1) next
}
print
}
' "$SERVICE_FILE" > "$TMP_SERVICE"
python3 - "$TMP_SERVICE" "$WEBTERM_PASSWORD" "$CURRENT_SECRET" "$DEFAULT_TTL_SECONDS" <<'PYEOF'
import sys
from pathlib import Path
import shlex
path = Path(sys.argv[1])
username = "izackp"
password = sys.argv[2]
secret = sys.argv[3]
ttl = sys.argv[4]
lines = path.read_text().splitlines()
insert = [
f"Environment=WEBTERM_AUTH_USERNAME={shlex.quote(username)}",
f"Environment=WEBTERM_AUTH_PASSWORD={shlex.quote(password)}",
f"Environment=WEBTERM_AUTH_COOKIE_SECRET={shlex.quote(secret)}",
f"Environment=WEBTERM_AUTH_SESSION_TTL_SECONDS={shlex.quote(ttl)}",
]
out = []
inserted = False
for line in lines:
out.append(line)
if line.strip() == "[Service]":
out.extend(insert)
inserted = True
if not inserted:
raise SystemExit("Could not find [Service] section in webterm.service")
path.write_text("\n".join(out) + "\n")
PYEOF
mv "$TMP_SERVICE" "$SERVICE_FILE"
fi
echo "Building frontend..."
make build
echo "Building Go binary..."
make build-go
echo "Installing binary..."
mkdir -p ~/go/bin
tmp_target=~/go/bin/webterm.new
cp bin/webterm "$tmp_target"
chmod +x "$tmp_target"
mv "$tmp_target" ~/go/bin/webterm
echo "Reloading user systemd config..."
systemctl --user daemon-reload
if [ "$INSTALLING" -eq 1 ]; then
echo "Enabling and starting service..."
systemctl --user enable --now webterm.service
else
echo "Restarting service..."
systemctl --user restart webterm.service
fi
echo "Done. Status:"
systemctl --user status webterm.service --no-pager
echo
echo "Listening sockets:"
ss -ltnp | grep ':8080' || true
echo
echo "Reachable URLs:"
echo " Local: http://127.0.0.1:8080/"
hostname -I 2>/dev/null | tr ' ' '\n' | sed '/^$/d' | while read -r ip; do
echo " LAN: http://$ip:8080/"
done