122 lines
2.6 KiB
Bash
122 lines
2.6 KiB
Bash
#!/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
|
||
|
||
NODE_BIN=$(command -v node)
|
||
INSTALL_DIR="/opt/clawd"
|
||
CONFIG_DIR="/etc/clawd"
|
||
ENV_FILE="$CONFIG_DIR/env"
|
||
SERVICE_FILE="/etc/systemd/system/$SERVICE"
|
||
|
||
echo "==> Writing service file: $SERVICE_FILE"
|
||
cat > "$SERVICE_FILE" <<EOF
|
||
[Unit]
|
||
Description=Claw Box Daemon
|
||
Documentation=https://git.cutos.ai/claw-daemon/clawd
|
||
After=NetworkManager.service
|
||
Wants=NetworkManager.service
|
||
|
||
[Service]
|
||
Type=simple
|
||
NotifyAccess=all
|
||
EnvironmentFile=$ENV_FILE
|
||
ExecStart=$NODE_BIN $INSTALL_DIR/bin/clawd.js
|
||
WorkingDirectory=$INSTALL_DIR
|
||
|
||
Restart=always
|
||
RestartSec=5
|
||
StartLimitInterval=300
|
||
StartLimitBurst=10
|
||
|
||
TimeoutStopSec=10
|
||
KillMode=mixed
|
||
KillSignal=SIGTERM
|
||
|
||
MemoryMax=256M
|
||
CPUQuota=50%
|
||
TasksMax=64
|
||
|
||
ProtectSystem=full
|
||
ReadWritePaths=$CONFIG_DIR /tmp /etc/hosts /etc/hostname
|
||
|
||
StandardOutput=journal
|
||
StandardError=journal
|
||
SyslogIdentifier=clawd
|
||
|
||
WatchdogSec=60
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
EOF
|
||
|
||
systemctl daemon-reload
|
||
echo "==> daemon-reload done"
|
||
|
||
echo "==> Restarting service: $SERVICE"
|
||
systemctl restart "$SERVICE"
|
||
|
||
echo "==> Service status:"
|
||
systemctl status "$SERVICE" --no-pager -l || true
|
||
|
||
echo "==> clawd update done" |