Browse Source

修复脚本bug

pull/71/head
somunslotus 1 year ago
parent
commit
e88bfd3461
3 changed files with 84 additions and 219 deletions
  1. +0
    -142
      k8s/AAdeploy.sh
  2. +4
    -4
      k8s/build_and_deploy.sh
  3. +80
    -73
      k8s/deploy.sh

+ 0
- 142
k8s/AAdeploy.sh View File

@@ -1,142 +0,0 @@
#!/bin/bash

# 记录开始时间
start=$(date +%s)
# 默认参数
service="manage-front"
env="dev"

show_help() {
echo "Usage: $0 [-s service] [-e environment]"
echo
echo "Options:"
echo " -s Service to deploy (manage-front, manage, front, all default: manage-front)"
echo " -e Environment (e.g., dev, test, default: dev)"
echo " -h Show this help message"
}

# 解析命令行参数
while getopts "s:e:h" opt; do
case $opt in
s) service=$OPTARG ;;
e) env=$OPTARG ;;
h) show_help; exit 0 ;;
\?) echo "Invalid option -$OPTARG" >&2; exit 1 ;;
esac
done

echo "Deploy service: $service, environment: $env"

# 根据环境设置 IP 地址
if [ "$env" == "dev" ]; then
remote_ip="172.20.32.181"
elif [ "$env" == "test" ]; then
remote_ip="172.20.32.185"
else
echo "Invalid environment - $env"
exit 1
fi

baseDir=/home/somuns/ci4s
tag=$(date +'%Y%m%d%H%M')
remote_deploy_dir=/home/deploy/manage-platform

# 构建镜像函数
build_image() {
local dockerfile=$1
local image=$2
cd ${baseDir}/k8s/dockerfiles
docker build -t ${image} -f ${dockerfile} .
if [ $? -ne 0 ]; then
echo "Build ${image} image fail"
exit 1
fi
docker push ${image}
}

# 复制和替换 YAML 文件函数
prepare_yaml() {
local yaml_file=$1
local image=$2

placeholder="\${${yaml_file%.yaml}-image}"
cd ${baseDir}/k8s/template-yaml
cp -rf ${yaml_file} deploy/
cd deploy/
sed -i "s|${placeholder}|${image}|g" ${yaml_file}
if [ $? -ne 0 ]; then
echo "Replace ${image} image fail"
exit 1
fi

# 建立远程目录并备份文件
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi"
if [ $? -ne 0 ]; then
echo "Failed to create remote directory or backup ${yaml_file}"
exit 1
else
echo "Successfully created remote directory and backup ${yaml_file}"
fi

scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
if [ $? -ne 0 ]; then
echo "Failed to copy ${yaml_file}"
exit 1
else
echo "Successfully copied ${yaml_file}"
fi
}

# 部署服务函数
deploy_service() {
local yaml_file=$1
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}"
if [ $? -ne 0 ]; then
echo "Failed to deploy ${yaml_file}"
exit 1
else
echo "Successfully deployed ${yaml_file}"
fi
}

deploy_nacos() {
local yaml_file=$1
scp ${baseDir}/k8s/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
deploy_service
}

build_and_deploy() {
local dockerfile=$1
local image=$2
local yaml_file=$3

build_image ${dockerfile} ${image}
prepare_yaml ${yaml_file} ${image}
deploy_service ${yaml_file}
}

# 构建和部署 manage 服务
if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then
build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
fi


# 构建和部署 front 服务
if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then
build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
fi


if [ "$service" == "all" ]; then
build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml"
build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml"
build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml"
deploy_nacos "k8s-3nacos.yaml"
fi


# 记录结束时间
end=$(date +%s)
echo "部署成功, 耗时: $((end-start))秒"

+ 4
- 4
k8s/build_and_deploy.sh View File

@@ -19,9 +19,9 @@ show_help() {
echo "Usage: $0 [-b branch] [-s service] [-e environment]" echo "Usage: $0 [-b branch] [-s service] [-e environment]"
echo echo
echo "Options:" echo "Options:"
echo " -b Branch to deploy"
echo " -s Service to deploy (manage-front, manage, front)"
echo " -e Environment (e.g., dev, prod)"
echo " -b Branch to deploy, default: master"
echo " -s Service to deploy (manage-front, manage, front, all, default: manage-front)"
echo " -e Environment (e.g., dev, test, default: dev)"
echo " -h Show this help message" echo " -h Show this help message"
} }


@@ -46,7 +46,7 @@ echo "build success"


# 部署 # 部署
echo "start deploy" echo "start deploy"
sh ${baseDir}/k8s/AAdeploy.sh -s ${service} -e ${env}
sh ${baseDir}/k8s/deploy.sh -s ${service} -e ${env}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Deploy failed" echo "Deploy failed"
exit 1 exit 1


+ 80
- 73
k8s/deploy.sh View File

@@ -10,7 +10,7 @@ show_help() {
echo "Usage: $0 [-s service] [-e environment]" echo "Usage: $0 [-s service] [-e environment]"
echo echo
echo "Options:" echo "Options:"
echo " -s Service to deploy (manage-front, manage, front, all ,default: manage-front)"
echo " -s Service to deploy (manage-front, manage, front, all default: manage-front)"
echo " -e Environment (e.g., dev, test, default: dev)" echo " -e Environment (e.g., dev, test, default: dev)"
echo " -h Show this help message" echo " -h Show this help message"
} }
@@ -25,7 +25,8 @@ while getopts "s:e:h" opt; do
esac esac
done done


echo "deploy service: $service, env: $env"
echo "Deploy service: $service, environment: $env"

