Add first-time install support to update.sh
CI / check (push) Has been cancelled

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>
This commit is contained in:
2026-06-20 08:41:08 -04:00
parent ac18f65094
commit 070844d5ac
+49 -4
View File
@@ -3,14 +3,16 @@ 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
echo "Missing service file: $SERVICE_FILE" >&2
exit 1
INSTALLING=1
fi
while getopts ":p" opt; do
@@ -26,6 +28,11 @@ while getopts ":p" opt; do
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
@@ -36,6 +43,39 @@ if [ "$UPDATE_AUTH_PASSWORD" -eq 1 ]; then
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)"
@@ -110,8 +150,13 @@ mv "$tmp_target" ~/go/bin/webterm
echo "Reloading user systemd config..."
systemctl --user daemon-reload
echo "Restarting service..."
systemctl --user restart webterm.service
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