autocfg_git.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2021 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero 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. # PopuFare 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 Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. verbose=0
  21. httpd_user="apache"
  22. git_dir="/home/bus/current_config"
  23. deploy_dir="/home/bus/new_bus/devel"
  24. deploy_cmd="deploy_config.sh"
  25. prog_name="${0##*/}"
  26. lockfile_name="/tmp/$prog_name.pid_lock"
  27. try_and_get_lock() {
  28. if [ -f $lockfile_name ]; then
  29. lock_pid="`cat $lockfile_name`"
  30. echo "ERROR: An instance of $prog_name is already running with PID $lock_pid." 2>&1
  31. exit 1
  32. else
  33. echo $$ > $lockfile_name
  34. if [ "$verbose" = "1" ]; then
  35. echo "Got lock: $lockfile_name for PID $$"
  36. fi
  37. fi
  38. }
  39. release_lock() {
  40. if [ -f $lockfile_name ]; then
  41. lock_pid="`cat $lockfile_name`"
  42. if [ "$lock_pid" -eq "$$" ]; then
  43. rm -f $lockfile_name
  44. if [ "$verbose" = "1" ]; then
  45. echo "Released lock: $lockfile_name for PID $$"
  46. fi
  47. else
  48. echo "WARNIG: release_lock() called for lock $lockfile_name belonging to PID $lock_pid. Lock not removed." 2>&1
  49. fi
  50. else
  51. echo "WARNIG: release_lock() called for nonexistent lock $lockfile_name" 2>&1
  52. fi
  53. }
  54. export_and_cleanup() {
  55. #Make sure we can execute commands as the subversion user (by trying to run /bin/true using sudo -u (see comment below)
  56. if sudo -u $httpd_user /bin/true; then
  57. $deploy_dir/webui/export_bus_files.php
  58. # This is _CRITICAL_ because if we do the export but can't remove the dropfiles
  59. #we will keep running the export every single time through the loop, which is _bad_.
  60. #To make sure this condition is met, the following line should be present in the
  61. #/etc/sudoers file.
  62. #
  63. #%apache ALL=(apache) NOPASSWD: ALL
  64. #
  65. sudo -u $httpd_user rm -f /tmp/new_drivers_exist /tmp/new_paddles_exist
  66. else
  67. echo -e "WARNIG: User \"$USER\" not permitted to execute commands as \"$httpd_user\" using sudo -u\nNot exporting paddles and drivers because drop files cannot be removed afterwards." 2>&1
  68. fi
  69. }
  70. cruft_remover() {
  71. git ls-files $1 | sed s:/$::g | sort > /tmp/oldlist
  72. ls $1 | sed s:/$::g | sort > /tmp/newlist
  73. for i in `diff /tmp/oldlist /tmp/newlist | grep "< " | sed s/^\<\ //g`; do git rm $1/$i; done
  74. rm -f /tmp/oldlist /tmp/newlist
  75. }
  76. get_rev() {
  77. git rev-parse HEAD
  78. }
  79. ######################################### ENTRY POINT #####################################################
  80. #Try and get a lock (bad things happen if two of this process try and run at once...
  81. try_and_get_lock
  82. #Change to the configuration sandbox that contains the current config snapshot
  83. pushd $git_dir > /dev/null
  84. #Ask SVN what our old [current as of right before we start making changes] revision is...
  85. old_rev=`get_rev`
  86. #See if we need to pump the database for new paddles or drivers or we've been forced...
  87. if [ "$1" = "-f" ] || [ -f /tmp/new_drivers_exist ] || [ -f /tmp/new_paddles_exist ] ; then
  88. #If this is the case, we want to do the export and remove the dropfiles when we're done...
  89. export_and_cleanup
  90. fi
  91. # Attempt to add all files in this directory (most will be ignored because they're already under version control)
  92. git add *
  93. #Commit these added and/or changed files...
  94. git commit -q -m "added stuff."
  95. #Remove any files from subversion that are no longer present
  96. cruft_remover .
  97. #Commit these removals
  98. git commit -q -m "removed cruft."
  99. # Perform a git update to catch any structure changes, or any files added from elsewhere.
  100. #This also effectively brings this sandbox up to the latest revision.
  101. #
  102. # DISABLED. Left as a placeholder for the future.
  103. #
  104. #git pull -q
  105. # Ask git what our new [after any changes have been applied] revision is...
  106. new_rev=`get_rev`
  107. #If these revision numbers differ, that means we need to push a new configuration out to the clients.
  108. if [ "$new_rev" != "$old_rev" ]; then
  109. #Change our working directory to the working directory of the deploy script
  110. pushd $deploy_dir > /dev/null
  111. # Signal to the deploy script that we are using an external verion control system and we want to
  112. #have our SVN revision number included in the config version string.
  113. export deploy_rev="$new_rev"
  114. #Execute the deploy script and tell it to go look in the SVN sandbox for its component config files
  115. ./$deploy_cmd $git_dir
  116. if [ "$verbose" = "1" ]; then
  117. echo "Configuration deployed."
  118. fi
  119. popd > /dev/null
  120. else
  121. if [ "$verbose" = "1" ]; then
  122. echo "No changes have been made."
  123. fi
  124. fi
  125. #Release our lock so that we can run another day (or minute as the case may be...
  126. release_lock
  127. popd > /dev/null