Created
November 7, 2017 10:59
-
-
Save davidedg/d5f561399f3dfbbb7a9dbb950c91006b to your computer and use it in GitHub Desktop.
Frontend Connectiol Tool
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 | |
export LOGTAG="frontend-connect" | |
function log { logger -t $LOGTAG -- $1; } | |
function die { | |
[[ -n "$1" ]] && log "$1" | |
echo "$1" | |
exit 1 | |
} | |
HOOK_connect() { # this can be overridden in cfg files | |
ssh -A -t $TARGETSRV | |
return 0 | |
} | |
DLG_choose() { # INPUT: array contents of choices | |
# compose DIALOG menu list | |
local i=0 | |
local dlg="" | |
local choice_arr=($@) | |
for e in ${choice_arr[@]}; do | |
dlg="${dlg}${i} ${e} " | |
let i+=1 | |
done | |
exec 3>&1; | |
answer=$(dialog --ascii-lines --backtitle "$DLG_BACKTITLE" --title "$DLG_TITLE" --ascii-lines --menu "$DLG_MENU" 0 0 ${#choice_arr[@]} $dlg 2>&1 1>&3); | |
result=$?; | |
exec 3>&-; | |
[[ $result -eq 0 ]] && CHOICE=${choice_arr[$answer]} | |
return $result | |
} | |
############################################################################### | |
## MAIN | |
############################################################################### | |
# Sample Cfg - to be overridden in config files | |
declare -a ENV=( | |
"Production" | |
"Staging" | |
"Development" | |
) | |
declare -a Production=( | |
"prd-srv01" | |
"prd-srv02" | |
) | |
declare -a Staging=( | |
"qa-srv01" | |
) | |
declare -a Development=( | |
"dev-srv01" | |
) | |
DLG1_BACKTITLE="Frontend Connection Tool" | |
DLG1_TITLE="Environment" | |
DLG1_MENU="Environment:" | |
DLG2_BACKTITLE="Frontend Connection Tool" | |
DLG2_TITLE="Target" | |
DLG2_MENU="Server:" | |
# OVERRIDE IN GLOBAL/PERSONAL CFG | |
# You can create a symlink to the program and name the cfg files after the symlink name | |
prog=`basename "$0"` | |
export GLOBALCFG=/usr/local/etc/$prog | |
export PERSONALCFG=$HOME/.$prog | |
[[ -r $GLOBALCFG ]] && source $GLOBALCFG | |
[[ -r $PERSONALCFG ]] && source $PERSONALCFG | |
[[ -z "$ENV" ]] && die "No defined Environments found in $GLOBALCFG or $PERSONALCFG" | |
# 1st level choice (Environment) | |
DLG_BACKTITLE=$DLG1_BACKTITLE | |
DLG_TITLE=$DLG1_TITLE | |
DLG_MENU=$DLG1_MENU | |
DLG_choose "${ENV[@]}" | |
[[ $? -eq 0 ]] || exit 0 | |
export TARGETENV=$CHOICE | |
# 2nd level choice (Target Server) | |
DLG_BACKTITLE=$DLG2_BACKTITLE | |
DLG_TITLE=$DLG2_TITLE | |
DLG_MENU=$DLG2_MENU | |
TARGETS_ARRNAME="${TARGETENV}[@]" | |
DLG_choose ${!TARGETS_ARRNAME} | |
[[ $? -eq 0 ]] || exit 0 | |
export TARGETSRV=$CHOICE | |
HOOK_connect | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment