Fabric外部节点部署自动化脚本
程序提供ip ,账户 ,密码,组织名称等。按照以下格式提供参数:
文件命名:ip_orgname_master.txt
文件内容:
master ip 账户 密码 networkname orgname orghostname
文件命名:ip_orgname_node.txt
文件内容:
node ip 账户 密码
脚本实现:
- 带参数文件传输功能
- 带参数远程执行功能
2.1 远程fabric环境检查 fabric_check.sh
2.2 远程检查系统环境和docker安装和检查:docker_check.sh
2.3 远程初始化swarm:swarm_init_master.sh、swarm_init_node.sh
2.3 远程启动组织的ca,couchdb,peer容器:org-start-stack_ip_orgname.sh
#!/bin/bash
# zoujiaojiao 2020-0829
# 变量全部大写,函数首字母大写下划线拼接
MASTER_FILE=$2_master.txt
echo $MASTER_FILE
NODE_FILE=$2_node.txt
TRANSFER_PROJECT=/
OTHER=$3
FABRIC_PATH=/mnt/nfs/vbaas/fabric
#获取主机IP账户密码信息
Get_Variable(){
TRANSFER_HOST=`echo ${LINE}|cut -d" " -f 2 `
TRANSFER_USER=`echo ${LINE}|cut -d" " -f 3`
TRANSFER_PASSWORD=`echo ${LINE}|cut -d" " -f 4`
NETWROK_NAME=`echo ${LINE}|cut -d" " -f 5`
ORG_NAME=`echo ${LINE}|cut -d" " -f 6`
ORG_HOST=`echo ${LINE}|cut -d" " -f 7`
TRANSFER_FILE=${OTHER:-${TRANSFER_HOST}_file.tar.gz}
echo ${TRANSFER_HOST} ${TRANSFER_USER} ${TRANSFER_PASSWORD} ${TRANSFER_FILE} ${NETWROK_NAME} ${ORG_NAME} ${ORG_HOST}
}
#所有主机执行操作
All_Hosts(){
cat $MASTER_FILE $NODE_FILE | while read LINE
do
Get_Variable
${ACTION}
done
}
#master执行操作
Master_Hosts(){
cat $MASTER_FILE | while read LINE
do
Get_Variable
REMOTE_SHELL="sh /swarm_init_master.sh"
${ACTION}
${REMOTE_TO_LOAL}
done
}
#node执行操作
Node_Hosts(){
cat $NODE_FILE | while read LINE
do
Get_Variable
${LOAL_TO_REMOTE}
${ACTION}
REMOTE_SHELL="sh /swarm_add_node.sh"
done
}
#生成客户端需要的文件
Touch_Files(){
cat $MASTER_FILE | while read LINE
do
Get_Variable
if [ ! ${OTHER} ];then
sed "s/MASTER_HOST/${TRANSFER_HOST}/g" swarm_init_master.sh > swarm_init_master_${TRANSFER_HOST}.sh
echo "TRANSFER_HOST:${TRANSFER_HOST}"
tar zcvf ${TRANSFER_HOST}_file.tar.gz swarm_init_master_${TRANSFER_HOST}.sh ${NODE_FILE} docker_check.sh fabric_check.sh swarm_add_node.sh fabric_bin.tar.gz
else
sed "s/NETWORKNAME/${NETWROK_NAME}/g" org-start-stack.sh > org-start-stack_${TRANSFER_HOST}_${ORG_NAME}.sh
sed -i "s/ORGNAME/${ORG_NAME}/g" org-start-stack_${MASTER_HOST}_${ORG_NAME}.sh > org-start-stack_${TRANSFER_HOST}_${ORG_NAME}.sh
tar zcvf ${OTHER}.tar.gz $FABRIC_PATH/networks/${NETWROK_NAME}/orgs/${ORG_NAME} $FABRIC_PATH/networks/${NETWROK_NAME}/crypto-config/peerOrganizations/${ORG_HOST} org-start-stack_${TRANSFER_HOST}_${ORG_NAME}.sh
fi
done
}
#fuction 文件传输
Remote_Transfer() {
expect -c "
set timeout -1
#spawn scp -r "$@"
spawn scp -r ${TRANSFER_FILE} ${TRANSFER_USER}@${TRANSFER_HOST}:${TRANSFER_PROJECT}
expect {
\"*assword\" { send \"${TRANSFER_PASSWORD}\r\" }
\"yes/no\" { send \"yes\r\"; exp_continue }
};
send \"exit\r\"
expect eof
"
}
#远程执行
Remote_Execution() {
expect -c "
set timeout 5
spawn ssh ${TRANSFER_USER}@${TRANSFER_HOST};
expect {
\"*assword\" { send \"${TRANSFER_PASSWORD}\r\" }
\"yes/no\" { send \"yes\r\"; exp_continue }
} ;
expect "#"
send \"${REMOTE_SHELL}\r\"
expect "#"
send \"exit\r\"
expect eof
"
}
#主函数
Main() {
case $1 in
"transfer")
Touch_Files
ACTION=Remote_Transfer
All_Hosts;;
"fabric")
ACTION=Remote_Execution
REMOTE_SHELL="tar zxvf /${TRANSFER_HOST}_file.tar.gz -C / && sh /fabric_check.sh"
All_Hosts;;
"docker")
ACTION=Remote_Execution
REMOTE_SHELL="sh /docker_check.sh"
All_Hosts;;
"swarm_master")
ACTION=Remote_Execution
REMOTE_TO_LOAL="Remote_Transfer \"${TRANSFER_USER}@${TRANSFER_HOST}:/${TRANSFER_HOST}_token ${TRANSFER_PROJECT}\""
Master_Hosts;;
"swarm_node")
ACTION=Remote_Execution
LOAL_TO_REMOTE="Remote_Transfer \"${TRANSFER_PROJECT}${TRANSFER_HOST}_token ${TRANSFER_USER}@${TRANSFER_HOST}:${TRANSFER_PROJECT}\""
Node_Hosts;;
"create_org")
ACTION=Remote_Transfer
REMOTE_SHELL="sh /org-start-stack_${TRANSFER_HOST}_${ORG_NAME}.sh"
Master_Hosts;;
*)
echo "Usage :"
echo "sh main.sh transfer master_ip_orgname \"filename1 filename2\""
echo " 传文件。默认传输当前master_host_file.tar.gz,自定义文件可以在双引号中写多个文件参数"
echo " "
echo "sh main.sh fabric master_ip_orgname"
echo " 安装和检查fabric环境"
echo " "
echo "sh main.sh docker master_ip_orgname"
echo " 安装和检查docker环境"
echo " "
echo "sh main.sh swarm_master master_ip_orgname"
echo " 初始化swarm集群master节点"
echo " "
echo "sh main.sh swarm_node master_ip_orgname"
echo " 添加swarm集群node节点"
echo " "
echo "sh main.sh create_org master_ip_orgname "
echo " 创建组织";;
esac
}
# 增加一个安装expect的函数,给node进行安装
#执行
Main $@