Last active
August 10, 2017 12:29
-
-
Save epcim/b44b548cadc566fcabc9f6195a19b1dc to your computer and use it in GitHub Desktop.
byobu auto create windows from salt-master accepted keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# wget https://gist.githubusercontent.com/epcim/b44b548cadc566fcabc9f6195a19b1dc/raw/byobu-salt.sh -O byobu-salt.sh; chmod +x *.sh | |
# set -x | |
# TODO | |
# on remote host source ~/.${SHELL}rc | |
# auto set -o vi | |
test -n SSH_AGENT || { | |
eval $(ssh-agent -s) | |
ssh-add | |
} | |
function filterMinions() { | |
H=$1 | |
# use xzy* instead of * to filter mionion names | |
if [[ $H == * ]] | |
then | |
# additionaly filter specific minion names | |
# declare -a WINDOWS=(cfg mas ctl gls log mon mdb mtr nal ntw prx rmq sql dbs ceph cpu cmp) | |
# for W in WINDOWS[@]; do if [[ "$H" =~ "$W" ]] ; then echo -n $H ; fi ; done | |
echo -n $H | |
fi | |
} | |
function saltHostShortName() { | |
H=$1 | |
echo -n "${H/[-0-9]*}" # short hostname from fqdn, ie: ctl | |
#echo -n "${H/.*}" # short hostname from fqdn, ie: ctl01 | |
} | |
# for local modifications (ie: custom saltHostShortName function) source .byobu-salt.sh, etc.. | |
test -e ".$( basename $0)" && source ".$(basename $0)" | |
# INIT & CONFIG | |
SESSION=${SESSION:-minions} | |
# use/create session | |
byobu-tmux list-sessions | grep "^$SESSION:" || byobu-tmux new-session -s $SESSION -dn $(hostname -f) "sudo -iE" | |
W=($(byobu-tmux list-windows -t $SESSION -F '#W' 2> /dev/null )) | |
# tmux config | |
# byobu-tmux select-layout tiled | |
# byobu-tmux set -g mouse on | |
WINDOWS=$@ | |
HOSTS=($(sudo salt-key -l acc |grep -v 'Accepted')) | |
WINDOWS=${WINDOWS:-$(for H in ${HOSTS[@]}; do echo -n $(saltHostShortName $H);done|sort -u)} # required/target windows | |
# add windows/panes | |
for H in ${HOSTS[@]}; do | |
[[ "filterMinions($H)" == "" ]] && continue # filtered | |
NM=$(saltHostShortName $H) | |
[[ "${W[*]} xxx" =~ $NM ]] && continue # skip, already open windows | |
[[ "${WINDOWS[*]}" =~ "$NM" ]] || continue # only required | |
_W=($(byobu-tmux list-windows -t $SESSION -F '#W')) | |
[[ "${_W[@]} xxx" =~ "$NM" ]] && { # check window exist | |
# eval number of tab/splits for each window, returns: | |
# ctl=3 | |
# prx=2 | |
# ... | |
eval $(byobu-tmux list-windows -t $SESSION -F "#W=#{window_panes}" 2>/dev/null |sed 1d) | |
SPLITBY='-h -p 50' #3rd and other tab/split cfg | |
if [ ! x"${!NM}" == "x" ] ; then | |
test ${!NM} -le 1 && SPLITBY='-h -p 40' # 1st,2nd tab/split (wider) | |
test ${!NM} -ge 3 && continue # open max 3 window splits | |
fi | |
# sudo -iE $SHELL | |
byobu-tmux split-window $SPLITBY -t $SESSION "ssh -t $USER@$H -oStrictHostKeyChecking=no sudo -i $SHELL " | |
echo "add window: $NM tab/split $((${!NM}+1))" | |
} || { | |
byobu-tmux new-window -n ${NM} -t $SESSION "ssh -t $USER@$H -oStrictHostKeyChecking=no sudo -i $SHELL " | |
echo "add window: $NM" | |
} | |
done; | |
# focuse & attach | |
for i in $(seq 0 $(byobu-tmux list-windows -t $SESSION -F "#I" |tail -n1)); do | |
byobu-tmux select-window -t $i; | |
byobu-tmux select-pane -t 0; | |
done | |
[[ $USER =~ (epcim|pmichalec) || EDITOR =~ (vi|vim|nvim|gvim) ]] && { | |
byobu-tmux set-window-option mode-keys vi | |
byobu-tmux run-shell "/usr/lib/byobu/include/tmux-send-command-to-all-windows 'set -o vi'" | |
byobu-tmux run-shell "/usr/lib/byobu/include/tmux-send-command-to-all-windows 'alias salt-call=salt-call\ --state-output=changes\ -lerror'" | |
byobu-tmux bind-key -n M-h previous-window | |
byobu-tmux bind-key -n M-l next-window | |
# broken | |
#byobu-tmux bind-key -n M-j send-keys "C-a" \; send-keys "C-o" | |
#byobu-tmux bind-key -n M-k swap-pane -s :. -t :.+ \; select-pane -t :.+ | |
#byobu-tmux bind-key -n M-j copy-mode \; send-keys M-NPage | |
#byobu-tmux bind-key -n M-k copy-mode \; send-keys M-PPage | |
} | |
byobu-tmux set-window-option -g status-right "" | |
byobu-tmux select-window -t 0 | |
byobu-tmux attach |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment