scwrypts v4.4 config adjustments; added os dependencies; removed dotwryn.env.vim references from setup; generate i3 config on setup
This commit is contained in:
2
scwrypts/remote/.config/env.template
Normal file
2
scwrypts/remote/.config/env.template
Normal file
@ -0,0 +1,2 @@
|
||||
#!/bin/zsh
|
||||
export REMOTE_TMUX_SESSION=
|
1
scwrypts/remote/.config/env.template.descriptions
Normal file
1
scwrypts/remote/.config/env.template.descriptions
Normal file
@ -0,0 +1 @@
|
||||
REMOTE_TMUX_SESSION | session name for remote connection (default 'remote')
|
6
scwrypts/remote/configure
vendored
Executable file
6
scwrypts/remote/configure
vendored
Executable file
@ -0,0 +1,6 @@
|
||||
#!/bin/zsh
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
EDIT "$REMOTE_CONNECTIONS_FILE"
|
||||
}
|
189
scwrypts/remote/connect
Executable file
189
scwrypts/remote/connect
Executable file
@ -0,0 +1,189 @@
|
||||
#!/bin/zsh
|
||||
use bastion --group remote
|
||||
use config --group remote
|
||||
#####################################################################
|
||||
|
||||
USAGE__options="
|
||||
-c, --command override configured remote command
|
||||
-s, --shell override configured remote login shell
|
||||
-t, --type one of the following connection types:
|
||||
- ssh (default) simple ssh execution
|
||||
- xserver ssh connection with remote-xserver settings
|
||||
- tmux connect directly to configured, remote tmux session
|
||||
|
||||
--no-rc disable loading the login RC for 'sh -c' commands
|
||||
--no-tty don't request a tty connection
|
||||
|
||||
--force-local-login force login through ssh when working with localhost
|
||||
|
||||
--use-bastion true / false override for bastion preference
|
||||
(default: session.name.bastion.preferred or 'false')
|
||||
"
|
||||
|
||||
USAGE__description="
|
||||
Connection / bastion wrapper for $REMOTE__TARGET. To configure,
|
||||
use 'scwrypts remote configure'
|
||||
"
|
||||
|
||||
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
[ $REMOTE__TARGET ] \
|
||||
|| ERROR 'missing REMOTE__TARGET context; this must be run through scwrypts' \
|
||||
|| return 1
|
||||
|
||||
local CONNECTION_TYPE=ssh
|
||||
local REMOTE_NAME=$REMOTE__TARGET
|
||||
local REMOTE_COMMAND LOGIN_SHELL
|
||||
local LOAD_RC=true
|
||||
local USE_TTY=true
|
||||
local FORCE_LOCAL_LOGIN=false
|
||||
local USE_BASTION
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
local _S=1
|
||||
case $1 in
|
||||
-t | --type ) ((_S+=1)); CONNECTION_TYPE=$2 ;;
|
||||
-c | --command ) ((_S+=1)); REMOTE_COMMAND=$2 ;;
|
||||
-s | --shell ) ((_S+=1)); LOGIN_SHELL=$2 ;;
|
||||
|
||||
--no-rc ) LOAD_RC=false ;;
|
||||
--no-tty ) USE_TTY=false ;;
|
||||
|
||||
--use-bastion )
|
||||
((_S+=1))
|
||||
USE_BASTION=$2
|
||||
case $USE_BASTION in
|
||||
true | false ) ;;
|
||||
* ) ERROR "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;;
|
||||
esac
|
||||
;;
|
||||
|
||||
--force-local-login ) FORCE_LOCAL_LOGIN=true ;;
|
||||
|
||||
* ) ERROR "unknown argument '$1'" ;;
|
||||
esac
|
||||
shift $_S
|
||||
done
|
||||
|
||||
CHECK_ERRORS
|
||||
|
||||
##########################################
|
||||
|
||||
GET_SSH_ARGS() {
|
||||
REMOTE__GET_SSH_ARGS \
|
||||
--type $CONNECTION_TYPE \
|
||||
--use-tty $USE_TTY \
|
||||
$REMOTE_NAME \
|
||||
;
|
||||
}
|
||||
|
||||
GET_SSH_ARGS >/dev/null \
|
||||
&& local SSH_ARGS=($(GET_SSH_ARGS)) \
|
||||
|| FAIL 1 'unable to load ssh args; aborting'
|
||||
|
||||
##########################################
|
||||
|
||||
local CONNECTION_STRING=$(REMOTE__GET_CONNECTION_STRING $REMOTE_NAME)
|
||||
[ $CONNECTION_STRING ] \
|
||||
|| FAIL 1 'unable to determine connection string'
|
||||
|
||||
##########################################
|
||||
|
||||
LOGIN_SHELL=$(\
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
"$LOGIN_SHELL" \
|
||||
".$REMOTE_NAME.shell" \
|
||||
".default.shell" \
|
||||
"zsh" \
|
||||
)
|
||||
|
||||
REMOTE_COMMAND=$(\
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
"$REMOTE_COMMAND" \
|
||||
".sessions.$REMOTE_NAME.$CONNECTION_TYPE.command" \
|
||||
".sessions.$REMOTE_NAME.command" \
|
||||
".default.$CONNECTION_TYPE.command" \
|
||||
".default.command" \
|
||||
)
|
||||
|
||||
[ $REMOTE_COMMAND ] || {
|
||||
case $CONNECTION_TYPE in
|
||||
tmux )
|
||||
local TMUX_SESSION_NAME=$(
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
".sessions.$REMOTE_NAME.tmux.session" \
|
||||
".default.tmux.session" \
|
||||
"wryn" \
|
||||
)
|
||||
local TMUX_ARGS=()
|
||||
|
||||
locale charmap 2>/dev/null | grep -qi 'UTF-8' \
|
||||
&& TMUX_ARGS+=(-u)
|
||||
|
||||
REMOTE_COMMAND="tmux ${TMUX_ARGS[@]} new-session -As $TMUX_SESSION_NAME"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
[ $USE_BASTION ] || {
|
||||
USE_BASTION=$(\
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
".sessions.$REMOTE_NAME.bastion.preferred" \
|
||||
'false' \
|
||||
;
|
||||
)
|
||||
}
|
||||
|
||||
local BASTION_HOST
|
||||
[[ $USE_BASTION =~ true ]] && {
|
||||
BASTION_HOST=$(\
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
".sessions.$REMOTE_NAME.bastion.session" \
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
[ $BASTION_HOST ] && {
|
||||
PASSTHROUGH_COMMAND="$(GET_PASSTHROUGH_PREFIX) connect $REMOTE_NAME -- -c $(printf "%q " "$REMOTE_COMMAND")"
|
||||
|
||||
BASTION_TARGET=$REMOTE_NAME REMOTE__TARGET=$BASTION_HOST MAIN --command "$PASSTHROUGH_COMMAND"
|
||||
return $?
|
||||
}
|
||||
|
||||
##########################################
|
||||
|
||||
[ $REMOTE_COMMAND ] && [[ $LOAD_RC =~ true ]] && {
|
||||
REMOTE_COMMAND="$LOGIN_SHELL -l -c 'source ~/.$(basename $LOGIN_SHELL)rc &>/dev/null; $REMOTE_COMMAND'"
|
||||
}
|
||||
|
||||
[ ! $REMOTE_COMMAND ] && {
|
||||
[[ $LOAD_RC =~ true ]] \
|
||||
&& REMOTE_COMMAND="$LOGIN_SHELL -l" \
|
||||
|| REMOTE_COMMAND="$LOGIN_SHELL"
|
||||
}
|
||||
|
||||
[ $BASTION_TARGET ] && CONNECTION_TYPE=bastion
|
||||
|
||||
DEBUG "
|
||||
attempting execution:
|
||||
netpath : $(hostnamectl --static) -> $([ $BASTION_TARGET ] && echo "$BASTION_TARGET -> ")$REMOTE_NAME
|
||||
type : $CONNECTION_TYPE
|
||||
connection : $REMOTE_NAME
|
||||
command : \"$REMOTE_COMMAND\"
|
||||
"
|
||||
|
||||
case $CONNECTION_STRING in
|
||||
localhost | $USER@localhost )
|
||||
eval "cd; $REMOTE_COMMAND"
|
||||
return $?
|
||||
;;
|
||||
esac
|
||||
|
||||
DEBUG "ssh ${SSH_ARGS[@]} $CONNECTION_STRING \"$REMOTE_COMMAND\""
|
||||
ssh ${SSH_ARGS[@]} $CONNECTION_STRING "$REMOTE_COMMAND"
|
||||
}
|
3
scwrypts/remote/lib/bastion.module.zsh
Normal file
3
scwrypts/remote/lib/bastion.module.zsh
Normal file
@ -0,0 +1,3 @@
|
||||
GET_PASSTHROUGH_PREFIX() {
|
||||
echo "SUBSCWRYPT=$((SUBSCWRYPT+1)) SCWRYPTS_LOG_LEVEL=$SCWRYPTS_LOG_LEVEL scwrypts"
|
||||
}
|
99
scwrypts/remote/lib/config.module.zsh
Normal file
99
scwrypts/remote/lib/config.module.zsh
Normal file
@ -0,0 +1,99 @@
|
||||
#####################################################################
|
||||
|
||||
DEPENDENCIES+=(yq)
|
||||
|
||||
#####################################################################
|
||||
|
||||
REMOTE__GET_CONNECTION_STRING() {
|
||||
local REMOTE_NAME="$1"
|
||||
[ $(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.host) ] \
|
||||
|| ERROR "no such connection $REMOTE_NAME exists" \
|
||||
|| return 1
|
||||
|
||||
local CONNECTION_HOST=$(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.host)
|
||||
[ $CONNECTION_HOST ] \
|
||||
|| ERROR "connection $REMOTE_NAME is misconfigured; missing 'host' field" \
|
||||
|| return 1
|
||||
|
||||
local CONNECTION_USER=$(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.user)
|
||||
[ $CONNECTION_USER ] || CONNECTION_USER=$(REMOTE__QUERY_CONNECTION .default.user)
|
||||
|
||||
[ $CONNECTION_USER ] \
|
||||
&& CONNECTION_STRING="${CONNECTION_USER}@${CONNECTION_HOST}" \
|
||||
|| CONNECTION_STRING="$CONNECTION_HOST" \
|
||||
;
|
||||
|
||||
echo $CONNECTION_STRING
|
||||
}
|
||||
|
||||
REMOTE__GET_SSH_ARGS() {
|
||||
local REMOTE_NAME
|
||||
local TYPE=ssh
|
||||
local USE_TTY=true
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-t | --type ) TYPE=$2; shift 1 ;;
|
||||
--use-tty ) USE_TTY=$2; shift 1 ;;
|
||||
|
||||
--no-tty ) USE_TTY=false ;;
|
||||
|
||||
* )
|
||||
[ $REMOTE_NAME ] && { ERROR "too many args :c"; return 1; }
|
||||
REMOTE_NAME=$1
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
local ARGS=()
|
||||
[ $REMOTE_NAME ] || {
|
||||
echo "${ARGS[@]}"
|
||||
return 0
|
||||
}
|
||||
|
||||
local PORT=$(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME.port)
|
||||
|
||||
[ $PORT ] && {
|
||||
case $TYPE in
|
||||
ssh | xserver | tmux ) ARGS+=(-p $PORT) ;;
|
||||
scp ) ARGS+=(-P $PORT) ;; # not really in use, just a sample
|
||||
* )
|
||||
WARNING "
|
||||
port is specified, but I'm not sure whether to use '-p' or '-P'
|
||||
|
||||
if this command fails, try adding your --type to the appropriate
|
||||
list in '$SCWRYPTS_ROOT__remote/lib/config.module.zsh'
|
||||
"
|
||||
ARGS+=(-p $PORT)
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
ARGS+=($(REMOTE__QUERY_CONNECTION .session.$REMOTE_NAME.$TYPE.args))
|
||||
|
||||
[[ $USE_TTY =~ true ]] && ARGS+=(-t)
|
||||
|
||||
echo "${ARGS[@]}"
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
REMOTE__QUERY_CONNECTION() {
|
||||
YQ -oy -r $@ "$REMOTE_CONNECTIONS_FILE" \
|
||||
| grep -v ^null$
|
||||
}
|
||||
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK() {
|
||||
while [[ $# -gt 0 ]] && [ ! $QUERY_RESULT ]
|
||||
do
|
||||
case $1 in
|
||||
.* ) QUERY_RESULT=$(REMOTE__QUERY_CONNECTION $1) ;;
|
||||
* ) QUERY_RESULT="$1" ;; # allows raw default value
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
echo $QUERY_RESULT
|
||||
}
|
46
scwrypts/remote/remote.scwrypts.zsh
Normal file
46
scwrypts/remote/remote.scwrypts.zsh
Normal file
@ -0,0 +1,46 @@
|
||||
SCWRYPTS_GROUPS+=(remote)
|
||||
[ $DOTWRYN ] || source "$HOME/.config/wryn/env.zsh"
|
||||
|
||||
export SCWRYPTS_TYPE__remote=zsh
|
||||
export SCWRYPTS_ROOT__remote="$DOTWRYN/scwrypts/remote"
|
||||
export SCWRYPTS_COLOR__remote='\033[0;34m'
|
||||
|
||||
DEPENDENCIES+=(yq)
|
||||
REMOTE_CONNECTIONS_FILE="$HOME/.config/wryn/remote-connections.toml"
|
||||
|
||||
SCWRYPTS__LIST_AVAILABLE_SCWRYPTS__remote() {
|
||||
[ -f "$REMOTE_CONNECTIONS_FILE" ] || {
|
||||
mkdir -p "$(dirname -- "$REMOTE_CONNECTIONS_FILE")" &>/dev/null
|
||||
echo "
|
||||
[sessions]
|
||||
|
||||
[sessions.$(hostnamectl --static)]
|
||||
enabled = true
|
||||
host = 'localhost'
|
||||
" | sed 's/^\s\+//; 1d; $d;' > "$REMOTE_CONNECTIONS_FILE"
|
||||
}
|
||||
|
||||
{
|
||||
yq -oy -r '.sessions | keys | .[]' "$REMOTE_CONNECTIONS_FILE" \
|
||||
| sed 's|^|connect/|'
|
||||
|
||||
echo "tmux/omni"
|
||||
echo "configure"
|
||||
echo "test"
|
||||
} | sed "s|^|$SCWRYPTS_TYPE__remote/|"
|
||||
}
|
||||
|
||||
SCWRYPTS__GET_RUNSTRING__remote__zsh() {
|
||||
local SCWRYPT_FILENAME
|
||||
case $SCWRYPT_NAME in
|
||||
connect/* )
|
||||
SCWRYPT_FILENAME="$SCWRYPTS_ROOT__remote/connect"
|
||||
echo "export REMOTE__TARGET=$(echo $SCWRYPT_NAME | sed 's|^.*connect/||')"
|
||||
;;
|
||||
* )
|
||||
SCWRYPT_FILENAME="$SCWRYPTS_ROOT__remote/$SCWRYPT_NAME"
|
||||
;;
|
||||
esac
|
||||
|
||||
SCWRYPTS__GET_RUNSTRING__zsh__generic "$SCWRYPT_FILENAME"
|
||||
}
|
129
scwrypts/remote/test
Executable file
129
scwrypts/remote/test
Executable file
@ -0,0 +1,129 @@
|
||||
#!/bin/zsh
|
||||
use bastion --group remote
|
||||
use config --group remote
|
||||
|
||||
DEPENDENCIES+=(timeout ssh)
|
||||
#####################################################################
|
||||
|
||||
USAGE__options='
|
||||
-n, --name session name to test
|
||||
-s, --connection-string explicit session host / ssh connection string to test
|
||||
-t, --maximum-timeout maximum connection timeout before failure (in seconds)
|
||||
-c, --command testing command; performs echo by default
|
||||
|
||||
--use-bastion true / false override for bastion preference
|
||||
(default: session.name.bastion.preferred or "false")
|
||||
'
|
||||
|
||||
USAGE__description='
|
||||
Tests whether you can connect to a particular session or
|
||||
host string.
|
||||
'
|
||||
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
local CONNECTION_STRING REMOTE_NAME
|
||||
local TIMEOUT_SECONDS=3
|
||||
local COMMAND='echo OK &>/dev/null'
|
||||
local USE_BASTION
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
local _S=1
|
||||
case $1 in
|
||||
-n | --name )
|
||||
((_S+=1))
|
||||
REMOTE_NAME=$2
|
||||
CONNECTION_STRING=$(REMOTE__GET_CONNECTION_STRING $REMOTE_NAME)
|
||||
;;
|
||||
-s | --connection_string )
|
||||
((_S+=1))
|
||||
CONNECTION_STRING="$2"
|
||||
;;
|
||||
-t | --maximum-timout )
|
||||
((_S+=1))
|
||||
TIMEOUT_SECONDS=$2
|
||||
[[ $TIMEOUT_SECONDS -gt 0 ]] \
|
||||
|| ERROR "invalid timeout seconds '$TIMEOUT_SECONDS'"
|
||||
;;
|
||||
-c | --command )
|
||||
((_S+=1))
|
||||
COMMAND=$2
|
||||
;;
|
||||
--use-bastion )
|
||||
((_S+=1))
|
||||
USE_BASTION=$2
|
||||
case $USE_BASTION in
|
||||
true | false ) ;;
|
||||
* ) ERROR "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;;
|
||||
esac
|
||||
;;
|
||||
* ) ERROR "unrecognized argument '$1'" ;;
|
||||
esac
|
||||
shift $_S
|
||||
done
|
||||
|
||||
[ $CONNECTION_STRING ] \
|
||||
|| ERROR "unable to determine connection string"
|
||||
|
||||
|
||||
[ $USE_BASTION ] || {
|
||||
USE_BASTION=$(\
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
".sessions.$REMOTE_NAME.bastion.preferred" \
|
||||
'false' \
|
||||
;
|
||||
)
|
||||
}
|
||||
|
||||
CHECK_ERRORS
|
||||
|
||||
|
||||
##########################################
|
||||
|
||||
local BASTION_HOST
|
||||
[[ $USE_BASTION =~ true ]] && {
|
||||
BASTION_HOST=$(\
|
||||
REMOTE__QUERY_CONNECTION_WITH_FALLBACK \
|
||||
".sessions.$REMOTE_NAME.bastion.session" \
|
||||
)
|
||||
}
|
||||
|
||||
[[ $USE_BASTION =~ true ]] && {
|
||||
[ $BASTION_HOST ] \
|
||||
|| ERROR "cannot connect to $REMOTE_NAME; no configured bastion host" \
|
||||
|| return 1
|
||||
}
|
||||
|
||||
case $CONNECTION_STRING in
|
||||
localhost | $USER@localhost )
|
||||
CONNECTION_TEST() { return 0; } # current user on local machine can always connect
|
||||
;;
|
||||
* )
|
||||
[[ $USE_BASTION =~ true ]] && {
|
||||
DEBUG "MAIN -n $BASTION_HOST -c \"$(GET_PASSTHROUGH_PREFIX) remote test -- -n $REMOTE_NAME -c \"$COMMAND\"\""
|
||||
BASTION_TARGET="$REMOTE_NAME" MAIN -n $BASTION_HOST -c "$(GET_PASSTHROUGH_PREFIX) remote test -- -n $REMOTE_NAME -c \"$COMMAND\""
|
||||
return $?
|
||||
}
|
||||
|
||||
CONNECTION_TEST() {
|
||||
[ $REMOTE_NAME ] && {
|
||||
[[ $(REMOTE__QUERY_CONNECTION .sessions.$REMOTE_NAME) =~ false ]] && {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
local REMOTE_ARGS=()
|
||||
REMOTE_ARGS+=($(REMOTE__GET_SSH_ARGS --type ssh $REMOTE_NAME))
|
||||
REMOTE_ARGS+=(-o BatchMode=yes)
|
||||
DEBUG "attempting\ntimeout $TIMEOUT_SECONDS ssh $REMOTE_ARGS $CONNECTION_STRING "'\'"\"$COMMAND"'\'"\"" >&2
|
||||
timeout --foreground $TIMEOUT_SECONDS ssh ${REMOTE_ARGS[@]} "$CONNECTION_STRING" "$COMMAND" >&2
|
||||
}
|
||||
;;
|
||||
esac
|
||||
|
||||
[ $REMOTE_NAME ] || REMOTE_NAME=explicit
|
||||
STATUS "testing connection $CONNECTION_STRING ($REMOTE_NAME$([ $BASTION_TARGET ] && echo " -> $BASTION_TARGET"))" \
|
||||
&& CONNECTION_TEST \
|
||||
&& SUCCESS "successfully connected to '$CONNECTION_STRING' ($REMOTE_NAME)" \
|
||||
|| ERROR "connection to '$CONNECTION_STRING ($REMOTE_NAME)' failed" \
|
||||
}
|
148
scwrypts/remote/tmux/manager
Executable file
148
scwrypts/remote/tmux/manager
Executable file
@ -0,0 +1,148 @@
|
||||
#!/bin/zsh
|
||||
#
|
||||
# works as a standalone zsh script
|
||||
#
|
||||
|
||||
#####################################################################
|
||||
|
||||
CONNECTIONS_FILE="$HOME/.config/wryn/remote-connections.toml"
|
||||
|
||||
OMNI_LOGDIR="$HOME/.local/share/scwrypts/dotwryn"
|
||||
OMNI_LOGFILE="$HOME/.local/share/scwrypts/dotwryn/omni.current.txt"
|
||||
|
||||
[ ! -d "$OMNI_LOGDIR" ] && mkdir -p "$OMNI_LOGDIR"
|
||||
|
||||
[ -f "$OMNI_LOGFILE" ] && {
|
||||
for x in {1..99}
|
||||
do
|
||||
[ ! -f "$OMNI_LOGDIR/omni.$x.txt" ] && break
|
||||
done
|
||||
|
||||
[ ! -f "$OMNI_LOGDIR/omni.$x.txt" ] && mv "$OMNI_LOGFILE" "$OMNI_LOGDIR/omni.$x.txt"
|
||||
}
|
||||
|
||||
echo "OMNI MANAGER START : $(date)" > "$OMNI_LOGFILE"
|
||||
|
||||
OMNI_SOCKET="omni.socket"
|
||||
OMNI_TMUX() { tmux -L $OMNI_SOCKET $@; }
|
||||
|
||||
CONFIG_QUERY() { yq -oy -r $@ "$CONNECTIONS_FILE" | grep -v ^null$; }
|
||||
|
||||
CONNECTED() {
|
||||
[ $WINDOW_ID ] \
|
||||
&& echo "\\033[1;32mconnected (window $WINDOW_ID)\\033[0m" >&2 \
|
||||
|| echo "\\033[1;32mconnected\\033[0m" >&2 \
|
||||
}
|
||||
DISCONNECTED() { echo "\\033[1;31mdisconnected\\033[0m" >&2; }
|
||||
|
||||
GET_UNIQUE_WINDOW_ID() {
|
||||
local MODE=use-default
|
||||
|
||||
local WINDOW_ID="$(eval "echo \$WINDOW_ID__$REMOTE_NAME")"
|
||||
: \
|
||||
&& [[ $(eval "echo \$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \
|
||||
&& [ $WINDOW_ID ] \
|
||||
&& echo $WINDOW_ID \
|
||||
&& return 0 \
|
||||
;
|
||||
|
||||
unset WINDOW_ID__$REMOTE_NAME &>>"$OMNI_LOGFILE"
|
||||
|
||||
WINDOW_ID="$1"
|
||||
WINDOW_ID=$(CONFIG_QUERY .sessions.$REMOTE_NAME.id)
|
||||
[ $WINDOW_ID ] && MODE=specify-preferred
|
||||
|
||||
WINDOW_ID_IS_TAKEN() {
|
||||
: \
|
||||
&& [[ $(OMNI_TMUX list-windows -t=omni | grep "^$WINDOW_ID:" | wc -l) -gt 0 ]] \
|
||||
&& [[ ! $(OMNI_TMUX list-windows -t=omni | grep "^$WINDOW_ID:" | awk '{print $2;}' =~ ^$REMOTE_NAME$) ]] \
|
||||
;
|
||||
}
|
||||
|
||||
local FALLBACK_STARTING_POINT
|
||||
case $MODE in
|
||||
use-default ) FALLBACK_STARTING_POINT=42 ;;
|
||||
specify-preferred ) FALLBACK_STARTING_POINT=69 ;;
|
||||
esac
|
||||
|
||||
local I=0
|
||||
while WINDOW_ID_IS_TAKEN
|
||||
do
|
||||
echo "$REMOTE_NAME tried to acquire window id $WINDOW_ID, but it is already in-use (trying $(($FALLBACK_STARTING_POINT-$I)))" >&2
|
||||
WINDOW_ID=$(($FALLBACK_STARTING_POINT-$I))
|
||||
((I+=1))
|
||||
[[ $I -gt 10 ]] && WINDOW_ID= && break
|
||||
done
|
||||
|
||||
echo $WINDOW_ID
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
sleep 1
|
||||
OMNI_TMUX new-window -t=omni-manager -dn 'harakiri' "
|
||||
while true
|
||||
do
|
||||
tmux -L $OMNI_SOCKET list-sessions | grep -v omni-manager | grep -qi omni || {
|
||||
tmux -L $OMNI_SOCKET kill-session -t omni-manager
|
||||
}
|
||||
sleep 5
|
||||
done
|
||||
"
|
||||
|
||||
while true
|
||||
do
|
||||
clear
|
||||
for REMOTE_NAME in $(CONFIG_QUERY '.sessions | keys | .[]')
|
||||
do
|
||||
WINDOW_ID=$(GET_UNIQUE_WINDOW_ID)
|
||||
export WINDOW_ID__$REMOTE_NAME=$WINDOW_ID
|
||||
|
||||
OMNI_TMUX list-windows -t=omni | awk '{print $2;}' | grep -q $REMOTE_NAME \
|
||||
&& continue
|
||||
|
||||
printf "testing connection $REMOTE_NAME..."
|
||||
echo "testing connection $REMOTE_NAME..." &>>"$OMNI_LOGFILE"
|
||||
scwrypts --name test --group remote --type zsh \
|
||||
-- \
|
||||
--name $REMOTE_NAME \
|
||||
--command 'command -v tmux' \
|
||||
&>>"$OMNI_LOGFILE" \
|
||||
&& export CAN_CONNECT__$REMOTE_NAME=true \
|
||||
|| export CAN_CONNECT__$REMOTE_NAME=false \
|
||||
;
|
||||
|
||||
[[ $(eval echo "\$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \
|
||||
&& echo " \\033[1;32m✔\\033[0m" >&2 \
|
||||
|| { echo " \\033[1;31m✖\\033[0m" >&2; continue; }
|
||||
|
||||
OMNI_TMUX new-window \
|
||||
-t=omni:$WINDOW_ID \
|
||||
-dn $REMOTE_NAME "
|
||||
source ~/.zshrc &>/dev/null
|
||||
TMUX='' scwrypts -n connect/$REMOTE_NAME -- --type tmux
|
||||
echo 'connection closed'
|
||||
sleep 2
|
||||
"
|
||||
|
||||
OMNI_TMUX list-window -t=omni | awk '{print $1;}' | grep -q '99:' && OMNI_TMUX kill-window -t omni:99
|
||||
|
||||
OMNI_TMUX list-windows -t=omni | awk '{print $2;}' | grep -q $REMOTE_NAME \
|
||||
|| export CAN_CONNECT__$REMOTE_NAME=false
|
||||
done
|
||||
|
||||
clear
|
||||
echo "connections:\n"
|
||||
local STATUS
|
||||
{
|
||||
for REMOTE_NAME in $(CONFIG_QUERY '.sessions | keys | .[]')
|
||||
do
|
||||
[[ $(eval "echo \$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \
|
||||
&& echo "${REMOTE_NAME}^$(eval "echo \$WINDOW_ID__$REMOTE_NAME")^\\033[1;32mconnected\\033[0m" \
|
||||
|| echo "${REMOTE_NAME}^-^\\033[1;31mdisconnected\\033[0m" \
|
||||
;
|
||||
done
|
||||
} | column -ts '^'
|
||||
echo "\nPress ENTER to search again now (checks every 60 seconds)" >&2
|
||||
read -t 60
|
||||
done
|
53
scwrypts/remote/tmux/omni
Executable file
53
scwrypts/remote/tmux/omni
Executable file
@ -0,0 +1,53 @@
|
||||
#!/bin/zsh
|
||||
DEPENDENCIES+=(tmux scwrypts)
|
||||
#####################################################################
|
||||
|
||||
USAGE__description="
|
||||
A program which automatically connects to any available connections
|
||||
described in the configuration file.
|
||||
(configure with 'scwrypts remote configure')
|
||||
|
||||
The omni-session is running it's own wrapper TMUX server with a special
|
||||
config. In the wrapper session, 'M-s' is overwritten as the prefix.
|
||||
Switch between connections by using 'M-s' followed by the session ID
|
||||
number (or use any other default default tmux navigation command). Full
|
||||
configuration can be found here:
|
||||
- $SCWRYPTS_ROOT__remote/omni/tmux.conf
|
||||
|
||||
Shut-down the omni-session by pressing 'M-Q' (Q not q! ALT + SHIFT + Q)
|
||||
|
||||
Disconnect (but leave the session running) with 'M-s, d'
|
||||
|
||||
A background process periodically attempts to refresh lost connections.
|
||||
Immediate retry can be forced with 'M-R'.
|
||||
|
||||
Running this command in an existing tmux session will likely result
|
||||
in an infinite loop, so:
|
||||
- don't run the omni-session in a TMUX session
|
||||
- don't connect to the omni-session directly (always use this scwrypt)
|
||||
"
|
||||
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
[[ $TERM =~ tmux ]] && ERROR "\n Cannot run tmux-omni within a tmux session!\n "
|
||||
CHECK_ERRORS
|
||||
|
||||
local OMNI_SOCKET="omni.socket"
|
||||
OMNI_TMUX() { tmux -L $OMNI_SOCKET $@; }
|
||||
|
||||
OMNI_TMUX list-sessions 2>/dev/null | grep -v omni-manager | grep -qi omni || {
|
||||
STATUS "initializing omni server"
|
||||
OMNI_TMUX kill-session -t=omni-manager >/dev/null 2>&1
|
||||
|
||||
OMNI_TMUX -f "$SCWRYPTS_ROOT__remote/tmux/tmux.conf" new -d -s omni \
|
||||
"echo searching for first connection...; sleep 30" \; \
|
||||
split-window "sleep 3; TMUX= tmux -L $OMNI_SOCKET a -t=omni-manager" \; \
|
||||
move-window -t 99 \;
|
||||
|
||||
OMNI_TMUX new -d -s omni-manager "$SCWRYPTS_ROOT__remote/tmux/manager"
|
||||
}
|
||||
|
||||
STATUS 'connecting to omni server'
|
||||
OMNI_TMUX a -t=omni
|
||||
}
|
70
scwrypts/remote/tmux/tmux.conf
Normal file
70
scwrypts/remote/tmux/tmux.conf
Normal file
@ -0,0 +1,70 @@
|
||||
# config (mostly inherited from ../../../config/tmux.conf)
|
||||
set -g default-terminal "tmux-256color"
|
||||
set -g mouse off
|
||||
set -sg escape-time 0
|
||||
set-option -g default-shell /bin/zsh
|
||||
set-option -gw xterm-keys on
|
||||
set-window-option -g mode-keys vi
|
||||
set-window-option -g status-keys vi
|
||||
setw -gq utf8 on
|
||||
set -g status-style fg=brightmagenta,bg=black
|
||||
set -g window-status-current-style fg=black,bg=brightmagenta
|
||||
set -g status-right "omni host : #(hostnamectl --static) | #(cat /sys/class/power_supply/BAT0/capacity)% "
|
||||
|
||||
# navigate between hosts with ALT+window-number
|
||||
set -g base-index 1
|
||||
|
||||
bind-key 2 select-window -t 1
|
||||
bind-key 2 select-window -t 2
|
||||
bind-key 3 select-window -t 3
|
||||
bind-key 4 select-window -t 4
|
||||
bind-key 5 select-window -t 5
|
||||
bind-key 6 select-window -t 6
|
||||
bind-key 7 select-window -t 7
|
||||
bind-key 8 select-window -t 8
|
||||
bind-key 9 select-window -t 9
|
||||
bind-key 0 select-window -t 10
|
||||
|
||||
# window / tile navigation settings from $DOTWRYN/config/tmux.conf
|
||||
# but default to nested session rather than host
|
||||
|
||||
bind -n M-h send-keys C-b Left
|
||||
bind -n M-H select-pane -L
|
||||
bind -n M-j send-keys C-b Down
|
||||
bind -n M-J select-pane -D
|
||||
bind -n M-k send-keys C-b Up
|
||||
bind -n M-K select-pane -U
|
||||
bind -n M-l send-keys C-b Right
|
||||
bind -n M-L select-pane -R
|
||||
|
||||
bind -n M-Left send-keys C-b C-Left
|
||||
bind -n M-S-Left resize-pane -L 2
|
||||
bind -n M-Down send-keys C-b C-Down
|
||||
bind -n M-S-Down resize-pane -D 2
|
||||
bind -n M-Up send-keys C-b C-Up
|
||||
bind -n M-S-Up resize-pane -U 2
|
||||
bind -n M-Right send-keys C-b C-Right
|
||||
bind -n M-S-Right resize-pane -R 2
|
||||
|
||||
bind-key -n M-Tab send-keys C-b n
|
||||
bind-key -n M-BTab send-keys C-b p
|
||||
|
||||
bind-key -n M-Enter send-keys C-b c
|
||||
|
||||
bind-key -n M-v send-keys M-v
|
||||
bind-key -n M-b send-keys M-b
|
||||
bind-key -n M-q send-keys M-q
|
||||
bind-key -n M-z send-keys M-z
|
||||
bind-key -n M-w send-keys C-M-w
|
||||
|
||||
bind-key -n M-Q kill-session
|
||||
|
||||
unbind-key C-b
|
||||
bind-key C-b send-prefix
|
||||
set-option -g prefix M-s
|
||||
bind-key M-s send-keys M-s
|
||||
|
||||
bind-key -n M-Q kill-session
|
||||
|
||||
# force reload now
|
||||
bind-key -n M-R send-keys -t omni-manager ENTER
|
Reference in New Issue
Block a user