gitpages-update.sh (2963B)
1 #!/bin/sh 2 # 3 # Copyright (c) 2026 Fred Großkopf 4 # 5 # Permission to use, copy, modify, and/or distribute this software for any 6 # purpose with or without fee is hereby granted, provided that the above 7 # copyright notice and this permission notice appear in all copies. 8 # 9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17 set -eu 18 PATH=/usr/local/bin:/usr/bin:/bin:$PATH 19 20 # --- Configuration --- 21 LOCK_DIR="${GITPAGES_LOCK_DIR:-/tmp/gitpages-update.lock}" 22 QUEUE_DIR="${GITPAGES_QUEUE_DIR:-/var/spool/gitpages/queue}" 23 LOG_FILE="${GITPAGES_LOG_FILE:-/var/log/gitpages/update.log}" 24 DEFAULT_CONFIG="/etc/gitpages.conf" 25 26 # --- Functions --- 27 error() { printf "[%s] ERROR: %s\n" "$(date)" "$1" >&2; } 28 log() { printf "[%s] %s\n" "$(date)" "$1"; } 29 30 get_config_val() { 31 [ -f "$1" ] && awk -F '=' -v key="$2" '$1 == key {gsub(/[[:space:]]/, "", $2); print $2}' "$1" | tail -n 1 32 } 33 34 process_repo() { 35 repo_path=$1 36 config_file=$2 37 38 repo_name=$(basename "$repo_path") 39 WEB_ROOT=$(get_config_val "$config_file" "WEB_ROOT") 40 GIT_RAW=$(get_config_val "$config_file" "GIT_RAW") 41 42 if ! gitpages-mirror-git.sh "$repo_path" "$WEB_ROOT/$GIT_RAW"; then 43 error "Failed to mirror $repo_name" 44 return 1 45 fi 46 47 # Use explicit arguments to avoid word splitting/shellcheck issues 48 if ! gitpages.sh -c "$config_file" "$WEB_ROOT/$GIT_RAW/$repo_name"; then 49 error "Failed to generate pages for $repo_name" 50 return 1 51 fi 52 } 53 54 main() { 55 processed=0 56 CONFIG_FILE="$DEFAULT_CONFIG" 57 58 if [ "${1:-}" = "-c" ]; then 59 [ $# -lt 2 ] && { 60 echo "Usage: $0 [-c config]" 61 exit 1 62 } 63 CONFIG_FILE="$2" 64 shift 2 65 fi 66 67 if ! mkdir "$LOCK_DIR" 2> /dev/null; then 68 log "Update already in progress. Exiting." 69 exit 0 70 fi 71 trap 'rmdir "$LOCK_DIR"' EXIT 72 73 if [ ! -d "$QUEUE_DIR" ]; then 74 log "Queue directory not found: $QUEUE_DIR" 75 return 0 76 fi 77 78 for jobfile in "$QUEUE_DIR"/*; do 79 [ -f "$jobfile" ] || continue 80 81 repo_path=$(cat "$jobfile") 82 83 if [ -d "$repo_path" ]; then 84 log "Starting update for: $repo_path (job: $jobfile)" 85 if process_repo "$repo_path" "$CONFIG_FILE"; then 86 processed=$((processed + 1)) 87 rm -f "$jobfile" 88 else 89 error "Failed to process $repo_path (job: $jobfile)" 90 fi 91 else 92 error "Repository directory $repo_path not found (job: $jobfile)" 93 fi 94 done 95 96 if [ "$processed" -gt 0 ]; then 97 log "Update completed." 98 fi 99 100 } 101 102 mkdir -p "$(dirname "$LOG_FILE")" 103 touch "$LOG_FILE" 2> /dev/null 104 exec >> "$LOG_FILE" 2>&1 105 main "$@"