From 579794152f9685b7d5a5ee906057f35d5c4b7927 Mon Sep 17 00:00:00 2001 From: Ralf Heiringhoff Date: Wed, 3 Sep 2014 15:06:59 +0200 Subject: [PATCH 1/2] rewrite post-merge to auto-generate tinc config --- scripts/post-merge | 59 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/scripts/post-merge b/scripts/post-merge index 373c349..b8fe8b5 100644 --- a/scripts/post-merge +++ b/scripts/post-merge @@ -1,10 +1,57 @@ #!/bin/bash -# post-merge hook für Debian um tinc zu reloaden +# post-merge hook for Debian systems +# +# 1. copy to .git/hooks +# 2. to test run "git pull -q" +# 3. create cronjob, which runs "git pull -q" in your ${icvpn} dir # -# 1. nach .git/hooks kopieren -# 2. git pull -q ausführen zum testen -# 3. cronjob erstellen, der git pull -q im richtigen -# Verzeichnis ausführt -/etc/init.d/tinc reload icvpn +icvpndir=/etc/tinc/icvpn/ +tinccfg=tinc.conf +export icvpndir tinccfg + +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} +gen_conf() { + test -w ${icvpndir}/${tinccfg} || \ + { echo "ERR: ${tinccfg} (${icvpndir}/${tinccfg}) is not writeable, exiting." >&2; exit 1; } + + # remove all ConnectTo Statements + 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} || \ + { echo "ERR: generated config was empty, zeh sky is falling, exiting." >&2; exit 2; } + + num_connectto=$(grep -s ConnectTo ${tmpfile} | wc -l) + [ ${num_connectto} -gt 20 ] || \ + { 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 + cat ${tmpfile} > ${icvpndir}/${tinccfg} + + invoke-rc.d tinc reload icvpn +} + +check_run hosts/[a-zA-Z] gen_conf + +rm -f ${tmpfile} +exit 0 From b2f40d0a766512d3a4915cc7f3af4a7d720db410 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Thu, 4 Sep 2014 13:29:31 +0200 Subject: [PATCH 2/2] refactor post-merge hook --- scripts/post-merge | 55 ++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 41 deletions(-) mode change 100644 => 100755 scripts/post-merge diff --git a/scripts/post-merge b/scripts/post-merge old mode 100644 new mode 100755 index b8fe8b5..dc97dd3 --- a/scripts/post-merge +++ b/scripts/post-merge @@ -4,54 +4,27 @@ # # 1. copy to .git/hooks # 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/ -tinccfg=tinc.conf -export icvpndir tinccfg - -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" +fail() { + echo "$1" >&2 + exit 1 } -# generate ${tinccfg} -gen_conf() { - test -w ${icvpndir}/${tinccfg} || \ - { echo "ERR: ${tinccfg} (${icvpndir}/${tinccfg}) is not writeable, exiting." >&2; exit 1; } +BASE=$(git rev-parse --show-toplevel) +TINCCFG=$BASE/tinc.conf +test -w $TINCCFG || fail "ERR: $TINCCFG is not writeable" - # remove all ConnectTo Statements - 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 +sed -i '/^ConnectTo/d' $TINCCFG - test -s ${tmpfile} || \ - { echo "ERR: generated config was empty, zeh sky is falling, exiting." >&2; exit 2; } +for HOST in hosts/*; do + # skip hosts without address + grep -q '^Address' -- "$HOST" || continue - num_connectto=$(grep -s ConnectTo ${tmpfile} | wc -l) - [ ${num_connectto} -gt 20 ] || \ - { 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; } + echo "ConnectTo = ${HOST##*/}" >> $TINCCFG +done - cp -f ${icvpndir}/${tinccfg} ${icvpndir}/${tinccfg}.old - cat ${tmpfile} > ${icvpndir}/${tinccfg} +invoke-rc.d tinc reload icvpn - invoke-rc.d tinc reload icvpn -} - -check_run hosts/[a-zA-Z] gen_conf - -rm -f ${tmpfile} exit 0