| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #!/usr/bin/env bash
- set -uo pipefail
- export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- export LC_ALL=C
- INTERNAL_ROOT="${ZQ_INTERNAL_WEBAPP:-/home/hxxy/zq/apps/internal/webapp}"
- REMOTE="${ZQ_EXTERNAL_SSH:-root@10.0.42.5}"
- SSH_KEY="${ZQ_SYNC_SSH_KEY:-/root/.ssh/zq_upload_sync_ed25519}"
- CONFIG_DIR="${ZQ_ANTITAMPER_CONFIG_DIR:-/etc/zq-antitamper}"
- STATE_DIR="${ZQ_ANTITAMPER_STATE_DIR:-/var/lib/zq-antitamper}"
- LOG="${ZQ_ANTITAMPER_LOG:-/var/log/zq-artifact-publisher.log}"
- STATUS="${ZQ_ANTITAMPER_STATUS:-/run/zq-artifact-publisher.status}"
- LOCK_FILE="${ZQ_ANTITAMPER_LOCK:-/run/zq-artifact-publisher.lock}"
- SYNC_INTERVAL="${ZQ_SYNC_INTERVAL:-5}"
- FULL_CHECK_INTERVAL="${ZQ_FULL_CHECK_INTERVAL:-600}"
- TRANSACTION_RETENTION_DAYS="${ZQ_TRANSACTION_RETENTION_DAYS:-14}"
- DRY_RUN="${ZQ_DRY_RUN:-0}"
- FORCE_CHECKSUM="${ZQ_FORCE_CHECKSUM:-0}"
- SSH_CMD="ssh -i ${SSH_KEY} -o BatchMode=yes -o StrictHostKeyChecking=yes -o ConnectTimeout=15 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"
- mkdir -p "${STATE_DIR}/transactions" "$(dirname "${LOG}")"
- exec 9>"${LOCK_FILE}"
- flock -n 9 || {
- printf '%s another publisher is already running\n' "$(date '+%F %T')" >> "${LOG}"
- exit 0
- }
- log_line() {
- printf '%s %s\n' "$(date '+%F %T')" "$*" >> "${LOG}"
- }
- count_itemized() {
- local pattern="$1" file="$2"
- grep -Ec "${pattern}" "${file}" 2>/dev/null || true
- }
- sync_path() {
- local label="$1" src_rel="$2" dest_rel="$3" rules="$4" checksum="$5" txn="$6"
- local out rc updated deleted newfiles backup_dir summary_file
- out="${STATE_DIR}/transactions/${txn}-${label}.log"
- summary_file="/run/zq-artifact-publisher-${label}.status"
- backup_dir=".zq-quarantine/${txn}/${label}"
- local -a args=(
- -aH --omit-dir-times --partial --delay-updates --delete-delay
- --backup --backup-dir="${backup_dir}" --itemize-changes
- --exclude=.DS_Store --exclude='._*' --exclude=.AppleDouble
- --exclude=Thumbs.db --exclude='*.tmp' --exclude='.zq-stage-*'
- --exclude='.zq-quarantine/'
- --filter="merge ${rules}"
- )
- [[ "${checksum}" == "1" ]] && args+=(--checksum)
- [[ "${DRY_RUN}" == "1" ]] && args+=(--dry-run)
- log_line "BEGIN label=${label} txn=${txn} checksum=${checksum} dry_run=${DRY_RUN}"
- rsync "${args[@]}" -e "${SSH_CMD}" \
- "${INTERNAL_ROOT}/${src_rel}" "${REMOTE}:${dest_rel}" >"${out}" 2>&1
- rc=$?
- updated=$(count_itemized '^<f' "${out}")
- deleted=$(count_itemized '^\*deleting' "${out}")
- newfiles=$(count_itemized '^<f\+' "${out}")
- if [[ "${rc}" -eq 0 ]]; then
- log_line "OK label=${label} txn=${txn} new=${newfiles} updated=${updated} deleted_or_quarantined=${deleted} log=${out}"
- else
- log_line "ERROR label=${label} txn=${txn} rc=${rc} log=${out}"
- tail -n 80 "${out}" | sed 's/^/ /' >> "${LOG}"
- fi
- printf 'label=%s txn=%s rc=%s new=%s updated=%s deleted_or_quarantined=%s checksum=%s dry_run=%s\n' \
- "${label}" "${txn}" "${rc}" "${newfiles}" "${updated}" "${deleted}" "${checksum}" "${DRY_RUN}" > "${summary_file}"
- return "${rc}"
- }
- prune_transactions() {
- find "${STATE_DIR}/transactions" -type f -mtime "+${TRANSACTION_RETENTION_DAYS}" -delete 2>/dev/null || true
- }
- run_cycle() {
- local checksum="$1"
- local txn errors=0
- txn="$(date '+%Y%m%dT%H%M%S%z')"
- sync_path upload "web/upload/" "upload/" \
- "${CONFIG_DIR}/baseline-upload.rules" "${checksum}" "${txn}" || errors=$((errors + 1))
- sync_path subassembly "web/subassembly/website/" "subassembly/website/" \
- "${CONFIG_DIR}/baseline-subassembly.rules" "${checksum}" "${txn}" || errors=$((errors + 1))
- printf 'time=%s txn=%s errors=%s checksum=%s dry_run=%s\n' \
- "$(date '+%F %T')" "${txn}" "${errors}" "${checksum}" "${DRY_RUN}" > "${STATUS}"
- if [[ "${errors}" -ne 0 ]]; then
- log_line "CYCLE_ERROR txn=${txn} errors=${errors}"
- fi
- prune_transactions
- return "${errors}"
- }
- mode="${1:-watch}"
- case "${mode}" in
- once)
- run_cycle "${FORCE_CHECKSUM}"
- ;;
- watch)
- last_full=0
- log_line "START interval=${SYNC_INTERVAL}s full_interval=${FULL_CHECK_INTERVAL}s dry_run=${DRY_RUN}"
- while true; do
- now="$(date +%s)"
- checksum=0
- if [[ "${last_full}" -eq 0 || $((now - last_full)) -ge "${FULL_CHECK_INTERVAL}" ]]; then
- checksum=1
- last_full="${now}"
- fi
- run_cycle "${checksum}"
- sleep "${SYNC_INTERVAL}"
- done
- ;;
- *)
- printf '用法:%s [watch|once]\n' "$0" >&2
- exit 2
- ;;
- esac
|