updates, especially for etiona
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index 959e892562f485afde4748c71cd66456c23f89ab..cdac34b690b40401c314c0138a543e7918674d09 100644 (file)
--- a/brc2
+++ b/brc2
@@ -29,7 +29,6 @@ export WCDHOME=/a
 source /a/bin/distro-functions/src/identify-distros
 source /a/bin/distro-functions/src/package-manager-abstractions
 source /a/bin/log-quiet/logq-function
-source /a/bin/small-misc-bash/psg-function
 # for x in /a/bin/bash_unpublished/source-!(.#*); do echo source $x; done
 source /a/bin/bash_unpublished/source-semi-priv
 source /a/bin/bash_unpublished/source-state
@@ -101,31 +100,38 @@ set -e
 conflink
 f=/var/lib/bind/db.b8.nz
 ser stop bind9
-s rm -fv $f.jnl
-s install -m 644 -o bind -g bind /p/c/machine_specific/linode/bind-initial/db.b8.nz $f
+sudo rm -fv $f.jnl
+sudo install -m 644 -o bind -g bind /p/c/machine_specific/linode/bind-initial/db.b8.nz $f
 ser restart bind9
 EOF
   done
 }
 
 bbk() { # btrbk wrapper
+
+  local pid
   c /
   local active=true
   systemctl is-active btrbk.timer || active=false
   if $active; then
-    ser disable btrbk.timer
+    ser stop btrbk.timer
   fi
-  if systemctl is-active btrbk.service; then
-    $active && ser enable btrbk.timer
+  if [[ $(systemctl is-active btrbk.service ||:) != inactive ]]; then
     echo "cron btrbk is already running"
+    if $active; then ser start btrbk.timer; fi
     return 1
   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 restarted due to failure
+    else
+      ser start btrbk.timer
+    fi
   fi
+  return $ret
 }
 
 bfg() { java -jar /a/opt/bfg-1.12.14.jar "$@"; }
@@ -134,25 +140,68 @@ 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"
+inttrap() {
+  pid=$1
+  # just passing on -INT doesnt work.
+  kill -TERM $pid
+  sleep .05
+  if [[ ! -e /proc/$pid ]]; then
+    if [[ $old_int_trap ]]; then
+      $old_int_trap
+    else
+      trap INT
+    fi
+  fi
 }
-bpush() {
-  [[ $1 ]] || return 1
+
+
+jrun() { # journal run. run args, log to journal, tail and grep the journal.
+  # Note, an alternative without systemd would be something like ts.
+  # Note: I tried doing cmd | pee "sudo systemd-cat" cat, but that
+  # had some problems like ctrl-c didnt work or something.
+  local pid pattern sedscript cmd_name ended
+  ret=0
+  case $1 in
+    -p)
+      pattern="$2|"
+      shift 2
+      ;;
+  esac
+  cmd_name=${1##*/}
+  sedscript="/$pattern$cmd_name/p;/^.{16}[^ ]+ $cmd_name\[[0-9]+]: ([^ ]*\/)?$cmd_name: exiting with status [0-9]+\$/q"
+  # We use >() so that $! is the pid of journalctl, otherwise its the sed pid and then
+  # if we kill that, it takes journalctl about 10 seconds to catch up, and we get
+  # an annoying message about job finishing then.
+  journalctl -qn2 -f &> >(sed -nr "$sedscript") &
+  # We kill this in prompt-command for the case that we ctrl-c the
+  # systemd-cat. i dont know any way to trap ctrl-c and still run the
+  # normal action for it. There might be a way, unsure.
+  jr_pid=$!
+  systemd-cat -t "$cmd_name" "$@" || ret=$?
+  if (( $ret )); then
+    echo "jrun: ERROR: $* returned $ret"
+  fi
+  # This justs lets the journal output its last line
+  # before the prompt comes up.
+  sleep .5
+  kill $jr_pid &>/dev/null ||:
+  unset jr_pid
+  fg &>/dev/null ||:
+}
+
+sm() {
   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 "$@"
+  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=$?
@@ -236,8 +285,9 @@ chrome() {
 
 # do all tee.
 # pipe to this, or just type like a shell
+# todo: test this
 dat() {
-  tee >(ssh frodo.b8.nz bash -l) >(ssh x2 bash -l) >(ssh tp.b8.nz bash -l) >(ssh kw bash -l) >(ssh tp.b8.nz bash -l)
+  tee >(ssh frodo.b8.nz) >(ssh x2) >(ssh tp.b8.nz) >(ssh kw) >(ssh tp.b8.nz)
 }
 da() { # do all
   local host
@@ -283,7 +333,7 @@ dup() {
       ran_d=true
       ;;&
     *DISTRO-END!*|*DISTRO!*)
-      pushd
+      pushd /
       /b/ds/distro-end || return $?
       popd
       ran_d=true
@@ -306,6 +356,8 @@ envload() { # load environment from a previous: export > file
   done < "$file"
 }
 
+failfunc() { asdf a b c; }
+failfunc2() { failfunc d e f; }
 
 # one that comes with distros is too old for newer devices
 fastboot() {
@@ -659,21 +711,21 @@ net-dev-info() {
   hr
   e "s lshw -C network"
   hr
-  s lshw -C network
+  sudo lshw -C network
 }
 
 nk() {
   ser stop NetworkManager
   ser stop dnsmasq
-  s resolvconf -d NetworkManager
+  sudo resolvconf -d NetworkManager
   ser start dnsmasq
-  s ifup br0
+  sudo ifup br0
 }
 ngo() {
-  s ifdown br0
+  sudo ifdown br0
   ser start NetworkManager
   sleep 4
-  s nmtui-connect
+  sudo nmtui-connect
 }
 
 otp() {
@@ -746,8 +798,8 @@ pumpa() {
 #rbp() { rbt post -o "$@"; }
 
 rebr() {
-  s ifdown br0
-  s ifup br0
+  sudo ifdown br0
+  sudo ifup br0
 }
 
 resolvcat() {
@@ -755,18 +807,29 @@ resolvcat() {
   m s nscd -i hosts
   f=/etc/resolv.conf
   echo $f:;  ccat $f
-  hr; m ser status dnsmasq | cat
   hr; s ss -lpn 'sport = 53'
-  #hr; echo dnsmasq is $(systemctl is-active dnsmasq)
-  f=/var/run/dnsmasq/resolv.conf
-  hr; echo $f:;  ccat $f
-  hr; m grr '^ *servers-file *=' /etc/dnsmasq.conf /etc/dnsmasq.d
-  f=/etc/dnsmasq-servers.conf
-  hr; echo $f:;  ccat $f
+  if systemctl is-enabled dnsmasq &>/dev/null || [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
+    # this will fail is dnsmasq is failed
+    hr; m ser status dnsmasq | cat || :
+    f=/var/run/dnsmasq/resolv.conf
+    hr; echo $f:;  ccat $f
+    hr; m grr '^ *(servers-file|server) *=|^ *no-resolv *$' /etc/dnsmasq.conf /etc/dnsmasq.d
+    f=/etc/dnsmasq-servers.conf
+    hr; echo $f:;  ccat $f
+  fi
+  if systemctl is-enabled systemd-resolved &>/dev/null || [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
+    hr; m ser status systemd-resolved | cat || :
+    hr; m systemd-resolve --status
+  fi
+
 }
 rcat() {
   resolvcat | less
 }
+reresolv() {
+  sudo nscd -i hosts
+  #sudo systemctl restart dnsmasq
+}
 
 # only run on MAIL_HOST. simpler to keep this on one system.
 r2eadd() { # usage: name url
@@ -860,14 +923,14 @@ spd() {
 }
 
 spend() {
-  s systemctl suspend
+  sudo systemctl suspend
 }
 
 # ssh, copy my universal config over if needed.
 
 # By default .bashrc is sourced for ALL ssh commands. This is wonky.
 # Normally, this file is not sourced when a script is run, but we can
-# override that by having #!/bin/bash -l.  I want the same thing for ssh
+# override that by sourcing ~/.bashrc.  I want the same thing for ssh
 # commands. when a local script runs an ssh command, bashrc should not be
 # sourced, unless we use a modified command.
 #
@@ -936,13 +999,15 @@ sl() {
     type=${tmp2: -1}
     if [[ $type == b ]]; then
       info_sec=${tmp::10}
-      if (( $(stat -c%Y /b/ds/brc) > info_sec || $(stat -c%Y /b/ds/.bashrc) > info_sec )); then
-        dorsync=true
-        rm -f $sshinfo
-      fi
+      for f in /b/ds/sl/.iank/*; do
+        if (( $(stat -L -c%Y $f) > info_sec )); then
+          dorsync=true
+          rm -f $sshinfo
+          break
+        fi
+      done
     fi
   else
-    dorsync=true
     # use this weird yes thing to ensure we know ssh succeeded
     if ! tmp=$(command ssh "${args[@]}" "$remote" "if test -e /a/bin/ds/.bashrc -a -L .bashrc; then echo yes; fi"); then
       echo failed sl test. doing plain ssh -v
@@ -951,13 +1016,12 @@ sl() {
     if [[ $tmp == yes ]]; then
       type=a
     else
+      dorsync=true
       type=b
     fi
   fi
-  if [[ $type == b ]] && $dorsync; then
-    if ! RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/sl/.iank "$remote":; then
-      echo WARNING: rsync failed. remove $sshinfo to try again
-    fi
+  if $dorsync; then
+    RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/sl/.iank "$remote":
   fi
   if $dorsync || ! $haveinfo; then
     sshinfo=/p/sshinfo/$now$type"$remote"
@@ -965,9 +1029,6 @@ sl() {
     chmod 666 $sshinfo
   fi
   if [[ $type == b ]]; then
-    if $dorsync; then
-      RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/sl/.iank "$remote":
-    fi
     if (( ${#@} )); then
 
       # Theres a couple ways to do this. im not sure whats best,
@@ -986,7 +1047,7 @@ sl() {
     if [[ -t 0 ]]; then
       BRC=t command ssh "${args[@]}" "$remote" ${@@Q}
     else
-      command ssh "${args[@]}" "$remote" BRC=t bash -l
+      command ssh "${args[@]}" "$remote" BRC=t bash
     fi
   fi
 }
@@ -995,7 +1056,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
@@ -1129,13 +1190,13 @@ vpn() {
 
   [[ $1 ]] || { echo need arg; return 1; }
   journalctl --unit=$vpn_service@$1 -f -n0 &
-  s systemctl start $vpn_service@$1
+  sudo systemctl start $vpn_service@$1
   # sometimes the ask-password agent does not work and needs a delay.
   sleep .5
   # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779240
   # noticed around 8-2017 after update from around stretch release
   # on debian testing, even though the bug is much older.
-  s systemd-tty-ask-password-agent
+  sudo systemd-tty-ask-password-agent
 }
 
 vpnoff() {
@@ -1145,7 +1206,7 @@ vpnoff() {
   else
     local vpn_service=openvpn
   fi
-  s systemctl stop $vpn_service@$1
+  sudo systemctl stop $vpn_service@$1
 }
 vpnoffc() { # vpn off client
   ser stop openvpn-nn@client
@@ -1169,6 +1230,8 @@ wtr() { curl wttr.in/boston; }
 
 xevkb() { xev -event keyboard; }
 
+ziva() { e "toot! i love dancing. fart"; }
+
 # * misc stuff
 
 # from curl cheat.sh/:bash_completion