nacos
#!/bin/bash # Copyright (c) 2024 xx.Co.Ltd. All rights reserved. # @Author: Muzhi # @Date: 2024-07-28 11:06:31 # @Last Modified by: Muzhi # @Last Modified time: 2024-07-28 11:06:31 LOG_ERROR() { local content=${1} echo -e "\033[31m${content}\033[0m" } LOG_INFO() { local content=${1} echo -e "\033[32m${content}\033[0m" } LOG_WARN() { local content=${1} echo -e "\033[33m${content}\033[0m" } LOG_LINK() { local content=${1} echo -e "\033[4m${content}\033[0m" } LOG_DEBUG() { local content=${1} echo -e "\033[34m${content}\033[0m" } # 显示脚本的用法 usage() { echo "Usage: $0 [OPTIONS] --namespace NAMESPACE" echo "Options:" echo "example: bash $0 -e test -s http://192.168.60.49:38848 -n nvpc-apps-01 -u test -p Nvx@1024 -g DEFAULT_GROUP" echo " bash $0 -e test -c nacos.conf" echo " -s | --server NACOS_ADDRESS Specify server address" echo " -n | --namespace NACOS_NAMESPACE Specify namespace" echo " -u | --username NACOS_USERNAME Specify username" echo " -p | --password NACOS_PASSWORD Specify password" echo " -g | --group NACOS_GROUP Specify group" echo " -d | --date DATA_ID Specify data id" echo " -c | --config CONFIG_FILE Specify config file" echo " -e | --env NACOS_ENV Specify env" echo " -v | --verbose Print log" echo " -h | --help Display this help message" exit 1 } # 解析参数 while [[ "$1" != "" ]]; do case $1 in -s | --server ) shift; NACOS_SERVER=$1 ;; -n | --namespace ) shift; NACOS_NAMESPACE=$1 ;; -u | --username ) shift; NACOS_USERNAME=$1 ;; -p | --password ) shift; NACOS_PASSWORD=$1 ;; -g | --group ) shift; NACOS_GROUP=$1 ;; -d | --data ) shift; DATA_ID=$1 ;; -c | --config ) shift; CONFIG_FILE=$1 ;; -e | --env ) shift; NACOS_ENV=$1 ;; -v | --verbose ) VERBOSE=1 ;; -h | --help ) usage ;; * ) echo "Unknown option: $1"; usage ;; esac shift done WORK_PATH=$(cd -P $(dirname $0); pwd) if [ "${NACOS_ENV}" = "" ]; then LOG_ERROR "env not specify" exit 1; fi if [ -n "${CONFIG_FILE}" ]; then if [ ! -f "${WORK_PATH}/env/${NACOS_ENV}/${CONFIG_FILE}" ]; then LOG_ERROR "${CONFIG_FILE} does not exist." exit 1 else source "${WORK_PATH}/env/${NACOS_ENV}/${CONFIG_FILE}" fi fi if [ "${NACOS_SERVER}" = "" -o "${NACOS_USERNAME}" = "" -o "${NACOS_PASSWORD}" = "" -o "${NACOS_GROUP}" = "" ]; then LOG_ERROR "Nacos server param not specify" exit 1; fi LOG_DEBUG "Nacos server: ${NACOS_SERVER}, username: ${NACOS_USERNAME}, namespace: ${NACOS_NAMESPACE}" request_login_result=$(curl -s -X POST "$NACOS_SERVER/nacos/v1/auth/login" -d "username=$NACOS_USERNAME&password=$NACOS_PASSWORD") request_access_token=$(jq -r '.accessToken' <<<"$request_login_result") push_nacos_content(){ data_id=$(basename "$1") nacos_local_content=$(cat "$1") nacos_remote_content=$(curl -s -X GET "$NACOS_SERVER/nacos/v1/cs/configs?accessToken=$request_access_token&tenant=$NACOS_NAMESPACE&dataId=$data_id&group=$NACOS_GROUP") if [ "$nacos_remote_content" != "$nacos_local_content" ]; then request_push_result=$(curl -m 10 -s -X POST "$NACOS_SERVER/nacos/v1/cs/configs" \ -d "accessToken=$request_access_token&tenant=$NACOS_NAMESPACE&dataId=$data_id&group=$NACOS_GROUP&type=yaml" \ --data-urlencode "content=$nacos_local_content") if [ "$request_push_result" != "true" ]; then LOG_ERROR "Nacos 配置推送失败: ${request_push_result}" exit 1 fi else LOG_INFO "Nacos 配置没有变动" fi } if [ -n "$request_access_token" ]; then data_id_array=$(find "${WORK_PATH}/templates/" -type f -name "*.yml" -exec realpath {} \;) for item in $data_id_array ; do push_nacos_content $item done else LOG_ERROR "Nacos 登陆失败: ${NACOS_SERVER}, username: ${NACOS_USERNAME}" exit 1 fi