sh byobu自动从salt-master接受键创建窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh byobu自动从salt-master接受键创建窗口相关的知识,希望对你有一定的参考价值。

#!/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

以上是关于sh byobu自动从salt-master接受键创建窗口的主要内容,如果未能解决你的问题,请参考以下文章

sh byobu默认窗口

sh 将byobu安装到CentOS 7

byobu 硬状态行在更新时滚动屏幕

如何在 ec2 ami 中安装 byobu

改用 screen 作为 byobu 的后端

byobu copy