update_loop.sh 5.6 KB

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