#!/bin/bash set -euo pipefail REPO_DIR="/opt/clawd" REMOTE="origin" BRANCH="main" SERVICE="clawd.service" NO_RESTART=false for arg in "$@"; do case "$arg" in --no-restart) NO_RESTART=true ;; esac done echo "==> clawd update start" date if [ ! -d "$REPO_DIR/.git" ]; then echo "ERROR: $REPO_DIR is not a git repository" exit 1 fi cd "$REPO_DIR" # 如果 remote 仍指向 GitHub,迁移到 git.cutos.ai CURRENT_REMOTE=$(git remote get-url "$REMOTE" 2>/dev/null || echo "") if echo "$CURRENT_REMOTE" | grep -q "github.com"; then echo "==> Migrating remote from GitHub to git.cutos.ai ..." git remote set-url "$REMOTE" https://git.cutos.ai/claw-daemon/clawd.git fi echo "==> Fetching latest from $REMOTE/$BRANCH ..." git fetch "$REMOTE" LOCAL_COMMIT="$(git rev-parse HEAD)" REMOTE_COMMIT="$(git rev-parse "$REMOTE/$BRANCH")" echo "==> Local : $LOCAL_COMMIT" echo "==> Remote: $REMOTE_COMMIT" if [ "$LOCAL_COMMIT" = "$REMOTE_COMMIT" ]; then echo "==> Already up to date. Skip upgrade." exit 0 fi echo "==> Updating working tree to $REMOTE/$BRANCH ..." git reset --hard "$REMOTE/$BRANCH" git clean -fd if git diff --name-only "$LOCAL_COMMIT" "$REMOTE_COMMIT" | grep -Eq '(^|/)(package.json|package-lock.json)$'; then echo "==> Dependency files changed, running npm install ..." npm install --prefix "$REPO_DIR" else echo "==> No dependency changes, skip npm install" fi echo "==> Current commit:" git log --oneline -1 if [ "$NO_RESTART" = true ]; then echo "==> --no-restart: skip systemctl restart (caller handles restart)" exit 0 fi # Patch service file if ReadWritePaths is missing /etc/hosts or /etc/hostname SERVICE_FILE="/etc/systemd/system/$SERVICE" if [ -f "$SERVICE_FILE" ]; then if ! grep -q "/etc/hosts" "$SERVICE_FILE" || ! grep -q "/etc/hostname" "$SERVICE_FILE"; then echo "==> Patching $SERVICE_FILE: adding /etc/hosts /etc/hostname to ReadWritePaths ..." sed -i 's|ReadWritePaths=\(.*\)/tmp\(.*\)|ReadWritePaths=\1/tmp\2 /etc/hosts /etc/hostname|' "$SERVICE_FILE" # If the pattern didn't match (different format), append directly if ! grep -q "/etc/hosts" "$SERVICE_FILE"; then sed -i 's|ReadWritePaths=.*|& /etc/hosts /etc/hostname|' "$SERVICE_FILE" fi systemctl daemon-reload echo "==> daemon-reload done" fi fi echo "==> Restarting service: $SERVICE" systemctl restart "$SERVICE" echo "==> Service status:" systemctl status "$SERVICE" --no-pager -l || true echo "==> clawd update done"