various fixes
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index fb14eaa76a854fda26146cf4f356126078620606..220f1241890f111bbf114f9fc5e1685342f2a768 100644 (file)
--- a/brc2
+++ b/brc2
@@ -54,6 +54,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,6 +93,20 @@ 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
+s rm -fv $f.jnl
+s 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
   c /
@@ -123,16 +152,17 @@ lipush() {
   local p a
   p=(/a/bin /a/exe /a/h /a/c /p/c/machine_specific/linode{,.hosts} /a/opt/{emacs,emacs-debianstable,mu})
   a="-ahviSAXPH --specials --devices --delete --relative --exclude-from=/p/c/li-rsync-excludes"
-  rsync $@ $a ${p[@]} root@l2.b8.nz:/
-  rsync $@ $a ${p[@]} /p/c/machine_specific/li root@iankelling.org:/
-  rsync $@ -ahviSAXPH root@iankelling.org:/a/h/proposed-comments/ /a/h/proposed-comments
+  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
@@ -233,6 +263,33 @@ 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
 }
+dig() {
+  command dig +nostats +nocmd "$@"
+}
+# Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
+digsort() {
+  local sec
+  sec=
+  dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do
+    if [[ $l == [^\;]* ]]; then
+      sec+="$l"$'\n'
+    else
+      if [[ $sec ]]; then
+        printf "%s" "$sec" | sort
+        sec=
+      fi
+      printf "%s\n" "$l"
+    fi
+  done
+}
+# note: only the soa master nameserver will respond with
+# ra "recursive answer" flag. That difference is meaningless afaik.
+# Same thing happens with gnu nameservers.
+digme() {
+  digsort "$@" @ns1.iankelling.org | tee /tmp/digme
+  diff -u /tmp/digme <(digsort "$@" @ns2.iankelling.org)
+}
+
 
 dup() {
   local ran_d
@@ -802,11 +859,33 @@ spend() {
 }
 
 # 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
+# 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)
 
@@ -835,53 +914,59 @@ 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}
+      if (( $(stat -c%Y /b/ds/brc) > info_sec || $(stat -c%Y /b/ds/.bashrc) > info_sec )); then
+        dorsync=true
+        rm -f $sshinfo
+      fi
     fi
   else
+    dorsync=true
     # 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
+    tmp=$(command ssh "${args[@]}" "$remote" "if test -e /a/bin/ds/.bashrc -a -L .bashrc; then echo yes; fi") || return
     if [[ $tmp == yes ]]; then
       type=a
     else
       type=b
-      old=true
     fi
   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
+    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,
       # 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 '"\"\$@\""' bash ${@@Q}
     else
-      command ssh -t "${args[@]}" "$remote" "INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc"
+      # -t gives us an interactive shell for normal ssh. -l makes us use the rcfile when piping commands.
+      command ssh -t "${args[@]}" "$remote" BRC=t INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc -l
     fi
   else
-    BASH_LOGIN_SHELL=true command ssh "$remote" "$@"
+    BRC=t command ssh "$remote" "$@"
   fi
 }
 sss() { # ssh solo
@@ -899,7 +984,7 @@ ssk() {
 }
 # plain limited ssh
 ssh() {
-  BASH_LOGIN_SHELL=true command ssh "$@"
+  BRC=t command ssh "$@"
 }