fix race condition
[distro-setup] / switch-mail-host
old mode 100644 (file)
new mode 100755 (executable)
index e52abdb..11c0f40
@@ -1,50 +1,6 @@
 #!/bin/bash
 
-err-bash-trace() {
-  local -i argc_index=0 frame i start=${1:-0} max_indent=8 indent
-  local source
-  local extdebug=false
-  if [[ $(shopt -p extdebug) == *-s* ]]; then
-    extdebug=true
-  fi
-  for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
-    argc=${BASH_ARGC[frame]}
-    argc_index+=$argc
-    ((frame < start)) && continue
-    if (( ${#BASH_SOURCE[@]} > 1 )); then
-      source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
-    fi
-    indent=$((frame-start + 1))
-    indent=$((indent < max_indent ? indent : max_indent))
-    printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
-    if $extdebug; then
-      for ((i=argc_index-1; i >= argc_index-argc; i--)); do
-        printf " %s" "${BASH_ARGV[i]}"
-      done
-    fi
-    echo \'
-  done
-  return 0
-}
-
-
-err-catch() {
-  set -E; shopt -s extdebug
-  _err-trap() {
-    err=$?
-    exec >&2
-    set +x
-    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: \`$BASH_COMMAND' returned $err"
-    err-bash-trace 2
-    set -e # err trap does not work within an error trap
-    "${_errcatch_cleanup[@]:-:}" # note :-: is to be compatible with set -u
-    echo "$0: exiting with code $err"
-    exit $err
-  }
-  trap _err-trap ERR
-  set -o pipefail
-}
-err-catch
+source /usr/local/lib/err
 
 usage() {
   cat <<EOF
@@ -70,13 +26,13 @@ restore_new_btrbk=false
 restore_old_btrbk=false
 cleanup() {
   if $restore_new_btrbk; then
-    $new_shell sudo systemctl start btrbk.timer
+    $new_shell sudo systemctl start btrbk.timer
   fi
   if $restore_old_btrbk; then
-    $old_shell sudo systemctl start btrbk.timer
+    $old_shell sudo systemctl start btrbk.timer
   fi
 }
-_errcatch_cleanup=cleanup
+_errcatch_cleanup=cleanup # used by sourced err
 
 pre="${0##*/}:"
 m() { printf "$pre %s\n"  "$*"; "$@"; }
@@ -103,8 +59,8 @@ old_host=$1
 new_host=$2
 source /a/bin/bash_unpublished/source-state
 
-if [[ $old_host != $MAIL_HOST ]]; then
-  read -p "warning: \$old_host != \$MAIL_HOST: $old_host != $MAIL_HOST, proceed? y/N "
+if [[ $old_host != "$MAIL_HOST" ]]; then
+  read -r -p "warning: \$old_host != \$MAIL_HOST: $old_host != $MAIL_HOST, proceed? y/N "
   if [[ $REPLY != [yY] ]]; then
     exit 1
   fi
@@ -128,10 +84,7 @@ if [[ ! $new_host || ! $old_host ]]; then
   exit 1
 fi
 
-
-source /a/bin/bash_unpublished/source-state
-
-
+e $new_shell hostname
 new_hostname=$($new_shell hostname)
 
 ########### end initial processing, begin actually modifying things ##########
@@ -146,11 +99,20 @@ if $old_shell systemctl is-active btrbk.timer; then
 fi
 
 btrbk_test="systemctl is-active btrbk.service"
-while [[ $($new_shell $btrbk_test) != inactive ]] || [[ $($old_shell $btrbk_test) != inactive ]]; do
-  echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
-  sleep 6
-  echo "$0: testing for btrbk activity in 2 seconds"
-  sleep 2
+while true; do
+  for shell in "$new_shell" "$old_shell"; do
+    e $shell $btrbk_test
+    status=$($shell $btrbk_test) ||:
+    case $status in
+      inactive|failed) : ;;
+      *)
+        e "btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
+        sleep 8
+        continue
+        ;;
+    esac
+  done
+  break
 done
 
 # ensure these are unused before doing anything