#!/bin/bash
# set -x
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
shell_path=$(echo $(dirname $0;pwd) | awk '{print $2}')
script_path="${shell_path}/scripts"
ssh_user=$(python ${script_path}/getConfig.py -a get -s preflight -k ssh_user -c ${shell_path}/preflight.ini)
ssh_password=$(python ${script_path}/getConfig.py -a get -s preflight -k ssh_password -c ${shell_path}/preflight.ini)
ssh_port=$(python ${script_path}/getConfig.py -a get -s preflight -k ssh_port -c ${shell_path}/preflight.ini)
logs_path=${shell_path}/logs
k8s_hosts=$(python ${script_path}/getConfig.py -a get -s preflight -k service_hosts -c ${shell_path}/preflight.ini | tr "," " ")
db_hosts=$(python ${script_path}/getConfig.py -a get -s preflight -k database_hosts -c ${shell_path}/preflight.ini | tr "," " ")
xh_hosts=$(python ${script_path}/getConfig.py -a get -s preflight -k bigdata_hosts -c ${shell_path}/preflight.ini | tr "," " ")
debug=$(python ${script_path}/getConfig.py -a get -s preflight -k debug -c ${shell_path}/preflight.ini)
shell_func_list=$(cat ${shell_path}/preflight.ini |grep "true" |grep shell_check |grep -v xh|grep -v grep| awk '{print $1}' | xargs echo)
shell_xh_func_list=$(cat ${shell_path}/preflight.ini |grep "true" |grep shell_check |grep xh|grep -v grep| awk '{print $1}' | xargs echo)
sshopts="ssh -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile=/dev/null\" -o \"LogLevel=quiet\" -F \"${shell_path}/ssh_config\""
scpopts="scp -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile=/dev/null\""
docker_data_mount_point=$(python ${script_path}/getConfig.py -a get -s item_options -k docker_data_mount_point -c ${shell_path}/preflight.ini)
source ${script_path}/utils.sh
service_host_num=0
for host in ${k8s_hosts};do
exsit=false
for h in ${hostlist};do
if [[ "${h}" == "${host}" ]]; then
exsit=true
break
fi
done
if ! "${exsit}";then
service_host_num=$(expr ${service_host_num} + 1)
hostlist="${hostlist} ${host}"
fi
done
for host in ${db_hosts};do
exsit=false
for h in ${hostlist};do
if [[ "${h}" == "${host}" ]]; then
exsit=true
break
fi
done
if ! "${exsit}";then
hostlist="${hostlist} ${host}"
fi
done
for host in ${xh_hosts};do
exsit=false
for h in ${hostlist};do
if [[ "${h}" == "${host}" ]]; then
exsit=true
break
fi
done
if ! "${exsit}";then
hostlist="${hostlist} ${host}"
fi
done
#sh -c "echo a > /etc/a.log" > /dev/null 2>&1
#
#if [ "$?" != "0" ]; then
# echo -e "\033[33m 用户权限不足,请使用root用户执行或使用sudo权限得用户执行 "sudo bash install.sh" \033[0m"
# exit
#fi
if [ ! -d "${logs_path}" ]; then
mkdir ${logs_path}
fi
function check_cmd_status() {
if [[ $? -eq 0 ]]; then
#echo -e "\033[36m $@ SUCCESSED!\033[0m"
echo -e "INFO $@ SUCCESSED!"
else
#echo -e "\033[41;37m $@ FAILED!\033[0m"
echo -e "ERROR $@ FAILED!"
error_count=$(expr ${error_count} + 1)
fi
}
#获取配置文件参数 设置远程脚本参数
docker_data_mount_point=$(python ${script_path}/getConfig.py -a get -s item_options -k docker_data_mount_point -c ${shell_path}/preflight.ini)
database_machine_cpu=$(python ${script_path}/getConfig.py -a get -s item_options -k database_machine_cpu -c ${shell_path}/preflight.ini)
database_machine_mem=$(python ${script_path}/getConfig.py -a get -s item_options -k database_machine_mem -c ${shell_path}/preflight.ini)
alone_service_machine_cpu=$(python ${script_path}/getConfig.py -a get -s item_options -k alone_service_machine_cpu -c ${shell_path}/preflight.ini)
alone_service_machine_mem=$(python ${script_path}/getConfig.py -a get -s item_options -k alone_service_machine_mem -c ${shell_path}/preflight.ini)
three_service_machine_cpu=$(python ${script_path}/getConfig.py -a get -s item_options -k three_service_machine_cpu -c ${shell_path}/preflight.ini)
three_service_machine_mem=$(python ${script_path}/getConfig.py -a get -s item_options -k three_service_machine_mem -c ${shell_path}/preflight.ini)
bigdata_machine_cpu=$(python ${script_path}/getConfig.py -a get -s item_options -k bigdata_machine_cpu -c ${shell_path}/preflight.ini)
bigdata_machine_mem=$(python ${script_path}/getConfig.py -a get -s item_options -k bigdata_machine_mem -c ${shell_path}/preflight.ini)
install_path=$(python ${script_path}/getConfig.py -a get -s item_options -k install_path -c ${shell_path}/preflight.ini)
cp -p ${script_path}/pre_check_func.sh.template ${script_path}/pre_check_func.sh
chmod +x ${script_path}/pre_check_func.sh
sed -i "s;docker_data_mount_point=\"docker_data_mount_point_default\";docker_data_mount_point=\"${docker_data_mount_point}\";g" ${script_path}/pre_check_func.sh
sed -i "s/service_host_num=\"service_host_num_default\"/service_host_num=\"${service_host_num}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/alone_service_machine_cpu=\"alone_service_machine_cpu_default\"/alone_service_machine_cpu=\"${alone_service_machine_cpu}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/alone_service_machine_mem=\"alone_service_machine_mem_default\"/alone_service_machine_mem=\"${alone_service_machine_mem}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/three_service_machine_cpu=\"three_service_machine_cpu_default\"/three_service_machine_cpu=\"${three_service_machine_cpu}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/three_service_machine_mem=\"three_service_machine_mem_default\"/three_service_machine_mem=\"${three_service_machine_mem}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/database_machine_cpu=\"database_machine_cpu_default\"/database_machine_cpu=\"${database_machine_cpu}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/database_machine_mem=\"database_machine_mem_default\"/database_machine_mem=\"${database_machine_mem}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/bigdata_machine_cpu=\"bigdata_machine_cpu_default\"/bigdata_machine_cpu=\"${bigdata_machine_cpu}\"/g" ${script_path}/pre_check_func.sh
sed -i "s/bigdata_machine_mem=\"bigdata_machine_mem_default\"/bigdata_machine_mem=\"${bigdata_machine_mem}\"/g" ${script_path}/pre_check_func.sh
sed -i "s;install_path=\"install_path_default\";install_path=\"${install_path}\";g" ${script_path}/pre_check_func.sh
#设置tmp权限
for host in ${hostlist}; do
expect_command="${sshopts} ${ssh_user}@${host} \"echo \'${ssh_password}\' | sudo chmod 777 /tmp\""
run_expect -t 30 -p "${ssh_password}" "${expect_command}" >/dev/null 2>&1
check_cmd_status "修改 ${host} tmp权限为777"
done
#创建临时目录
for host in ${hostlist}; do
expect_command="${sshopts} ${ssh_user}@${host} \"echo \'${ssh_password}\' | sudo -S rm -rf /tmp/pre_check;mkdir -p /tmp/pre_check\""
run_expect -t 30 -p "${ssh_password}" "${expect_command}" >/dev/null 2>&1
check_cmd_status "mkdir ${host} /tmp/pre_check"
done
#传脚本到目标服务器
for host in ${hostlist}; do
expect_command="${scpopts} ${script_path}/pre_check_func.sh ${script_path}/get_drive_letter.py ${ssh_user}@${host}:/tmp/pre_check"
run_expect -t 30 -p "${ssh_password}" "${expect_command}" >/dev/null 2>&1
#check_cmd_status "scp ${src_file}到${host}服务器"
check_cmd_status "scp 脚本到${host}服务器 "
done
#error_count=0
##测试sudo权限
#for host in ${hostlist}; do
#
# expect_command="${sshopts} ${ssh_user}@${host} \"echo '${ssh_password}' | sudo -S id -u\""
# run_expect -t 30 -p ${ssh_password} ${expect_command} >/dev/null 2>&1
# check_cmd_status "注释 ${host} /etc/sudoers #Defaults requiretty "
#done
#
#if [[ ${error_count} -eq 0 ]];then
# echo "测试sudo权限 SUCCESS" | tee -a ${logs_path}/check_result.log
#else
# echo "测试sudo权限 FAILED" | tee -a ${logs_path}/check_result.log
#fi
#根据检测项循环执行目标主机上的check function
for f in ${shell_func_list};do
if [[ ${debug} == false ]];