# 根据环境设置 IP 地址 # 根据环境设置 IP 地址
if [ "$env" == "dev" ]; then if [ "$env" == "dev" ]; then
remote_ip="172.20.32.181" remote_ip="172.20.32.181"
@@ -37,102 +38,108 @@ else
fi fi


baseDir=/home/somuns/ci4s baseDir=/home/somuns/ci4s
cd ${baseDir}/k8s/dockerfiles
mkdir -p ${baseDir}/k8s/template-yaml/deploy

tag=$(date +'%Y%m%d%H%M') tag=$(date +'%Y%m%d%H%M')
managent=172.20.32.187/ci4s/managent:${tag}
front=172.20.32.187/ci4s/ci4s-front:${tag}

remote_deploy_dir=/home/deploy/manage-platform remote_deploy_dir=/home/deploy/manage-platform
manager_yaml=k8s-7management.yaml
front_yaml=k8s-12front.yaml


# 构建 manage 镜像
if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then
# 构建镜像函数
build_image() {
local dockerfile=$1
local image=$2
cd ${baseDir}/k8s/dockerfiles cd ${baseDir}/k8s/dockerfiles
docker build -t ${managent} -f managent-dockerfile .
if [ "$?" -ne "0" ]; then
echo "build ${managent} image fail"
docker build -t ${image} -f ${dockerfile} .
if [ $? -ne 0 ]; then
echo "Build ${image} image fail"
exit 1 exit 1
fi fi
docker push ${image}
}

# 复制和替换 YAML 文件函数
prepare_yaml() {
local yaml_file=$1
local image=$2


placeholder="\${${yaml_file%.yaml}-image}"
cd ${baseDir}/k8s/template-yaml cd ${baseDir}/k8s/template-yaml
# 复制 YAML 文件
cp -rf ${manager_yaml} deploy/
# 镜像替换
cp -rf ${yaml_file} deploy/
cd deploy/ cd deploy/
placeholder="\${k8s-7management-image}"
#sed -i "s#managenent-image#${managent}#g" ${manager_yaml}
sed -i "s#$placeholder#${managent}#g" ${manager_yaml}
if [ "$?" -ne "0" ];then
echo "replace ${managent} image fail"
exit 3
sed -i "s|${placeholder}|${image}|g" ${yaml_file}
if [ $? -ne 0 ]; then
echo "Replace ${image} image fail"
exit 1
fi fi
# 推送镜像
docker push ${managent}
# 部署服务
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${manager_yaml} ]; then mv ${remote_deploy_dir}/${manager_yaml} ${remote_deploy_dir}/k8s-7management.yaml.bak; fi"
scp ${baseDir}/k8s/template-yaml/deploy/${manager_yaml} root@$remote_ip:${remote_deploy_dir}/${manager_yaml}

# 建立远程目录并备份文件
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Failed to copy ${managent} yaml file"
echo "Failed to create remote directory or backup ${yaml_file}"
exit 1 exit 1
else else
echo "Successfully copied ${managent} yaml file"
echo "Successfully created remote directory and backup ${yaml_file}"
fi fi
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${manager_yaml}"

scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Failed to deploy ${managent} image"
echo "Failed to copy ${yaml_file}"
exit 1 exit 1
else else
echo "Successfully deployed ${managent} image"
echo "Successfully copied ${yaml_file}"
fi fi
fi

}


# 构建 front 镜像
if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then
cd ${baseDir}/k8s/dockerfiles
docker build -t ${front} -f nginx-dockerfile .
if [ "$?" -ne "0" ];then
echo "build ${front} image fail"
exit 2
# 部署服务函数
deploy_service() {
local yaml_file=$1
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}"
if [ $? -ne 0 ]; then
echo "Failed to deploy ${yaml_file}"
exit 1
else
echo "Successfully deployed ${yaml_file}"
fi fi
}


# 复制 YAML 文件
cd ${baseDir}/k8s/template-yaml
cp -rf ${front_yaml} deploy/
deploy_nacos() {
local yaml_file=$1
scp ${baseDir}/k8s/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
deploy_service ${yaml_file}
}


# 镜像替换
cd deploy/
placeholder="\${k8s-12front-image}"
#sed -i "s#front-image#${front}#g" ${front_yaml}
sed -i "s#$placeholder#${front}#g" ${front_yaml}
if [ "$?" -ne "0" ];then
echo "replace ${front} image fail"
exit 4
fi
build_and_deploy() {
local dockerfile=$1
local image=$2
local yaml_file=$3

build_image ${dockerfile} ${image}
prepare_yaml ${yaml_file} ${image}
deploy_service ${yaml_file}
}

# 构建和部署 manage 服务
if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then
build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
fi




# 镜像推送
docker push ${front}
# 构建和部署 front 服务
if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then
build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
fi


#部署服务
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${front_yaml} ]; then mv ${remote_deploy_dir}/${front_yaml} ${remote_deploy_dir}/k8s-12front.yaml.bak; fi"
scp ${baseDir}/k8s/template-yaml/deploy/${front_yaml} root@$remote_ip:${remote_deploy_dir}/${front_yaml}
if [ $? -ne 0 ]; then
echo "Failed to copy ${managent} yaml file"
exit 1
else
echo "Successfully copied ${managent} yaml file"
fi
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${front_yaml}"
if [ $? -ne 0 ]; then
echo "Failed to deploy ${front} image"
exit 1
else
echo "Successfully deployed ${front} image"
fi

if [ "$service" == "all" ]; then
#部署前端
build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
#部署管理平台
build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
#部署认证中心
build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml"
#部署网关
build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml"
#部署系统服务
build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml"
#部署配置中心
deploy_nacos "k8s-3nacos.yaml"
fi fi






Loading…
Cancel
Save