update_loop.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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"
  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. echo "## heartbeat"
  86. #See if we need to spawn our client supervisor
  87. if [ ! -f $supervisor_pidfile ]; then
  88. spawn_supervisor;
  89. sleep $SLEEP_AFTER_SPAWN;
  90. else
  91. if [ ! -d /proc/`cat $supervisor_pidfile` ]; then
  92. spawn_supervisor;
  93. sleep $SLEEP_AFTER_SPAWN;
  94. fi
  95. fi
  96. if [ -f $TUNNEL_DROPFILE ]; then echo "Checking for updates..."; fi
  97. check_update |
  98. (
  99. while read client_file checksum file_size server_path file_version; do
  100. ## DEBUG
  101. echo "# UPDATES DISABLED TEMPORARILY"
  102. break;
  103. # If we've been asked to abort this update, do so before we start downloading if possible
  104. #
  105. if [ -f $UPDATE_ABORT_DROPFILE ]; then
  106. echo "Aborting update on request!";
  107. # generate new SSH target from dropfiles
  108. #
  109. generate_ssh_targets
  110. # clear the flag
  111. #
  112. rm -f $UPDATE_ABORT_DROPFILE
  113. # break out of the loop
  114. #
  115. break;
  116. fi
  117. # Generate dropfiles to tell the rest of the system what we're up to
  118. #
  119. echo "$file_size" > /tmp/$client_file.expected_size
  120. echo "$checksum" > /tmp/$client_file.expected_md5
  121. echo "$file_version" > /tmp/$client_file.expected_version
  122. echo "Downloading $client_file version $file_version to /tmp ($file_size bytes)"
  123. # Initiate a secure download to fetch this package down from the server
  124. #
  125. scp -i $SSH_IDENTITY $SCP_OPTIONS -P $SSH_PORT $SSH_TARGET:$server_path /tmp/$client_file
  126. # If we've already downloaded when we get the abort message
  127. #
  128. if [ -f $UPDATE_ABORT_DROPFILE ]; then
  129. echo "Aborting update on request!";
  130. # Clean up after ourselves
  131. #
  132. rm -f /tmp/$client_file.expected_size /tmp/$client_file.expected_md5
  133. rm -f /tmp/$client_file.expected_version /install.sh /tmp/install.sh /tmp/$client_file
  134. # generate new SSH target from dropfiles
  135. #
  136. generate_ssh_targets
  137. # clear the flag
  138. #
  139. rm -f $UPDATE_ABORT_DROPFILE
  140. # break out of the loop
  141. #
  142. break;
  143. fi
  144. # If we indeed _got_ a file to begin with...
  145. #
  146. if [ -f /tmp/$client_file ]; then
  147. # Start a subshell in which we...
  148. #
  149. (
  150. # set our pwd to /tmp/ where we downloaded our package
  151. #
  152. cd /tmp/;
  153. # Execute our package installer script which will check the package checksum
  154. # and assuming that matches, it will extract the installer script embedded in the
  155. # package and execute it.
  156. #
  157. $BASEDIR/bin/apply_update.sh $client_file $checksum $file_size $server_path $file_version
  158. );
  159. fi
  160. # Clean up after ourselves (removing our during-update dropfiles)
  161. #
  162. rm -f /tmp/$client_file.expected_size /tmp/$client_file.expected_md5
  163. # as well as the update file itself
  164. #
  165. rm -f /tmp/$client_file.expected_version /install.sh /tmp/install.sh /tmp/$client_file
  166. done
  167. )
  168. echo "Sleeping..."
  169. sleep $SLEEP_BETWEEN_UPDATES;
  170. done