GBase8a V86 & V95 自动安装步骤及脚本

一. 文件说明:

1 db_config.conf 为待搭建集群配置基础信息(环境配置信息、环境基础信息、节点规划信息、用户密码信息、VC划分信息、硬件检测信息、其他配置信息)
2 pre_check.sh 根据db_config.conf配置参数对集群安装前环境检查,支持V86 V95版本
3 autoinstall.sh 根据db_config.conf配置参数进行GBase8a集群安装,支持V86 V95版本
4 force_unistall.sh 根据db_config.conf配置参数进行已装集群卸载,支持V86 V95版本
5 logger.sh 脚本执行时输出日志级别相关脚本
6 sshtrust.sh 系统用户互信脚本
7 db_source.conf 软件安装包所在FTP服务器路径及安装包版本,如无FTP服务器,则将安装包解压在db_config.conf中install_source_dir目录

 

二. 操作步骤(root用户执行):

## 2.1 准备安装脚本、工具、安装包
#创建脚本存放目录,将相关脚本上传至/opt/gbase_workspace/scripts/autoinstall/bin/目录下
[root@GBASEMA01 ~]# mkdir -p /opt/gbase_workspace/scripts/autoinstall/bin
#创建MPP安装包存放目录,将MPP安装包上传至/opt/gbase_workspace/setup/目录下,并解压成gcinstall
[root@GBASEMA01 ~]# mkdir -p /opt/gbase_workspace/setup
#创建相关工具存放目录,将C3工具、FIO工具RPM包上传至/opt/gbase_workspace/tools/目录下
[root@GBASEMA01 ~]# mkdir -p /opt/gbase_workspace/tools

## 2.2 参数配置
[root@GBASEMA01 ~]# cd /opt/gbase_workspace/scripts/autoinstall/bin
[root@GBASEMA01 bin]# vim db_config.conf     ## 按安装需求填写参数值

## 2.3 环境检查
[root@GBASEMA01 bin]# sh pre_check.sh        ##无报错则通过

## 2.4 软件安装
[root@GBASEMA01 bin]# sh autoinstall.sh

## 2.5 验证集群
#管理节点与核心节点服务状态皆为OPEN,则验证通过
[root@GBASEMA01 modifyParam_newInstall]# su - gbase -c "gcadmin"

注:可根据项目需要,增加其他运维代码/脚本,如调整数据库参数、开启审计日志、配置监控任务等

 

附录 1 db_config.conf

###### 参数项中标记 * 部分必须确定并填写正确,其他内容暂可不修改 ######

#### 1) 集群CONFIG配置信息
## * 环境名称,每套环境一个名称,用于识别本集群。
GBaseName=DEMO
## * 集群模式,v953多vc模式设置成1,v86版本及v95版本兼容设置成2
GBaseMode=1
## * 集群是否多实例,是:1, 否:0
InstanceType=1

#### 2) 集群环境基础信息
## 软件安装目录(缺省,默认为/opt目录) 
install_target=/opt
## SSH协议端口(缺省,默认为22) 
ssh_port=22
## * 软件安装版本,可选:gbase8a_v862/gbase8a_v952/gbase8a_v953
db_source_file=gbase8a_v953
## * 集群字符集 缺省值utf8,可选值utf8 utf8mb4 gbk 
characterSet=utf8mb4

#### 3) 集群节点规划信息
## * 节点私网IP地址前缀
ip_prefix=192.168
## * 管理节点私网IP地址列表(多个IP以","分割)
coor_ip_list=192.168.195.10
## * 数据节点第一个实例私网IP地址列表(多个IP以","分割)
data_ip_list1=192.168.195.101,192.168.195.103
## * 数据节点第二个实例私网IP地址列表,InstanceType=1时必填
data_ip_list2=192.168.195.102,192.168.195.104

#### 4) 操作系统及数据库用户信息
## * 操作系统root用户密码
root_pwd=gbase
## * 操作系统gbase用户密码
gbase_pwd=gbase
## * 数据库root用户初始密码
db_root_new_pwd=X@gbase2024
## * 数据库gbase用户初始密码
db_gbase_new_pwd=X@gbase2024
## 数据库gbase用户默认密码(暂无需修改)
db_gbase_pwd=gbase20110531

#### 5) VC划分配置信息
# V862 或 V95  使用默认VC(无VC)时,以下内容均设置为空 。
# V952 或 V953 使用 VC 时,按以下格式设置,最多可以设置3组。注释不需要加引号。(节点双实例IP必须在同一个VC中)
## * 第一个VC信息
vc_name1=vc1
vc_comm1=vc1name
vc_list1=192.168.195.101,192.168.195.102
## * 第二个VC信息
vc_name2=vc2
vc_comm2=vc2name
vc_list2=192.168.195.103,192.168.195.104
## * 第三个VC信息
vc_name3=
vc_comm3=
vc_list3=

#### 6) 内存配置检测
## 是否跳过 内存检测 1: 跳过,0或空: 不跳过
skip_memory_test=1
## 内存检测标准。#256写245 512写500
#  管理节点内存最小值(单位为GB)
coor_memory_GB=245
coor_swap_GB=30
#  数据节点内存最小值(单位为GB)
data_memory_GB=500
data_swap_GB=30

#### 7) 磁盘挂载检测
## 是否跳过 磁盘挂载检测 1: 跳过,0或空: 不跳过
skip_diskmount_test=1
## 磁盘检测标准:
# 磁盘挂载目录
mount_point=/opt
# 管理节点磁盘容量最小值(单位为TB)
coor_disk_size_TB=5
# 数据节点磁盘容量最小值(单位为TB)
data_disk_size_TB=30

#### 8) 磁盘性能测试
## 是否跳过 fio测试 1: 跳过, 0或空: 不跳过
skip_fio_test=1
## 管理节点fio测试IOPS值
coor_fio_iops=500
## 数据节点fio测试IOPS值
data_fio_iops=1000

#### 9) 其他配置信息
## * 软件包镜像文件地址 local-本地,test-测试,值为空时默认为local
mirror_site=local
## 软件包存放目录
install_source_dir=/opt/gbase_workspace/setup
## C3工具安装包路径+名称
c3_pack=/opt/gbase_workspace/tools/c3-5.1.2-1.noarch.rpm
## fio安装包路径+名称
fio_pack=/opt/gbase_workspace/tools/fio-3.7-1.el7.x86_64.rpm
## 软件安装日志输出级别: #debug:0; info:1; warn:2; error:3
loglevel=0

附录 2 pre_check.sh

#!/bin/bash
###############################################################################
##creator : TuJunbing                                                        ##
##create time: 2024-7-10                                                     ##
##Decription: pre_check.sh                                                   ##
##Version: 1.1 			                                                     ##
###############################################################################

wd=$(cd `dirname $0` && pwd)
updir=`dirname ${wd}`

logdir=${updir}/log
if [ ! -d "${logdir}" ]
then
	mkdir -p ${logdir}
fi

logfile=${logdir}/pre_check_`date '+%Y%m%d'`.log
export loglevel=$(grep loglevel ${wd}/db_config.conf | awk -F= '{print $2}' | awk '$1=$1')

log(){
	if [ "${1,,}" == "error" ] || [ "${1,,}" == "warn" ] 
	then
		sh logger.sh ${logfile} $1 "[$0] [line:`caller |awk '{print $1}'`] $2"
	else
		sh logger.sh ${logfile} $1 "$2"
	fi
}

log debug "wd=${wd} updir=${updir}"

flag=0

read_config()
{
	log debug "${FUNCNAME[@]}"
	config_file=${wd}/db_config.conf
	db_source_conf_file=${wd}/db_source.conf

	#删除环境检查标识(1:已检查通过)
	sed -i '/check_finish_env/d' ${config_file}
	
	while read line 
	do
		comment=$(echo $line | grep "#")
		if [ "${comment}" != "" ]
		then
			continue
		fi
			
		line_trim=`echo "${line}" |awk -F# '{print $1}' |grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		if [ -z "${line_trim}" ]
		then
			continue
		fi
		
		varname=`echo ${line_trim} | awk -F= '{print $1}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		varvalue=`echo ${line_trim} | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`

		if [[ ${varname} =~ 'pwd' ]] || [[ ${varname} =~ 'password' ]]
		then
			varvaluemask=********
		else
			varvaluemask=${varvalue}
		fi

		#log debug "varname='${varname}',varvalue='${varvaluemask}'"

		if [ -z "${varvalue}" ]
		then 
			#unset varname
			log debug "读取配置参数 ${varname}=${varvalue}"
		else
			log debug "读取配置参数 ${varname}=${varvaluemask}"
			export ${varname}="${varvalue}"
		fi

	done < ${config_file}

	if [ -n "${mirror_site}" ] 
	then
		if [ "${mirror_site}" == "local" ] || [ -z "${mirror_site}" ]
		then
			gc_dir="${install_source_dir}/gcinstall"
			
			if [ ! -d "${gc_dir}" ]
			then
				log warn "未检查到已解压的安装程序gcinstall,请在开始安装前将软件安装包存放至目录:${install_source_dir}内,并对其进解压处理。"
			else
				
				# 安装包版本与配置文件检查
				src_version=`grep release_version ${gc_dir}/BUILDINFO | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
				if [ $(echo ${src_version} |grep '8\.6\.2' |wc -l) -gt 0 ]; then 
					db_version=gbase8a_v862
				elif [ $(echo ${src_version} |grep '9\.5\.2' |wc -l) -gt 0 ]; then 
					db_version=gbase8a_v952
				elif [ $(echo ${src_version} |grep '9\.5\.3' |wc -l) -gt 0 ]; then 
					db_version=gbase8a_v953
				else 
					db_version=""
				fi
				
				if [ "${db_source_file}" != "${db_version}" ]
				then
					flag=1
					log error "配置文件中软件安装版本参数“db_source_file”值 ${db_source_file} 与软件安装包的数据库版本 ${db_version} 不一致,请确保软件安装的版本与实际安装版本一致。"
				fi
				
				# 只有V953版本因BUILDINFOarch项,而且安装包不唯一,其他版本暂无解决方案
				src_arch=`grep arch ${gc_dir}/BUILDINFO | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
				cpu_arch=`lscpu | grep Architecture | awk -F: '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
				
				if [ -z "${src_arch}" ]
				then
					log warn "操作系统CPU类型 ${cpu_arch} 未检查出软件安装包所适配CPU类型,请人工核对二者是否适配。"
				elif [ "${src_arch}" != "${cpu_arch}" ]
				then
					flag=1
					log error "操作系统CPU类型 ${cpu_arch} 与软件安装包所适配CPU类型 ${src_arch} 不一致,请确保软件安装包适配本操作系统版本及CPU类型。"
				fi
			fi
		else
			while read line 
			do
				line_trim=`echo "${line}" |awk -F# '{print $1}' |grep -o "[^ ]\+\( \+[^ ]\+\)*"`

				varname=`echo ${line_trim} | awk -F= '{print $1}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
				varvalue=`echo ${line_trim} | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`

				if [ "${varname}" == "${mirror_site}" ] || [ "${varname}" == "${db_source_file}" ]
				then
		 
					if [[ ${varname} =~ 'pwd' ]] || [[ ${varname} =~ 'password' ]]
					then
						varvaluemask=********
					else
						varvaluemask=${varvalue}
					fi
			
					#log debug "varname='${varname}',varvalue='${varvaluemask}'"
			
					if [ -z "${varvalue}" ]
					then 
						unset varname
					else
						log debug "读取配置参数 ${varname}=${varvaluemask}"
						export ${varname}="${varvalue}"
					fi
				fi			

			done < ${db_source_conf_file}

			log debug "读取配置参数 full_target_url=${!mirror_site}/${!db_source_file}"
			export full_target_url=${!mirror_site}/${!db_source_file}
		fi
	fi
}


check_config()
{
	## 空值检查
	isNull GBaseName "${GBaseName}"
	isNull GBaseMode "${GBaseMode}"
	isNull InstanceType "${InstanceType}"
	isNull db_source_file "${db_source_file}"
	isNull ip_prefix "${ip_prefix}"
	isNull coor_ip_list "${coor_ip_list}"
	isNull data_ip_list1 "${data_ip_list1}"
	isNull root_pwd "${root_pwd}"
	isNull gbase_pwd "${gbase_pwd}"
	isNull db_gbase_new_pwd "${db_gbase_new_pwd}"
	isNull db_root_new_pwd "${db_root_new_pwd}"
	isNull db_gbase_pwd "${db_gbase_pwd}"
	isNull loglevel "${loglevel}"
	isNull install_source_dir "${install_source_dir}"
	isNull c3_pack "${c3_pack}"
	
	##字符串检查
	validStr vc_name1 "${vc_name1}" 64
	validStr vc_name2 "${vc_name2}" 64
	validStr vc_name3 "${vc_name3}" 64
	validStr vc_comm1 "${vc_comm1}" 60
	validStr vc_comm2 "${vc_comm2}" 60
	validStr vc_comm3 "${vc_comm3}" 60
	
	## 集群模式参数检查
	if [ "${GBaseMode}" == 1 ]
	then
		if [ -z "${vc_name1}" ] || [ -z "${vc_comm1}" ] || [ -z "${vc_list1}" ]
		then
			log error "“GBaseMode”参数值为“1”时,“vc_name1”、“vc_comm1”、“vc_list1”参数必须有值!"
			flag=1
		fi
		
		if [ "${db_source_file}" != "gbase8a_v953" ]
		then
			log error "“GBaseMode”参数值为“1”时,“db_source_file”参数必须为“gbase8a_v953”!"
			flag=1
		fi
	elif [ "${GBaseMode}" != 1 ] && [ "${GBaseMode}" != 2 ]
	then
		log error "“GBaseMode”参数值必须为 1 或 2!"
		flag=1
	fi
	
	## 多实例参数检查
	if [ "${InstanceType}" == 0 ]
	then		
		if [ -n "${data_ip_list2}" ]
		then
			log error "“InstanceType”参数值为“0”时,“data_ip_list2”参数值必须为空!"
			flag=1
		fi
	elif [ "${InstanceType}" == 1 ]
	then
		if [ -z "${data_ip_list2}" ]
		then
			log error "“InstanceType”参数值为“1”时,“data_ip_list2”参数必须有值!"
			flag=1
		fi
	elif [ "${InstanceType}" != 1 ] && [ "${InstanceType}" != 0 ]
	then
		log error "“InstanceType”参数值必须为 0 或 1!"
		flag=1
	fi
	
	## 软件安装版本参数检查
	if [ -n "${db_source_file}" ]
	then
		if [ "${db_source_file}" != "gbase8a_v862" ] && [ "${db_source_file}" != "gbase8a_v952" ] && [ "${db_source_file}" != "gbase8a_v953" ]
		then
			log error "“db_source_file”参数值必须为 gbase8a_v862 gbase8a_v952 gbase8a_v953 其中之一!"
			flag=1
		fi
		
		if [ "${db_source_file}" == "gbase8a_v862" ]
		then
			if [ "${set_variables}" != 0 ]
			then
				log error "“db_source_file”参数值为“gbase8a_v862”时,“set_variables”参数必须为“0”!"
				flag=1
			fi
		fi
	fi
	
	## 集群字符集参数检查
	if [ -n "${characterSet}" ]
	then
		if [ "${characterSet}" != "utf8" ] && [ "${characterSet}" != "utf8mb4" ] && [ "${characterSet}" != "gbk" ]
		then
			log error "“characterSet”参数值必须为 utf8 utf8mb4 gbk 其中之一!"
			flag=1
		fi
	fi
	
	## 数据节点IP地址检查
	dataIP=${data_ip_list1}
	if [ -n "${data_ip_list2}" ]
	then
		dataIP=${data_ip_list1},${data_ip_list2} 
	fi
	
	if [ -n "${vc_list1}" ] && [ "${GBaseMode}" == 1 ]
	then
		vc1_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		vc1_tmp=( $(echo ${vc1_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc1_tmp[@]}
		do
			if [ `echo "${dataIP[@]}" | grep -w "${ip}" | wc -l` == 0 ] 
			then
				log error "“vc_list1”参数值 ${ip} 不在参数data_ip_list的值中!"
				flag=1
			fi
		done
	fi
	
	if [ -n "${vc_list2}" ] && [ "${GBaseMode}" == 1 ]
	then
		vc2_tmp=( `echo ${vc_list2} |sed 's/,/ /g'` )
		vc2_tmp=( $(echo ${vc2_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc2_tmp[@]}
		do
			if [ `echo "${dataIP[@]}" | grep -w "${ip}" | wc -l` == 0 ] 
			then
				log error "“vc_list2”参数值 ${ip} 不在参数data_ip_list的值中!"
				flag=1
			fi
		done
		
		if [ -z "${vc_list1}" ]
		then
			log error "“vc_list2”参数有值时, 参“vc_list1”必须有值!"
			flag=1
		fi
		
		vc1_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		vc1_tmp=( $(echo ${vc1_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc2_tmp[@]}
		do
			if [ `echo "${vc1_tmp[@]}" | grep -w "${ip}" | wc -l` -gt 0 ] 
			then
				log error "“vc_list2”参数值 ${ip} 存在“vc_list1”参数中!"
				flag=1
			fi
		done
	fi
	
	if [ -n "${vc_list3}" ] && [ "${GBaseMode}" == 1 ]
	then
		vc3_tmp=( `echo ${vc_list3} |sed 's/,/ /g'` )
		vc3_tmp=( $(echo ${vc3_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc3_tmp[@]}
		do
			if [ `echo "${dataIP[@]}" | grep -w "${ip}" | wc -l` == 0 ] 
			then
				log error "“vc_list3”参数值 ${ip} 不在参数data_ip_list的值中!"
				flag=1
			fi
		done
		
		vc1_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		vc1_tmp=( $(echo ${vc1_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc3_tmp[@]}
		do
			if [ `echo "${vc1_tmp[@]}" | grep -w "${ip}" | wc -l` -gt 0 ] 
			then
				log error "“vc_list3”参数值 ${ip} 存在“vc_list1”参数中!"
				flag=1
			fi
		done
		
		if [ -z "${vc_list2}" ]
		then
			log error "“vc_list3”参数有值时, 参“vc_list2”必须有值!"
			flag=1
		fi
		
		vc2_tmp=( `echo ${vc_list2} |sed 's/,/ /g'` )
		vc2_tmp=( $(echo ${vc2_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc3_tmp[@]}
		do
			if [ `echo "${vc2_tmp[@]}" | grep -w "${ip}" | wc -l` -gt 0 ] 
			then
				log error "“vc_list3”参数值 ${ip} 存在“vc_list2”参数中!"
				flag=1
			fi
		done
	fi
	
	## 内存配置参数检查
	if [ "${skip_memory_test}" != 1 ]
	then
		if [ -z "${coor_memory_GB}" ] || [ -z "${coor_swap_GB}" ] || [ -z "${data_memory_GB}" ] || [ -z "${data_swap_GB}" ]
		then
			log error "“skip_memory_test”参数值为“1”时,“coor_memory_GB”、“coor_swap_GB”、“data_memory_GB”、“data_swap_GB”参数必须有值!"
			flag=1
		fi
	elif [ "${skip_memory_test}" != 1 ] && [ "${skip_memory_test}" != 0 ] && [ -n "${skip_memory_test}" ]
	then
		log error "“skip_memory_test”参数值必须为 1(跳过); 0或空(不跳过)!"
		flag=1
	fi
	
	## 磁盘挂载参数检查
	if [ "${skip_diskmount_test}" != 1 ]
	then
		if [ -z "${mount_point}" ] || [ -z "${coor_disk_size_TB}" ] || [ -z "${data_disk_size_TB}" ]
		then
			log error "“skip_diskmount_test”参数值为“1”时,“mount_point”、“coor_disk_size_TB”、“data_disk_size_TB”参数必须有值!"
			flag=1
		fi
	elif [ "${skip_diskmount_test}" != 1 ] && [ "${skip_diskmount_test}" != 0 ] && [ -n "${skip_diskmount_test}" ]
	then
		log error "“skip_diskmount_test”参数值必须为 1(跳过); 0或空(不跳过)!"
		flag=1
	fi
	
	## 磁盘性能参数检查
	if [ "${skip_fio_test}" != 1 ]
	then
		if [ -z "${coor_fio_iops}" ] || [ -z "${data_fio_iops}" ]
		then
			log error "“skip_fio_test”参数值为“1”时,“coor_fio_iops”、“data_fio_iops”参数必须有值!"
			flag=1
		fi
	elif [ "${skip_fio_test}" != 1 ] && [ "${skip_fio_test}" != 0 ] && [ -n "${skip_fio_test}" ]
	then
		log error "“skip_fio_test”参数值必须为 1(跳过); 0或空(不跳过)!"
		flag=1
	fi
	
	## 软件包存放目录检查
	if [ ! -d "${install_source_dir}" ]
	then
		log error "未找到软件包存放目录“${install_source_dir}”!"
		flag=1
	fi
	
	## C3工具安装包检查
	if [ ! -f "${c3_pack}" ]
	then
		log error "未找到C3工具安装包“${c3_pack}”!"
		flag=1
	fi

	
	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m配置文件填写有误,\033[0m\033[40;31m详情请查看 ${logfile}\033[0m"
		exit 1
	fi
}


ssh_trust_root()
{
	log debug "检查expect包..."
	if [ `rpm -qa expect |wc -l` == 0 ]
	then
		yum -y install expect >/dev/null 2>&1

		if [ `rpm -qa expect |wc -l` == 0 ]
		then
			log error "缺少expect包,自动通过yum安装失败,请手动安装。"
			exit 1
		fi
	fi
	
	if [ ! -f ~/.ssh/id_rsa ]	
	then
		log info "生成id_rsa文件"
		ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q 
	fi

	ssh_script=${wd}/sshtrust.sh
	ssh_ip_list=/tmp/ip.list

	> ${ssh_ip_list}
	
	for ip in ${array1[@]} ${array2[@]}
	do
		if [ ! -f ~/.ssh/known_hosts ] || [ `cat ~/.ssh/known_hosts |grep -w ${ip} |wc -l ` == 0 ]
		then
			ssh-keyscan -t ecdsa ${ip} >> ~/.ssh/known_hosts
		fi
	done

	for ip in ${array1[@]} ${array2[@]}
	do	
		log debug $ip
		echo ${ip} >> ${ssh_ip_list}
	done

	expect ${wd}/sshtrust.sh ${ssh_ip_list} root ${root_pwd} > /dev/null 2>&1

	log info "root互信配置完毕。"

}


ssh_trust_gbase()
{
	if [ ! -f ~gbase/.ssh/id_rsa ]	
	then
		log info "生成id_rsa文件"
		su - gbase -c "ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q "
	fi

	ssh_script=${wd}/sshtrust.sh
	ssh_ip_list=/tmp/ip.list

	> ${ssh_ip_list}
	
	for ip in ${array1[@]} ${array2[@]}
	do
	if [ ! -f ~gbase/.ssh/known_hosts ] || [ `cat ~gbase/.ssh/known_hosts |grep -w ${ip} |wc -l ` == 0 ]
	then
		su - gbase -c  "ssh-keyscan -t ecdsa ${ip} >> ~/.ssh/known_hosts 2>/dev/null"
	fi
	done

	for ip in ${array1[@]} ${array2[@]}
	do	
		log debug $ip
		echo ${ip} >> ${ssh_ip_list}
	done 

	su - gbase -c "expect ${wd}/sshtrust.sh ${ssh_ip_list} gbase ${gbase_pwd} > /dev/null 2>&1"

	log info "gbase互信配置完毕。"

}


check_node_stats()
{
	#flag=0

	coor=( `echo ${coor_ip_list} |sed 's/,/ /g'` )
	#coor=( $(echo ${coor[@]} | sed "s/ /\n/g" |sort -u) )
	coor=( $(echo ${coor[@]} | sed "s/ /\n/g" ) )

	data=( `echo ${data_ip_list1} |sed 's/,/ /g'` )
	#data=( $(echo ${data[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
	data=( $(echo ${data[@]} | sed "s/ /\n/g" ) )

	array1=( `echo ${coor_ip_list} |sed 's/,/ /g'` `echo ${data_ip_list1} |sed 's/,/ /g'`)
	array1=( $(echo ${array1[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )

	if  [ "${InstanceType}" == 1 ]
	then
		array2=( `echo ${data_ip_list2} |sed 's/,/ /g'`)
	fi

	#echo ${array1[@]} ${array2[@]}

	for ip in ${array1[@]} ${array2[@]}
	do

		log debug "尝试ping $ip"
		pingstats=`ping -c 1 -w 1 ${ip} |grep -o "[0-9] received"|awk '{print $1}'`
		if [ "${pingstats}" == 0 ] 
		then
			flag=1
			log error "ping ${ip} 失败,请检查节点状态是否正常。"
		fi

	done

	# Identitify local ip 
	for ip in `ip a |grep -w inet | grep -v '127.0' |awk '{print $2}' |awk -F'/' '{print $1}'`
	do
		if [[ " ${array1[@]} " =~ " ${ip} " ]]
		then
			log info "本机地址=${ip}"
			export local_ip=${ip}
			break
		fi
	done

	# Second coor ip 
	for ip in ${coor[@]} 
	do
		if [ ${ip} != "${local_ip}" ] 
		then
			export second_coor_ip=${ip}
			break
		fi
	done 
	log debug "second_coor_ip=${second_coor_ip}"

	# c3_vc1
	if [ "${GBaseMode}" == 1 ] &&  [ ! -z "${vc_name1}" ] 
	then
		c3_vc1=()
		vc_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		#vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
		vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" ) )

		log debug "vc_tmp[@]=${vc_tmp[@]}"

		for ip in ${vc_tmp[@]}
		do
			if [ `echo "${data[@]}" | grep -w "${ip}" | wc -l ` -gt 0 ] 
			then
				c3_vc1=(${c3_vc1[@]} ${ip})
				#break
			fi
		done
	fi

	# c3_vc2
	if [ "${GBaseMode}" == 1 ] &&  [ ! -z "${vc_name2}" ] 
	then
		c3_vc2=()
		vc_tmp=( `echo ${vc_list2} |sed 's/,/ /g'` )
		#vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
		vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" ) )

		for ip in ${vc_tmp[@]}
		do
			if [ `echo "${data[@]}" | grep -w "${ip}" | wc -l ` -gt 0 ] 
			then
				c3_vc2=(${c3_vc2[@]} ${ip})
				#break
				#echo ${c3_vc2[@]}
			fi
		done
	fi

	#echo '${data[@]}='${data[@]}

	# c3_vc3
	if [ "${GBaseMode}" == 1 ] && [ ! -z "${vc_name3}" ] 
	then
		c3_vc3=()
		vc_tmp=( `echo ${vc_list3} |sed 's/,/ /g'` )
		#vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
		vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" ) )

		for ip in ${vc_tmp[@]}
		do
			if [ `echo "${data[@]}" | grep -w "${ip}" | wc -l ` -gt 0 ] 
			then
				c3_vc3=(${c3_vc3[@]} ${ip})
				#break
			fi
		done
	fi

}


install_c3()
{
	log debug ${c3_pack}
	
	if [ `rpm -qa c3 |wc -l` == 0  ]
	then
		yum -y install rsync >/dev/null 2>&1
		rpm -ivh ${c3_pack} --force
	fi

	log info "配置c3.conf文件。"
	log debug c3_conf
	tmp_c3_conf=/tmp/c3.conf
	> ${tmp_c3_conf}

	# coor 
	echo "cluster coor {" >> ${tmp_c3_conf} 
	echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
	echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
	for ip in ${coor[@]}
	do
		echo -e "\t${ip}" >> ${tmp_c3_conf}
	done 	
	echo "}" >> ${tmp_c3_conf}

	# data 
	echo "cluster data {" >> ${tmp_c3_conf} 
	echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
	echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
	for ip in ${data[@]}
	do
		echo -e "\t${ip}" >> ${tmp_c3_conf}
	done 	
	echo "}" >> ${tmp_c3_conf}

	# all
	echo "cluster all {" >> ${tmp_c3_conf} 
	echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
	echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
	for ip in ${array1[@]}
	do
		echo -e "\t${ip}" >> ${tmp_c3_conf}
	done 	
	echo "}" >> ${tmp_c3_conf}


	# vc1
	if [ ! -z "${vc_name1}" ] && [ ! -z "${c3_vc1}" ]
	then
		echo "cluster ${vc_name1} {" >> ${tmp_c3_conf} 
		echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
		echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
		for ip in ${c3_vc1[@]}
		do
			echo -e "\t${ip}" >> ${tmp_c3_conf}
		done 	
		echo "}" >> ${tmp_c3_conf}
	fi
	# vc2
	if [ ! -z "${vc_name2}" ] && [ ! -z "${c3_vc2}" ]
	then
		echo "cluster ${vc_name2} {" >> ${tmp_c3_conf} 
		echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
		echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
		for ip in ${c3_vc2[@]}
		do
			echo -e "\t${ip}" >> ${tmp_c3_conf}
		done 	
		echo "}" >> ${tmp_c3_conf}
	fi
	# vc3
	if [ ! -z "${vc_name3}" ] && [ ! -z "${c3_vc3}" ]
	then
		echo "cluster ${vc_name3} {" >> ${tmp_c3_conf} 
		echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
		echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
		for ip in ${c3_vc3[@]}
		do
			echo -e "\t${ip}" >> ${tmp_c3_conf}
		done 	
		echo "}" >> ${tmp_c3_conf}
	fi

	#cat ${tmp_c3_conf}
	cp -f ${tmp_c3_conf} /etc/c3.conf

	log info "配置/etc/hosts文件。"
	# /etc/hosts
	tmp_hosts=/tmp/hosts
	cat /etc/hosts |grep localhost |grep -v ^# > ${tmp_hosts}

	echo >> ${tmp_hosts}
	echo "# coor " >> ${tmp_hosts} 
	for ip in ${coor[@]}
	do
		hostname=`ssh ${ip} hostname`
		echo -e "${ip}\t${hostname}" >> ${tmp_hosts} 
	done
:<<!
/etc/hosts 里面,不填写data节点,该段注释
	echo >> ${tmp_hosts}
	echo "# data1 " >> ${tmp_hosts} 
	for ip in ${data[@]}
	do
		hostname=`ssh ${ip} hostname`
		echo -e "${ip}\t${hostname}" >> ${tmp_hosts} 
	done

	echo >> ${tmp_hosts}
	echo "# data2 " >> ${tmp_hosts} 
	for ip in ${array2[@]}
	do
		hostname=`ssh ${ip} hostname`
		echo -e "${ip}\t${hostname}" >> ${tmp_hosts} 
	done
!
	mv -f /etc/hosts /etc/hosts.bak
	cp -f ${tmp_hosts} /etc/hosts 

	for ip in ${array1[@]}
	do
		scp ${tmp_hosts} ${ip}:/etc/hosts >/dev/null 2>&1
	done
}


check_gbase_service()
{
	# coor gcware服务	
	# coor gclusterd服务
	coor_array=( `echo ${coor_ip_list} |sed 's/,/ /g'` )
	for ip in ${coor_array[@]}
	do
		gcware=`ssh ${ip} 'ps -ef|grep gcware|grep -v grep'`
		if [ -n "${gcware}" ]
		then
			flag=1
			log error "节点 ${ip} 已存在GBase 8a的gcware服务,请检查该节点是否已安装GBase 8a软件。 "
		fi
		
		gclusterd=`ssh ${ip} 'ps -ef|grep gclusterd|grep -v grep'`
		if [ -n "${gclusterd}" ]
		then
			flag=1
			log error "节点 ${ip} 已存在GBase 8a的gclusterd服务,请检查该节点是否已安装GBase 8a软件。 "
		fi
	done
	
	#Data gbased服务
	data1_array=( `echo ${data_ip_list1} |sed 's/,/ /g'` )
	if  [ "${InstanceType}" == 1 ]
	then
		data2_array=( `echo ${data_ip_list2} |sed 's/,/ /g'`)
	fi
	for ip in ${data1_array[@]} ${data2_array[@]}
	do

		gbased=`ssh ${ip} 'ps -ef|grep gbased|grep -v grep'`
		if [ -n "${gbased}" ]
		then
			flag=1
			log error "节点 ${ip} 已存在GBase 8a的gbased服务,请检查该节点是否已安装GBase 8a软件。 "
		fi

	done
	
	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m检测未通过,\033[0m\033[40;31m详情请查看 ${logfile}。\033[0m"
		exit 1
	fi
}


check_os()
{
	base_ver=`cat /etc/system-release`

	log info "本机操作系统版本为\"${base_ver}\"。"

	if [ `echo ${base_ver} |grep -E "Red.*Hat|NeoKylin|Kylin" |wc -l ` == 0 ]
	then
		flag=1
		log error "操作系统应为 RedHat 或 Neokylin 或 Kylin."

	fi

	for ip in ${array1[@]}
	do
		os_ver=`ssh ${ip} cat /etc/system-release`
		log debug "${ip} OS:  ${os_ver}"
		if [ "${os_ver}" != "${base_ver}" ]
		then
			flag=1
			log error "节点 ${ip} 的操作系统版本为 \"${os_ver}\",与本机不同。 "
		fi

	done
}


install_rpms()
{
	#cexec all: 'yum -y install bc bzip2 *cgroup* rsync lsof gdb numactl pcre krb5-libs cyrus-sasl-lib nss nss-util nss-softokn-freebl nspr keyutils-libs libidn zlib libgpg-error libgcrypt ncurses-libs libcom_err openldap python-libs libgcc libselinux libgomp libstdc glibc libdb libuuid'  > /tmp/pre_check.log 2>&1

	#nodes=`cexec -p all: 'rpm -qa bzip2 bc libcgroup-tools rsync lsof gdb numactl pcre krb5-libs cyrus-sasl-lib nss nss-util nss-softokn-freebl nspr keyutils-libs libidn zlib libgpg-error libgcrypt ncurses-libs libcom_err openldap python-libs libgcc libselinux libgomp libstdc glibc libdb libuuid |wc -l' | awk '{if ($3<30) print $2 }'`
	
	cexec all: 'yum -y install bc bzip2 *cgroup* rsync lsof gdb numactl'  > /tmp/pre_check.log 2>&1

	nodes=`cexec -p all: 'rpm -qa bzip2 bc libcgroup-tools rsync lsof gdb numactl |wc -l' | awk '{if ($3<7) print $2 }'`

	if [ ! -z "${nodes}" ]
	then
		flag=1
		log error "以下节点安装rpm包失败,请检查 /tmp/pre_check.log  文件。"
		echo "${nodes}" |tee -a ${logfile} 
	fi
}


check_diskspace()
{
	if [ "${skip_diskmount_test}" == 1 ]
	then
		log info "跳过磁盘检测。。"
		return 
	fi

	tmp_file=/tmp/tmpfile
	
	# coor 
	cexec -p coor: "df -PBT ${mount_point} |grep ${mount_point}" |awk '{print $2,$4}' >${tmp_file}
	while read ip size
	do
		log debug "$ip  $size "
		if [ -z "${size}" ]
		then
			flag=1
			log error "管理节点 ${ip} 未单独挂载 ${mount_point}  "
		elif [ `echo ${size} | tr -d 'T'` -lt ${coor_disk_size_TB} ]
		then
			flag=1
			log error "管理节点 ${ip} ${mount_point} 空间为 ${size}, 小于${coor_disk_size_TB}T 。"
		fi

	done < ${tmp_file}

	# data 
	cexec -p data: "df -PBT ${mount_point} |grep ${mount_point}" |awk '{print $2,$4}' > ${tmp_file} 
	while read ip size
	do
		log debug "$ip  $size"
		if [ -z "${size}" ]
		then
			flag=1
			log error "数据节点 ${ip} 未单独挂载 ${mount_point} "
		elif [ `echo ${size} | tr -d 'T'` -lt ${data_disk_size_TB} ]
		then
			flag=1
			log error "数据节点 ${ip} ${mount_point} 空间为 ${size}, 小于${data_disk_size_TB}T 。"
		fi

	done < ${tmp_file}
}


fio_test()
{
	if [ "${skip_fio_test}" == 1 ] 
	then
		log info "跳过fio检测。"
		return 
	fi
	
	if [ -z "${fio_pack}" ]
	then
		log error "“${fio_pack}”参数值为空,请正确填写!"
		flag=1
	fi

	tmp_file=/tmp/tmpfile
	
	log info "安装fio软件包。"
	fio_rpm=`basename ${fio_pack}`
	cpush all: ${fio_pack} /tmp
	cexec all: "yum -y install /tmp/${fio_rpm}" >/dev/null 2>&1 

	log info "执行fio测试,该操作用时较长,请耐心等待。。。"
	echo "fio -filename=/opt/testfio.dat -direct=1 -iodepth 1 -thread -rw=randrw -rwmixread=70 -ioengine=psync -bs=4k -size=100G -numjobs=50 -runtime=180 -group_reporting -name=randrw_70read_4k" > /tmp/fio.sh
	cpush all: /tmp/fio.sh /tmp
	cexec all: "sh /tmp/fio.sh >/tmp/fio.log "

	log info "fio测试完毕,开始收集结果。"
	cexec -p coor: 'cat /tmp/fio.log |grep IOPS' |awk '{print $2,$3,$4,$5}' |awk -F':|=|,' '{print $1,$2,$4,$6} ' > ${tmp_file}_coor
	cexec -p data: 'cat /tmp/fio.log |grep IOPS' |awk '{print $2,$3,$4,$5}' |awk -F':|=|,' '{print $1,$2,$4,$6} ' > ${tmp_file}_data
	
	while read ip option iops rate
	do
		log info "${ip} ${option} ${iops} ${rate}"
		if [ "${iops}" -lt "${coor_fio_iops}" ]
		then
			flag=1
			log error "${ip} 的 ${option} IOPS 为 ${iops},低于标准 ${coor_fio_iops}"
		fi
	done < ${tmp_file}_coor

	while read ip option iops rate
	do
		log info "${ip} ${option} ${iops} ${rate}"
		if [ "${iops}" -lt "${data_fio_iops}" ]
		then
			flag=1
			log error "${ip} 的 ${option} IOPS 为 ${iops},低于标准 ${data_fio_iops}"
		fi
	done < ${tmp_file}_data
}


check_network()
{
	log info "管理节点网卡信息:"
	cexec coor: 'ip a |grep -w inet |grep ' $ip_prefix |tee -a ${logfile}
	log info "数据节点网卡信息:"
	cexec data: 'ip a |grep -w inet |grep ' $ip_prefix |tee -a ${logfile}
}


check_cpu()
{
	echo -e "\nIP地址\t\tThreads\tCPUs\tCores\t超线程" |tee -a ${logfile}
	cexec -p all: 'echo `cat /proc/cpuinfo  |grep processor |wc -l;  cat /proc/cpuinfo |grep "physical id" |sort -u  |wc -l ; cat /proc/cpuinfo |grep "cpu cores" |sort -u  |awk "{print \\$NF}"`' | awk -v OFS='\t' '{if($3==$4*$5) print "\033[32m"$2,$3,$4,$5,"否\033[0m"; else print "\033[33m"$2,$3,$4,$5,"是\033[0m"}' |tee -a ${logfile}
}


check_memory()
{
	if [ "${skip_memory_test}" == 1 ]	
	then
		log info "跳过内存检测。"
		return 
	fi

	tmp_file=/tmp/tmpfile 

	cexec -p coor: 'echo `free -g |grep Mem; free -g |grep Swap`' |awk '{print $2,$4,$11}' > ${tmp_file}

	while read ip mem swap
	do
		log debug "coor 节点 ${ip} 内存 ${mem}G , SWAP ${swap}G. "
		#echo $ip $mem $swap
		if [ "${mem}" -lt ${coor_memory_GB} ]
		then
			flag=1
			log error "节点 ${ip} 内存大小为 ${mem}G, 未达要求。"

		fi

		if [ "${swap}" -lt ${coor_swap_GB} ]
		then
			flag=1
			log error "节点 ${ip} SWAP大小为 ${swap}G, 未达要求。"
		fi

	done < ${tmp_file}

	cexec -p data: 'echo `free -g |grep Mem; free -g |grep Swap`' |awk '{print $2,$4,$11}' > ${tmp_file}

	while read ip mem swap
	do
		log debug "data 节点 ${ip} 内存 ${mem}G , SWAP ${swap}G. "
		#echo $ip $mem $swap
		if [ "${mem}" -lt ${data_memory_GB} ]
		then
			flag=1
			log error "节点 ${ip} 内存大小为 ${mem}未达要求。"

		fi

		if [ "${swap}" -lt ${data_swap_GB} ]
		then
			flag=1
			log error "节点 ${ip} SWAP大小为 ${swap}G, 未达要求。"
		fi

	done < ${tmp_file}
}


create_gbase_user()
{
	logger info "创建gbase用户。"
	cexec all: "id gbase >/dev/null 2>&1 || useradd gbase && echo gbase:\"${gbase_pwd}\" |chpasswd "  >/dev/null
	logger info "创建完毕。"
}

modify_folder_user()
{
	if [ -n "${install_target}" ]
	then
		cexec all: "if [ ! -d "${install_target}" ]; then mkdir -p ${install_target}; fi" >/dev/null
		cexec all: "chown -R gbase:gbase ${install_target} " >/dev/null
	else
		cexec all: "chown -R gbase:gbase /opt " >/dev/null
	fi
}


second_c3()
{
	# 多vc模式,删除c3.conf中data部分 
    if [ "${GBaseMode}" == 1 ] &&  [ ! -z "${vc_name1}" ]
    then
		log debug "本集群为多VC模式,清理c3.conf中的data部分。"
		sed -i '/cluster data/,/}/d' /etc/c3.conf
	fi

	# 第二管理节点IP为空,表示只有一个管理节点,不执行
	if [ -z "${second_coor_ip}" ]
	then
		return 
	fi

	log info "第二个管理节点IP地址为: ${second_coor_ip}"
	
	# c3.rpm
	scp ${c3_pack} ${second_coor_ip}:/tmp >/dev/null
	tmpname=`basename ${c3_pack} `

	log info "第二管理节点安装rpm包。"
	log debug "在 ${second_coor_ip} 节点安装c3.rpm包, rpm -ivh /tmp/${tmpname} --force "
	ssh -n ${second_coor_ip} "rpm -ivh /tmp/${tmpname} --force"

	# c3.conf 
	log info "配置第二管理节点c3.conf"
	cat /etc/c3.conf | sed "/head node/s/${local_ip}/${second_coor_ip}/g"  > /tmp/c3.conf
	scp /tmp/c3.conf ${second_coor_ip}:/etc >/dev/null 

	# /etc/hosts 
	log info "配置第二管理节点/etc/hosts"
	scp /etc/hosts ${second_coor_ip}:/etc >/dev/null 

	# ssh-key
	# root 
	log info "配置第二管理节点root和gbase互信"
	scp -rp ~/.ssh/* ${second_coor_ip}:~/.ssh >/dev/null

	# gbase 
	su - gbase -lc "scp -rp ~gbase/.ssh/* ${second_coor_ip}:~gbase/.ssh >/dev/null"
}


isNull()
{
	local name="$1"
	local var="$2"
	
	if [ -z "${var}" ]
	then
		log error "“${name}”参数值为空,请正确填写!"
		flag=1
	fi
}


validStr()
{
	local strname="$1"
	local strvar="$2"
	local strlen="$3"
	
	if [ -n "${strvar}" ]
	then
        for i in `seq ${#strvar}`
        do
			char=${strvar:$i-1:1}
			case "$char" in
				[a-z]|[A-Z]) ;;
				[0-9]) ;;
				" ") ;;
				"_") ;;
				",") ;;
				"!") ;;
				".") ;;
				*)
					log error "“$strname”参数值不合法,要求必须是英文字符“大小写字母、数字、空格 、下划线_、逗号, 点.”,不能有汉字或中文符号。"
					flag=1
			esac
        done
		
		if [ -n "${strlen}" ] && [ "${strlen}" -gt 0 ]
		then
			if [ ${#strvar} -gt ${strlen} ]
			then
				log error "“$strname”参数值不合法,长度不能超过${strlen},当前长度为${#strvar}。"
				flag=1
			fi
		fi
	fi
}


main()
{
	step=0

	cat ${logfile} >> ${logfile}_old
	> ${logfile}
	echo -e "\n\n" |tee -a ${logfile}
	log info "Step0: 开始执行安装检查..."

	if [ "$USER" != "root" ]
	then
		log error "本脚本须在root系统用户下运行。"
		exit 1
	fi

	### 
	###  下载及解压 gbase_workspace.tar.gz 工作需提前手工进行 ,本脚本不做检验
	###

	step=$((step+1))
	log info "Step${step}: 读取配置文件..."
	read_config
	
	step=$((step+1))
	log info "Step${step}: 检查配置文件..."
	check_config
	
#:<<!
	step=$((step+1))
	log info "Step${step}: 检查节点状态..."
	check_node_stats

	step=$((step+1))
	log info "Step${step}: 配置root用户互信..."
	ssh_trust_root
	
	step=$((step+1))
	log info "Step${step}: 检查GBase服务..."
	check_gbase_service

	step=$((step+1))
	log info "Step${step}: 安装C3工具..."
	install_c3

	step=$((step+1))
	log info "Step${step}: 操作系统版本检查..."
	check_os

	step=$((step+1))
	log info "Step${step}: 安装必要的rpm包..."
	install_rpms

	step=$((step+1))
	log info "Step${step}: 检查磁盘空间..."
	check_diskspace
#!
	step=$((step+1))
	log info "Step${step}: 执行磁盘fio测试..."
	fio_test

#:<<!
	step=$((step+1))
	log info "Step${step}: 检查网卡绑定..."
	check_network

	step=$((step+1))
	log info "Step${step}: 检查CPU ..."
	check_cpu

	step=$((step+1))
	log info "Step${step}: 检查内存 ..."
	check_memory
        
#!

	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m检测未通过,\033[0m\033[40;31m详情请查看 ${logfile}。\033[0m"
		exit 1
	else
		step=$((step+1))
		log info "Step${step}: 创建gbase用户..."
		create_gbase_user

		step=$((step+1))
		log info "Step${step}: 配置gbase用户互信..."
		ssh_trust_gbase

		step=$((step+1))
		log info "Step${step}: 配置第二管理节点C3..."
		second_c3

		step=$((step+1))
		log info "Step${step}: 修改目录属主..."
		modify_folder_user
		log info "\033[5;40;32m自动检测通过,CPU、网络等请查看日志人工判断。\033[40;32m\033[0m本次日志文件存放在 ${logfile} ,之前的日志已转移至 ${logfile}_old。\033[0m"
		
		#增加环境检查标识(1:已检查通过)
		sed -i '$a\check_finish_env=1' ${config_file}
	fi
}

main


附录 3 autoinstall.sh

#!/bin/bash
###############################################################################
##creator : TuJunbing                                                        ##
##create time: 2024-7-10                                                     ##
##Decription: autoinstall.sh                                                 ##
##Version: 1.1  	                                                         ##
###############################################################################

wd=$(cd `dirname $0` && pwd)
updir=`dirname ${wd}`

logdir=${updir}/log
if [ ! -d "${logdir}" ]
then
	mkdir -p ${logdir}
fi

logfile=${logdir}/autoinstall_`date '+%Y%m%d'`.log
export loglevel=$(grep loglevel ${wd}/db_config.conf | awk -F= '{print $2}' | awk '$1=$1')

log(){
	if [ "${1,,}" == "error" ] || [ "${1,,}" == "warn" ] 
	then
		sh logger.sh ${logfile} $1 "[$0] [line:`caller |awk '{print $1}'`] $2"
	else
		sh logger.sh ${logfile} $1 "$2"
	fi
}

log debug "wd=${wd} updir=${updir}"

flag=0


read_config()
{
	log debug "${FUNCNAME[@]}"
	config_file=${wd}/db_config.conf
	db_source_conf_file=${wd}/db_source.conf

	while read line 
	do
		comment=$(echo $line | grep "#")
		if [ "${comment}" != "" ]
		then
			continue
		fi
			
		line_trim=`echo "${line}" |awk -F# '{print $1}' |grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		if [ -z "${line_trim}" ]
		then
			continue
		fi
		
		varname=`echo ${line_trim} | awk -F= '{print $1}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		varvalue=`echo ${line_trim} | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`

		if [[ ${varname} =~ 'pwd' ]] || [[ ${varname} =~ 'password' ]]
		then
			varvaluemask=********
		else
			varvaluemask=${varvalue}
		fi

		#log debug "varname='${varname}',varvalue='${varvaluemask}'"

		if [ -z "${varvalue}" ]
		then 
			#unset varname
			log debug "读取配置参数 ${varname}=${varvalue}"
		else
			log debug "读取配置参数 ${varname}=${varvaluemask}"
			export ${varname}="${varvalue}"
		fi

	done < ${config_file}

	if [ -n "${mirror_site}" ] 
	then
		if [ "${mirror_site}" != "local" ] && [ -n "${mirror_site}" ]
		then
			while read line 
			do
				line_trim=`echo "${line}" |awk -F# '{print $1}' |grep -o "[^ ]\+\( \+[^ ]\+\)*"`

				varname=`echo ${line_trim} | awk -F= '{print $1}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
				varvalue=`echo ${line_trim} | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`

				if [ "${varname}" == "${mirror_site}" ] || [ "${varname}" == "${db_source_file}" ]
				then
		 
					if [[ ${varname} =~ 'pwd' ]] || [[ ${varname} =~ 'password' ]]
					then
						varvaluemask=********
					else
						varvaluemask=${varvalue}
					fi
			
					#log debug "varname='${varname}',varvalue='${varvaluemask}'"
			
					if [ -z "${varvalue}" ]
					then 
						unset varname
					else
						log debug "读取配置参数 ${varname}=${varvaluemask}"
						export ${varname}="${varvalue}"
					fi
				fi			

			done < ${db_source_conf_file}

			log debug "读取配置参数 full_target_url=${!mirror_site}/${!db_source_file}"
			export full_target_url=${!mirror_site}/${!db_source_file}
		fi
	fi
}


check_config()
{
	## 空值检查
	isNull GBaseName "${GBaseName}"
	isNull GBaseMode "${GBaseMode}"
	isNull InstanceType "${InstanceType}"
	isNull db_source_file "${db_source_file}"
	isNull ip_prefix "${ip_prefix}"
	isNull coor_ip_list "${coor_ip_list}"
	isNull data_ip_list1 "${data_ip_list1}"
	isNull root_pwd "${root_pwd}"
	isNull gbase_pwd "${gbase_pwd}"
	isNull db_gbase_new_pwd "${db_gbase_new_pwd}"
	isNull db_root_new_pwd "${db_root_new_pwd}"
	isNull db_gbase_pwd "${db_gbase_pwd}"
	isNull loglevel "${loglevel}"
	isNull install_source_dir "${install_source_dir}"
	isNull c3_pack "${c3_pack}"
	
	##字符串检查
	validStr vc_name1 "${vc_name1}" 64
	validStr vc_name2 "${vc_name2}" 64
	validStr vc_name3 "${vc_name3}" 64
	validStr vc_comm1 "${vc_comm1}" 60
	validStr vc_comm2 "${vc_comm2}" 60
	validStr vc_comm3 "${vc_comm3}" 60
	
	## 集群模式参数检查
	if [ "${GBaseMode}" == 1 ]
	then
		if [ -z "${vc_name1}" ] || [ -z "${vc_comm1}" ] || [ -z "${vc_list1}" ]
		then
			log error "“GBaseMode”参数值为“1”时,“vc_name*”、“vc_comm*”、“vc_list*”参数必须有值!"
			flag=1
		fi
		
		if [ "${db_source_file}" != "gbase8a_v953" ]
		then
			log error "“GBaseMode”参数值为“1”时,“db_source_file”参数必须为“gbase8a_v953”!"
			flag=1
		fi
	elif [ "${GBaseMode}" != 1 ] && [ "${GBaseMode}" != 2 ]
	then
		log error "“GBaseMode”参数值必须为 1 或 2!"
		flag=1
	fi
	
	## 多实例参数检查
	if [ "${InstanceType}" == 0 ]
	then	
		if [ -n "${data_ip_list2}" ]
		then
			log error "“InstanceType”参数值为“0”时,“data_ip_list2”参数值必须为空!"
			flag=1
		fi
	elif [ "${InstanceType}" == 1 ]
	then
		if [ -z "${data_ip_list2}" ]
		then
			log error "“InstanceType”参数值为“1”时,“data_ip_list2”参数必须有值!"
			flag=1
		fi
	elif [ "${InstanceType}" != 1 ] && [ "${InstanceType}" != 0 ]
	then
		log error "“InstanceType”参数值必须为 0 或 1!"
		flag=1
	fi
	
	## 软件安装版本参数检查
	if [ -n "${db_source_file}" ]
	then
		if [ "${db_source_file}" != "gbase8a_v862" ] && [ "${db_source_file}" != "gbase8a_v952" ] && [ "${db_source_file}" != "gbase8a_v953" ]
		then
			log error "“db_source_file”参数值必须为 gbase8a_v862 gbase8a_v952 gbase8a_v953 其中之一!"
			flag=1
		fi
		
		if [ "${db_source_file}" == "gbase8a_v862" ]
		then
			if [ "${set_variables}" != 0 ]
			then
				log error "“db_source_file”参数值为“gbase8a_v862”时,“set_variables”参数必须为“0”!"
				flag=1
			fi
		fi
	fi
	
	## 集群字符集参数检查
	if [ -n "${characterSet}" ]
	then
		if [ "${characterSet}" != "utf8" ] && [ "${characterSet}" != "utf8mb4" ] && [ "${characterSet}" != "gbk" ]
		then
			log error "“characterSet”参数值必须为 utf8 utf8mb4 gbk 其中之一!"
			flag=1
		fi
	fi
	
	## 数据节点IP地址检查
	dataIP=${data_ip_list1}
	if [ -n "${data_ip_list2}" ]
	then
		dataIP=${data_ip_list1},${data_ip_list2} 
	fi
	
	if [ -n "${vc_list1}" ] && [ "${GBaseMode}" == 1 ]
	then
		vc1_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		vc1_tmp=( $(echo ${vc1_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc1_tmp[@]}
		do
			if [ `echo "${dataIP[@]}" | grep -w "${ip}" | wc -l` == 0 ] 
			then
				log error "“vc_list1”参数值 ${ip} 不在参数data_ip_list的值中!"
				flag=1
			fi
		done
	fi
	
	if [ -n "${vc_list2}" ] && [ "${GBaseMode}" == 1 ]
	then
		vc2_tmp=( `echo ${vc_list2} |sed 's/,/ /g'` )
		vc2_tmp=( $(echo ${vc2_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc2_tmp[@]}
		do
			if [ `echo "${dataIP[@]}" | grep -w "${ip}" | wc -l` == 0 ] 
			then
				log error "“vc_list2”参数值 ${ip} 不在参数data_ip_list的值中!"
				flag=1
			fi
		done
		
		if [ -z "${vc_list1}" ]
		then
			log error "“vc_list2”参数有值时, 参“vc_list1”必须有值!"
			flag=1
		fi
		
		vc1_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		vc1_tmp=( $(echo ${vc1_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc2_tmp[@]}
		do
			if [ `echo "${vc1_tmp[@]}" | grep -w "${ip}" | wc -l` -gt 0 ] 
			then
				log error "“vc_list2”参数值 ${ip} 存在“vc_list1”参数中!"
				flag=1
			fi
		done
	fi
	
	if [ -n "${vc_list3}" ] && [ "${GBaseMode}" == 1 ]
	then
		vc3_tmp=( `echo ${vc_list3} |sed 's/,/ /g'` )
		vc3_tmp=( $(echo ${vc3_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc3_tmp[@]}
		do
			if [ `echo "${dataIP[@]}" | grep -w "${ip}" | wc -l` == 0 ] 
			then
				log error "“vc_list3”参数值 ${ip} 不在参数data_ip_list的值中!"
				flag=1
			fi
		done
		
		vc1_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		vc1_tmp=( $(echo ${vc1_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc3_tmp[@]}
		do
			if [ `echo "${vc1_tmp[@]}" | grep -w "${ip}" | wc -l` -gt 0 ] 
			then
				log error "“vc_list3”参数值 ${ip} 存在“vc_list1”参数中!"
				flag=1
			fi
		done
		
		if [ -z "${vc_list2}" ]
		then
			log error "“vc_list3”参数有值时, 参“vc_list2”必须有值!"
			flag=1
		fi
		
		vc2_tmp=( `echo ${vc_list2} |sed 's/,/ /g'` )
		vc2_tmp=( $(echo ${vc2_tmp[@]} | sed "s/ /\n/g" ) )
		for ip in ${vc3_tmp[@]}
		do
			if [ `echo "${vc2_tmp[@]}" | grep -w "${ip}" | wc -l` -gt 0 ] 
			then
				log error "“vc_list3”参数值 ${ip} 存在“vc_list2”参数中!"
				flag=1
			fi
		done
	fi
	
	## 内存配置参数检查
	if [ "${skip_memory_test}" != 1 ]
	then
		if [ -z "${coor_memory_GB}" ] || [ -z "${coor_swap_GB}" ] || [ -z "${data_memory_GB}" ] || [ -z "${data_swap_GB}" ]
		then
			log error "“skip_memory_test”参数值为“1”时,“coor_memory_GB”、“coor_swap_GB”、“data_memory_GB”、“data_swap_GB”参数必须有值!"
			flag=1
		fi
	elif [ "${skip_memory_test}" != 1 ] && [ "${skip_memory_test}" != 0 ] && [ -n "${skip_memory_test}" ]
	then
		log error "“skip_memory_test”参数值必须为 1(跳过); 0或空(不跳过)!"
		flag=1
	fi
	
	## 磁盘挂载参数检查
	if [ "${skip_diskmount_test}" != 1 ]
	then
		if [ -z "${mount_point}" ] || [ -z "${coor_disk_size_TB}" ] || [ -z "${data_disk_size_TB}" ]
		then
			log error "“skip_diskmount_test”参数值为“1”时,“mount_point”、“coor_disk_size_TB”、“data_disk_size_TB”参数必须有值!"
			flag=1
		fi
	elif [ "${skip_diskmount_test}" != 1 ] && [ "${skip_diskmount_test}" != 0 ] && [ -n "${skip_diskmount_test}" ]
	then
		log error "“skip_diskmount_test”参数值必须为 1(跳过); 0或空(不跳过)!"
		flag=1
	fi
	
	## 磁盘性能参数检查
	if [ "${skip_fio_test}" != 1 ]
	then
		if [ -z "${coor_fio_iops}" ] || [ -z "${data_fio_iops}" ]
		then
			log error "“skip_fio_test”参数值为“1”时,“coor_fio_iops”、“data_fio_iops”参数必须有值!"
			flag=1
		fi
	elif [ "${skip_fio_test}" != 1 ] && [ "${skip_fio_test}" != 0 ] && [ -n "${skip_fio_test}" ]
	then
		log error "“skip_fio_test”参数值必须为 1(跳过); 0或空(不跳过)!"
		flag=1
	fi
	
	## 软件包存放目录检查
	if [ ! -d "${install_source_dir}" ]
	then
		log error "未找到软件包存放目录“${install_source_dir}”!"
		flag=1
	fi
	
	## C3工具安装包检查
	if [ ! -f "${c3_pack}" ]
	then
		log error "未找到C3工具安装包“${c3_pack}”!"
		flag=1
	fi
	
	
	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m配置文件填写有误,\033[0m\033[40;31m详情请查看 ${logfile}\033[0m"
		exit 1
	fi
}


check_gbase_service()
{
	# coor gcware服务	
	# coor gclusterd服务
	coor_array=( `echo ${coor_ip_list} |sed 's/,/ /g'` )
	for ip in ${coor_array[@]}
	do
		gcware=`ssh ${ip} 'ps -ef|grep gcware|grep -v grep'`
		if [ -n "${gcware}" ]
		then
			flag=1
			log error "节点 ${ip} 已存在GBase 8a的gcware服务,请检查该节点是否已安装GBase 8a软件。 "
		fi
		
		gclusterd=`ssh ${ip} 'ps -ef|grep gclusterd|grep -v grep'`
		if [ -n "${gclusterd}" ]
		then
			flag=1
			log error "节点 ${ip} 已存在GBase 8a的gclusterd服务,请检查该节点是否已安装GBase 8a软件。 "
		fi
	done
	
	#Data gbased服务
	data1_array=( `echo ${data_ip_list1} |sed 's/,/ /g'` )
	if  [ "${InstanceType}" == 1 ]
	then
		data2_array=( `echo ${data_ip_list2} |sed 's/,/ /g'`)
	fi
	for ip in ${data1_array[@]} ${data2_array[@]}
	do

		gbased=`ssh ${ip} 'ps -ef|grep gbased|grep -v grep'`
		if [ -n "${gbased}" ]
		then
			flag=1
			log error "节点 ${ip} 已存在GBase 8a的gbased服务,请检查该节点是否已安装GBase 8a软件。 "
		fi

	done
	
	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m节点服务存在异常情况,无法继续安装软件,\033[0m\033[40;31m详情请查看 ${logfile}。\033[0m"
		exit 1
	fi
}


download_src_file()
{
	if [ "${mirror_site}" != "local" ] && [ -n "${mirror_site}" ]
	then
		if [ ! -f ${install_source_dir}/`basename ${full_target_url}` ]
		then
			wget ${full_target_url} -P ${install_source_dir} --dns-timeout=5 --connect-timeout=3 --read-timeout=180 --tries=2
			if [ $? != 0 ] 
			then
				log error "自动下载安装程序失败,请手动下载。\r\n文件名:`basename ${full_target_url}`, \r\n存放至目录: ${install_source_dir} 内,重新运行本脚本。"
				exit 1
			fi
		fi

		log info "解压安装程序"
		tar xf ${install_source_dir}/`basename ${full_target_url}` -C ${install_source_dir}
	fi
		
	gc_dir="${install_source_dir}/gcinstall"
	log debug "检查安装程序"
	if [ ! -d "${gc_dir}" ]
	then
		flag=1
		log error "未检查到已解压的安装程序gcinstall,请将软件安装包存放至目录:${install_source_dir} 内,并对其进解压和授权。"
		exit 1
	else
		# 安装包版本与配置文件检查
		src_version=`grep release_version ${gc_dir}/BUILDINFO | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		if [ $(echo ${src_version} |grep '8\.6\.2' |wc -l) -gt 0 ]; then 
			db_version=gbase8a_v862
		elif [ $(echo ${src_version} |grep '9\.5\.2' |wc -l) -gt 0 ]; then 
			db_version=gbase8a_v952
		elif [ $(echo ${src_version} |grep '9\.5\.3' |wc -l) -gt 0 ]; then 
			db_version=gbase8a_v953
		else 
			db_version=""
		fi
		
		if [ "${db_source_file}" != "${db_version}" ]
		then
			flag=1
			log error "配置文件中软件安装版本参数“db_source_file”值 ${db_source_file} 与软件安装包的数据库版本 ${db_version} 不一致,请确保软件安装的版本与实际安装版本一致。"
		fi
		
		# 只有V953版本因BUILDINFOarch项,而且安装包不唯一,其他版本暂无解决方案
		src_arch=`grep arch ${gc_dir}/BUILDINFO | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		cpu_arch=`lscpu | grep Architecture | awk -F: '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		
		if [ -z "${src_arch}" ]
		then
			log warn "操作系统CPU类型 ${cpu_arch} 未检查出软件安装包所适配CPU类型,请人工核对二者是否适配。"
		elif [ "${src_arch}" != "${cpu_arch}" ]
		then
			flag=1
			log error "操作系统CPU类型 ${cpu_arch} 与软件安装包所适配CPU类型 ${src_arch} 不一致,请确保软件安装包适配本操作系统版本及CPU类型。"
		fi
		
		chown -R gbase:gbase ${install_source_dir}
	fi
	
	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m软件安装包有误,\033[0m\033[40;31m详情请查看 ${logfile}\033[0m"
		exit 1
	fi
}


check_node_stats()
{
	#flag=0

	coor=( `echo ${coor_ip_list} |sed 's/,/ /g'` )
	#coor=( $(echo ${coor[@]} | sed "s/ /\n/g" |sort -u) )
	coor=( $(echo ${coor[@]} | sed "s/ /\n/g" ) )

	data=( `echo ${data_ip_list1} |sed 's/,/ /g'` )
	#data=( $(echo ${data[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
	data=( $(echo ${data[@]} | sed "s/ /\n/g" ) )

	array1=( `echo ${coor_ip_list} |sed 's/,/ /g'` `echo ${data_ip_list1} |sed 's/,/ /g'`)
	array1=( $(echo ${array1[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )

	if  [ "${InstanceType}" == 1 ]
	then
		array2=( `echo ${data_ip_list2} |sed 's/,/ /g'`)
	fi

	#echo ${array1[@]} ${array2[@]}

	for ip in ${array1[@]} ${array2[@]}
	do

		log debug "尝试ping $ip"
		pingstats=`ping -c 1 -w 1 ${ip} |grep -o "[0-9] received"|awk '{print $1}'`
		if [ "${pingstats}" == 0 ] 
		then
			flag=1
			log error "ping ${ip} 失败,请检查节点状态是否正常。"
		fi

	done

	# Identitify local ip 
	for ip in `ip a |grep -w inet | grep -v '127.0' |awk '{print $2}' |awk -F'/' '{print $1}'`
	do
		if [[ " ${array1[@]} " =~ " ${ip} " ]]
		then
			log info "本机地址=${ip}"
			export local_ip=${ip}
			break
		fi
	done

	# Second coor ip 
	for ip in ${coor[@]} 
	do
		if [ ${ip} != "${local_ip}" ] 
		then
			export second_coor_ip=${ip}
			break
		fi
	done 
	log debug "second_coor_ip=${second_coor_ip}"

	# c3_vc1
	if [ "${GBaseMode}" == 1 ] &&  [ ! -z "${vc_name1}" ] 
	then
		c3_vc1=()
		vc_tmp=( `echo ${vc_list1} |sed 's/,/ /g'` )
		#vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
		vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" ) )

		log debug "vc_tmp[@]=${vc_tmp[@]}"

		for ip in ${vc_tmp[@]}
		do
			if [ `echo "${data[@]}" | grep -w "${ip}" | wc -l ` -gt 0 ] 
			then
				c3_vc1=(${c3_vc1[@]} ${ip})
				#break
			fi
		done
	fi

	# c3_vc2
	if [ "${GBaseMode}" == 1 ] &&  [ ! -z "${vc_name2}" ] 
	then
		c3_vc2=()
		vc_tmp=( `echo ${vc_list2} |sed 's/,/ /g'` )
		#vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
		vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" ) )

		for ip in ${vc_tmp[@]}
		do
			if [ `echo "${data[@]}" | grep -w "${ip}" | wc -l ` -gt 0 ] 
			then
				c3_vc2=(${c3_vc2[@]} ${ip})
				#break
				#echo ${c3_vc2[@]}
			fi
		done
	fi

	#echo '${data[@]}='${data[@]}

	# c3_vc3
	if [ "${GBaseMode}" == 1 ] && [ ! -z "${vc_name3}" ] 
	then
		c3_vc3=()
		vc_tmp=( `echo ${vc_list3} |sed 's/,/ /g'` )
		#vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" |sort -t'.' -k3,3n -k4,4n -u) )
		vc_tmp=( $(echo ${vc_tmp[@]} | sed "s/ /\n/g" ) )

		for ip in ${vc_tmp[@]}
		do
			if [ `echo "${data[@]}" | grep -w "${ip}" | wc -l ` -gt 0 ] 
			then
				c3_vc3=(${c3_vc3[@]} ${ip})
				#break
			fi
		done
	fi

}


update_c3()
{
	log debug ${c3_pack}
	
	if [ `rpm -qa c3 |wc -l` == 0  ]
	then
		yum -y install rsync >/dev/null 2>&1
		rpm -ivh ${c3_pack} --force
	fi

	log info "配置c3.conf文件。"
	log debug c3_conf
	tmp_c3_conf=/tmp/c3.conf
	> ${tmp_c3_conf}

	# coor 
	echo "cluster coor {" >> ${tmp_c3_conf} 
	echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
	echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
	for ip in ${coor[@]}
	do
		echo -e "\t${ip}" >> ${tmp_c3_conf}
	done 	
	echo "}" >> ${tmp_c3_conf}

	# data 
	echo "cluster data {" >> ${tmp_c3_conf} 
	echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
	echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
	for ip in ${data[@]}
	do
		echo -e "\t${ip}" >> ${tmp_c3_conf}
	done 	
	echo "}" >> ${tmp_c3_conf}

	# all
	echo "cluster all {" >> ${tmp_c3_conf} 
	echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
	echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
	for ip in ${array1[@]}
	do
		echo -e "\t${ip}" >> ${tmp_c3_conf}
	done 	
	echo "}" >> ${tmp_c3_conf}


	# vc1
	if [ ! -z "${vc_name1}" ] && [ ! -z "${c3_vc1}" ]
	then
		echo "cluster ${vc_name1} {" >> ${tmp_c3_conf} 
		echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
		echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
		for ip in ${c3_vc1[@]}
		do
			echo -e "\t${ip}" >> ${tmp_c3_conf}
		done 	
		echo "}" >> ${tmp_c3_conf}
	fi
	# vc2
	if [ ! -z "${vc_name2}" ] && [ ! -z "${c3_vc2}" ]
	then
		echo "cluster ${vc_name2} {" >> ${tmp_c3_conf} 
		echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
		echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
		for ip in ${c3_vc2[@]}
		do
			echo -e "\t${ip}" >> ${tmp_c3_conf}
		done 	
		echo "}" >> ${tmp_c3_conf}
	fi
	# vc3
	if [ ! -z "${vc_name3}" ] && [ ! -z "${c3_vc3}" ]
	then
		echo "cluster ${vc_name3} {" >> ${tmp_c3_conf} 
		echo -e "\t${local_ip}:${local_ip}\t#head node" >> ${tmp_c3_conf}
		echo -e "\tdead remove-index-00" >> ${tmp_c3_conf}
		for ip in ${c3_vc3[@]}
		do
			echo -e "\t${ip}" >> ${tmp_c3_conf}
		done 	
		echo "}" >> ${tmp_c3_conf}
	fi

	#cat ${tmp_c3_conf}
	cp -f ${tmp_c3_conf} /etc/c3.conf
	
	
	# 多vc模式,删除c3.conf中data部分 
    if [ "${GBaseMode}" == 1 ] &&  [ ! -z "${vc_name1}" ]
    then
		log debug "本集群为多VC模式,清理c3.conf中的data部分。"
		sed -i '/cluster data/,/}/d' /etc/c3.conf
	fi

	log info "配置/etc/hosts文件。"
	# /etc/hosts
	tmp_hosts=/tmp/hosts
	cat /etc/hosts |grep localhost |grep -v ^# > ${tmp_hosts}

	echo >> ${tmp_hosts}
	echo "# coor " >> ${tmp_hosts} 
	for ip in ${coor[@]}
	do
		hostname=`ssh ${ip} hostname`
		echo -e "${ip}\t${hostname}" >> ${tmp_hosts} 
	done
:<<!
/etc/hosts 里面,不填写data节点,该段注释
	echo >> ${tmp_hosts}
	echo "# data1 " >> ${tmp_hosts} 
	for ip in ${data[@]}
	do
		hostname=`ssh ${ip} hostname`
		echo -e "${ip}\t${hostname}" >> ${tmp_hosts} 
	done

	echo >> ${tmp_hosts}
	echo "# data2 " >> ${tmp_hosts} 
	for ip in ${array2[@]}
	do
		hostname=`ssh ${ip} hostname`
		echo -e "${ip}\t${hostname}" >> ${tmp_hosts} 
	done
!
	mv -f /etc/hosts /etc/hosts.bak
	cp -f ${tmp_hosts} /etc/hosts 

	for ip in ${array1[@]}
	do
		scp ${tmp_hosts} ${ip}:/etc/hosts >/dev/null 2>&1
	done
}


prepare_demo_file()
{
	log info "准备demo.options文件。"

	demo_file=${install_source_dir}/gcinstall/demo.options

	# V953 demo.options file
	if [ "${db_source_file}" == "gbase8a_v953" ]
	then

		# 8 params : installPrefix、coordinateHost、coordinateHostNodeID、gcwareHost、gcwareHostNodeID、dataHost、dbaPwd、rootPwd
		
		#installPrefix
		if [ -n "${install_target}" ]
		then
			install_target=${install_target//\//\\\/};
			sed -i "s/^installPrefix.*$/installPrefix= ${install_target}/g" ${demo_file}
		fi

		# coor hosts
		sed -i "s/^coordinateHost =.*$/coordinateHost = ${coor_ip_list}/g" ${demo_file}
		sed -i "s/^gcwareHost =.*$/gcwareHost = ${coor_ip_list}/g" ${demo_file}

		# coor NodeID
		nodeID=`echo "${coor_ip_list}"  |sed 's/,/\n/g' |awk -F. '{print NR}' |paste -sd ','`
		sed -i "s/^coordinateHostNodeID =.*$/coordinateHostNodeID = ${nodeID}/g" ${demo_file}
		sed -i "s/^gcwareHostNodeID =.*$/gcwareHostNodeID = ${nodeID}/g" ${demo_file}

		# data hosts
		if [ "${InstanceType}" == 1 ]
		then
			data_list=${data_ip_list1},${data_ip_list2} 	
		else
			data_list=${data_ip_list1}
		fi
		sed -i "s/^dataHost =.*$/dataHost = ${data_list}/g" ${demo_file}

		# password
		sed -i "s/^rootPwd =.*$/rootPwd = '${root_pwd}'/g" ${demo_file}
		sed -i "s/^dbaPwd =.*$/dbaPwd = '${gbase_pwd}'/g" ${demo_file}

		# charset
		if [ -z "${characterSet}" ]
		then
			sed -i "s/^characterSet/#characterSet/g" ${demo_file}
		else
			sed -i "s/^.*characterSet.*$/characterSet = ${characterSet}/g"  ${demo_file}
		fi
		
		# sshPort
		if [ -z "${ssh_port}" ]
		then
			sed -i "s/^sshPort/#sshPort/g" ${demo_file}
		else
			sed -i "s/^.*sshPort.*$/sshPort = ${ssh_port}/g"  ${demo_file}
		fi

	elif [ "${db_source_file}" == "gbase8a_v952" ]
        then

                # 6 params : installPrefix、coordinateHost、coordinateHostNodeID、dataHost、dbaPwd、rootPwd

                #installPrefix
				if [ -n "${install_target}" ]
				then
					install_target=${install_target//\//\\\/};
					sed -i "s/^installPrefix.*$/installPrefix= ${install_target}/g" ${demo_file}
				fi
		
				# coor hosts
                sed -i "s/^coordinateHost =.*$/coordinateHost = ${coor_ip_list}/g" ${demo_file}

                # coor NodeID
                nodeID=`echo "${coor_ip_list}"  |sed 's/,/\n/g' |awk -F. '{print NR}' |paste -sd ','`
                sed -i "s/^coordinateHostNodeID =.*$/coordinateHostNodeID = ${nodeID}/g" ${demo_file}

                # data hosts
                data_list=${data_ip_list1}
                sed -i "s/^dataHost =.*$/dataHost = ${data_list}/g" ${demo_file}

                # password
                sed -i "s/^rootPwd =.*$/rootPwd = '${root_pwd}'/g" ${demo_file}
                sed -i "s/^dbaPwd =.*$/dbaPwd = '${gbase_pwd}'/g" ${demo_file}

		# charset 
		if [ -z "{$characterSet}" ]
		then
			sed -i "s/^characterSet/#characterSet/g" ${demo_file}
		else
			sed -i "s/^.*characterSet.*$/characterSet = ${characterSet}/g"  ${demo_file}
		fi
		
		# sshPort
		if [ -z "${ssh_port}" ]
		then
			sed -i "s/^sshPort/#sshPort/g" ${demo_file}
		else
			sed -i "s/^.*sshPort.*$/sshPort = ${ssh_port}/g"  ${demo_file}
		fi
		
	elif [ "${db_source_file}" == "gbase8a_v862" ]
        then

                # 7 params : installPrefix、coordinateHost、coordinateHostNodeID、dataHost、dbaPwd、rootPwd、loginUserPwd

                #installPrefix
				if [ -n "${install_target}" ]
				then
					install_target=${install_target//\//\\\/};
					sed -i "s/^installPrefix.*$/installPrefix= ${install_target}/g" ${demo_file}
				fi
		
				# coor hosts
                sed -i "s/^coordinateHost =.*$/coordinateHost = ${coor_ip_list}/g" ${demo_file}

                # coor NodeID
                nodeID=`echo "${coor_ip_list}"  |sed 's/,/\n/g' |awk -F. '{print NR}' |paste -sd ','`
                sed -i "s/^coordinateHostNodeID =.*$/coordinateHostNodeID = ${nodeID}/g" ${demo_file}

                # data hosts
                data_list=${data_ip_list1}
                sed -i "s/^dataHost =.*$/dataHost = ${data_list}/g" ${demo_file}

                # password
                sed -i "s/^rootPwd =.*$/rootPwd = '${root_pwd}'/g" ${demo_file}
                sed -i "s/^loginUserPwd =.*$/loginUserPwd = '${root_pwd}'/g" ${demo_file}
                sed -i "s/^dbaPwd =.*$/dbaPwd = '${gbase_pwd}'/g" ${demo_file}

		# charset 
		if [ -z "{$characterSet}" ]
		then
			sed -i "s/^characterSet/#characterSet/g" ${demo_file}
		else
			sed -i "s/^.*characterSet.*$/characterSet = ${characterSet}/g"  ${demo_file}
		fi
		
		# sshPort
		if [ -z "${ssh_port}" ]
		then
			sed -i "s/^sshPort/#sshPort/g" ${demo_file}
		else
			sed -i "s/^.*sshPort.*$/sshPort = ${ssh_port}/g"  ${demo_file}
		fi
		
	fi

	log debug "demo.options文件内容:\r\n`cat ${demo_file} |sed '/Pwd/s/=.*$/= \*\*\*\*\*\*/g'`"

}


set_sys_env()
{

	if [ "${db_source_file}" == "gbase8a_v953" ] || [ "${db_source_file}" == "gbase8a_v952" ] 
	then
		cpush all: ${install_source_dir}/gcinstall/SetSysEnv.py /tmp
		cexec all: "python /tmp/SetSysEnv.py --installPrefix=${install_target} --dbaUser=gbase --cgroup"
	fi 
}


install_db()
{
	cd ${install_source_dir}/gcinstall

	if [ -f gcinstall.log ]
	then
		cat gcinstall.log >> gcinstall_old.log
		> gcinstall.log
	fi

	if [ "${db_source_file}" == "gbase8a_v862" ]
	then
		src_version=`grep release_version ${install_source_dir}/gcinstall/BUILDINFO | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		if [ $(echo ${src_version} |grep '8\.6\.2\.43' |wc -l) -gt 0 ]; then 
			python gcinstall.py --silent=demo.options -a
		else 
			python gcinstall.py --silent=demo.options
		fi
	elif [ "${db_source_file}" == "gbase8a_v952" ]
	then
		python gcinstall.py --silent=demo.options -a
	else
		python gcinstall.py --silent=demo.options -i -a
	fi

	errorcount=`cat ${install_source_dir}/gcinstall/gcinstall.log | tail -10 | grep -i error | wc -l `
	failcount=`cat ${install_source_dir}/gcinstall/gcinstall.log | grep -Ei "fail to install cluster" | wc -l`
	msg=`cat gcinstall.log | tail -10 | grep -i error ` 
	
	chown -R gbase:gbase ${install_source_dir}
	cd ${wd}

	if [ "${errorcount}" -gt 0 ] || [ "${failcount}" -gt 0 ] 
	then
		log error "软件安装过程中出错,详细信息请查看 ${install_source_dir}/gcinstall/gcinstall.log"
		exit 1
	else
		set_sys_env
		log info "软件安装完毕。"
	fi
}


create_vc_xml()
{
	vcnamevar=vc_name$1
	vccommvar=vc_comm$1
	vclistvar=vc_list$1

	filename=${install_source_dir}/gcinstall/${!vcnamevar}.xml
	distfilename=${install_source_dir}/gcinstall/${!vcnamevar}_dist.xml
	log debug "vc$1: ${filename} ${!vccommvar} ${!vclistvar}"

	> ${filename}

	# header 
	echo "<?xml version='1.0' encoding=\"utf-8\"?>" >> ${filename}
	echo -e "<servers>\n" >> ${filename}


	# body
	# V953 双实例,每个rack2节点,patten1。 V862、952及953单实例,只设1个rack,patten2。
	if [ "${InstanceType}" == 1 ]
	then
		pattern="pattern 1"

:<<!
修改生成vc_xml的方式
		echo ${!vclistvar} |sed 's/,/\n/g' | sort -t'.' -k3,3n -k4,4n |while read ip
		do
			ip_last_num=`echo ${ip} |awk -F. '{print $NF}'`
			if [ $((ip_last_num%2)) == "${even_num}" ]    
			then
				echo -e "        <node ip=\"${ip}\"/>" >> ${filename}
				echo -e "    </rack>" >> ${filename}
			else
				echo -e "    <rack>" >> ${filename}
				echo -e "        <node ip=\"${ip}\"/>" >> ${filename}
			fi

		done 
!
		
		# 从C3获取节点IP(不重复),然后ssh获取所有IP地址,判断是否在 ${!vclistvar} 内,写入rack 。
		for machine_ip in `cexec -p ${!vcnamevar}: hostname |sort -k 3|awk '{print $2}' |sed 's/://g'`
		do
			log debug "检测物理机 ${machine_ip} 的实例IP"
			echo -e "    <rack>" >> ${filename}
			for node_ip in `ssh ${machine_ip} /usr/sbin/ip a |grep ${ip_prefix} | awk '{print $2}' | awk -F'/' '{print $1}'`
			do
				if [ `echo "${!vclistvar}" | grep -w "${node_ip}" | wc -l ` -gt 0 ]
				then
					echo -e "        <node ip=\"${node_ip}\"/>" >> ${filename}
					log debug "        ${node_ip} 在 ${!vcnamevar} 中"
				else
					log debug "        ${node_ip} 不在 ${!vcnamevar} 中"
				fi
			done
			echo -e "    </rack>" >> ${filename}
		done

	else
		pattern="pattern 2"	
		echo -e "    <rack>" >> ${filename} 
		echo ${!vclistvar} |sed 's/,/\n/g' | sort -t'.' -k3,3n -k4,4n |while read ip
		do
			echo -e "        <node ip=\"${ip}\"/>" >> ${filename}

		done 
		echo -e "    </rack>" >> ${filename}

	fi

	# name & comment
	echo -e "\n    <vc_name name=\"${!vcnamevar}\"/>" >> ${filename}
	echo -e "    <comment message=\"${!vccommvar}\"/>\n" >> ${filename}

	# tail 
	echo "</servers>" >> ${filename}


	# distribution file	
	cat ${filename} |grep -vE "vc_name|comment" > ${distfilename}

	log debug "${filename} 文件内容:`cat ${filename}`"
	
	# duplicateNum
	duplicateNum=1
	if [ -n "${!vclistvar}" ]
	then
		data_array=(${!vclistvar//,/ })
		if [ "${#data_array[@]}" == 1 ]
		then
			duplicateNum=0
		fi
	fi
	
	if [ "${db_source_file}" == "gbase8a_v953" ]
	then
		dba_os_password_str="dba_os_password '********'"
		dba_os_password_val="dba_os_password '${gbase_pwd}'"
	else
		dba_os_password_str=""
		dba_os_password_val=""
	fi
	
	if [ "${db_source_file}" == "gbase8a_v862" ]
	then
		#dba_password_str="db_root_pwd '********'"
		#dba_password_val="db_root_pwd '${root_pwd}'"
		dba_password_str=""
		dba_password_val=""
	elif [ "${db_source_file}" == "gbase8a_v952" ]
	then
		dba_password_str="db_user gbase db_pwd '********'"
		dba_password_val="db_user gbase db_pwd '${db_gbase_pwd}'"
	else
		dba_password_str="db_user gbase db_pwd '********' dba_os_password '********'"
		dba_password_val="db_user gbase db_pwd '${db_gbase_pwd}' dba_os_password '${gbase_pwd}'"
	fi

	# create vc & distribution 
	log debug "执行创建VC及distribution命令: su - gbase -c \"gcadmin createvc ${filename} && gcadmin distribution ${distfilename} p 1 d ${duplicateNum} ${dba_password_str} ${pattern} vc ${!vcnamevar} && gccli -ugbase -p******** -e'use vc ${!vcnamevar}; initnodedatamap'\""
	su - gbase -c "gcadmin createvc ${filename} && gcadmin distribution ${distfilename} p 1 d ${duplicateNum} ${dba_password_val} ${pattern} vc ${!vcnamevar} && gccli -ugbase -p${db_gbase_pwd} -e 'use vc ${!vcnamevar}; initnodedatamap'"
	if [ $? == 0 ]
	then
		log info "VC: ${!vcnamevar} 创建成功。"
	else
		log error "VC: ${!vcnamevar} 创建失败,因软件已安装,需人工解决此问题。"
	fi

}


create_vc()
{
	# V8版本、V9的兼容模式、或者设置了VC模式但vc_name为空,将创建为兼容模式
	if [ "$db_source_file" == "gbase8a_v862" ] || [ "${GBaseMode}" == 2 ] || [ -z "${vc_name1}" ]
	then
		log info "无VC或默认VC"
		if [ "$db_source_file" == "gbase8a_v953" ] && [ "${InstanceType}" == 1 ]
		then
			pattern="pattern 1"
		fi
		
		# duplicateNum
		if [ "${InstanceType}" == 1 ]
		then
			data_list=${data_ip_list1},${data_ip_list2} 	
		else
			data_list=${data_ip_list1}
		fi
		
		duplicateNum=1
		if [ -n "${data_list}" ]
		then
			data_array=(${data_list//,/ })
			if [ "${#data_array[@]}" == 1 ]
			then
				duplicateNum=0
			fi
		fi
		
		if [ "${db_source_file}" == "gbase8a_v862" ]
		then
			#dba_password_str="db_root_pwd '********'"
			#dba_password_val="db_root_pwd '${root_pwd}'"
			dba_password_str=""
			dba_password_val=""
		elif [ "${db_source_file}" == "gbase8a_v952" ]
		then
			dba_password_str="db_user gbase db_pwd '********'"
			dba_password_val="db_user gbase db_pwd '${db_gbase_pwd}'"
		else
			dba_password_str="db_user gbase db_pwd '********' dba_os_password '********'"
			dba_password_val="db_user gbase db_pwd '${db_gbase_pwd}' dba_os_password '${gbase_pwd}'"
		fi

		log debug "执行创建distribution命令: su - gbase -c \"gcadmin distribution ${install_source_dir}/gcinstall/gcChangeInfo.xml p 1 d ${duplicateNum} ${dba_password_str} ${pattern}; gccli -ugbase -p******** -e'initnodedatamap'\""
		su - gbase -c "gcadmin distribution ${install_source_dir}/gcinstall/gcChangeInfo.xml p 1 d ${duplicateNum} ${dba_password_val} ${pattern} ; gccli -ugbase -p${db_gbase_pwd} -e 'initnodedatamap ;' "

		if [ $? == 0 ]
		then
			log info "distribution 创建成功。"
		else
			log error "distribution  创建失败,因软件已安装,需人工解决此问题。"
		fi

	else
		log info "创建vc1 :${vc_name1}"
		create_vc_xml 1

		if [ ! -z "${vc_name2}" ] && [ ! -z "${vc_list2}" ]
		then
			log info "创建vc2 : ${vc_name2}"
			create_vc_xml 2
			if [ ! -z "${vc_name3}" ] && [ ! -z "${vc_list3}" ]
			then
				log info "创建vc3 : ${vc_name3}"
				create_vc_xml 3
			fi
		fi

	fi
}


change_db_pwd()
{
	if [ ! -z "${db_root_new_pwd}" ] 
	then
		log debug "修改数据库root密码"
		su - gbase -c "gccli -ugbase -p${db_gbase_pwd} -e \" set password for root = password('${db_root_new_pwd}'); \" " 
		if [ $? == 0 ]
		then
			log info "修改数据库root密码成功。"
		else
			log error "修改数据库root密码失败,因软件已安装,需人工解决此问题。"
			flag=1
		fi
	fi

	if [ ! -z "${db_gbase_new_pwd}" ] 
	then
		log debug "修改数据库gbase密码"
		su - gbase -c "gccli -ugbase -p${db_gbase_pwd} -e \" set password for gbase = password('${db_gbase_new_pwd}'); \" " 
		if [ $? == 0 ]
		then
			log info "修改数据库gbase密码成功。"
		else
			log error "修改数据库gbase密码失败,因软件已安装,需人工解决此问题。"
			flag=1
		fi
	fi

}


check_cluster()
{
	status=1
	
	for xh in `seq 1 12`
	do
		
		if ! su - gbase -c "command -v gcadmin" >/dev/null 2>&1;
		then 
			log warn "gcadmin命令未找到。"
			status=1
			break
		fi
		
		log info "检查集群状态,第 ${xh} 次..."
		coor_status=1
		if [ `su - gbase -lc "gcadmin |grep gcware[1-9] | grep -E 'CLOSE|OFFLINE' | wc -l"` -gt 0 ] || [ `su - gbase -lc "gcadmin |grep coordinator[1-9] | grep -E 'CLOSE|OFFLINE' | wc -l"` -gt 0 ] 
		then
			log warn '管理集群部分节点状态不正常。'
			
			su - gbase -lc "gcadmin |grep gcware[1-9] | grep -E 'CLOSE|OFFLINE'" |tee -a ${logfile}
			su - gbase -lc "gcadmin |grep coordinator[1-9] | grep -E 'CLOSE|OFFLINE'" |tee -a ${logfile}
			coor_status=0
		fi
		
		vcList=`su - gbase -lc gcadmin showcluster d|grep "virtual cluster" | awk -F: '{print $2}' |sed 's/,//g'`
		data_status=1
		if [ -n "${vcList}" ]
		then
			for vcname in ${vcList}
			do
				if [ `su - gbase -lc "gcadmin showcluster vc ${vcname}|grep node[1-9] | grep -E 'CLOSE|OFFLINE' | wc -l"` -gt 0 ]
				then
					log warn "${vcname}集群部分节点状态不正常。"
					su - gbase -lc "gcadmin showcluster vc ${vcname}|grep node[1-9] | grep -E 'CLOSE|OFFLINE'" |tee -a ${logfile}
					data_status=0
					continue
				fi
			done
		fi
		
		if [ `su - gbase -lc "gcadmin showcluster d | grep -E 'CLOSE|OFFLINE' | wc -l"` -gt 0 ]
		then
			log warn '计算集群部分节点状态不正常。'
			su - gbase -lc "gcadmin showcluster d | grep -E 'CLOSE|OFFLINE'" |tee -a ${logfile}
			data_status=0
		fi
		
		
		if [ "${coor_status}" == 0 ] || [ "${data_status}" == 0 ] 
		then
			sleep 10
		else
			status=0
			break
		fi
		
	done

	if [ "${status}" == 1 ] 
	then
		log info '集群状态不正常,请检查原因。。。'
		log info '排除故障后,输入C继续,或者输入Q退出后手工处理。'
		while true
		do
			read -p '请输入:继续[C]、退出[Q]:' inputflag
			log  info "您的输入是: ${inputflag}"
			if [ "${inputflag}" == "c" ] || [ "${inputflag}" == "C" ]  
			then
				break	
			elif [ "${inputflag}" == "q" ] || [ "${inputflag}" == "Q" ]
			then
				log error "安装过程中遇到错误,结束安装。"
				exit 1
			fi
		done
		check_cluster
	else
		log info '集群状态检测通过。'
	fi
}


auto_start()
{
	log debug "设置gcware_services服务开机自启动"
	cexec coor: 'sed -i "/gcware_services/d" /etc/rc.d/rc.local; echo "su - gbase -c \"gcware_services all start\"" >> /etc/rc.d/rc.local'
	
	log debug "设置gcluster_services服务开机自启动"
	cexec all: 'sed -i "/gcluster_services/d" /etc/rc.d/rc.local; echo "su - gbase -c \"gcluster_services all start\"" >> /etc/rc.d/rc.local'
}


restart_services()
{
	
	if [ "$db_source_file" == "gbase8a_v862" ] 
	then
		cexec all: 'service gcware restart'
	else
		su - gbase -c "cexec all: 'gcluster_services all restart'"
	fi
	
	check_cluster
}


show_clusterinfo()
{
	dbversion=`su - gbase -c 'gclusterd -V'`
	log info "\033[40;32m集群版本为:${dbversion}\033[40;32m\033[0m"
	
	dbcharact=`su - gbase -c "gccli -ugbase -p${db_gbase_new_pwd} -Ns -e \"show variables like '%character_set_database%'\""`
	if [ "${dbcharact}" != "" ]
	then 
		dbcharactValue=`echo ${dbcharact} | awk '{print $2}'`
		log info "\033[40;32m集群字符集为:${dbcharactValue}\033[40;32m\033[0m"
	fi
}



isNull()
{
	local name="$1"
	local var="$2"
	
	if [ -z "${var}" ]
	then
		log error "“${name}”参数值为空,请正确填写!"
		flag=1
	fi
}


validStr()
{
	local strname="$1"
	local strvar="$2"
	local strlen="$3"
	
	if [ -n "${strvar}" ]
	then
        for i in `seq ${#strvar}`
        do
			char=${strvar:$i-1:1}
			case "$char" in
				[a-z]|[A-Z]) ;;
				[0-9]) ;;
				" ") ;;
				"_") ;;
				",") ;;
				"!") ;;
				".") ;;
				*)
					log error "“$strname”参数值不合法,要求必须是英文字符“大小写字母、数字、空格 、下划线_、逗号, 点.”,不能有汉字或中文符号。"
					flag=1
			esac
        done
		
		if [ -n "${strlen}" ] && [ "${strlen}" -gt 0 ]
		then
			if [ ${#strvar} -gt ${strlen} ]
			then
				log error "“$strname”参数值不合法,长度不能超过${strlen},当前长度为${#strvar}。"
				flag=1
			fi
		fi
	fi
}


main()
{
	step=0
	
	cat ${logfile} >> ${logfile}_old
	> ${logfile}
	echo -e "\n\n" |tee -a ${logfile}
	log info "开始执行自动安装..."

	if [ "$USER" != "root" ]
	then
		log error "本脚本须在root用户下运行。"
		exit 1
	fi

	### 
	###  下载及解压 gbase_workspace.tar.gz 工作需提前手工进行 ,本脚本不做检验
	###

	step=$((step+1))
	log info "Step${step}: 读取配置文件..."
	read_config
	
	if [ "${check_finish_env}" != 1 ]
	then
		log error "请在执行软件自动安装前,对各节点环境进行检查(执行pre_check.sh脚本)."
		exit 1
	fi
	
	step=$((step+1))
	log info "Step${step}: 检查配置文件..."
	check_config
	
	step=$((step+1))
	log info "Step${step}: 检查GBase服务..."
	check_gbase_service
	
	step=$((step+1))
	log info "Step${step}: 检查节点状态..."
	check_node_stats
	
	step=$((step+1))
	log info "Step${step}: 更新C3配置..."
	update_c3
	
#:<<!
	step=$((step+1))
	log info "Step${step}: 准备软件安装程序..."
	download_src_file

	step=$((step+1))
	log info "Step${step}: 准备demo.options文件..."
	prepare_demo_file

	step=$((step+1))
	log info "Step${step}: 执行SetSysEnv.py环境设置..."
	set_sys_env

	step=$((step+1))
	log info "Step${step}: 执行软件安装..."
	install_db
	
#!
	step=$((step+1))
	log info "Step${step}: 检查集群安装状态..."
	check_cluster

	step=$((step+1))
	log info "Step${step}: 创建VC和分布..."
	create_vc 
	
#:<<!
	step=$((step+1))
	log info "Step${step}: 修改数据库用户密码..."
	change_db_pwd
#!

	step=$((step+1))
	log info "Step${step}: 再次执行SetSysEnv.py..."
	set_sys_env
#!

	if [ "${flag}" == 1 ]
	then
		log error "\033[5;40;31m安装过程中发生异常,\033[0m\033[40;31m详情请查看 ${logfile}。\033[0m"
		exit 1
	else
		
		step=$((step+1))
		log info "Step${step}: 初始化集群 - 设置开机自启动..."
		auto_start
		
		step=$((step+1))
		log info "Step${step}: 初始化集群 - 重新启动集群服务..."
		restart_services
	
		step=$((step+1))
		log info "Step${step}: 获取集群信息..."
		show_clusterinfo
		
		log info "\033[5;40;32m *** 软件已安装完成,欢迎使用! *** \033[40;32m\033[0m"
		
	fi
}

main

附录 4 force_unistall.sh

#!/bin/bash
###############################################################################
##creator : TuJunbing                                                        ##
##create time: 2024-7-10                                                     ##
##Decription: force_unistall.sh                                              ##
##Version: 1.1 			                                                     ##
###############################################################################

wd=$(cd `dirname $0` && pwd)
updir=`dirname ${wd}`

logdir=${updir}/log
logfile=${logdir}/unistall_`date '+%Y%m%d'`.log
export loglevel=$(grep loglevel ${wd}/db_config.conf | awk -F= '{print $2}' | awk '$1=$1')

log(){
	if [ "${1,,}" == "error" ] || [ "${1,,}" == "warn" ] 
	then
		sh logger.sh ${logfile} $1 "[$0] [line:`caller |awk '{print $1}'`] $2"
	else
		sh logger.sh ${logfile} $1 "$2"
	fi
}

log debug "wd=${wd} updir=${updir}"

flag=0

read_config()
{
	log debug "${FUNCNAME[@]}"
	config_file=${wd}/db_config.conf
	db_source_conf_file=${wd}/db_source.conf

	while read line 
	do
		comment=$(echo $line | grep "#")
		if [ "${comment}" != "" ]
		then
			continue
		fi
			
		line_trim=`echo "${line}" |awk -F# '{print $1}' |grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		if [ -z "${line_trim}" ]
		then
			continue
		fi
		
		varname=`echo ${line_trim} | awk -F= '{print $1}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		varvalue=`echo ${line_trim} | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`

		if [[ ${varname} =~ 'pwd' ]] || [[ ${varname} =~ 'password' ]]
		then
			varvaluemask=********
		else
			varvaluemask=${varvalue}
		fi

		#log debug "varname='${varname}',varvalue='${varvaluemask}'"

		if [ -z "${varvalue}" ]
		then 
			#unset varname
			log debug "读取配置参数 ${varname}=${varvalue}"
		else
			log debug "读取配置参数 ${varname}=${varvaluemask}"
			export ${varname}="${varvalue}"
		fi

	done < ${config_file}

	if [ -n "${mirror_site}" ] 
	then
		if [ "${mirror_site}" == "local" ] || [ -z "${mirror_site}" ]
		then
			gc_dir="${install_source_dir}/gcinstall"
			
			if [ ! -d "${gc_dir}" ]
			then
				log error "未检查到已解压的安装程序gcinstall,请在开始卸载前将软件安装包存放至目录:${install_source_dir}内,并对其进解压处理。"
				flag=1
			fi
		else
			while read line 
			do
				line_trim=`echo "${line}" |awk -F# '{print $1}' |grep -o "[^ ]\+\( \+[^ ]\+\)*"`

				varname=`echo ${line_trim} | awk -F= '{print $1}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
				varvalue=`echo ${line_trim} | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`

				if [ "${varname}" == "${mirror_site}" ] || [ "${varname}" == "${db_source_file}" ]
				then
		 
					if [[ ${varname} =~ 'pwd' ]] || [[ ${varname} =~ 'password' ]]
					then
						varvaluemask=********
					else
						varvaluemask=${varvalue}
					fi
			
					#log debug "varname='${varname}',varvalue='${varvaluemask}'"
			
					if [ -z "${varvalue}" ]
					then 
						unset varname
					else
						log debug "读取配置参数 ${varname}=${varvaluemask}"
						export ${varname}="${varvalue}"
					fi
				fi			

			done < ${db_source_conf_file}

			log debug "读取配置参数 full_target_url=${!mirror_site}/${!db_source_file}"
			export full_target_url=${!mirror_site}/${!db_source_file}
		fi
	fi
}


check_demo_file()
{
	log info "检查demo.options文件。"

	demo_file=${install_source_dir}/gcinstall/demo.options
	if [ -f "${demo_file}" ]
	then
		log warn "请人工核对demo.options文件参数信息与实际一致。"
		log info "demo.options文件内容:\r\n`cat ${demo_file} |sed '/Pwd/s/=.*$/= \*\*\*\*\*\*/g'`"
	fi
}


uninstall_cluster()
{
	if [ "${db_source_file}" == "gbase8a_v862" ]
	then
		src_version=`grep release_version ${install_source_dir}/gcinstall/BUILDINFO | awk -F= '{print $2}'|grep -o "[^ ]\+\( \+[^ ]\+\)*"`
		if [ $(echo ${src_version} |grep '8\.6\.2\.43' |wc -l) -gt 0 ]; then 
			su - gbase -c "cd ${install_source_dir}/gcinstall; python unInstall.py --silent=demo.options -a "
		else 
			su - gbase -c "cd ${install_source_dir}/gcinstall; python unInstall.py --silent=demo.options "
		fi
	else
		su - gbase -c "cd ${install_source_dir}/gcinstall; python unInstall.py --silent=demo.options -a " 
	fi
}


stop_all_services()
{
	if [ "${db_source_file}" == "gbase8a_v862" ]
	then
		cexec all: 'service gcware stop'
	else
		su - gbase -c 'cexec all: gcluster_services all stop'
		su - gbase -c 'cexec coor: gcware_services all stop'
	fi
}


main()
{
	step=0

	cat ${logfile} >> ${logfile}_old
	> ${logfile}
	echo -e "\n\n" |tee -a ${logfile}
	log info "开始执行卸载检查..."

	if [ "$USER" != "root" ]
	then
		log error "本脚本须在root用户下运行。"
		exit 1
	fi

	step=$((step+1))
	log info "Step${step}: 读取配置文件..."
	read_config
	
	step=$((step+1))
	log info "Step${step}: 检查demo文件..."
	check_demo_file
	
	if [ "${flag}" -eq 1 ]
	then
		log error "\033[5;40;31m检查未通过,\033[0m\033[40;31m详情请查看 ${logfile}。\033[0m"
		exit 1
	else
		# 确认是否卸载
		echo -ne "\e[31m您确认要卸载整个集群吗?该操作无法回退!!!确认请输入([Y,y,YES,yes],取消请输入[N,n,NO,no]) \e[0m:" 
		while true
		do
			read -r input
			case $input in
				[yY][eE][sS]|[yY])
					echo "您输入的是 $input, 继续执行卸载."
					
					step=$((step+1))
					log info "Step${step}: 停止集群服务..."
					stop_all_services

					step=$((step+1))
					log info "Step${step}: 卸载集群软件..."
					uninstall_cluster
				
					exit 1
					;;
				[nN][oO]|[nN])
					echo "您输入的是 $input, 取消卸载."
					exit 1	       	
					;;
				*)
					echo "无效输入..., 请输入 [Y,y,YES,yes] or [N,n,NO,no]:"
					;;
			esac
		done
	fi
}


main

附录 5 logger.sh

#!/bin/bash

wd=$(cd `dirname $0` && pwd)

logfile=$1

#loglevel=0 #debug:0; info:1; warn:2; error:3

function log ()
{

	local msg;local logtype
	logtype=${1,,}

	if [ "${logtype}" == 'warning' ]
	then
		logtype=warn
		
	fi

	msg=$2
	datetime=`date +'%F %H:%M:%S'`

	if [[ ! "${msg}" =~ "Step" ]]
	then
		msg="    ${msg}"
	fi
	#使用内置变量$LINENO不行,不能显示调用那一行行号
	#logformat="[${logtype}]\t${datetime}\tfuncname:${FUNCNAME[@]} [line:$LINENO]\t${msg}"
	#logformat="[${logtype}]\t${datetime}\tfuncname: ${FUNCNAME[@]/log/}\t[line:`caller 0 | awk '{print $1}'`]\t${msg}"
	logformat="${datetime}\t[${logtype^^}]\t${msg}"
	#funname格式为log error main,如何取中间的error字段,去掉log好办,再去掉main,用echo awk? ${FUNCNAME[0]}不能满足多层函数嵌套

	{	

		case $logtype in
			debug)
				[[ $loglevel -le 0 ]] && echo -e "\e[32m${logformat}\e[0m" ;;
			info)
				[[ $loglevel -le 1 ]] && echo -e "\e[36m${logformat}\e[0m" ;;
			warn)
				[[ $loglevel -le 2 ]] && echo -e "\e[33m${logformat}\e[0m" ;;
			error)
				[[ $loglevel -le 3 ]] && echo -e "\e[31m${logformat}\e[0m" ;;
			esac

	} | tee -a $logfile

}

log $2 "$3"

附录 6 sshtrust.sh

#!/usr/bin/expect
###############################################################################
##creator : Tupac Tu
##create time: 2024-02-01
##Decription: 配置互信脚本(传参 IP文件名 OS用户账户 OS用户密码)
##Method: expect sshtrust.sh filename username password [sshport]
##Version: 1.0
###############################################################################

set filename [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]

set f [open $filename r]
while { [gets $f ip]>=0} {
	spawn ssh-copy-id -i $username@$ip
	expect {
		"*yes/no" {send "yes\r";exp_continue}
		"*password:" {send "$password\r";exp_continue}
	}
}
close $f

附录 7 db_source.conf


#################################################
# mirror sites 镜像网站地址                     #
#################################################

# 测试
test=ftp://gbase:gbase@192.168.195.20//opt/gbase_workspace/setup



##################################################
# db source file                                 #
##################################################
# V862 
gbase8a_v862=8.6.2/RHEL7.3-x86_64-8.6.2_build43-R30-nolicense/Setup/GBase8a_MPP_Cluster-NoLicense-8.6.2_build43-R30-redhat7.3-x86_64.tar.bz2

# V952 
gbase8a_v952=9.5.2/GBase8a_MPP_Cluster-9.5.2.42-129983-rhel7.3/GBase8a_MPP_Cluster-NoLicense-9.5.2.42-129983-redhat7.3-x86_64.tar.bz2

# V953 
gbase8a_v953=9.5.3/GBase8a_MPP_Cluster-9.5.3.27.20_patch.15-rhel7/GBase8a_MPP_Cluster-NoLicense-9.5.3.27.20_patch.15-r

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/318306.html

(0)
上一篇 2025年11月26日 23:04
下一篇 2025年11月26日 23:07

相关推荐

发表回复

登录后才能评论