deploy_config.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2020 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 DBUSER='root'
  21. export DB='busdb'
  22. verbose=0
  23. function usage
  24. {
  25. echo "Usage: $0 [config_dir]"
  26. echo
  27. echo "The environment variable 'deploy_rev' if present will cause the assigned version string to incorporate a revision number from an external version control system, otherwise the revision number is just a timestamp."
  28. exit 1;
  29. }
  30. # If there is an external version control system in use, the calling script may throw
  31. #us a bone and give us an externally managed version number that we can use in place of the
  32. #time component of our timestamp. This makes it easy, for instance, to check out and inspect
  33. #a problematic config version after the fact if needed if you know EITHER when it was rolled, or
  34. #what version control revision it received.
  35. if [ -n "$deploy_rev" ]; then
  36. #In the case of external version control, our version string becomes YYYYMMDDvExternalRev
  37. #Example: Revision 1234 rolled on May 14th 2012 would generate a version string of '20120514v1234'
  38. version_string="`date '+%Y%m%d'`v${deploy_rev}"
  39. else
  40. #In the "dumb" base case, our version string is "YYYYMMDDHHMMSS"
  41. version_string="`date +%Y%m%d%H%M%S`"
  42. fi
  43. config_file="config-$version_string.tgz"
  44. cfg_temp="cfg_temp"
  45. rm -rf $cfg_temp/*
  46. #if nobody's set the COPY_KEYS flag in the environment, don't bother copying the keys
  47. if [ -z "$COPY_KEYS" ]; then
  48. COPY_KEYS="0"
  49. fi
  50. cfg_to="$cfg_temp/mnt/data2/config"
  51. cfgsvr_to="$cfg_to/server"
  52. if ! mkdir -p $cfg_to; then echo "Cannot make directory $cfg_to"; exit; fi
  53. if ! mkdir -p $cfgsvr_to; then echo "Cannot make directory $cfgsvr_to"; exit; fi
  54. if [ "$COPY_KEYS" -ne "0" ]; then
  55. cfg_ssh_to="$cfg_temp/root/.ssh"
  56. cfg_ppp_to="$cfg_temp/etc/ppp"
  57. if ! mkdir -p $cfg_ssh_to; then echo "Cannot make directory $cfg_ssh_to"; exit; fi
  58. if ! mkdir -p $cfg_ppp_to; then echo "Cannot make directory $cfg_ppp_to"; exit; fi
  59. key_dirs="etc root"
  60. else
  61. key_dirs=""
  62. fi
  63. do_key_copy()
  64. {
  65. if [ "$COPY_KEYS" -ne "0" ]; then
  66. cp $1/authorized_keys $cfg_ssh_to
  67. cp $1/known_hosts $cfg_ssh_to
  68. cp $1/id_rsa_bus $cfg_ppp_to
  69. fi
  70. }
  71. if [ -z "$1" -o ! -d "$1" ]; then
  72. if [ "$verbose" = "1" ]; then
  73. echo "Pulling test config files from source tree..."
  74. fi
  75. cp DIU/menu.xml DIU/drivers.txt $cfg_to
  76. cp passdb/init.scm passdb/rfid_patterns.txt passdb/rules.scm $cfg_to
  77. cp paddlemgr/101.paddle paddlemgr/9900.paddle $cfg_to
  78. cp keys/server_list $cfgsvr_to
  79. cp avls/avls_freq.txt $cfg_to
  80. do_key_copy keys
  81. else
  82. if [ "$verbose" = "1" ]; then
  83. echo "Pulling config files from $1"
  84. fi
  85. cp $1/menu.xml $1/drivers.txt $1/init.scm $1/rules.scm $1/*.paddle $1/rfid_patterns.txt $1/avls_freq.txt $cfg_to
  86. cp $1/server_list $cfgsvr_to
  87. do_key_copy $1
  88. if [ -f $1/remove_cruft.sh ]; then cp $1/remove_cruft.sh $cfg_to; fi
  89. if [ -f $1/crontab ]; then cp $1/crontab $cfg_to; fi
  90. if [ -f $1/passes_diagnostic.sh ]; then cp $1/passes_diagnostic.sh $cfg_to; fi
  91. if [ -f $1/pass_count ]; then cp $1/pass_count $cfg_to; fi
  92. fi
  93. if [ -n "$2" ]; then
  94. cp $2 $cfg_temp/install.sh
  95. else
  96. echo '#!/bin/sh' > $cfg_temp/install.sh
  97. echo 'target_dir="/"' >> $cfg_temp/install.sh
  98. echo 'tarball="config.tgz"' >> $cfg_temp/install.sh
  99. echo 'tar -C $target_dir -zxf $tarball;' >> $cfg_temp/install.sh
  100. echo 'echo "$target_dir" > /tmp/pkg_extract_path' >> $cfg_temp/install.sh
  101. echo 'sync; killall -HUP client_supervisor;' >> $cfg_temp/install.sh
  102. echo 'echo "Done.";' >> $cfg_temp/install.sh
  103. echo 'if [ -f /mnt/data2/config/remove_cruft.sh ]; then . /mnt/data2/config/remove_cruft.sh; fi' >> $cfg_temp/install.sh
  104. echo 'if [ -f $1/crontab ]; then cp $1/crontab $cfg_to; fi ' >> $cfg_temp/install.sh
  105. echo 'if [ -f $1/passes_diagnostic.sh ]; then cp $1/passes_diagnostic.sh $cfg_to; fi' >> $cfg_temp/install.sh
  106. echo 'if [ -f $1/pass_count ]; then cp $1/pass_count $cfg_to; fi' >> $cfg_temp/install.sh
  107. chmod a+x $cfg_temp/install.sh
  108. fi
  109. pushd $cfg_temp > /dev/null
  110. tar -zcf $config_file install.sh mnt $key_dirs
  111. mv $config_file ..
  112. popd > /dev/null
  113. filesize="`stat $config_file | grep 'Size:' | egrep -o '[0-9]+' | head -n 1`"
  114. checksum="`md5sum $config_file | cut -d' ' -f1`"
  115. destpath="/home/bus/configs/$config_file"
  116. cp $config_file $destpath
  117. echo "INSERT INTO update_level (client_file , checksum , file_size , file_path , fileversion) VALUES ('config.tgz', '$checksum', $filesize, '$destpath', '$version_string');" | mysql -u $DBUSER -D $DB