source: vendors/FCM-2017.10.0/sbin/post-commit-bg @ 10672

Last change on this file since 10672 was 10672, checked in by nicolasmartin, 3 years ago

Reimport latest FCM release

File size: 6.9 KB
Line 
1#!/bin/bash
2#-------------------------------------------------------------------------------
3# (C) British Crown Copyright 2006-17 Met Office.
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. Checks the size of the revision dump. Warns if it exceeds a threshold.
39#   4. If this changeset has a change to "^/svnperms.conf", install its HEAD
40#      revision at "$REPOS/hooks/", or remove it from "$REPOS/hooks/" if it is
41#      removed from the HEAD.
42#   5. Runs "$REPOS/hooks/post-commit-bg-custom" and/or
43#      "$REPOS/hooks/post-commit-background-custom", if available.
44#   6. E-mails the host user account on error.
45#
46# ENVIRONMENT VARIABLES
47#   FCM_SITE_HOME
48#     The root location of site configuration.
49#   FCM_SVN_HOOK_COMMIT_DUMP_DIR
50#     The path to dump commit deltas. Generate a commit delta if specified.
51#   FCM_SVN_HOOK_TRAC_ROOT_DIR
52#     The root directories of Trac environments. Update corresponding Trac
53#     environment if specified.
54#   FCM_SVN_HOOK_REPOS_SUFFIX
55#     A suffix that should be removed from the basename of REPOS to get the
56#     name of the Trac environment. (Default is "".)
57#
58# FILES
59#   $REPOS/hooks/post-commit-bg-custom
60#   $REPOS/hooks/post-commit-background-custom
61#-------------------------------------------------------------------------------
62set -eu
63. "$(dirname $0)/trac_hook"
64
65REPOS=$1
66REV=$2
67TXN=$3
68
69export PATH=${PATH:-'/usr/local/bin:/bin:/usr/bin'}:$(dirname $0)
70THIS=$(basename $0)
71USER=${USER:-$(whoami)}
72LOG_REV="$REPOS/log/$THIS-$REV.log"
73
74main() {
75    local RET_CODE=0
76    local NOW=$(date -u +%FT%H:%M:%SZ)
77    local AUTHOR=$(svnlook author -r "$REV" "$REPOS")
78    echo "$NOW+ $REV by $AUTHOR"
79
80    # Dump revision delta
81    if [[ -n ${FCM_SVN_HOOK_COMMIT_DUMP_DIR:-} ]]; then
82        if [[ ! -d "$FCM_SVN_HOOK_COMMIT_DUMP_DIR" ]]; then
83            mkdir -p "$FCM_SVN_HOOK_COMMIT_DUMP_DIR" || true
84        fi
85        local NAME=$(basename "$REPOS")
86        local DUMP="$FCM_SVN_HOOK_COMMIT_DUMP_DIR/$NAME-$REV.gz"
87        local TMP_DUMP="$FCM_SVN_HOOK_COMMIT_DUMP_DIR/$NAME-$REV-tmp.gz"
88        echo "svnadmin dump -r$REV --incremental --deltas $REPOS | gzip \\"
89        echo "    | (dd 'conv=fsync' \"of=$TMP_DUMP\" 2>/dev/null)"
90        svnadmin dump "-r$REV" --incremental --deltas "$REPOS" | gzip \
91            | (dd 'conv=fsync' "of=$TMP_DUMP" 2>/dev/null) || RET_CODE=$?
92        if [[ -s "${TMP_DUMP}" ]]; then
93            echo "mv \"${TMP_DUMP}\" \"${DUMP}\""
94            mv "${TMP_DUMP}" "${DUMP}" || RET_CODE=$?
95        else
96            echo "[WARN] ${NAME}-${REV}: zero dump size" >&2
97            rm -f "${TMP_DUMP}"
98        fi
99    fi
100
101    # Resync Trac
102    trac_hook "$REPOS" "$REV" added || RET_CODE=$?
103
104    # Check size - send warning email if threshold exceeded
105    local MB=1048576
106    local THRESHOLD=10
107    local SIZE_THRESHOLD_FILE="${REPOS}/hooks/pre-commit-size-threshold.conf"
108    if [[ -f "${SIZE_THRESHOLD_FILE}" && -r "${SIZE_THRESHOLD_FILE}" ]]; then
109        THRESHOLD=$(<"${SIZE_THRESHOLD_FILE}")
110    fi
111    local REV_FILE="${REPOS}/db/revs/$((${REV} / 1000))/${REV}"
112    local REV_FILE_SIZE=$(du -b -s "${REV_FILE}" | cut -f 1)
113    if ((${REV_FILE_SIZE} > ${THRESHOLD} * ${MB})); then
114        echo "REV_FILE_SIZE=${REV_FILE_SIZE} # >${THRESHOLD}MB"
115        RET_CODE=1
116    elif ((${REV_FILE_SIZE} > ${MB})); then
117        echo "REV_FILE_SIZE=${REV_FILE_SIZE} # >1MB <${THRESHOLD}MB"
118        RET_CODE=1
119    else
120        echo "REV_FILE_SIZE=${REV_FILE_SIZE} # <1MB"
121    fi
122
123    # Install commit.conf and svnperms.conf, if necessary
124    local NAME=$(basename "${REPOS}")
125    if [[ -n "${FCM_SVN_HOOK_REPOS_SUFFIX:-}" ]]; then
126        NAME="${NAME%${FCM_SVN_HOOK_REPOS_SUFFIX}}"
127    fi
128    local CHANGED=$(svnlook changed -r "${REV}" "${REPOS}")
129    local FILE=
130    for FILE in 'commit.conf' 'svnperms.conf'; do
131        # Ignore if there is a site override
132        if [[ -n "${FCM_SITE_HOME:-}" \
133            && -e "${FCM_SITE_HOME:-}/svn-hooks/${NAME}/${FILE}" ]]
134        then
135            continue
136        fi
137        if grep -q "^....${FILE}\$" <<<"${CHANGED}"; then
138            # Don't specify revision, so always look at latest.
139            if svnlook filesize "${REPOS}" "${FILE}" >/dev/null 2>&1; then
140                echo "svnlook cat ${REPOS} ${FILE} >${REPOS}/hooks/${FILE}"
141                svnlook cat "${REPOS}" "${FILE}" >"${REPOS}/hooks/${FILE}"
142            else
143                echo "rm -f ${REPOS}/hooks/${FILE}"
144                rm -f "${REPOS}/hooks/${FILE}"
145            fi
146        fi
147    done
148
149    # If relevant, notify owners
150    local COMMIT_CONFIG="${REPOS}/hooks/commit.conf"
151    if grep -q 'notify-owner' "$COMMIT_CONFIG" 2>/dev/null; then
152        local ADDRS=$('post-commit-bg-notify-who' "$REPOS" "$REV" "$TXN")
153        if [[ -n $ADDRS ]]; then
154            SUBJECT="-s$(basename $REPOS)@$REV by $AUTHOR"
155            FROM=
156            if [[ -n ${FCM_SVN_HOOK_NOTIFICATION_FROM:-} ]]; then
157                FROM="-r${FCM_SVN_HOOK_NOTIFICATION_FROM:-}"
158            fi
159            echo -n "svn log -v -r \"$REV\" \"file://$REPOS\""
160            echo " | mail \"$FROM\" \"$SUBJECT\" \"$ADDRS\""
161            svn log -v -r "$REV" "file://$REPOS" \
162                | mail "$FROM" "$SUBJECT" "$ADDRS"
163        fi
164    fi
165
166    # Custom hook
167    local CUSTOM_HOOK=
168    for CUSTOM_HOOK in \
169        "$REPOS/hooks/$THIS-custom" \
170        "$REPOS/hooks/post-commit-background-custom"
171    do
172        if [[ -x "$CUSTOM_HOOK" ]]; then
173            echo "$CUSTOM_HOOK $REPOS $REV $TXN"
174            "$CUSTOM_HOOK" "$REPOS" "$REV" "$TXN" || RET_CODE=$?
175        fi
176    done
177
178    echo "RET_CODE=$RET_CODE"
179    return $RET_CODE
180}
181
182if ! main 1>$LOG_REV 2>&1; then
183    if [[ -n ${FCM_SVN_HOOK_ADMIN_EMAIL:-} ]]; then
184        mail -s "[$THIS] $REPOS@$REV" "$FCM_SVN_HOOK_ADMIN_EMAIL" <"$LOG_REV" \
185            || true
186    fi
187fi
188cat "$LOG_REV"
189rm -f "$LOG_REV"
190exit
Note: See TracBrowser for help on using the repository browser.