Files
incubator-seata/script/config-center/zk/zk-config.sh
wangchunxiang c2453d6434 chore(project): 添加项目配置文件和忽略规则
- 添加 Babel 配置文件支持 ES6+ 语法转换
- 添加 ESLint 忽略规则和配置文件
- 添加 Git 忽略规则文件
- 添加 Travis CI 配置文件
- 添加 1.4.2 版本变更日志文件
- 添加 Helm 图表辅助模板文件
- 添加 Helm 忽略规则文件
2026-03-27 17:36:48 +08:00

103 lines
2.5 KiB
Bash

#!/usr/bin/env bash
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at、
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# The purpose is to sync the local configuration(config.txt) to zk.
# This script need to rely on zk.
while getopts ":h:p:z:" opt
do
case $opt in
h)
host=$OPTARG
;;
p)
port=$OPTARG
;;
z)
zkHome=$OPTARG
;;
?)
echo " USAGE OPTION: $0 [-h host] [-p port] [-z zkHome] "
exit 1
;;
esac
done
if [[ -z ${host} ]]; then
host=localhost
fi
if [[ -z ${port} ]]; then
port=2181
fi
if [[ -z ${zkHome} ]]; then
echo " zk home is empty, please usage option: [-z zkHome] "
exit 1
fi
zkAddr=$host:$port
root="/seata"
tempLog=$(mktemp -u)
echo "ZK address is $zkAddr"
echo "ZK home is $zkHome"
echo "ZK config root node is $root"
function check_node() {
"$2"/bin/zkCli.sh -server "$1" ls ${root} >/dev/null 2>"${tempLog}"
}
function create_node() {
"$2"/bin/zkCli.sh -server "$1" create ${root} "" >/dev/null
}
function create_subNode() {
"$2"/bin/zkCli.sh -server "$1" create "${root}/$3" "$4" >/dev/null
}
function delete_node() {
"$2"/bin/zkCli.sh -server $1 rmr ${root} "" >/dev/null
}
check_node "${zkAddr}" "${zkHome}"
if [[ $(cat "${tempLog}") =~ "No such file or directory" ]]; then
echo " ZK home is error, please enter correct zk home! "
exit 1
elif [[ $(cat "${tempLog}") =~ "Exception" ]]; then
echo " Exception error, please check zk cluster status or if the zk address is entered correctly! "
exit 1
elif [[ $(cat "${tempLog}") =~ "Node does not exist" ]]; then
create_node "${zkAddr}" "${zkHome}"
else
read -p "${root} node already exists, now delete ${root} node in zk, y/n: " result
if [[ ${result} == "y" ]]; then
echo "Delete ${root} node..."
delete_node "${zkAddr}" "${zkHome}"
create_node "${zkAddr}" "${zkHome}"
else
exit 0
fi
fi
for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
key=${line%%=*}
value=${line#*=}
echo "Set" "${key}" "=" "${value}"
create_subNode "${zkAddr}" "${zkHome}" "${key}" "${value}"
done