| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #!/bin/bash
- #
- # Copyright (c) 2021 Clementine Computing LLC.
- #
- # This file is part of PopuFare.
- #
- # PopuFare is free software: you can redistribute it and/or modify
- # it under the terms of the GNU Affero General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # PopuFare is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Affero General Public License for more details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
- #
- verbose=0
- httpd_user="apache"
- git_dir="/home/bus/current_config"
- deploy_dir="/home/bus/new_bus/devel"
- deploy_cmd="deploy_config.sh"
- prog_name="${0##*/}"
- lockfile_name="/tmp/$prog_name.pid_lock"
- try_and_get_lock() {
- if [ -f $lockfile_name ]; then
- lock_pid="`cat $lockfile_name`"
- echo "ERROR: An instance of $prog_name is already running with PID $lock_pid." 2>&1
- exit 1
- else
- echo $$ > $lockfile_name
- if [ "$verbose" = "1" ]; then
- echo "Got lock: $lockfile_name for PID $$"
- fi
- fi
- }
- release_lock() {
- if [ -f $lockfile_name ]; then
- lock_pid="`cat $lockfile_name`"
- if [ "$lock_pid" -eq "$$" ]; then
-
- rm -f $lockfile_name
- if [ "$verbose" = "1" ]; then
- echo "Released lock: $lockfile_name for PID $$"
- fi
- else
- echo "WARNIG: release_lock() called for lock $lockfile_name belonging to PID $lock_pid. Lock not removed." 2>&1
- fi
- else
- echo "WARNIG: release_lock() called for nonexistent lock $lockfile_name" 2>&1
- fi
- }
- export_and_cleanup() {
- #Make sure we can execute commands as the subversion user (by trying to run /bin/true using sudo -u (see comment below)
- if sudo -u $httpd_user /bin/true; then
- $deploy_dir/webui/export_bus_files.php
- # This is _CRITICAL_ because if we do the export but can't remove the dropfiles
- #we will keep running the export every single time through the loop, which is _bad_.
- #To make sure this condition is met, the following line should be present in the
- #/etc/sudoers file.
- #
- #%apache ALL=(apache) NOPASSWD: ALL
- #
- sudo -u $httpd_user rm -f /tmp/new_drivers_exist /tmp/new_paddles_exist
- else
- 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
- fi
- }
- cruft_remover() {
- git ls-files $1 | sed s:/$::g | sort > /tmp/oldlist
- ls $1 | sed s:/$::g | sort > /tmp/newlist
- for i in `diff /tmp/oldlist /tmp/newlist | grep "< " | sed s/^\<\ //g`; do git rm $1/$i; done
- rm -f /tmp/oldlist /tmp/newlist
- }
- get_rev() {
- git rev-parse HEAD
- }
- ######################################### ENTRY POINT #####################################################
- #Try and get a lock (bad things happen if two of this process try and run at once...
- try_and_get_lock
- #Change to the configuration sandbox that contains the current config snapshot
- pushd $git_dir > /dev/null
- #Ask SVN what our old [current as of right before we start making changes] revision is...
- old_rev=`get_rev`
- #See if we need to pump the database for new paddles or drivers or we've been forced...
- if [ "$1" = "-f" ] || [ -f /tmp/new_drivers_exist ] || [ -f /tmp/new_paddles_exist ] ; then
-
- #If this is the case, we want to do the export and remove the dropfiles when we're done...
- export_and_cleanup
- fi
- # Attempt to add all files in this directory (most will be ignored because they're already under version control)
- git add *
- #Commit these added and/or changed files...
- git commit -q -m "added stuff."
- #Remove any files from subversion that are no longer present
- cruft_remover .
- #Commit these removals
- git commit -q -m "removed cruft."
- # Perform a git update to catch any structure changes, or any files added from elsewhere.
- #This also effectively brings this sandbox up to the latest revision.
- #
- # DISABLED. Left as a placeholder for the future.
- #
- #git pull -q
- # Ask git what our new [after any changes have been applied] revision is...
- new_rev=`get_rev`
- #If these revision numbers differ, that means we need to push a new configuration out to the clients.
- if [ "$new_rev" != "$old_rev" ]; then
- #Change our working directory to the working directory of the deploy script
- pushd $deploy_dir > /dev/null
- # Signal to the deploy script that we are using an external verion control system and we want to
- #have our SVN revision number included in the config version string.
- export deploy_rev="$new_rev"
- #Execute the deploy script and tell it to go look in the SVN sandbox for its component config files
- ./$deploy_cmd $git_dir
-
- if [ "$verbose" = "1" ]; then
- echo "Configuration deployed."
- fi
- popd > /dev/null
- else
- if [ "$verbose" = "1" ]; then
- echo "No changes have been made."
- fi
- fi
- #Release our lock so that we can run another day (or minute as the case may be...
- release_lock
- popd > /dev/null
|