update_loop.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. rm $supervisor_pidfile
  52. # then run the whole shebang...
  53. #
  54. $BASEDIR/bin/client_supervisor $supervisor_args &
  55. echo $! > $supervisor_pidfile
  56. echo Spawned client_supervisor. PID = `cat $supervisor_pidfile`
  57. # If we DON'T have one, we are either a new system or just badly broken so...
  58. #
  59. else
  60. # Go and remove all package records so we can start fresh...
  61. #
  62. rm -f $BASEDIR/config/*.tgz.*
  63. $BASEDIR/bin/showmessage -b "Wait (~ 15 min)"
  64. echo "..."
  65. fi
  66. }
  67. function check_update
  68. {
  69. if [ ! -f $TUNNEL_DROPFILE ]; then return 1; fi;
  70. (
  71. if [ -f "$eqnum_file" ]; then
  72. echo -en "`cat $eqnum_file`";
  73. else
  74. echo -en "0"
  75. fi
  76. for file in `ls $checksum_path/*.checksum`; do
  77. echo -en "\t`echo $file | sed -r 's/^.*\/(.*)\.checksum$/\1/'`=`cat $file`";
  78. done;
  79. echo;
  80. ) | nc localhost $UPDATE_DAEMON_PORT
  81. }
  82. while true; do
  83. echo "## heartbeat"
  84. #See if we need to spawn our client supervisor
  85. if [ ! -f $supervisor_pidfile ]; then
  86. spawn_supervisor;
  87. sleep $SLEEP_AFTER_SPAWN;
  88. else
  89. if [ ! -d /proc/`cat $supervisor_pidfile` ]; then
  90. spawn_supervisor;
  91. sleep $SLEEP_AFTER_SPAWN;
  92. fi
  93. fi
  94. if [ -f $TUNNEL_DROPFILE ]; then echo "Checking for updates..."; fi
  95. check_update |
  96. (
  97. while read client_file checksum file_size server_path file_version; do
  98. # If we've been asked to abort this update, do so before we start downloading if possible
  99. #
  100. if [ -f $UPDATE_ABORT_DROPFILE ]; then
  101. echo "Aborting update on request!";
  102. # generate new SSH target from dropfiles
  103. #
  104. generate_ssh_targets
  105. # clear the flag
  106. #
  107. rm -f $UPDATE_ABORT_DROPFILE
  108. # break out of the loop
  109. #
  110. break;
  111. fi
  112. # Generate dropfiles to tell the rest of the system what we're up to
  113. #
  114. echo "$file_size" > /tmp/$client_file.expected_size
  115. echo "$checksum" > /tmp/$client_file.expected_md5
  116. echo "$file_version" > /tmp/$client_file.expected_version
  117. echo "Downloading $client_file version $file_version to /tmp ($file_size bytes)"
  118. # Initiate a secure download to fetch this package down from the server
  119. #
  120. scp -i $SSH_IDENTITY $SCP_OPTIONS -P $SSH_PORT $SSH_TARGET:$server_path /tmp/$client_file
  121. # If we've already downloaded when we get the abort message
  122. #
  123. if [ -f $UPDATE_ABORT_DROPFILE ]; then
  124. echo "Aborting update on request!";
  125. # Clean up after ourselves
  126. #
  127. rm -f /tmp/$client_file.expected_size /tmp/$client_file.expected_md5
  128. rm -f /tmp/$client_file.expected_version /install.sh /tmp/install.sh /tmp/$client_file
  129. # generate new SSH target from dropfiles
  130. #
  131. generate_ssh_targets
  132. # clear the flag
  133. #
  134. rm -f $UPDATE_ABORT_DROPFILE
  135. # break out of the loop
  136. #
  137. break;
  138. fi
  139. # If we indeed _got_ a file to begin with...
  140. #
  141. if [ -f /tmp/$client_file ]; then
  142. # Start a subshell in which we...
  143. #
  144. (
  145. # set our pwd to /tmp/ where we downloaded our package
  146. #
  147. cd /tmp/;
  148. # Execute our package installer script which will check the package checksum
  149. # and assuming that matches, it will extract the installer script embedded in the
  150. # package and execute it.
  151. #
  152. $BASEDIR/bin/apply_update.sh $client_file $checksum $file_size $server_path $file_version
  153. );
  154. fi
  155. # Clean up after ourselves (removing our during-update dropfiles)
  156. #
  157. rm -f /tmp/$client_file.expected_size /tmp/$client_file.expected_md5
  158. # as well as the update file itself
  159. #
  160. rm -f /tmp/$client_file.expected_version /install.sh /tmp/install.sh /tmp/$client_file
  161. done
  162. )
  163. echo "Sleeping..."
  164. sleep $SLEEP_BETWEEN_UPDATES;
  165. done