#!/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 . # verbose=0 httpd_user="apache" svn_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() { svn ls $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 svn rm $1/$i; done rm -f /tmp/oldlist /tmp/newlist } get_rev() { svn info | egrep "^Revision: [0-9]+$" | egrep -o "[0-9]+$" } ######################################### 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 $svn_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) svn add * -q 2>/dev/null #Commit these added and/or changed files... svn commit --non-interactive -m "added stuff." -q #Remove any files from subversion that are no longer present cruft_remover . #Commit these removals svn commit --non-interactive -m "removed cruft." -q # Perform an SVN update to catch any structure changes, or any files added from elsewhere. #This also effectively brings this sandbox up to the latest revision. svn update --non-interactive -q #Ask SVN 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 $svn_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