[5129] | 1 | #!/bin/bash |
---|
| 2 | #------------------------------------------------------------------------------- |
---|
| 3 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
| 4 | # |
---|
| 5 | # This file is part of FCM, tools for managing and building source code. |
---|
| 6 | # |
---|
| 7 | # FCM is free software: you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # FCM is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | #------------------------------------------------------------------------------- |
---|
| 20 | # NAME |
---|
| 21 | # post-commit-bg |
---|
| 22 | # |
---|
| 23 | # SYNOPSIS |
---|
| 24 | # post-commit-bg REPOS REV |
---|
| 25 | # |
---|
| 26 | # ARGUMENTS |
---|
| 27 | # REPOS - the path to the Subversion repository |
---|
| 28 | # REV - the revision of the commit |
---|
| 29 | # TXN - the commit transaction that becomes the revision |
---|
| 30 | # |
---|
| 31 | # DESCRIPTION |
---|
| 32 | # This script performs the post-commit tasks of a Subversion repository in |
---|
| 33 | # the background. |
---|
| 34 | # |
---|
| 35 | # The script does the following: |
---|
| 36 | # 1. Creates an incremental revision dump of the current revision. |
---|
| 37 | # 2. Update corresponding Trac environment, if relevant. |
---|
| 38 | # 3. Size check. Warn if transaction exceeds 2MB, or the number of |
---|
| 39 | # MB specified in "$REPOS/hooks/post-commit-size-threshold.conf". |
---|
| 40 | # 4. If this changeset has a change to "^/svnperms.conf", install its HEAD |
---|
| 41 | # revision at "$REPOS/hooks/", or remove it from "$REPOS/hooks/" if it is |
---|
| 42 | # removed from the HEAD. |
---|
| 43 | # 5. Runs "$REPOS/hooks/post-commit-bg-custom" and/or |
---|
| 44 | # "$REPOS/hooks/post-commit-background-custom", if available. |
---|
| 45 | # 6. E-mails the host user account on error. |
---|
| 46 | # |
---|
| 47 | # ENVIRONMENT VARIABLES |
---|
| 48 | # FCM_SITE_HOME |
---|
| 49 | # The root location of site configuration. |
---|
| 50 | # FCM_SVN_HOOK_COMMIT_DUMP_DIR |
---|
| 51 | # The path to dump commit deltas. Generate a commit delta if specified. |
---|
| 52 | # FCM_SVN_HOOK_TRAC_ROOT_DIR |
---|
| 53 | # The root directories of Trac environments. Update corresponding Trac |
---|
| 54 | # environment if specified. |
---|
| 55 | # FCM_SVN_HOOK_REPOS_SUFFIX |
---|
| 56 | # A suffix that should be removed from the basename of REPOS to get the |
---|
| 57 | # name of the Trac environment. (Default is "".) |
---|
| 58 | # |
---|
| 59 | # FILES |
---|
| 60 | # $REPOS/hooks/post-commit-bg-custom |
---|
| 61 | # $REPOS/hooks/post-commit-background-custom |
---|
| 62 | #------------------------------------------------------------------------------- |
---|
| 63 | set -eu |
---|
| 64 | . "$(dirname $0)/trac_hook" |
---|
| 65 | |
---|
| 66 | REPOS=$1 |
---|
| 67 | REV=$2 |
---|
| 68 | TXN=$3 |
---|
| 69 | |
---|
| 70 | export PATH=${PATH:-'/usr/local/bin:/bin:/usr/bin'}:$(dirname $0) |
---|
| 71 | THIS=$(basename $0) |
---|
| 72 | USER=${USER:-$(whoami)} |
---|
| 73 | LOG_REV="$REPOS/log/$THIS-$REV.log" |
---|
| 74 | |
---|
| 75 | main() { |
---|
| 76 | local RET_CODE=0 |
---|
| 77 | local NOW=$(date -u +%FT%H:%M:%SZ) |
---|
| 78 | local AUTHOR=$(svnlook author -r "$REV" "$REPOS") |
---|
| 79 | echo "$NOW+ $REV by $AUTHOR" |
---|
| 80 | |
---|
| 81 | # Dump revision delta |
---|
| 82 | if [[ -n ${FCM_SVN_HOOK_COMMIT_DUMP_DIR:-} ]]; then |
---|
| 83 | if [[ ! -d "$FCM_SVN_HOOK_COMMIT_DUMP_DIR" ]]; then |
---|
| 84 | mkdir -p "$FCM_SVN_HOOK_COMMIT_DUMP_DIR" || true |
---|
| 85 | fi |
---|
| 86 | local NAME=$(basename "$REPOS") |
---|
| 87 | local DUMP="$FCM_SVN_HOOK_COMMIT_DUMP_DIR/$NAME-$REV.gz" |
---|
| 88 | local TMP_DUMP="$FCM_SVN_HOOK_COMMIT_DUMP_DIR/$NAME-$REV-tmp.gz" |
---|
| 89 | echo "svnadmin dump -r$REV --incremental --deltas $REPOS | gzip \\" |
---|
| 90 | echo " | (dd 'conv=fsync' \"of=$TMP_DUMP\" 2>/dev/null)" |
---|
| 91 | svnadmin dump "-r$REV" --incremental --deltas "$REPOS" | gzip \ |
---|
| 92 | | (dd 'conv=fsync' "of=$TMP_DUMP" 2>/dev/null) || RET_CODE=$? |
---|
| 93 | if [[ -s "${TMP_DUMP}" ]]; then |
---|
| 94 | echo "mv \"${TMP_DUMP}\" \"${DUMP}\"" |
---|
| 95 | mv "${TMP_DUMP}" "${DUMP}" || RET_CODE=$? |
---|
| 96 | else |
---|
| 97 | echo "[WARN] ${NAME}-${REV}: zero dump size" >&2 |
---|
| 98 | rm -f "${TMP_DUMP}" |
---|
| 99 | fi |
---|
| 100 | fi |
---|
| 101 | |
---|
| 102 | # Resync Trac |
---|
| 103 | trac_hook "$REPOS" "$REV" added || RET_CODE=$? |
---|
| 104 | |
---|
| 105 | # Check size - send warning email if threshold exceeded |
---|
| 106 | local MB=1048576 |
---|
| 107 | local THRESHOLD=2 |
---|
| 108 | local SIZE_THRESHOLD_FILE="${REPOS}/hooks/post-commit-size-threshold.conf" |
---|
| 109 | if [[ -f "${SIZE_THRESHOLD_FILE}" && -r "${SIZE_THRESHOLD_FILE}" ]]; then |
---|
| 110 | THRESHOLD=$(<"${SIZE_THRESHOLD_FILE}") |
---|
| 111 | fi |
---|
| 112 | local REV_FILE="${REPOS}/db/revs/$((${REV} / 1000))/${REV}" |
---|
| 113 | local REV_FILE_SIZE=$(du -b -s "${REV_FILE}" | cut -f 1) |
---|
| 114 | if ((${REV_FILE_SIZE} > ${THRESHOLD} * ${MB})); then |
---|
| 115 | echo "REV_FILE_SIZE=${REV_FILE_SIZE} # >${THRESHOLD}MB" |
---|
| 116 | RET_CODE=1 |
---|
| 117 | else |
---|
| 118 | echo "REV_FILE_SIZE=${REV_FILE_SIZE} # <${THRESHOLD}MB" |
---|
| 119 | fi |
---|
| 120 | |
---|
| 121 | # Install commit.conf and svnperms.conf, if necessary |
---|
| 122 | local NAME=$(basename "${REPOS}") |
---|
| 123 | if [[ -n "${FCM_SVN_HOOK_REPOS_SUFFIX:-}" ]]; then |
---|
| 124 | NAME="${NAME%${FCM_SVN_HOOK_REPOS_SUFFIX}}" |
---|
| 125 | fi |
---|
| 126 | local CHANGED=$(svnlook changed -r "${REV}" "${REPOS}") |
---|
| 127 | local FILE= |
---|
| 128 | for FILE in 'commit.conf' 'svnperms.conf'; do |
---|
| 129 | # Ignore if there is a site override |
---|
| 130 | if [[ -n "${FCM_SITE_HOME:-}" \ |
---|
| 131 | && -e "${FCM_SITE_HOME:-}/svn-hooks/${NAME}/${FILE}" ]] |
---|
| 132 | then |
---|
| 133 | continue |
---|
| 134 | fi |
---|
| 135 | if grep -q "^....${FILE}\$" <<<"${CHANGED}"; then |
---|
| 136 | # Don't specify revision, so always look at latest. |
---|
| 137 | if svnlook filesize "${REPOS}" "${FILE}" >/dev/null 2>&1; then |
---|
| 138 | echo "svnlook cat ${REPOS} ${FILE} >${REPOS}/hooks/${FILE}" |
---|
| 139 | svnlook cat "${REPOS}" "${FILE}" >"${REPOS}/hooks/${FILE}" |
---|
| 140 | else |
---|
| 141 | echo "rm -f ${REPOS}/hooks/${FILE}" |
---|
| 142 | rm -f "${REPOS}/hooks/${FILE}" |
---|
| 143 | fi |
---|
| 144 | fi |
---|
| 145 | done |
---|
| 146 | |
---|
| 147 | # If relevant, notify owners |
---|
| 148 | local COMMIT_CONFIG="${REPOS}/hooks/commit.conf" |
---|
| 149 | if grep -q 'notify-owner' "$COMMIT_CONFIG" 2>/dev/null; then |
---|
| 150 | local ADDRS=$('post-commit-bg-notify-who' "$REPOS" "$REV" "$TXN") |
---|
| 151 | if [[ -n $ADDRS ]]; then |
---|
| 152 | SUBJECT="-s$(basename $REPOS)@$REV by $AUTHOR" |
---|
| 153 | FROM= |
---|
| 154 | if [[ -n ${FCM_SVN_HOOK_NOTIFICATION_FROM:-} ]]; then |
---|
| 155 | FROM="-r${FCM_SVN_HOOK_NOTIFICATION_FROM:-}" |
---|
| 156 | fi |
---|
| 157 | echo -n "svn log -v -r \"$REV\" \"file://$REPOS\"" |
---|
| 158 | echo " | mail \"$FROM\" \"$SUBJECT\" \"$ADDRS\"" |
---|
| 159 | svn log -v -r "$REV" "file://$REPOS" \ |
---|
| 160 | | mail "$FROM" "$SUBJECT" "$ADDRS" |
---|
| 161 | fi |
---|
| 162 | fi |
---|
| 163 | |
---|
| 164 | # Custom hook |
---|
| 165 | local CUSTOM_HOOK= |
---|
| 166 | for CUSTOM_HOOK in \ |
---|
| 167 | "$REPOS/hooks/$THIS-custom" \ |
---|
| 168 | "$REPOS/hooks/post-commit-background-custom" |
---|
| 169 | do |
---|
| 170 | if [[ -x "$CUSTOM_HOOK" ]]; then |
---|
| 171 | echo "$CUSTOM_HOOK $REPOS $REV $TXN" |
---|
| 172 | "$CUSTOM_HOOK" "$REPOS" "$REV" "$TXN" || RET_CODE=$? |
---|
| 173 | fi |
---|
| 174 | done |
---|
| 175 | |
---|
| 176 | echo "RET_CODE=$RET_CODE" |
---|
| 177 | return $RET_CODE |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if ! main 1>$LOG_REV 2>&1; then |
---|
| 181 | if [[ -n ${FCM_SVN_HOOK_ADMIN_EMAIL:-} ]]; then |
---|
| 182 | FROM= |
---|
| 183 | if [[ -n ${FCM_SVN_HOOK_NOTIFICATION_FROM:-} ]]; then |
---|
| 184 | FROM="-r${FCM_SVN_HOOK_NOTIFICATION_FROM:-}" |
---|
| 185 | fi |
---|
| 186 | mail "$FROM" -s "[$THIS] $REPOS@$REV" \ |
---|
| 187 | "$FCM_SVN_HOOK_ADMIN_EMAIL" <"$LOG_REV" || true |
---|
| 188 | fi |
---|
| 189 | fi |
---|
| 190 | cat "$LOG_REV" |
---|
| 191 | rm -f "$LOG_REV" |
---|
| 192 | exit |
---|