set -e
appNamespace=nvpc-apps-01
appInfraConfigmap=baize-infra-cm
appSetVersion=
appConfigVersion=
partyNum=
directory=${HOME}/baize
appRepoURL=git@192.168.50.7:baize/baize-edge-infra.git
appRepoRevision=feature/3.2.0-val
databaseRepoURL=git@192.168.50.7:baize/baize-database.git
databaseRepoRevision=release/3.2.0
appIntranetIp=
appExtranetIp=
appVirtualIp=
# k8s containerd/docker拉取镜像的harbor 地址(格式: ip:port, port不指定默认443)
registryServer=
registryUsername=
registryPassword=
config=startup/latest/values.conf.example
print_bold_green(){
echo -e "\033[1;32m${1}\033[0m"
}
# 显示脚本的用法
usage() {
echo ""
echo "Usage: $0 [OPTIONS]"
echo "Example: "
echo 'bash '$0' -c startup/3.2.0/values_test_50_67.conf \'
echo "-v 3.2.0 -o 3.2.0 -p 1 -b feature/3.2.0-val -r release/3.2.0 -t 118.0.0.1"
echo ""
echo 'bash '$0' -c startup/3.2.0/values_test_50_67.conf \'
echo '-v 3.2.0 -o 3.2.0 -p 1 -b feature/3.2.0-val -r release/3.2.0 -t 118.0.0.1 -i 192.168.50.32 \'
echo '-g git@192.168.50.7:baize/baize-edge-infra.git -u git@192.168.50.7:baize/baize-database.git'
echo "Options:"
echo '
-c | --config ) shift; config=$1 ;;
-v | --appSetVersion ) shift; appSetVersion=$1 ;;
-o | --appConfigVersion ) shift; appConfigVersion=$1 ;;
-p | --partyNum ) shift; partyNum=$1 ;;
-d | --directory ) shift; directory=$1 ;;
-g | --appRepoURL ) shift; appRepoURL=$1 ;;
-b | --appRepoRevision ) shift; appRepoRevision=$1 ;;
-u | --databaseRepoURL ) shift; databaseRepoURL=$1 ;;
-r | --databaseRepoRevision ) shift; databaseRepoRevision=$1 ;;
-i | --appIntranetIp ) shift; appIntranetIp=$1 ;;
-e | --appExtranetIp ) shift; appExtranetIp=$1 ;;
-t | --appVirtualIp ) shift; appVirtualIp=$1 ;;
-n | --registryServer ) shift; registryServer=$1 ;;
-x | --registryUsername ) shift; registryUsername=$1 ;;
-w | --registryPassword ) shift; registryPassword=$1 ;;
-h | --help ) usage; exit 1 ;;
* ) echo "Unknown option: $1"; usage; exit 1 ;;
'
}
check_variable() {
local var_name=$1
local var_value=${!var_name} # 使用间接引用获取变量的值
if [ -z "$var_value" ]; then
echo "Error: $var_name 不能为空"
usage
exit 1
fi
}
# 解析参数
while [[ "$1" != "" ]]; do
case $1 in
-c | --config ) shift; config=$1 ;;
-v | --appSetVersion ) shift; appSetVersion=$1 ;;
-o | --appConfigVersion ) shift; appConfigVersion=$1 ;;
-p | --partyNum ) shift; partyNum=$1 ;;
-d | --directory ) shift; directory=$1 ;;
-g | --appRepoURL ) shift; appRepoURL=$1 ;;
-b | --appRepoRevision ) shift; appRepoRevision=$1 ;;
-u | --databaseRepoURL ) shift; databaseRepoURL=$1 ;;
-r | --databaseRepoRevision ) shift; databaseRepoRevision=$1 ;;
-i | --appIntranetIp ) shift; appIntranetIp=$1 ;;
-e | --appExtranetIp ) shift; appExtranetIp=$1 ;;
-t | --appVirtualIp ) shift; appVirtualIp=$1 ;;
-n | --registryServer ) shift; registryServer=$1 ;;
-x | --registryUsername ) shift; registryUsername=$1 ;;
-w | --registryPassword ) shift; registryPassword=$1 ;;
-h | --help ) usage; exit 1 ;;
* ) echo "Unknown option: $1"; usage; exit 1 ;;
esac
shift
done
if [ -z "${appIntranetIp}" ];then
# host_ip_list=$(hostname -I)
# echo "机器IP列表: ${host_ip_list[*]}, 请选择"
# for item in ${host_ip_list[@]}; do
# print_bold_green "${item}"
# read -p "请输入[Y|y]确认,[X|x]退出,[其他任意键]继续获取IP: " REPLY0
# if [[ $REPLY0 =~ ^[Yy]$ ]]; then
# appIntranetIp="${item}"
# break
# elif [[ $REPLY0 =~ ^[Xx]$ ]]; then
# exit 1
# else
# continue
# fi
# done
host_ip_list=($(hostname -I))
echo "机器IP列表: ${host_ip_list[*]}, 请选择"
i=0
while [ -z "${appIntranetIp}" ]; do
item=${host_ip_list[$i]}
print_bold_green "${item}"
read -p "请输入[Y|y]确认,[X|x]退出,[其他任意键]继续获取IP: " REPLY0
if [[ $REPLY0 =~ ^[Yy]$ ]]; then
appIntranetIp="${item}"
break
elif [[ $REPLY0 =~ ^[Xx]$ ]]; then
exit 1
else
i=$((i+1))
fi
if [ $i -ge ${#host_ip_list[@]} ];then
i=0
fi
done
echo -n "你选择的IP是: "; print_bold_green "${appIntranetIp}"
fi
# appConfigVersion 如果没有指定,则同appSetVersion
appConfigVersion=${appConfigVersion:-"${appSetVersion}"}
appExtranetIp=${appExtranetIp:-"${appIntranetIp}"}
appVirtualIp=${appVirtualIp:-"${appIntranetIp}"}
check_variable partyNum
check_variable appVirtualIp
check_variable appIntranetIp
check_variable appExtranetIp
#check_variable appSetVersion
#check_variable appConfigVersion
#check_variable appRepoURL
#check_variable appRepoRevision
#check_variable databaseRepoURL
#check_variable databaseRepoRevision
#check_variable registryServer
#check_variable registryUsername
#check_variable registryPassword
export partyNum
export appVirtualIp
export appIntranetIp
export appExtranetIp
export appSetVersion
export appConfigVersion
export appRepoURL
export appRepoRevision
export databaseRepoURL
export databaseRepoRevision
export registryServer
export registryUsername
export registryPassword
echo "put configmap ${appNamespace} ${appInfraConfigmap}"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: ${appInfraConfigmap}
namespace: ${appNamespace}
data:
partyNum: "$(echo -n "${partyNum}")"
appVirtualIp: "$(echo -n "${appVirtualIp}")"
appIntranetIp: "$(echo -n "${appIntranetIp}")"
appExtranetIp: "$(echo -n "${appExtranetIp}")"
appSetVersion: "$(echo -n "${appSetVersion}")"
appConfigVersion: "$(echo -n "${appConfigVersion}")"
appRepoURL: "$(echo -n "${appRepoURL}")"
appRepoRevision: "$(echo -n "${appRepoRevision}")"
databaseRepoURL: "$(echo -n "${databaseRepoURL}")"
databaseRepoRevision: "$(echo -n "${databaseRepoRevision}")"
registryServer: "$(echo -n "${registryServer}")"
registryUsername: "$(echo -n "${registryUsername}")"
registryPassword: "$(echo -n "${registryPassword}")"
EOF
mkdir -p ${directory}/{.ssh,.kube}
if [ ! -f ${directory}/.kube/config ];then
# 把k8s config文件 config 放到 ${directory}/.kube/目录下
cp $HOME/.kube/config ${directory}/.kube/
fi
if [ ! -f ${directory}/.ssh/id_rsa ];then
# 把git密钥文件 id_rsa, id_rsa.pub 放到 ${directory}/.ssh/目录下
cp $HOME/.ssh/{id_rsa,id_rsa.pub} ${directory}/.ssh/
fi
if [ ! -f ${directory}/ssh-agent.sh ];then
echo '#!/bin/bash
eval "$(ssh-agent -s)"
ssh-add '${directory}'/.ssh/id_rsa' | tee ${directory}/ssh-agent.sh
fi
source ${directory}/ssh-agent.sh
# infra仓库名字
appRepoName=$(echo "$appRepoURL" | awk -F'/' '{print $NF}'| sed 's/\.git$//')
if [ ! -d ${directory}/${appRepoName} ];then
cd ${directory}
git clone -b ${appRepoRevision} ${appRepoURL} && cd "$(basename $_ .git)"
else
cd ${directory}/${appRepoName}
git checkout ${appRepoRevision}
git pull
fi
bash startup/install.sh -c ${config} -a migrate-nacos,migrate-database,migrate-app -m -s -y