update_loop.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2019 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. export BASEDIR="/home/bus"
  21. . $BASEDIR/bin/common_values.sh
  22. # Do this at startup, and again if we've gotten a dropfile flag telling us to do so...
  23. #
  24. generate_ssh_targets
  25. supervisor_pidfile="/tmp/client_supervisor.pid"
  26. supervisor_args="$BASEDIR/bin/ipc_server $BASEDIR/bin/billdb $BASEDIR/bin/piu_minder $BASEDIR/bin/diu_minder $BASEDIR/bin/passdb $BASEDIR/bin/avls $BASEDIR/bin/paddlemgr $BASEDIR/bin/gps_minder"
  27. eqnum_file="$EQUIP_NUM_FILE"
  28. checksum_path="$CHECKSUM_AND_VERSION_PATH"
  29. function spawn_supervisor
  30. {
  31. # If we have a client supervisor binary to run
  32. #
  33. if [ -f $BASEDIR/bin/client_supervisor ]; then
  34. # # If we also have a config file for the UI
  35. # #
  36. # if [ -f $BASEDIR/config/menu.xml ]; then
  37. # rm $supervisor_pidfile
  38. #
  39. # # then run the whole shebang...
  40. # #
  41. # $BASEDIR/bin/client_supervisor $supervisor_args &
  42. # echo $! > $supervisor_pidfile
  43. # echo Spawned client_supervisor. PID = `cat $supervisor_pidfile`
  44. #
  45. # # if we DON'T have one, we still need to download a config before we're viable...
  46. # #
  47. # else
  48. # rm -f config.tgz.*
  49. # $BASEDIR/bin/showmessage -b "Downloading Config..."
  50. # fi
  51. # If we've gotten here, the client_supervisor process should
  52. # be running or is stale, so cleanup and respawn
  53. #
  54. rm -f $supervisor_pidfile
  55. # then run the whole shebang...
  56. #
  57. $BASEDIR/bin/client_supervisor $supervisor_args &
  58. echo $! > $supervisor_pidfile
  59. echo Spawned client_supervisor. PID = `cat $supervisor_pidfile`
  60. # If we DON'T have one, we are either a new system or just badly broken so...
  61. #
  62. else
  63. # Go and remove all package records so we can start fresh...
  64. #
  65. rm -f $BASEDIR/config/*.tgz.*
  66. $BASEDIR/bin/showmessage -b "Wait (~ 15 min)"
  67. fi
  68. }
  69. function check_update
  70. {
  71. if [ ! -f $TUNNEL_DROPFILE ]; then return 1; fi;
  72. (
  73. if [ -f "$eqnum_file" ]; then
  74. echo -en "`cat $eqnum_file`";
  75. else
  76. echo -en "0"
  77. fi
  78. for file in `ls $checksum_path/*.checksum`; do
  79. echo -en "\t`echo $file | sed -r 's/^.*\/(.*)\.checksum$/\1/'`=`cat $file`";
  80. done;
  81. echo;
  82. ) | nc -q1 localhost $UPDATE_DAEMON_PORT
  83. }
  84. while true; do
  85. dt=`date +'%s'`
  86. echo "## [$dt] update_loop: heartbeat"
  87. #See if we need to spawn our client supervisor
  88. if [ ! -f $supervisor_pidfile ]; then
  89. spawn_supervisor;
  90. sleep $SLEEP_AFTER_SPAWN;
  91. else
  92. if [ ! -d /proc/`cat $supervisor_pidfile` ]; then
  93. spawn_supervisor;
  94. sleep $SLEEP_AFTER_SPAWN;
  95. fi
  96. fi
  97. if [ -f $TUNNEL_DROPFILE ]; then echo "Checking for updates..."; fi
  98. check_update |
  99. (
  100. while read client_file checksum file_size server_path file_version; do
  101. # If we've been asked to abort this update, do so before we start downloading if possible
  102. #
  103. if [ -f $UPDATE_ABORT_DROPFILE ]; then
  104. echo "Aborting update on request!";
  105. # generate new SSH target from dropfiles
  106. #
  107. generate_ssh_targets
  108. # clear the flag
  109. #
  110. rm -f $UPDATE_ABORT_DROPFILE
  111. # break out of the loop
  112. #
  113. break;
  114. fi
  115. # Generate dropfiles to tell the rest of the system what we're up to
  116. #
  117. echo "$file_size" > /tmp/$client_file.expected_size
  118. echo "$checksum" > /tmp/$client_file.expected_md5
  119. echo "$file_version" > /tmp/$client_file.expected_version
  120. echo "Downloading $client_file version $file_version to /tmp ($file_size bytes)"
  121. # Initiate a secure download to fetch this package down from the server
  122. #
  123. scp -i $SSH_IDENTITY $SCP_OPTIONS -P $SSH_PORT $SSH_TARGET:$server_path /tmp/$client_file
  124. # If we've already downloaded when we get the abort message
  125. #
  126. if [ -f $UPDATE_ABORT_DROPFILE ]; then
  127. echo "Aborting update on request!";
  128. # Clean up after ourselves
  129. #
  130. rm -f /tmp/$client_file.expected_size /tmp/$client_file.expected_md5
  131. rm -f /tmp/$client_file.expected_version /install.sh /tmp/install.sh /tmp/$client_file
  132. # generate new SSH target from dropfiles
  133. #
  134. generate_ssh_targets
  135. # clear the flag
  136. #
  137. rm -f $UPDATE_ABORT_DROPFILE
  138. # break out of the loop
  139. #
  140. break;
  141. fi
  142. # If we indeed _got_ a file to begin with...
  143. #
  144. if [ -f /tmp/$client_file ]; then
  145. # Start a subshell in which we...
  146. #
  147. (
  148. # set our pwd to /tmp/ where we downloaded our package
  149. #
  150. cd /tmp/;
  151. # Execute our package installer script which will check the package checksum
  152. # and assuming that matches, it will extract the installer script embedded in the
  153. # package and execute it.
  154. #
  155. $BASEDIR/bin/apply_update.sh $client_file $checksum $file_size $server_path $file_version
  156. );
  157. fi
  158. # Clean up after ourselves (removing our during-update dropfiles)
  159. #
  160. rm -f /tmp/$client_file.expected_size /tmp/$client_file.expected_md5
  161. # as well as the update file itself
  162. #
  163. rm -f /tmp/$client_file.expected_version /install.sh /tmp/install.sh /tmp/$client_file
  164. done
  165. )
  166. echo "Sleeping..."
  167. sleep $SLEEP_BETWEEN_UPDATES;
  168. done