Files
lightOps/scripts/update-from-git.sh

132 lines
3.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -Eeuo pipefail
REPO_DIR="/opt/lightops"
BRANCH="main"
SERVER_SERVICE="lightops-server"
AGENT_SERVICE="lightops-agent"
LOG_FILE="/var/log/lightops/update.log"
LOCK_FILE="/tmp/lightops-update.lock"
while [[ $# -gt 0 ]]; do
case "$1" in
--repo-dir)
REPO_DIR="${2:?missing --repo-dir value}"
shift 2
;;
--branch)
BRANCH="${2:?missing --branch value}"
shift 2
;;
--server-service)
SERVER_SERVICE="${2:?missing --server-service value}"
shift 2
;;
--agent-service)
AGENT_SERVICE="${2:?missing --agent-service value}"
shift 2
;;
--log-file)
LOG_FILE="${2:?missing --log-file value}"
shift 2
;;
*)
echo "未知参数:$1" >&2
exit 2
;;
esac
done
mkdir -p "$(dirname "$LOG_FILE")"
exec >>"$LOG_FILE" 2>&1
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "[$(date -Is)] 已有更新任务正在执行"
exit 1
fi
if [[ "$(id -u)" -eq 0 ]]; then
SUDO=()
else
SUDO=(sudo -n)
fi
echo "[$(date -Is)] 开始更新 LightOps"
echo "仓库目录:$REPO_DIR"
echo "目标分支:$BRANCH"
cd "$REPO_DIR"
if [[ ! -d .git ]]; then
echo "当前目录不是 Git 仓库"
exit 1
fi
if [[ -n "$(git status --porcelain)" ]]; then
echo "仓库存在未提交改动,为避免覆盖本地修改,已停止更新"
git status --short
exit 1
fi
git fetch origin "$BRANCH"
CURRENT_COMMIT="$(git rev-parse HEAD)"
REMOTE_COMMIT="$(git rev-parse "origin/$BRANCH")"
echo "当前提交:$CURRENT_COMMIT"
echo "远程提交:$REMOTE_COMMIT"
if [[ "$CURRENT_COMMIT" == "$REMOTE_COMMIT" ]]; then
echo "已经是最新版本"
exit 0
fi
git checkout "$BRANCH"
git pull --ff-only origin "$BRANCH"
echo "[$(date -Is)] 构建前端"
cd "$REPO_DIR/web"
if [[ -f package-lock.json ]]; then
npm ci
else
npm install
fi
npm run build
echo "[$(date -Is)] 构建 Rust 二进制"
cd "$REPO_DIR"
cargo build --release -p lightops-server -p lightops-agent
STAMP="$(date +%Y%m%d%H%M%S)"
BACKUP_DIR="$REPO_DIR/target/update-backups/$STAMP"
mkdir -p "$BACKUP_DIR"
backup_if_exists() {
local path="$1"
if [[ -f "$path" ]]; then
cp -a "$path" "$BACKUP_DIR/$(basename "$path")"
fi
}
backup_if_exists "/usr/local/bin/lightops-server"
backup_if_exists "/usr/local/bin/lightops-agent"
backup_if_exists "$REPO_DIR/lightops-server"
backup_if_exists "$REPO_DIR/lightops-agent"
echo "[$(date -Is)] 替换二进制"
"${SUDO[@]}" install -m 755 "$REPO_DIR/target/release/lightops-server" /usr/local/bin/lightops-server
"${SUDO[@]}" install -m 755 "$REPO_DIR/target/release/lightops-agent" /usr/local/bin/lightops-agent
# 兼容旧 service 文件ExecStart=/opt/lightops/lightops-server
install -m 755 "$REPO_DIR/target/release/lightops-server" "$REPO_DIR/lightops-server"
install -m 755 "$REPO_DIR/target/release/lightops-agent" "$REPO_DIR/lightops-agent"
echo "[$(date -Is)] 重启服务"
if systemctl list-unit-files "$AGENT_SERVICE.service" >/dev/null 2>&1; then
"${SUDO[@]}" systemctl restart "$AGENT_SERVICE" || true
fi
if systemctl list-unit-files "$SERVER_SERVICE.service" >/dev/null 2>&1; then
"${SUDO[@]}" systemctl restart "$SERVER_SERVICE"
else
echo "未找到 $SERVER_SERVICE.service请手动重启 Server"
fi
echo "[$(date -Is)] 更新完成"