ff_update_peers 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. #
  3. # Simple script to update fastd peers from git upstream
  4. # and only send HUP to fastd when changes happend.
  5. #
  6. # (C) 2014 Helge Jung <hej@c3pb.de>
  7. # (C) 2014 Maximilian Wilhelm <max@rfc2324.org>
  8. #
  9. # CONFIGURE THIS TO YOUR PEER DIRECTORY
  10. DEFAULT_PEERS_DIRECTORY="/etc/freifunk/peers"
  11. SSH_IDENTITY="/root/.ssh/ffho_peers_git.id_rsa"
  12. getCurrentVersion() {
  13. # Get hash from latest revision
  14. git log --format=format:%H -1 | tr -d '\n'
  15. }
  16. [ -z "$RELOAD_FASTD" ] && RELOAD_FASTD="yes"
  17. [ -z "$PEERS_DIRECTORY" ] && PEERS_DIRECTORY=$DEFAULT_PEERS_DIRECTORY
  18. if [ ! -d "$PEERS_DIRECTORY" ]; then
  19. echo "Peers directory not found ($PEERS_DIRECTORY). Cannot update."
  20. exit 1
  21. fi
  22. if [ ! -d "${PEERS_DIRECTORY}/.git" ]; then
  23. echo "Peers directory does not seem to be a git repository. Cannot update."
  24. exit 1
  25. fi
  26. cd "${PEERS_DIRECTORY}"
  27. ssh-add -l > /dev/null 2>&1
  28. if [ "$?" -ne 0 ]; then
  29. eval $(ssh-agent) > /dev/null
  30. ssh-add "${SSH_IDENTITY}" 2> /dev/null
  31. SSH_AGENT_STARTED=yes
  32. else
  33. SSH_AGENT_STARTED=no
  34. fi
  35. # Get current version hash
  36. LAST_REVISION="$(getCurrentVersion)"
  37. if [ "$(git status --porcelain)" ]; then
  38. echo "Local changes to peers directory. Cowardly refusing to update this repository!" >&2
  39. exit 1
  40. fi
  41. if ! git pull --quiet --rebase >/dev/null; then
  42. echo "Update of peers repository failed... :-(" >&2
  43. exit 2
  44. fi
  45. # Get new version hash
  46. GIT_NEW_REVISION="$(getCurrentVersion)"
  47. if [ "${LAST_REVISION}" != "${GIT_NEW_REVISION}" ]; then
  48. if [ "$RELOAD_FASTD" != "no" ]; then
  49. fastd_pids=$(pidof fastd)
  50. if [ ! "${fastd_pids}" ]; then
  51. echo "Oh noes, there is no fastd running here.. Thou shalt fix this now, br0!" >&2
  52. exit 3
  53. fi
  54. kill -HUP ${fastd_pids}
  55. fi
  56. # Get list of commits since last local version
  57. num_commits="$(git log --abbrev-commit --pretty=oneline ${LAST_REVISION}..${GIT_NEW_REVISION} | wc -l)"
  58. last_msg="$(git log --abbrev-commit --pretty=oneline ${LAST_REVISION}..${GIT_NEW_REVISION} | head -n1)"
  59. if [ ! -z "$GENALIASES_FILE" ]; then
  60. ./gen_aliases.sh . > "$GENALIASES_FILE"
  61. ALIASES_UPDATED=1
  62. else
  63. ALIASES_UPDATED=0
  64. fi
  65. echo -n "Peers"
  66. if [ $ALIASES_UPDATED -eq 1 ]; then echo -n " and aliases"; fi
  67. echo " updated: ${num_commits} commits (last: ${last_msg})"
  68. fi
  69. if [ "$SSH_AGENT_STARTED" = "yes" ]; then
  70. ssh-agent -k > /dev/null
  71. unset SSH_AGENT_PID
  72. fi