From 0e28bb6aca467d69cd893a1440a5640594f83c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Thu, 7 May 2026 02:56:11 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82=20=D0=B0=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D0=BC=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/auto_update_check.sh | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 scripts/auto_update_check.sh diff --git a/scripts/auto_update_check.sh b/scripts/auto_update_check.sh new file mode 100644 index 0000000..353e2e1 --- /dev/null +++ b/scripts/auto_update_check.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +set -u + +SOURCE_DIR="${VIRTUALITY_SOURCE_DIR:-/opt/virtuality/source}" +STATE_DIR="/var/lib/virtuality/update" +STATE_FILE="${STATE_DIR}/state.json" +LOCK_FILE="${STATE_DIR}/auto_update.lock" +LOG_FILE="/var/log/virtuality/update.log" +REMOTE="${VIRTUALITY_UPDATE_REMOTE:-origin}" +BRANCH="${VIRTUALITY_UPDATE_BRANCH:-main}" +APPLY_SCRIPT="${SOURCE_DIR}/scripts/apply_github_update.sh" + +mkdir -p "$STATE_DIR" "$(dirname "$LOG_FILE")" + +now() { date '+%Y-%m-%d %H:%M:%S'; } +json_escape() { python3 -c 'import json,sys; print(json.dumps(sys.stdin.read())[1:-1])'; } +log() { echo "[$(now)] [auto] $*" | tee -a "$LOG_FILE"; } +write_state() { + local status="$1" + local message="$2" + local escaped + escaped="$(printf '%s' "$message" | json_escape)" + cat > "$STATE_FILE" <> "$LOG_FILE" 2>&1; then + log "git fetch failed" + write_state "error" "Автообновление: git fetch завершился ошибкой" + exit 0 + fi + + latest="$(git rev-parse "${REMOTE}/${BRANCH}" 2>/dev/null || true)" + if [ -z "$latest" ]; then + log "cannot read remote commit ${REMOTE}/${BRANCH}" + write_state "error" "Автообновление: не удалось прочитать удалённый commit ${REMOTE}/${BRANCH}" + exit 0 + fi + + if [ "$current" = "$latest" ]; then + log "no updates: ${current:0:12}" + write_state "idle" "Автообновление: новых обновлений нет" + exit 0 + fi + + if [ ! -f "$APPLY_SCRIPT" ]; then + log "apply script not found: $APPLY_SCRIPT" + write_state "error" "Автообновление: не найден скрипт установки обновления" + exit 0 + fi + + log "update found: ${current:0:12} -> ${latest:0:12}" + write_state "running" "Автообновление: найдено обновление, запускаем установку" + VIRTUALITY_SOURCE_DIR="$SOURCE_DIR" VIRTUALITY_UPDATE_REMOTE="$REMOTE" VIRTUALITY_UPDATE_BRANCH="$BRANCH" bash "$APPLY_SCRIPT" +) 9>"$LOCK_FILE"