Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -e
  4.  
  5. if [ -z "$GIT_DIR" ]; then
  6. echo "Don't run this script from the command line." >&2
  7. exit 1
  8. fi
  9.  
  10. if git rev-parse --is-inside-work-tree && [ "$(git symbolic-ref HEAD | sed 's!refs\/heads\/!!')" == "master" ]; then
  11. DATE=$(date '+%Y%m%d')
  12. IFS=$'\n'
  13. TMP=$(mktemp -dt)
  14. LASTMERGE=$(git rev-parse head~2)
  15. for f in $(git diff --name-only "$LASTMERGE" HEAD); do
  16. echo "doing $f"
  17. # extract the date from the zone file
  18. OLDSERIAL=$(sed -n '/IN[[:space:]]\+SOA/,/)/ {
  19. : attempt
  20. s/.*([\n\t ]*\([[:digit:]]\+\).*/\1/
  21. t done
  22. N
  23. b attempt
  24. : done
  25. p
  26. }' "$f")
  27. OLDDATE=$(echo "$OLDSERIAL" | cut -c1-8)
  28. OLDCOUNT=$(echo "$OLDSERIAL" | cut -c9-)
  29. if (( "$DATE" > "$OLDDATE" )); then
  30. # since it's already greater than the old serial, just append 00
  31. SERIAL="${DATE}00"
  32. elif (( "$DATE" == "$OLDDATE" )); then
  33. # we have some work to do. add 1 to the old bit
  34. COUNT=$(printf '%02d' $((OLDCOUNT + 1)))
  35. SERIAL="${DATE}${COUNT}"
  36. else
  37. # old serial number is bigger than the new one. don't bother
  38. SERIAL=$OLDSERIAL
  39. continue
  40. fi
  41. sed -e '/IN[[:space:]]\+SOA/,/)/ {
  42. : attempt
  43. s/\(([\n\t ]*\)[[:digit:]]\+/\1'"${SERIAL}"'/
  44. t done
  45. N
  46. b attempt
  47. : done
  48. }' "$f" >"$TMP/$f"
  49. if named-checkconf "$TMP/$f"; then
  50. cp "$TMP/$f" "$f"
  51. # were there actually changes?
  52. git diff --quiet "$f" && git add "$f"
  53. else
  54. echo Promblem in "$f"
  55. fi
  56. done
  57. rm -rf "$TMP"
  58. git commit -m 'Update serials '"$SERIAL"' (pre-commit script)'
  59. rndc reload
  60. fi
  61. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement