From 44d46b0cffd2a597ed9539c0b97dbe62257d58c9 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Thu, 3 Sep 2020 20:20:23 -0400 Subject: [PATCH] mostly improve sl for remote emacs --- .bashrc | 20 +-- .gitignore | 2 + brc | 143 +++++++++++++++----- brc2 | 74 ++++++++++ distro-end | 70 ++++------ path-add-function | 2 +- pkgs | 1 + sl/.iank/path-add-function | 1 + sl/.ianketiona/.bashrc | 1 + sl/.ianketiona/.emacs.d | 1 + sl/.ianketiona/.inputrc | 1 + sl/.ianketiona/brc | 1 + sl/.ianketiona/err | 1 + sl/.ianketiona/info | 1 + sl/.ianketiona/lib-src | 1 + sl/.ianketiona/lisp | 1 + sl/.ianketiona/ll-function | 1 + sl/.ianketiona/package-manager-abstractions | 1 + sl/.ianketiona/path-add-function | 1 + sl/.iankflidas/.bashrc | 1 + sl/.iankflidas/.emacs.d | 1 + sl/.iankflidas/.inputrc | 1 + sl/.iankflidas/brc | 1 + sl/.iankflidas/err | 1 + sl/.iankflidas/info | 1 + sl/.iankflidas/lib-src | 1 + sl/.iankflidas/lisp | 1 + sl/.iankflidas/ll-function | 1 + sl/.iankflidas/package-manager-abstractions | 1 + sl/.iankflidas/path-add-function | 1 + sl/rsync-excludes | 17 +++ 31 files changed, 270 insertions(+), 82 deletions(-) create mode 120000 sl/.iank/path-add-function create mode 120000 sl/.ianketiona/.bashrc create mode 120000 sl/.ianketiona/.emacs.d create mode 120000 sl/.ianketiona/.inputrc create mode 120000 sl/.ianketiona/brc create mode 120000 sl/.ianketiona/err create mode 120000 sl/.ianketiona/info create mode 120000 sl/.ianketiona/lib-src create mode 120000 sl/.ianketiona/lisp create mode 120000 sl/.ianketiona/ll-function create mode 120000 sl/.ianketiona/package-manager-abstractions create mode 120000 sl/.ianketiona/path-add-function create mode 120000 sl/.iankflidas/.bashrc create mode 120000 sl/.iankflidas/.emacs.d create mode 120000 sl/.iankflidas/.inputrc create mode 120000 sl/.iankflidas/brc create mode 120000 sl/.iankflidas/err create mode 120000 sl/.iankflidas/info create mode 120000 sl/.iankflidas/lib-src create mode 120000 sl/.iankflidas/lisp create mode 120000 sl/.iankflidas/ll-function create mode 120000 sl/.iankflidas/package-manager-abstractions create mode 120000 sl/.iankflidas/path-add-function create mode 100644 sl/rsync-excludes diff --git a/.bashrc b/.bashrc index 09c5aa9..2bb1722 100644 --- a/.bashrc +++ b/.bashrc @@ -63,24 +63,24 @@ else # source brc and brc2 if they exist. We have to readlink because we # could be using sl() from ./brc. _tmp=$(readlink -f ${BASH_SOURCE[0]}) - _tmp=${_tmp%/*} - _tmp2=$_tmp/brc - if [[ -s $_tmp2 ]]; then + bashrc_dir=${_tmp%/*} + _tmp=$bashrc_dir/brc + if [[ -s $bashrc_dir ]]; then # shellcheck source=./brc - source $_tmp2 + source $_tmp fi # brc2 is for things i dont necessarily want on every system - _tmp2=$_tmp/brc2 - if [[ -s $_tmp2 ]]; then + _tmp=$bashrc_dir/brc2 + if [[ -s $_tmp ]]; then # shellcheck source=./brc2 - source $_tmp2 + source $_tmp else # This check is for when running the sl() command, # and the remote host got its type misidentified. - _tmp2=$_tmp/../brc2 - if [[ -s $_tmp2 ]]; then + _tmp=$bashrc_dir/../brc2 + if [[ -s $_tmp ]]; then # shellcheck source=./brc2 - source $_tmp2 + source $_tmp fi fi ###### End sourcing of files ##### diff --git a/.gitignore b/.gitignore index d70eb01..afa57c5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ /a/roles/grafana /a/roles/node-exporter /a/roles/alertmanager +/sl/.ianketiona/src/ +/sl/.iankflidas/src/ diff --git a/brc b/brc index 4785f14..76bcf1d 100644 --- a/brc +++ b/brc @@ -8,9 +8,9 @@ # 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 +elif [[ -s $bashrc_dir/err ]]; then # shellcheck source=/a/bin/errhandle/err - source ~/.iank/err + source $bashrc_dir/err fi # In t8, it runs clear_console for login shells by default. I don't want @@ -185,13 +185,24 @@ export SYSTEMD_LESS=$LESS export NNN_COLORS=2136 -export SL_FILES_DIR=/b/ds/sl/.iank +export SL_FILES_DIR=/b/ds/sl/basic/.iank export SL_INFO_DIR=/p/sshinfo +export SL_RSYNC_ARGS="--exclude-from=/b/ds/sl/rsync-excludes" # * include files - +if [[ -s $bashrc_dir/path-add-function ]]; then + source $bashrc_dir/path-add-function + if [[ $SSH_CLIENT && -e $bashrc_dir/src/emacs ]]; then + path-add "$bashrc_dir/lib-src" "$bashrc_dir/src" + if [[ ! -e ~/.emacs.d ]]; then + # todo: patch emacs so it will look elsewhere. this is kinda sad: + # https://emacs.stackexchange.com/questions/4253/how-to-start-emacs-with-a-custom-user-emacs-directory + ln -sf $bashrc_dir/.emacs.d ~ + fi + fi +fi # if someone exported $SOE (stop on error), catch errors. # @@ -214,9 +225,9 @@ fi if [[ -s /a/bin/small-misc-bash/ll-function ]]; then source /a/bin/small-misc-bash/ll-function -elif [[ -s ~/.iank/ll-function ]]; then +elif [[ -s $bashrc_dir/ll-function ]]; then # shellcheck source=/a/bin/small-misc-bash/ll-function - source ~/.iank/ll-function + source $bashrc_dir/ll-function fi @@ -693,6 +704,19 @@ and works in older versions of git which did not have that." echo "${p%%/.git}" } +g() { + + local $narg + if [[ $DISPLAY ]]; then + narg=-n + fi + if pgrep -u $EUID emacsclient && (( $# )); then + emacsclient -a "" $narg "$@" + else + emacsclient -a "" $narg -c "$@" + fi +} + gh() { # i got an error, gh not found when doing a pull request, it seems like it wants itself in it\'s path. local _oldpath="$PATH" @@ -1162,6 +1186,20 @@ sk() { # remote system, you can use sl --rsync, or the function for that slr # below. +# SL_TEST_CMD: Env var. Meant to be used to vary the files synced +# depending on the remote host. Run this string on the remote host the +# first time sl is run (or if we run slr). The result is passed to +# SL_TEST_HOOK. For example, +# export SL_TEST_CMD=". /etc/os-release ; echo \${VERSION//[^a-zA-Z0-9]/}" + +# SL_TEST_HOOK: Env var. It is run as $SL_TEST_HOOK. This can set +# $SL_FILES_DIR to vary the files synced. + +# SL_RSYNC_ARGS: Env var. String of arguments passed to rsync. For +# example to exclude files within a directory. Note, excluded +# files wont be deleted on rsync, you can add --delete-excluded +# to the rsync command if that is desired. + # For when ~/.bashrc is already customized on the remote server, you # might find it problematic that ~/.bashrc is sourced for ALL ssh # commands, even in scripts. This paragraph is all about that. bash @@ -1184,17 +1222,10 @@ sl() { # .bashrc. This means the outer shell still ran the default .bashrc, # but that is the best we can do. - local now args remote dorsync haveinfo tmpa sshinfo tmp tmp2 type info_sec force_rsync sync_dirname + local now args remote dorsync haveinfo tmpa sshinfo tmp tmp2 type info_sec force_rsync \ + sync_dirname testcmd extra_info testbool files_sec sl_test_cmd sl_test_hook declare -a args tmpa - if [[ ! $SL_INFO_DIR || ! $SL_FILES_DIR ]]; then - echo error: missing '$SL_' env vars >&2 - return 1 - fi - - sync_dirname=${SL_FILES_DIR##*/} - - now=$(date +%s) # ssh [-1246Antivivisectionist] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] # [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address] @@ -1215,6 +1246,33 @@ sl() { shift fi + sl_test_cmd=$SL_TEST_CMD + sl_test_hook=$SL_TEST_HOOK + sl_rsync_args=$SL_RSYNC_ARGS + while [[ $1 ]]; do + case "$1" in + --rsync) + force_rsync=true + ;; + --sl-test-cmd) + sl_test_cmd="$2" + shift + ;; + --sl-test-hook) + sl_test_hook="$2" + shift + ;; + --sl_rsync_args) + sl_rsync_args="$2" + shift + ;; + *) + break + ;; + esac + shift + done + while [[ $1 ]]; do case "$1" in # note we dont support things like -4oOption @@ -1234,12 +1292,19 @@ sl() { ;; esac done - remote="$1"; shift + remote="$1" if [[ ! $remote ]]; then echo $0: error hostname required >&2 return 1 fi + shift + if [[ ! $SL_INFO_DIR ]]; then + echo error: missing '$SL_INFO_DIR' env var >&2 + return 1 + fi + + now=$(date +%s) dorsync=false haveinfo=false tmpa=($SL_INFO_DIR/???????????"$remote") @@ -1255,36 +1320,51 @@ sl() { tmp=${sshinfo[0]##*/} tmp2=${tmp::11} type=${tmp2: -1} - if [[ $type == b ]]; then - info_sec=${tmp::10} - for f in $SL_FILES_DIR/*; do - if (( $(stat -L -c%Y $f) > info_sec )); then - dorsync=true - rm -f $sshinfo - break - fi - done - fi + extra_info=$(cat $sshinfo) else - # use this weird yes thing to ensure we know ssh succeeded - if ! tmp=$(LC_USEBASHRC=yes command ssh "${args[@]}" "$remote" "if test -e $SL_FILES_DIR/.bashrc -a -L .bashrc; then echo '$LC_USEBASHRC'; fi"); then + # we test for string to know ssh succeeded + testbool="test -e $SL_FILES_DIR/.bashrc -a -L .bashrc -a -v LC_USEBASHRC" + testcmd="if $testbool; then printf y; else printf n; fi" + if ! tmp=$(LC_USEBASHRC=y command ssh "${args[@]}" "$remote" "$testcmd; $sl_test_cmd"); then echo failed sl test. doing plain ssh -v command ssh -v "${args[@]}" "$remote" fi - if [[ $tmp == yes ]]; then + if [[ $tmp == y* ]]; then type=a else dorsync=true type=b fi + extra_info="${tmp:1}" fi + if [[ $sl_test_hook ]]; then + $sl_test_hook "$extra_info" + fi + + if $haveinfo && [[ $type == b ]]; then + info_sec=${tmp::10} + read files_sec _ < <(find -L $SL_FILES_DIR -printf "%T@ %p\n" | sort -nr || [[ $? == 141 ]]) + files_sec=${files_sec%%.*} + if (( files_sec > info_sec )); then + dorsync=true + rm -f $sshinfo + fi + fi + + sync_dirname=${SL_FILES_DIR##*/} + + if [[ ! $SL_FILES_DIR ]]; then + echo error: missing '$SL_FILES_DIR' env var >&2 + return 1 + fi + if $dorsync; then - RSYNC_RSH="ssh ${args[*]}" rsync -rptL $SL_FILES_DIR "$remote": + RSYNC_RSH="ssh ${args[*]}" m rsync -rptL --delete $sl_rsync_args $SL_FILES_DIR "$remote": fi if $dorsync || ! $haveinfo; then sshinfo=$SL_INFO_DIR/$now$type"$remote" [[ -e $SL_INFO_DIR ]] || mkdir -p $SL_INFO_DIR - touch $sshinfo + printf "%s\n" "$extra_info" >$sshinfo chmod 666 $sshinfo fi if [[ $type == b ]]; then @@ -1308,6 +1388,7 @@ sl() { command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash fi fi + # this function inspired from https://github.com/Russell91/sshrc } slr() { diff --git a/brc2 b/brc2 index aa48435..322a869 100644 --- a/brc2 +++ b/brc2 @@ -40,6 +40,80 @@ source /a/bin/log-quiet/logq-function # * functions +export SL_TEST_CMD=". /etc/os-release ; echo \${VERSION//[^a-zA-Z0-9]/}" +export SL_TEST_HOOK=slemacs + +slemacs() { + local arg + arg="$1" + case $arg in + 90Etiona) + export SL_FILES_DIR=/b/ds/sl/.ianketiona + ;; + 80Flidas) + export SL_FILES_DIR=/b/ds/sl/.iankflidas + ;; + *) + export SL_FILES_DIR=/b/ds/sl/.iank + ;; + esac +} + +slb() { # sl basic + export SL_FILES_DIR=/b/ds/sl/.iank + sl --sl-test-cmd "" --sl-test-hook "" --sl_rsync_args "" "$@" +} + + +mkschroot() { + distro=$1 + shift + case $distro in + trisquel) + repo=http://mirror.fsf.org/trisquel/ + ;; + ubuntu) + repo=http://archive.ubuntu.com/ubuntu/ + ;; + debian) + repo=http://deb.debian.org/debian/ + ;; + esac + n=$1 + shift + if schroot -l | grep -xFq chroot:$n; then + echo "$0: $n schroot already installed, skipping" + return 0 + fi + apps=($@) + d=/nocow/schroot/$n + sd /etc/schroot/chroot.d/$n.conf <