mostly fixes for new error handling
authorIan Kelling <ian@iankelling.org>
Tue, 5 Nov 2019 20:56:46 +0000 (15:56 -0500)
committerIan Kelling <ian@iankelling.org>
Tue, 5 Nov 2019 21:32:45 +0000 (16:32 -0500)
23 files changed:
brc
brc2
conflink
demohost-mount
desktop-20-autostart.sh
disabled/kodi-setup
disabled/phabricator-setup
distro-begin
distro-end
distro-pkgs
dsremote
dynamic-ip-update
fsf-vpn-dns-cleanup
input-setup
machine_specific/kd/filesystem/etc/cron.daily/check-lets-encrypt-ssl-settings
mail-route
mail-setup
music-tag-sync
sl/.iank/err [new symlink]
ssh-emacs-setup
switch-mail-host
trusted-network
untrusted-network

diff --git a/brc b/brc
index e8737aaef201894a24cc29ac2dfe1fde45b6ab8b..f9439887573b4f0eed84950526c8ada10ae265f6 100644 (file)
--- a/brc
+++ b/brc
@@ -3,7 +3,14 @@
 # SPDX-License-Identifier: AGPL-3.0-or-later
 # this gets sourced. shebang is just for file mode detection
 
-source /a/bin/errhandle/err
+# Use source ~/.bashrc instead of doing bash -l when running a script
+# so this can set extdebug and avoid the bash debugger.
+if [[ -s /a/bin/errhandle/err ]]; then
+  source /a/bin/errhandle/err
+elif [[ -s ~/.iank/err ]]; then
+  # shellcheck source=/a/bin/errhandle/err
+  source ~/.iank/err
+fi
 
 # * settings
 
@@ -195,12 +202,13 @@ if [[ -s /usr/share/wcd/wcd-include.sh ]]; then
 fi
 
 if [[ -s /a/bin/small-misc-bash/ll-function ]]; then
-  # shellcheck source=/a/bin/small-misc-bash/ll-function
   source /a/bin/small-misc-bash/ll-function
 elif [[ -s ~/.iank/ll-function ]]; then
+  # shellcheck source=/a/bin/small-misc-bash/ll-function
   source ~/.iank/ll-function
 fi
 
+
 # * functions
 
 
@@ -295,7 +303,7 @@ caf() {
   find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
        -o -name .hg -prune -o -name .editor-backups -prune \
        -o -name .undo-tree-history -prune \) \
-       -exec bash -lc 'hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
+       -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
 
 }
 
@@ -488,7 +496,7 @@ eless() {
   less /var/log/exim4/mainlog
 }
 eqcat() {
-  exiqgrep -i | while read i; do
+  exiqgrep -i | while read -r i; do
     exim -Mvh $i; hr; exim -Mvb $i; hr;
     exigrep $i /var/log/exim4/mainlog; hr
   done
@@ -624,10 +632,12 @@ gr() {
 }
 
 grr() { # grep recursive
+  # Don't return 1 on nonmatch because this is meant to be
+  # interactive, not in a conditional.
   if [[ ${#@} == 1 ]]; then
-    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" .
+    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" . || [[ $? == 1 ]]
   else
-    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@"
+    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" || [[ $? == 1 ]]
   fi
 }
 rg() {
@@ -636,7 +646,7 @@ rg() {
 
 hr() { # horizontal row. used to break up output
 
-  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo {1..${COLUMNS:-60}})
+  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
   echo
 }
 
@@ -836,6 +846,23 @@ pk1() {
   esac
 }
 
+psg () {
+  local x y help
+  help="Usage: psg [--help] GREP_ARGS
+grep ps and output in a nice format"
+  if [[ $1 == --help ]]; then
+    echo "$help"
+    return
+  fi
+  x=$(sudo ps -eF)
+  # final grep is because some commands tend to have a lot of trailing spaces
+  y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') ||:
+  if [[ $y ]]; then
+    echo "$x" | head -n 1
+    echo "$y"
+  fi
+}
+
 pubip() { curl -4s https://icanhazip.com; }
 pubip6() { curl -6s https://icanhazip.com; }
 whatismyip() { pubip; }
@@ -866,6 +893,7 @@ q() { # start / launch a program in the backround and redir output to null
 # shellcheck disable=SC2120
 r() {
   history -a # save history
+  trap ERR # this avoids a segfault
   exit ${1:0}
   # i had this redir, not sure why
   #  exit "$@" 2>/dev/null
@@ -919,6 +947,7 @@ s() {
   # with root owned files.
   #
   if [[ $EUID != 0 || $1 == -* ]]; then
+    # shellcheck disable=SC2034
     SUDOD="$PWD" sudo -i "$@"
   else
     "$@"
diff --git a/brc2 b/brc2
index 959e892562f485afde4748c71cd66456c23f89ab..df695d2157336734a926e88f940bc0e730b2b1a4 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
@@ -236,8 +235,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
@@ -867,7 +867,7 @@ spend() {
 
 # 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,10 +936,13 @@ 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
@@ -986,7 +989,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
 }
index 093dc2d725cb4901221e0daf4266c7b2069d8b1e..55d7a02489dc6b1d1ae3cbf7d0f3152778d73215 100755 (executable)
--- a/conflink
+++ b/conflink
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 source /a/bin/errhandle/err
-_errcatch_cleanup() {
+errcatch-cleanup() {
   echo 1 >~/.local/conflink
 }
 
index 706a516540d6d9f4b39946fb195229d4c08463b3..90d2b42497cedfa56e7420e8a74a235c49a7935e 100755 (executable)
@@ -1,7 +1,5 @@
-#!/bin/bash -l
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
-
+#!/bin/bash
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 
 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
 
index 03be1c58356cb5c0d063977214a6b8bc2c871560..ed83642aa02c9a9173695cd846da4e7e7155fb6e 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 # first 2 alternatives showed under ubuntu 14.04, second 2 under arch at 11/2015
-
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 if [[ $1 ]]; then
     right_monitor_rotation=left
 else
index ef4a4f8923497f8bab78b6d5ef5c096210cd90d8..6a4f8f8143d58defa6acb19ed37cf79f4589fafe 100644 (file)
@@ -1,6 +1,7 @@
-#!/bin/bash -l
+#!/bin/bash
 
 # this is from distro-end
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 
 pi kodi
 
index ebd93606f752f8d688990d7d126bf7699f185cb5..212ac62f7c76f9362c6d916bd935053869ce1b1f 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,8 +18,7 @@
 # Automated phabricator setup. Not currently using it,
 # but it worked last time I tried it.
 
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 
 set -x
 
index 0377b1c1526ffe9409d5b4b57c98c615cf96337a..75b9b05942ce0ae042d0cd3b188bdfcb2f1a0317 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -44,9 +44,9 @@
 
 
 ### make ssh interactive shell run better. for when running line interactively line by line
-sudo bash -c 'source /a/c/.bashrc && source /a/exe/ssh-emacs-setup'
-
+sudo bash -c '/a/exe/ssh-emacs-setup'
 
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 ##### setup error handling
 interactive=true  # set this to false to force set -x
 [[ $- == *i* ]] || interactive=false
@@ -55,7 +55,7 @@ if ! $interactive; then
 fi
 source /a/bin/errhandle/err
 
-_errcatch_cleanup() {
+errcatch-cleanup() {
   echo 1 >~/.local/distro-begin
 }
 
index f8419aa886069d35d5760902ceecf542cbd53ab5..a057d779b672c992f2707b855316975d819fcd09 100755 (executable)
@@ -1,6 +1,7 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2019 Ian Kelling
 # SPDX-License-Identifier: AGPL-3.0-or-later
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 
 ### setup
 source /a/bin/errhandle/err
@@ -11,7 +12,7 @@ if [[ $EUID == 0 ]]; then
   exit 1
 fi
 
-_errcatch_cleanup() {
+errcatch-cleanup() {
   echo 1 >~/.local/distro-end
 }
 
index 5fdd13c2aea6697797e144dbb4bd5b04bc4c2ed5..b69af1a68933b2a0573702924cb428b5e5615bb4 100755 (executable)
@@ -1,6 +1,7 @@
-#!/bin/bash -l
-
-source /a/bin/errhandle/err
+#!/bin/bash
+# Copyright (C) 2019 Ian Kelling
+# SPDX-License-Identifier: AGPL-3.0-or-later
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 
 
 usage() {
index 2557b73c76ca4c5e68a0f55fd78fad2e21e88637..13a5176b05311abfb841dc303c4597bc13919bc9 100755 (executable)
--- a/dsremote
+++ b/dsremote
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
+
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
index 1f55f4c458145c6c600d6910a1108aee4521fd21..e589f15dddfbc5765f4f3684d8cf12e310383937 100755 (executable)
@@ -1,6 +1,5 @@
-#!/bin/bash -l
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+#!/bin/bash
+source ~/.bashrc
 
 fqdn=$(hostname -f)
 
index be7fb582a99248b01d5a25490ee26f10ee2be6e5..d9e755c126e6e2491569bc1c37abec3195da174f 100755 (executable)
@@ -6,6 +6,6 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 if ! echo | /a/exe/cedit fsf /etc/dnsmasq-servers.conf; then
   if systemctl is-active dnsmasq >/dev/null; then
     nscd -i hosts
-    systemctl reload dnsmasq
+    systemctl restart dnsmasq
   fi
 fi
index ba390dad1adf5e2ca5747bf430c5c53c6ba16bfd..6c94a540b7920dd7fe56d435bdf9d097ebeb98b6 100755 (executable)
@@ -1,4 +1,5 @@
-#!/bin/bash -l
+#!/bin/bash
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 set -x
 # Copyright (C) 2016 Ian Kelling
 
index 87e45c021940d716cee3a8bcdc3c863c8932c3a7..a3fbfc6b7da014e0df98d2f21b86148b4c642fdd 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+source ~/.bashrc
+
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
index 332f1edbf592728bd916dedb2816c50b3c67c20a..2dc237da63c3b563716d1c9616e5d07c1ebb4a63 100755 (executable)
@@ -60,7 +60,7 @@ up() {
     fi
   fi
   e() { echo "$0: $*"; "$@"; }
-  _errcatch_cleanup=stop
+  errcatch-cleanup() { stop; }
   modify
   # we leave it as is even when stopping, because we would like it to be default, but the only way
   # to change the default is for every device, and I want to avoid that, even though I wouldn't mind, others users of this script might.
index 68478bec55a07860a6c86d82ec3cefaae97704c9..98d5c415ae931655f906570ba4bdbca7862d0fe2 100755 (executable)
@@ -676,7 +676,7 @@ EOF
 server=/mail.iankelling.org/127.0.1.1
 EOF
     if systemctl is-active dnsmasq >/dev/null; then
-      m systemctl reload dnsmasq
+      m systemctl restart dnsmasq
       m nscd -i hosts
     fi
 
@@ -810,7 +810,7 @@ EOF
     echo | /a/exe/cedit mail /etc/dnsmasq-servers.conf || [[ $? == 1 ]]
     if systemctl is-active dnsmasq >/dev/null; then
       m nscd -i hosts
-      m systemctl reload dnsmasq
+      m systemctl restart dnsmasq # reload does not ensure new config is used
     fi
 
     m systemctl disable mailclean.timer &>/dev/null ||:
index 0f6e7d98e48d4c49accd9681c3506f5909d1c06e..06248e02175b7b35880137b6355c50e9016a8cd5 100644 (file)
@@ -1,6 +1,7 @@
-#!/bin/bash -l
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+#!/bin/bash
+# Copyright (C) 2019 Ian Kelling
+# SPDX-License-Identifier: AGPL-3.0-or-later
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
 # cd /k/music
 # find -type f -name '*.flac' | while read -r f; do
 #     mkdir -p "../flacs/$(dirname "$f")"
diff --git a/sl/.iank/err b/sl/.iank/err
new file mode 120000 (symlink)
index 0000000..c877367
--- /dev/null
@@ -0,0 +1 @@
+/a/bin/errhandle/err
\ No newline at end of file
index 80976f056df0eb22e1f135f42e4b6c4b180d6de6..65679fde3459fc7f6b833cfe52b70e99cf7573bb 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +17,8 @@ if [[ $EUID != 0 ]]; then
     sudo "$0"
     exit
 fi
+if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi
+
 
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
index a0c590d0ae8b3287715584cda9eaf26119eb8c15..8f0912f7246e1e9fd62af985c8365215f999bda1 100755 (executable)
@@ -24,7 +24,7 @@ EOF
 
 restore_new_btrbk=false
 restore_old_btrbk=false
-cleanup() {
+errcatch-cleanup() {
   if $restore_new_btrbk; then
     e WARNING: due to failure, btrbk.timer may need manual restoration:
     e $new_shell sudo systemctl start btrbk.timer
@@ -100,12 +100,10 @@ new_hostname=$($new_shell hostname)
 
 if $new_shell systemctl is-active btrbk.timer; then
   m $new_shell sudo systemctl stop btrbk.timer
-  _errcatch_cleanup=cleanup
   restore_new_btrbk=true
 fi
 if $old_shell systemctl is-active btrbk.timer; then
   m $old_shell sudo systemctl stop btrbk.timer
-  _errcatch_cleanup=cleanup
   restore_old_btrbk=true
 fi
 
@@ -150,13 +148,19 @@ EOF
 # ipv6 i think.
 
 
+if ! m btrbk-run -v $bbk_args $mp_args; then
+  ret=$?
+  err "failed initial btrbk"
+  exit $ret
+fi
+
 
 m $old_shell /a/exe/primary-setup $new_hostname
 
-if ! m btrbk-run -v $bbk_args $mp_args; then
+if ! m btrbk-run -v $bbk_args -m /o; then
   ret=$?
   bang="$(printf "$(tput setaf 5)█$(tput sgr0)%.0s" 1 2 3 4 5 6 7)"
-  e $bang failed btrbk. restoring old host as primary
+  e $bang failed btrbk of /o. restoring old host as primary
   m $old_shell /a/exe/primary-setup localhost
   exit $ret
 fi
index 220c963c41cbcfde7d7b44d57900b6256c2e969c..7cd754cf861476854f5ab6fe69f7e076b37da98a 100755 (executable)
@@ -12,5 +12,5 @@ if [[ -e /etc/dnsmasq.d/untrusted-network.conf ]]; then
   rm -f /etc/dnsmasq.d/untrusted-network.conf
 
   nscd -i hosts
-  systemctl reload dnsmasq
+  systemctl restart dnsmasq
 fi
index 9cbf4ab37c1d7b13530827b2888acdaf68ceef41..b4fdb70e0e5304af943e683d34d0074d77c56e2b 100755 (executable)
@@ -20,5 +20,5 @@ stop-dns-rebind
 EOF
 
   nscd -i hosts
-  systemctl reload dnsmasq
+  systemctl restart dnsmasq
 fi