zq-artifact-publisher.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env bash
  2. set -uo pipefail
  3. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  4. export LC_ALL=C
  5. INTERNAL_ROOT="${ZQ_INTERNAL_WEBAPP:-/home/hxxy/zq/apps/internal/webapp}"
  6. REMOTE="${ZQ_EXTERNAL_SSH:-root@10.0.42.5}"
  7. SSH_KEY="${ZQ_SYNC_SSH_KEY:-/root/.ssh/zq_upload_sync_ed25519}"
  8. CONFIG_DIR="${ZQ_ANTITAMPER_CONFIG_DIR:-/etc/zq-antitamper}"
  9. STATE_DIR="${ZQ_ANTITAMPER_STATE_DIR:-/var/lib/zq-antitamper}"
  10. LOG="${ZQ_ANTITAMPER_LOG:-/var/log/zq-artifact-publisher.log}"
  11. STATUS="${ZQ_ANTITAMPER_STATUS:-/run/zq-artifact-publisher.status}"
  12. LOCK_FILE="${ZQ_ANTITAMPER_LOCK:-/run/zq-artifact-publisher.lock}"
  13. SYNC_INTERVAL="${ZQ_SYNC_INTERVAL:-5}"
  14. FULL_CHECK_INTERVAL="${ZQ_FULL_CHECK_INTERVAL:-600}"
  15. TRANSACTION_RETENTION_DAYS="${ZQ_TRANSACTION_RETENTION_DAYS:-14}"
  16. DRY_RUN="${ZQ_DRY_RUN:-0}"
  17. FORCE_CHECKSUM="${ZQ_FORCE_CHECKSUM:-0}"
  18. SSH_CMD="ssh -i ${SSH_KEY} -o BatchMode=yes -o StrictHostKeyChecking=yes -o ConnectTimeout=15 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"
  19. mkdir -p "${STATE_DIR}/transactions" "$(dirname "${LOG}")"
  20. exec 9>"${LOCK_FILE}"
  21. flock -n 9 || {
  22. printf '%s another publisher is already running\n' "$(date '+%F %T')" >> "${LOG}"
  23. exit 0
  24. }
  25. log_line() {
  26. printf '%s %s\n' "$(date '+%F %T')" "$*" >> "${LOG}"
  27. }
  28. count_itemized() {
  29. local pattern="$1" file="$2"
  30. grep -Ec "${pattern}" "${file}" 2>/dev/null || true
  31. }
  32. sync_path() {
  33. local label="$1" src_rel="$2" dest_rel="$3" rules="$4" checksum="$5" txn="$6"
  34. local out rc updated deleted newfiles backup_dir summary_file
  35. out="${STATE_DIR}/transactions/${txn}-${label}.log"
  36. summary_file="/run/zq-artifact-publisher-${label}.status"
  37. backup_dir=".zq-quarantine/${txn}/${label}"
  38. local -a args=(
  39. -aH --omit-dir-times --partial --delay-updates --delete-delay
  40. --backup --backup-dir="${backup_dir}" --itemize-changes
  41. --exclude=.DS_Store --exclude='._*' --exclude=.AppleDouble
  42. --exclude=Thumbs.db --exclude='*.tmp' --exclude='.zq-stage-*'
  43. --exclude='.zq-quarantine/'
  44. --filter="merge ${rules}"
  45. )
  46. [[ "${checksum}" == "1" ]] && args+=(--checksum)
  47. [[ "${DRY_RUN}" == "1" ]] && args+=(--dry-run)
  48. log_line "BEGIN label=${label} txn=${txn} checksum=${checksum} dry_run=${DRY_RUN}"
  49. rsync "${args[@]}" -e "${SSH_CMD}" \
  50. "${INTERNAL_ROOT}/${src_rel}" "${REMOTE}:${dest_rel}" >"${out}" 2>&1
  51. rc=$?
  52. updated=$(count_itemized '^<f' "${out}")
  53. deleted=$(count_itemized '^\*deleting' "${out}")
  54. newfiles=$(count_itemized '^<f\+' "${out}")
  55. if [[ "${rc}" -eq 0 ]]; then
  56. log_line "OK label=${label} txn=${txn} new=${newfiles} updated=${updated} deleted_or_quarantined=${deleted} log=${out}"
  57. else
  58. log_line "ERROR label=${label} txn=${txn} rc=${rc} log=${out}"
  59. tail -n 80 "${out}" | sed 's/^/ /' >> "${LOG}"
  60. fi
  61. printf 'label=%s txn=%s rc=%s new=%s updated=%s deleted_or_quarantined=%s checksum=%s dry_run=%s\n' \
  62. "${label}" "${txn}" "${rc}" "${newfiles}" "${updated}" "${deleted}" "${checksum}" "${DRY_RUN}" > "${summary_file}"
  63. return "${rc}"
  64. }
  65. prune_transactions() {
  66. find "${STATE_DIR}/transactions" -type f -mtime "+${TRANSACTION_RETENTION_DAYS}" -delete 2>/dev/null || true
  67. }
  68. run_cycle() {
  69. local checksum="$1"
  70. local txn errors=0
  71. txn="$(date '+%Y%m%dT%H%M%S%z')"
  72. sync_path upload "web/upload/" "upload/" \
  73. "${CONFIG_DIR}/baseline-upload.rules" "${checksum}" "${txn}" || errors=$((errors + 1))
  74. sync_path subassembly "web/subassembly/website/" "subassembly/website/" \
  75. "${CONFIG_DIR}/baseline-subassembly.rules" "${checksum}" "${txn}" || errors=$((errors + 1))
  76. printf 'time=%s txn=%s errors=%s checksum=%s dry_run=%s\n' \
  77. "$(date '+%F %T')" "${txn}" "${errors}" "${checksum}" "${DRY_RUN}" > "${STATUS}"
  78. if [[ "${errors}" -ne 0 ]]; then
  79. log_line "CYCLE_ERROR txn=${txn} errors=${errors}"
  80. fi
  81. prune_transactions
  82. return "${errors}"
  83. }
  84. mode="${1:-watch}"
  85. case "${mode}" in
  86. once)
  87. run_cycle "${FORCE_CHECKSUM}"
  88. ;;
  89. watch)
  90. last_full=0
  91. log_line "START interval=${SYNC_INTERVAL}s full_interval=${FULL_CHECK_INTERVAL}s dry_run=${DRY_RUN}"
  92. while true; do
  93. now="$(date +%s)"
  94. checksum=0
  95. if [[ "${last_full}" -eq 0 || $((now - last_full)) -ge "${FULL_CHECK_INTERVAL}" ]]; then
  96. checksum=1
  97. last_full="${now}"
  98. fi
  99. run_cycle "${checksum}"
  100. sleep "${SYNC_INTERVAL}"
  101. done
  102. ;;
  103. *)
  104. printf '用法:%s [watch|once]\n' "$0" >&2
  105. exit 2
  106. ;;
  107. esac