211 lines
5.4 KiB
Bash
Executable File
211 lines
5.4 KiB
Bash
Executable File
#!/usr/bin/zsh
|
|
WA_REPO=$HOME/wa_repos
|
|
|
|
|
|
|
|
|
|
|
|
echo "################################################"
|
|
echo " basic init of wiseadvice node "
|
|
echo "################################################"
|
|
|
|
function choose_from_menu() {
|
|
local prompt="$1" outvar="$2"
|
|
shift
|
|
shift
|
|
local options=("$@") cur=0 count=${#options[@]} index=0
|
|
local esc=$(echo -en "\e") # cache ESC as test doesn't allow esc codes
|
|
printf "$prompt\n"
|
|
while true
|
|
do
|
|
# list all options (option list is zero-based)
|
|
index=0
|
|
for o in "${options[@]}"
|
|
do
|
|
if [ "$index" == "$cur" ]
|
|
then echo -e " >\e[7m$o\e[0m" # mark & highlight the current option
|
|
else echo " $o"
|
|
fi
|
|
index=$(( $index + 1 ))
|
|
done
|
|
read -s -n3 key # wait for user to key in arrows or ENTER
|
|
if [[ $key == $esc[A ]] # up arrow
|
|
then cur=$(( $cur - 1 ))
|
|
[ "$cur" -lt 0 ] && cur=0
|
|
elif [[ $key == $esc[B ]] # down arrow
|
|
then cur=$(( $cur + 1 ))
|
|
[ "$cur" -ge $count ] && cur=$(( $count - 1 ))
|
|
elif [[ $key == "" ]] # nothing, i.e the read delimiter - ENTER
|
|
then break
|
|
fi
|
|
echo -en "\e[${count}A" # go up to the beginning to re-render
|
|
done
|
|
# export the selection to the requested output variable
|
|
printf -v $outvar "${options[$cur]}"
|
|
}
|
|
function clone_repo_with_retry(){
|
|
REPO_NAME=$1
|
|
echo "starting to pull into $(pwd)"
|
|
git clone git@git.service.wiseadvice.eu:wiseadvice_apps/$REPO_NAME.git -b main
|
|
if [ "$?" != "0" ]; then
|
|
echo " "
|
|
echo "Have you checked? "
|
|
echo "add deployment user as colaborator to the cimp repo"
|
|
echo "and add deployment user to the team shared_deployments"
|
|
echo "https://git.service.wiseadvice.eu/org/wiseadvice_apps/teams/shared_deployments"
|
|
echo -n "Have you checked? Should we try again? type yes: "
|
|
read -r yes_no
|
|
if [ "$yes_no" = "yes" ]; then
|
|
echo "starting to pull into $(pwd)"
|
|
git clone git@git.service.wiseadvice.eu:wiseadvice_apps/$REPO_NAME.git -b main
|
|
else
|
|
echo "Aborting"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
if [ "$?" != "0" ]; then
|
|
echo "Aborting"
|
|
exit 0
|
|
fi
|
|
}
|
|
|
|
|
|
# Oh-my-zsh compatible bash ssh-agent start script
|
|
function _start_agent() {
|
|
if [[ -f "$ssh_env_cache" ]]; then
|
|
. "$ssh_env_cache" > /dev/null
|
|
fi
|
|
|
|
if [[ -S "$SSH_AUTH_SOCK" ]]; then
|
|
return 0
|
|
fi
|
|
|
|
echo "Starting ssh-agent ..."
|
|
ssh-agent -s | sed '/^echo/d' > "$ssh_env_cache"
|
|
chmod 600 "$ssh_env_cache"
|
|
. "$ssh_env_cache" > /dev/null
|
|
}
|
|
|
|
function start_ssh_agent(){
|
|
SHORT_HOST="${HOSTNAME/.*/}"
|
|
ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
|
|
|
|
_start_agent
|
|
|
|
unset ssh_env_cache
|
|
unset -f _start_agent
|
|
|
|
ssh-add ~/.ssh/deploy_key
|
|
}
|
|
|
|
|
|
|
|
if [ -d "$WA_REPO" ]; then
|
|
echo "$WA_REPO does exist, skipping cloning"
|
|
source $WA_REPO/export_client_name.sh
|
|
echo "found client name: $CLIENT_NAME"
|
|
source $HOME/.zshrc
|
|
git_pull
|
|
else
|
|
echo -n "client name: "
|
|
read -r CLIENT_NAME
|
|
|
|
if [ ! -f $HOME/.ssh/deploy_key ]; then
|
|
|
|
echo "create ssh key"
|
|
echo -n "ssh passphrase: "
|
|
|
|
stty_orig=$(stty -g) # save original terminal setting.
|
|
stty -echo
|
|
read -r passphrase
|
|
stty "$stty_orig" # restore terminal setting.
|
|
|
|
|
|
echo "1. client name: $CLIENT_NAME"
|
|
|
|
mkdir -p $HOME/.ssh
|
|
|
|
ssh-keygen -t rsa -N "$passphrase" -C "deploy_key_$CLIENT_NAME" -f $HOME/.ssh/deploy_key
|
|
|
|
echo "please upload now pub key bay logging into user $CLIENT_NAME keys ..."
|
|
|
|
echo "firefox https://git.service.wiseadvice.eu/user/settings/keys"
|
|
echo " "
|
|
|
|
cat $HOME/.ssh/deploy_key.pub
|
|
fi
|
|
|
|
|
|
|
|
echo " "
|
|
echo -n "ready to pull? type yes: "
|
|
read -r ready_to_pull
|
|
|
|
|
|
if [ "$ready_to_pull" = "yes" ]; then
|
|
start_ssh_agent
|
|
|
|
echo "preparing"
|
|
|
|
if grep "git.service.wiseadvice.eu" $HOME/.ssh/config > /dev/null
|
|
then
|
|
echo "ssh configuration already set"
|
|
else
|
|
echo "set ssh configuration"
|
|
cat <<EOT >> $HOME/.ssh/config
|
|
Host git.service.wiseadvice.eu
|
|
HostName git.service.wiseadvice.eu
|
|
IdentityFile ~/.ssh/deploy_key
|
|
Port 8422
|
|
IdentitiesOnly yes
|
|
EOT
|
|
fi
|
|
|
|
|
|
mkdir -p $WA_REPO
|
|
|
|
echo "export CLIENT_NAME=$CLIENT_NAME" > $WA_REPO/export_client_name.sh
|
|
source $WA_REPO/export_client_name.sh
|
|
|
|
cd $WA_REPO
|
|
clone_repo_with_retry cimp_$CLIENT_NAME
|
|
clone_repo_with_retry shared_resources_k8s
|
|
clone_repo_with_retry common_sysadmin
|
|
|
|
|
|
ln -sf $WA_REPO/common_sysadmin/k8s $HOME/kubeconfs_common
|
|
ln -sf $WA_REPO/shared_resources_k8s/k8s $HOME/kubeconfs_common
|
|
ln -sf $WA_REPO/shared_resources_k8s/manifest.sh $HOME/.zshrc
|
|
ln -sf $WA_REPO/cimp_$CLIENT_NAME/k8s $HOME/kubeconfs
|
|
|
|
echo "source .zshrc after pull"
|
|
source $HOME/.zshrc
|
|
else
|
|
echo "Aborting"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
selections=(
|
|
"Init Master"
|
|
"Join Worker"
|
|
"Stop Setup"
|
|
)
|
|
|
|
choose_from_menu "Please make a choice:" selected_choice "${selections[@]}"
|
|
|
|
if [ "$selected_choice" = "Init Master" ]; then
|
|
echo "<=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=>"
|
|
echo "initializing K8s Master Set-Up"
|
|
source $WA_REPO/shared_resources_k8s/k8s/init_master.sh
|
|
else if [ "$selected_choice" = "Join Worker" ]; then
|
|
echo "<=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=>"
|
|
echo "initializing K8s Master Set-Up"
|
|
source $WA_REPO/shared_resources_k8s/k8s/init_master.sh
|
|
else
|
|
echo "Aborting"
|
|
exit 0
|
|
if |