updates, especially for etiona
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index 7c33bea3251ebbd08cea977f6b72dff156be36d2..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
@@ -54,6 +53,21 @@ anki() {
   fi
 }
 
+acat() {
+  shopt -s nullglob
+  hrcat /m/md/alerts/new/* /m/md/alerts/cur/*
+  shopt -u nullglob
+}
+aclear() {
+  shopt -s nullglob
+  files=(/m/md/alerts/new/* /m/md/alerts/cur/*)
+  if (( ${#files[@]} )); then
+    rm -f ${files[@]}
+  fi
+  shopt -u nullglob
+  system-status _
+}
+
 ap() {
   # pushd in case current directory has an ansible.cfg file
   pushd /a/xans >/dev/null
@@ -78,23 +92,46 @@ astudio() {
   /a/opt/android-studio/bin/studio.sh "$@" &r;
 }
 
+bindpush() {
+  lipush || return 1
+  for h in li l2; do
+    sl $h <<'EOF' || return 1
+set -e
+conflink
+f=/var/lib/bind/db.b8.nz
+ser stop bind9
+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"
-  $active && ser enable btrbk.timer
+  jrun -p btrbk btrbk-run "$@"
+  if $active; then
+    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 "$@"; }
@@ -103,37 +140,87 @@ bigclock() {
   xclock -digital -update 1 -face 'arial black-80:bold'
 }
 
-bpull() {
-  [[ $1 ]] || return 1
-  c /
-  # run latest
-  install-my-scripts
-  switch-mail-host $1 $HOSTNAME | 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 $HOSTNAME $1 | pee cat "systemd-cat -t switch-mail-host"
+  jrun -p btrbk switch-mail-host "$@"
+  return $ret
 }
+
 lipush() {
-  rsync $@ --delete-excluded -ahviSAXPH --specials --devices --delete --relative \
-        --exclude-from=/p/c/li-rsync-excludes /a/bin /a/exe /a/h /a/c /p/c/machine_specific/li /a/opt/{emacs,emacs-debianstable,mu} root@li:/
-}
-lipushnoe() {
-  rsync $@ --delete-excluded -ahviSAXPH --specials --devices --delete --relative \
+  # 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-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=$?
+  m rsync "$@" $a ${p[@]} /p/c/machine_specific/li root@li.b8.nz:/ || ret=$?
+  m rsync "$@" -ahviSAXPH root@iankelling.org:/a/h/proposed-comments/ /a/h/proposed-comments || ret=$?
+  return $ret
+}
+lipushnoe() { # noe = noemacs. for running faster.
+  rsync "$@" --delete-excluded -ahviSAXPH --specials --devices --delete --relative \
         --exclude-from=/p/c/li-rsync-excludes /a/bin /a/exe /a/h /a/c /p/c/machine_specific/li  root@li:/
 }
 
-
+#### begin bitcoin related things
 btc() {
   local f=/etc/bitcoin/bitcoin.conf
   # importprivkey will timeout if using the default of 15 mins.
   # upped it to 1 hour.
   bitcoin-cli -rpcclienttimeout=60000 -$(s grep rpcuser= $f) -$(s grep rpcpassword= $f) "$@"
 }
-
 btcusd() { # $1 btc in usd
   local price
   price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
@@ -160,6 +247,7 @@ satoshi() { # $1 satoshi in usd
     printf "$%.2f\n" "$(echo "scale=10; $price * $1"| bc -l)"
   fi
 }
+#### end bitcoin related things
 
 
 
@@ -195,14 +283,16 @@ chrome() {
 }
 
 
-dat() { # do all tee, for more complex scripts
-  tee >(ssh frodo bash -l) >(bash -l) >(ssh x2 bash -l) >(ssh tp bash -l)
+# do all tee.
+# pipe to this, or just type like a shell
+# todo: test this
+dat() {
+  tee >(ssh frodo.b8.nz) >(ssh x2) >(ssh tp.b8.nz) >(ssh kw) >(ssh tp.b8.nz)
 }
 da() { # do all
   local host
-  "$@"
-  for host in x2 tp kd; do
-    ssh $host $(printf "")
+  for host in x2 kw tp.b8.nz x3.b8.nz frodo.b8.nz; do
+    ssh $host "$@"
   done
 }
 
@@ -226,8 +316,36 @@ debian_pick_mirror () {
   sudo sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc/apt/sources.list
   sudo apt-get update
 }
-
-
+digme() {
+  digdiff @ns{1,2}.iankelling.org "$@"
+}
+
+
+dup() {
+  local ran_d
+  ran_d=false
+  system-status _
+  case $PS1 in
+    *DISTRO-BEGIN!*|*DISTRO!*)
+      pushd /
+      /b/ds/distro-begin || return $?
+      popd
+      ran_d=true
+      ;;&
+    *DISTRO-END!*|*DISTRO!*)
+      pushd /
+      /b/ds/distro-end || return $?
+      popd
+      ran_d=true
+      ;;&
+    *CONFLINK*)
+      if ! $ran_d; then
+        conflink
+      fi
+      ;;
+  esac
+  system-status _
+}
 
 envload() { # load environment from a previous: export > file
   local file=${1:-$HOME/.${USER}_env}
@@ -238,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() {
@@ -316,11 +436,15 @@ fdroid_pkgs=(
 fdup() {
   local -A installed updated
   local p
-  fdroidcl update
+  # tried putting this in go buildscript cronjob,
+  # but it failed with undefined: os.UserCacheDir. I expect its due to
+  # an environment variable missing, but its easier just to stick it here.
+  m go get -u mvdan.cc/fdroidcl || return 1
+  m fdroidcl update
   if fdroidcl search -u | grep ^org.fdroid.fdroid; then
     fdroidcl install org.fdroid.fdroid
     sleep 5
-    fdroidcl update
+    fdroidcl update
   fi
   for p in $(fdroidcl search -i| grep -o "^\S\+"); do
     installed[$p]=true
@@ -330,14 +454,14 @@ fdup() {
   done
   for p in ${fdroid_pkgs[@]}; do
     if ! ${installed[$p]:-false}; then
-      fdroidcl install $p
+      fdroidcl install $p
       # sleeps are just me being paranoid since replicant has a history of crashing when certain apps are installed
       sleep 5
     fi
   done
   for p in ${!installed[@]}; do
     if ! ${updated[$p]:-true}; then
-      fdroidcl install $p
+      fdroidcl install $p
       sleep 5
     fi
   done
@@ -447,12 +571,10 @@ fw() {
   firefox -P default "$@" >/dev/null 2>&1
 }
 
-
 gitian() {
   git config user.email ian@iankelling.org
 }
 
-
 # at least in flidas, things rely on gpg being gpg1
 gpg() {
   command gpg2 "$@"
@@ -467,7 +589,6 @@ gse() {
       --suppress-cc=self "$@"
 }
 
-
 hstatus() {
   # do git status on published repos.
   c /a/bin/githtml
@@ -483,11 +604,16 @@ hstatus() {
   done
 }
 
-
 idea() {
   /a/opt/idea-IC-163.7743.44/bin/idea.sh "$@" &r
 }
 
+ilog() {
+  chan=${1:-#fsfsys}
+  # use * instead of -r since that does sorted order
+  ssh root@iankelling.org "cd /var/lib/znc/moddata/log/iank/freenode/$chan && hr && for x in *; do echo \$x; cat \$x; hr; done" | less +G
+}
+
 o() {
   if type gvfs-open &> /dev/null ; then
     gvfs-open "$@"
@@ -505,7 +631,7 @@ jtail() {
   journalctl -n 10000 -f "$@" | jfilter
 }
 jr() { journalctl "$@" | jfilter | less ; }
-jrf() { journalctl -f "$@" | jfilter; }
+jrf() { journalctl -n 200 -f "$@" | jfilter; }
 
 
 kff() { # keyboardio firmware flash
@@ -571,32 +697,35 @@ mdt() {
   firefox /tmp/mdtest.html
 }
 
-
-
 mo() { xset dpms force off; } # monitor off
 
+myirc() {
+  chan=${1:-fsf-office}
+  # use * instead of -r since that does sorted order
+  ssh root@iankelling.org "cd /var/lib/znc/moddata/log/iank/freenode/#$chan; grep '\<iank.*' *"
+}
+
 net-dev-info() {
   e "lspci -nnk|gr -iA2 net"
   lspci -nnk|gr -iA2 net
   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() {
@@ -669,19 +798,37 @@ pumpa() {
 #rbp() { rbt post -o "$@"; }
 
 rebr() {
-  s ifdown br0
-  s ifup br0
+  sudo ifdown br0
+  sudo ifup br0
 }
 
 resolvcat() {
   local f
+  m s nscd -i hosts
   f=/etc/resolv.conf
   echo $f:;  ccat $f
-  hr; echo dnsmasq is $(systemctl is-active dnsmasq)
-  f=/var/run/dnsmasq/resolv.conf
-  hr; echo $f:;  ccat $f
-  f=/etc/dnsmasq-servers.conf
-  hr; echo $f:;  ccat $f
+  hr; s ss -lpn 'sport = 53'
+  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.
@@ -729,6 +876,10 @@ scssl() {
   scss-lint -c /a/opt/thoughtbot-guides/style/sass/.scss-lint.yml "$@"
 }
 
+skbrc() {
+  sk -e 2120,245 /b/ds/brc /b/ds/brc2
+}
+
 skaraoke() {
   local tmp out
   out=${2:-${1%.*}.sh}
@@ -772,15 +923,37 @@ 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 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.
+#
+# So, in my bashrc, test for conditions of noninteractive ssh and return
+# if so.  And we don't keep the rest of the code in .bashrc, because
+# even though we return, we parse the whole file which can cause errors
+# as we develop it.
+#
+# To test for an overriding condition: bash builtin vars and env show no
+# difference in ssh vs local, except shell level which is not
+# reliable. one option is to use an environment variable. env variables
+# sent across ssh are strictly limited. We could override an obscure
+# unused LC_var, like telephone, but I don't want to run into some edge
+# case where that messes things up. I choose to set SendEnv and
+# AcceptEnv ssh config vars to allow the environment variable BRC to
+# propagate across ssh, and for hosts I don't control, I start an inner
+# shell with it set, which doubles up as a way to have a nondefault
+# bashrc.
 sl() {
   # inspired from https://github.com/Russell91/sshrc
 
 
-  local args info_date info_t type now tmp old sshinfo
+  local args info_date info_t type now tmp tmp2 old sshinfo cmd haveinfo dorsync info_sec
   declare -a args tmpa
   now=$(date +%s)
 
@@ -795,7 +968,7 @@ sl() {
       -[1246AaCfGgKkMNnqsTtVvXxYy])
         args+=("$1"); shift
         ;;
-      -[bcDEeFIiLlmOopQRSWw])
+      -[bcDEeFIiLlmOopQRSWw]*)
         # -oOption etc is valid
         if (( ${#1} >= 3 )); then
           args+=("$1"); shift
@@ -809,71 +982,86 @@ sl() {
     esac
   done
   remote="$1"; shift
-  old=false
+  if [[ ! $remote ]]; then
+    echo $0: error hostname required >&2
+    return 1
+  fi
+  dorsync=false
+  haveinfo=false
   tmpa=(/p/sshinfo/???????????"$remote")
   sshinfo=${tmpa[0]}
   if [[ -e $sshinfo ]]; then
+    haveinfo=true
+  fi
+  if $haveinfo; then
     tmp=${sshinfo[0]##*/}
-    info_date=${tmp::11}
-    type=${info_date: -1}
-    info_date=${info_date::10}
-    info_sec=$(date -d @$info_date +%s)
-    # debug
-    #e $(( $(stat -c%Y /b/ds/brc) - $(date -d @$info_date +%s) ))
-    if (( $(stat -c%Y /b/ds/brc) > info_sec || $(stat -c%Y /b/ds/.bashrc) > info_sec )); then
-      old=true
+    tmp2=${tmp::11}
+    type=${tmp2: -1}
+    if [[ $type == b ]]; then
+      info_sec=${tmp::10}
+      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
     # use this weird yes thing to ensure we know ssh succeeded
-    tmp=$(command ssh "${args[@]}" "$remote" "if test -e /p/sshinfo; then echo yes; fi") || return
+    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
+      command ssh -v "${args[@]}" "$remote"
+    fi
     if [[ $tmp == yes ]]; then
       type=a
     else
+      dorsync=true
       type=b
-      old=true
     fi
   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"
+    touch $sshinfo
+    chmod 666 $sshinfo
+  fi
   if [[ $type == b ]]; then
-    if $old; then
-      RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/.iank "$remote":
-      rm -f $sshinfo
-      sshinfo=/p/sshinfo/$now$type"$remote"
-      touch $sshinfo
-      chmod 666 $sshinfo
-    fi
     if (( ${#@} )); then
 
       # Theres a couple ways to do this. im not sure whats best,
       # but relying on bash 4.4+ escape quoting seems most reliable.
-
-      command ssh -t "${args[@]}" "$remote" "INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc -c ${@@Q}"
-      # this way is bad
-      #      command ssh -t "${args[@]}" "$remote" "printf \"%s; exit\" \"$*\" >.iank/brc2
-      #INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc"
-
+      command ssh "${args[@]}" "$remote" \
+              BRC=t bash -c '.\ .iank/.bashrc\;"\"\$@\""' bash ${@@Q}
+    elif [[ ! -t 0 ]]; then
+      # This case is when commands are being piped to ssh.
+      # Normally, no bashrc gets sourced.
+      # But, since we are doing all this, lets source it because we can.
+      cat <(echo . .iank/.bashrc) - | command ssh "${args[@]}" "$remote" BRC=t bash
     else
-      command ssh -t "${args[@]}" "$remote" "INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc"
+      command ssh -t "${args[@]}" "$remote" BRC=t INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc
     fi
   else
-    BASH_LOGIN_SHELL=true command ssh "$remote" "$@"
+    if [[ -t 0 ]]; then
+      BRC=t command ssh "${args[@]}" "$remote" ${@@Q}
+    else
+      command ssh "${args[@]}" "$remote" BRC=t bash
+    fi
   fi
 }
 sss() { # ssh solo
-  ssh -oControlMaster=no -oControlPath=/ "$@"
+  sl -oControlMaster=no -oControlPath=/ "$@"
 }
 # kill off old shared socket then ssh
 ssk() {
-  local -a opts=()
-  while [[ $1 == -* ]]; do
-    opts+=("$1")
-    shift
-  done
-  m pkill -f "^ssh: /tmp/ssh_mux_${USER}_${1#*@}_22_"
-  m ssh "${opts[@]}" "$@"
+  m ssh -O exit "$@" || [[ $? == 255 ]]
+  m sl "$@"
 }
 # plain limited ssh
 ssh() {
-  BASH_LOGIN_SHELL=true command ssh "$@"
+  BRC=t command ssh "$@"
 }
 
 
@@ -1002,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() {
@@ -1018,10 +1206,14 @@ 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
+}
+vpnc() {
+  ser start openvpn-nn@client
 }
-
-
 
 
 vspicy() { # usage: VIRSH_DOMAIN
@@ -1038,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