Browse Source

server side deploy for bus fleet

clementinecomputing 4 năm trước cách đây
mục cha
commit
fc42f642e9
1 tập tin đã thay đổi với 149 bổ sung0 xóa
  1. 149 0
      server/scripts/deploy_config.sh

+ 149 - 0
server/scripts/deploy_config.sh

@@ -0,0 +1,149 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 Clementine Computing LLC.
+#
+# This file is part of PopuFare.
+#
+# PopuFare is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# PopuFare is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
+#
+
+export DBUSER='root'
+export DB='busdb'
+
+verbose=0
+
+function usage
+{
+  echo "Usage: $0 [config_dir]"
+  echo
+  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."
+  exit 1;
+}
+
+#    If there is an external version control system in use, the calling script may throw
+#us a bone and give us an externally managed version number that we can use in place of the
+#time component of our timestamp.  This makes it easy, for instance, to check out and inspect
+#a problematic config version after the fact if needed if you know EITHER when it was rolled, or
+#what version control revision it received.
+if [ -n "$deploy_rev" ]; then
+  #In the case of external version control, our version string becomes YYYYMMDDvExternalRev
+  #Example:  Revision 1234 rolled on May 14th 2012 would generate a version string of '20120514v1234'
+  version_string="`date '+%Y%m%d'`v${deploy_rev}"
+else
+  #In the "dumb" base case, our version string is "YYYYMMDDHHMMSS"
+  version_string="`date +%Y%m%d%H%M%S`"
+fi
+
+config_file="config-$version_string.tgz"
+cfg_temp="cfg_temp"
+
+rm -rf $cfg_temp/*
+
+
+#if nobody's set the COPY_KEYS flag in the environment, don't bother copying the keys
+if [ -z "$COPY_KEYS" ]; then
+  COPY_KEYS="0"
+fi
+
+cfg_to="$cfg_temp/mnt/data2/config"
+cfgsvr_to="$cfg_to/server"
+
+if ! mkdir -p $cfg_to; then echo "Cannot make directory $cfg_to"; exit; fi
+
+if ! mkdir -p $cfgsvr_to; then echo "Cannot make directory $cfgsvr_to"; exit; fi
+
+
+
+if [ "$COPY_KEYS" -ne "0" ]; then
+
+  cfg_ssh_to="$cfg_temp/root/.ssh"
+  cfg_ppp_to="$cfg_temp/etc/ppp"
+
+  if ! mkdir -p $cfg_ssh_to; then echo "Cannot make directory $cfg_ssh_to"; exit; fi
+  if ! mkdir -p $cfg_ppp_to; then echo "Cannot make directory $cfg_ppp_to"; exit; fi
+
+  key_dirs="etc root"
+
+else
+  key_dirs=""
+fi
+
+
+do_key_copy()
+{
+  if [ "$COPY_KEYS" -ne "0" ]; then
+    cp $1/authorized_keys $cfg_ssh_to
+    cp $1/known_hosts $cfg_ssh_to
+    cp $1/id_rsa_bus $cfg_ppp_to
+  fi
+}
+  
+
+
+if [ -z "$1" -o ! -d "$1" ]; then
+  if [ "$verbose" = "1" ]; then 
+    echo "Pulling test config files from source tree..."
+  fi
+  cp DIU/menu.xml DIU/drivers.txt $cfg_to
+  cp passdb/init.scm  passdb/rfid_patterns.txt  passdb/rules.scm $cfg_to
+  cp paddlemgr/101.paddle  paddlemgr/9900.paddle $cfg_to
+  cp keys/server_list $cfgsvr_to
+  cp avls/avls_freq.txt $cfg_to
+
+  do_key_copy keys
+else
+  if [ "$verbose" = "1" ]; then
+    echo "Pulling config files from $1"
+  fi
+  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
+  cp $1/server_list $cfgsvr_to
+  do_key_copy $1
+  if [ -f $1/remove_cruft.sh ]; then cp $1/remove_cruft.sh $cfg_to; fi
+  if [ -f $1/crontab ]; then cp $1/crontab $cfg_to; fi
+  if [ -f $1/passes_diagnostic.sh ]; then cp $1/passes_diagnostic.sh $cfg_to; fi
+  if [ -f $1/pass_count ]; then cp $1/pass_count $cfg_to; fi
+fi
+
+if [ -n "$2" ]; then
+  cp $2 $cfg_temp/install.sh
+else
+  echo '#!/bin/sh' > $cfg_temp/install.sh
+  echo 'target_dir="/"' >> $cfg_temp/install.sh
+  echo 'tarball="config.tgz"' >> $cfg_temp/install.sh
+  echo 'tar -C $target_dir -zxf $tarball;' >> $cfg_temp/install.sh
+  echo 'echo "$target_dir" > /tmp/pkg_extract_path' >> $cfg_temp/install.sh
+  echo 'sync; killall -HUP client_supervisor;' >> $cfg_temp/install.sh
+  echo 'echo "Done.";' >> $cfg_temp/install.sh
+  echo 'if [ -f /mnt/data2/config/remove_cruft.sh ]; then . /mnt/data2/config/remove_cruft.sh; fi' >> $cfg_temp/install.sh
+  echo 'if [ -f $1/crontab ]; then cp $1/crontab $cfg_to; fi ' >> $cfg_temp/install.sh
+  echo 'if [ -f $1/passes_diagnostic.sh ]; then cp $1/passes_diagnostic.sh $cfg_to; fi' >> $cfg_temp/install.sh
+  echo 'if [ -f $1/pass_count ]; then cp $1/pass_count $cfg_to; fi' >> $cfg_temp/install.sh
+  chmod a+x $cfg_temp/install.sh
+fi
+
+pushd $cfg_temp > /dev/null
+  tar -zcf $config_file install.sh mnt $key_dirs
+  mv $config_file ..
+popd > /dev/null
+  
+filesize="`stat $config_file | grep 'Size:' | egrep -o '[0-9]+' | head -n 1`"
+checksum="`md5sum $config_file | cut -d' ' -f1`"
+
+destpath="/home/bus/configs/$config_file"
+
+cp $config_file $destpath
+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
+
+
+