refactor post-merge hook
This commit is contained in:
parent
579794152f
commit
b2f40d0a76
55
scripts/post-merge
Normal file → Executable file
55
scripts/post-merge
Normal file → Executable file
|
@ -4,54 +4,27 @@
|
||||||
#
|
#
|
||||||
# 1. copy to .git/hooks
|
# 1. copy to .git/hooks
|
||||||
# 2. to test run "git pull -q"
|
# 2. to test run "git pull -q"
|
||||||
# 3. create cronjob, which runs "git pull -q" in your ${icvpn} dir
|
# 3. create cronjob running "git pull -q -C /etc/tinc/icvpn/"
|
||||||
#
|
#
|
||||||
|
|
||||||
icvpndir=/etc/tinc/icvpn/
|
fail() {
|
||||||
tinccfg=tinc.conf
|
echo "$1" >&2
|
||||||
export icvpndir tinccfg
|
exit 1
|
||||||
|
|
||||||
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
|
|
||||||
tmpfile=$(mktemp)
|
|
||||||
export PATH tmpfile
|
|
||||||
|
|
||||||
# get changed files
|
|
||||||
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
|
|
||||||
|
|
||||||
# run $2 if $1 matches changed files
|
|
||||||
check_run() {
|
|
||||||
echo "$changed_files" | grep --quiet "$1" && eval "$2"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# generate ${tinccfg}
|
BASE=$(git rev-parse --show-toplevel)
|
||||||
gen_conf() {
|
TINCCFG=$BASE/tinc.conf
|
||||||
test -w ${icvpndir}/${tinccfg} || \
|
test -w $TINCCFG || fail "ERR: $TINCCFG is not writeable"
|
||||||
{ echo "ERR: ${tinccfg} (${icvpndir}/${tinccfg}) is not writeable, exiting." >&2; exit 1; }
|
|
||||||
|
|
||||||
# remove all ConnectTo Statements
|
sed -i '/^ConnectTo/d' $TINCCFG
|
||||||
grep -v 'ConnectTo' ${icvpndir}/${tinccfg} > ${tmpfile}
|
|
||||||
# add ConnectTo Statements for all hosts
|
|
||||||
for host in ${icvpndir}/hosts/[a-zA-Z]*; do
|
|
||||||
echo "ConnectTo = ${host##*/}" >> ${tmpfile}
|
|
||||||
done
|
|
||||||
|
|
||||||
test -s ${tmpfile} || \
|
for HOST in hosts/*; do
|
||||||
{ echo "ERR: generated config was empty, zeh sky is falling, exiting." >&2; exit 2; }
|
# skip hosts without address
|
||||||
|
grep -q '^Address' -- "$HOST" || continue
|
||||||
|
|
||||||
num_connectto=$(grep -s ConnectTo ${tmpfile} | wc -l)
|
echo "ConnectTo = ${HOST##*/}" >> $TINCCFG
|
||||||
[ ${num_connectto} -gt 20 ] || \
|
done
|
||||||
{ echo "ERR: generated config has ${num_connectto} <= 20 ConnectTo lines, safety 1st, exiting." >&2; exit 30; }
|
|
||||||
num_all=$(cat ${tmpfile} | wc -l)
|
|
||||||
[ ${num_all} -gt 25 ] || \
|
|
||||||
{ echo "ERR: generated config has ${num_all} <= 25 lines, safety 1st, exiting." >&2; exit 31; }
|
|
||||||
|
|
||||||
cp -f ${icvpndir}/${tinccfg} ${icvpndir}/${tinccfg}.old
|
invoke-rc.d tinc reload icvpn
|
||||||
cat ${tmpfile} > ${icvpndir}/${tinccfg}
|
|
||||||
|
|
||||||
invoke-rc.d tinc reload icvpn
|
|
||||||
}
|
|
||||||
|
|
||||||
check_run hosts/[a-zA-Z] gen_conf
|
|
||||||
|
|
||||||
rm -f ${tmpfile}
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue