various fixes and improvements
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index 3f0e9ec06504d50d2f43289e9ebf2a423196a0be..891e0953b668e7e69d847f54e5f4ad24f768fff5 100644 (file)
--- a/brc2
+++ b/brc2
@@ -108,6 +108,8 @@ EOF
 }
 
 bbk() { # btrbk wrapper
+
+  local pid
   c /
   local active=true
   systemctl is-active btrbk.timer || active=false
@@ -121,10 +123,15 @@ bbk() { # btrbk wrapper
   fi
   # run latest
   install-my-scripts
-  btrbk-run "$@" |& pee cat "systemd-cat -t btrbk-run"
+  jrun -p btrbk btrbk-run "$@"
   if $active; then
-    ser enable btrbk.timer
+    if (( $ret )); then
+      echo bbk: WARNING: btrbk.timer not reenabled due to failure
+    else
+      ser enable btrbk.timer
+    fi
   fi
+  return $ret
 }
 
 bfg() { java -jar /a/opt/bfg-1.12.14.jar "$@"; }
@@ -133,25 +140,67 @@ bigclock() {
   xclock -digital -update 1 -face 'arial black-80:bold'
 }
 
-bpull() {
-  [[ $1 ]] || return 1
-  c /
-  # run latest
-  install-my-scripts
-  switch-mail-host pull $1 |& pee cat "systemd-cat -t switch-mail-host"
+_jrun() { # journal run. run args, log to journal, tail and grep the journal.
+  local pid pattern jr_pid sedscript cmd_name ended
+  ret=0
+  case $1 in
+    -p)
+      pattern="$2\|"
+      shift 2
+      ;;
+  esac
+  cmd_name=${1##*/}
+  systemd-cat -t "$cmd_name" "$@" 2>&1 &
+  pid=$!
+  sedscript="/$pattern$cmd_name/p;/^.{16}[^ ]+ $cmd_name\[$pid]: ([^ ]+ +)?exiting with status [0-9]+\$/q"
+  journalctl -S "4 seconds ago" -f |& sed -nr "$sedscript" 2>&1 &
+  jr_pid=$!
+  wait $pid 2>&1 || ret=$?
+  if (( $ret )); then
+    echo "$0: ERROR: $* returned $ret"
+  fi
+  jobs %'journalctl -S "4 seconds ago" -f' &>/dev/null
+  ended=false
+  # give it about 5 seconds to find the end of the log
+  for (( i=0; i<20; i++ )); do
+    if jobs %'journalctl -S "4 seconds ago" -f' &>/dev/null; then
+      sleep .3 2>&1
+    else
+      ended=true
+      break
+    fi
+  done
+  if ! $ended; then
+    kill $jr_pid 2>&1
+  fi
+}
+jrun() {
+  # ditching stderr avoids the jobs status change output.
+  _jrun "$@" 2>/dev/null
 }
-bpush() {
-  [[ $1 ]] || return 1
+
+sm() {
+  (( $# == 2 )) || return 1
+  action=$1
+  shift
+  host=$1
+  shift
+  case $action in
+    pull|push) : ;;
+    *) return 1 ;;
+  esac
   c /
   # run latest
   install-my-scripts
-  switch-mail-host push $1 |& pee cat "systemd-cat -t switch-mail-host"
+  jrun -p btrbk switch-mail-host $action $host
+  return $ret
 }
+
 lipush() {
   # note, i had --delete-excluded, but that deletes all files in --exclude-from on
   # the remote site, which doesn't make sense, so not sure why i had it.
   local p a
-  p=(/a/bin /a/exe /a/h /a/c /p/c/machine_specific/linode{,.hosts} /a/opt/{emacs,emacs-debianstable,mu})
+  p=(/a/bin /a/exe /a/h /a/c /p/c/machine_specific/linode{,.hosts} /a/opt/{emacs-debianstable,mu})
   a="-ahviSAXPH --specials --devices --delete --relative --exclude-from=/p/c/li-rsync-excludes"
   ret=0
   m rsync "$@" $a ${p[@]} /p/c/machine_specific/l2 root@l2.b8.nz:/ || ret=$?
@@ -283,7 +332,7 @@ dup() {
       ran_d=true
       ;;&
     *DISTRO-END!*|*DISTRO!*)
-      pushd
+      pushd /
       /b/ds/distro-end || return $?
       popd
       ran_d=true
@@ -993,7 +1042,7 @@ sss() { # ssh solo
 }
 # kill off old shared socket then ssh
 ssk() {
-  m ssh -O exit "$@"
+  m ssh -O exit "$@" || [[ $? == 255 ]]
   m sl "$@"
 }
 # plain limited ssh