版权属于:
桑帅东的博客
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
最近在部署程序的时候发现,一共三个语言,6台设备,每台设备3个模块,每个模块3种语言,总共6台设备下来要部署54个程序,工作重复繁琐不说,很容易出现漏部署或者错误,想到了使用一键脚本部署程序的方式,把程序的tar包和shell脚本分别通过统一入口放置,在入口处执行脚本,分别把tar包和执行脚本远程发送到对应的机器和对应的模块上去执行,这样就不用一个一个模块上传程序,再操作,因此想到了一种方式ssh免密登录的方式,通过一台机器去连接到其他所有要部署的机器上,把程序传输过去,前提是这些机器必须是在同一网段内,如果是在一个交换机下面,必须是在同一vlan下面。好了废话不多说,接下去第一步实现免密登录。
免密登录很简单,网上一搜很容易就能知道怎么做,我们看下过程:
客户端生成公私钥
此客户端就是我们要统一放置程序的地方,需要部署的机器我们称之为服务端,本地客户端生成公私钥:(一路回车默认即可)
ssh-keygen
上面这个命令会在用户目录.ssh文件夹下创建公私钥
cd ~/.ssh
ls
上传公钥到服务端
我们随机选择一台机器作为服务端,然后把第一步生成的公钥复制一份传到服务端,然后把公钥内容写入到authorized_keys文件内
cat id_rsa.pub >authorized_keys
可以看到客户端写入到服务器的 id_rsa.pub (公钥)内容。
测试下免密登录
ssh root@10.21.4.155
第一次需要输入yes才能访问!
可以看到我们的免密登录功能做好了,剩下的就是怎么把我们的程序,脚本发送过去,批量执行的问题了。
我们做了一个ci.sh的一键部署脚本,所有的程序tar包和部署脚本放在了deploy目录,执行ci.sh就可以了
#!/bin/bash
set -e
#######################################################################
# #4、#5、#6的相应模块的调试口ip
left2="10.21.4.121"
left2_2="10.21.4.122"
right2="10.21.4.124"
schedule2="10.21.4.123"
executor2_5="10.21.4.125"
executor2_6="10.21.4.126"
executor2_7="10.21.4.127"
left3="10.21.4.131"
left3_2="10.21.4.132"
right3="10.21.4.134"
schedule3="10.21.4.133"
executor3_5="10.21.4.135"
executor3_6="10.21.4.136"
executor3_7="10.21.4.137"
# 部署的时候指定执行体被复制到板卡的哪个目录
function help_info()
{
echo "help"
}
#######################################################################
script_dir=$(cd "$(dirname "$0")"; pwd)
script_name=$(basename $0)
script_path=$script_dir/$script_name
real_script_path=$(readlink -f `which $script_path`)
real_script_dir=$(dirname $real_script_path)
image_dir=$real_script_dir/deploy
wip="30.30.30.23"
bip="192.168.1.46"
machine_num=0
ip="0.0.0.0"
isw=0
isb=0
isdeploy=0
com=0
lange=0
short_opts="b:w:n:i:dc:l:h"
ARGS=`getopt -o ${short_opts} -n 'switch.sh' -- "$@"`
if [[ $? != 0 ]]; then
echo "Terminating..."
exit 1
fi
#将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "${ARGS}"
while true
do
case "$1" in
-w)
isw=1
wip=$2
shift 2
;;
-b)
isb=1
bip=$2
shift 2
;;
-n)
machine_num=$2
shift 2
;;
-d)
isdeploy=1
shift 1
;;
-c)
com=$2
#echo $com
shift 2
;;
-l)
lange=$2
#echo $lange
shift 2
;;
-h)
help_info
shift 1
;;
--)
module=$2
shift 2
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
############################################################
#处理剩余的无法识别的参数
unknown_arg=$@
for arg in $unknown_arg
do
echo "unknown argument $arg"
done
if [[ ${#unknown_arg} -ne 0 ]];then
exit 127
fi
############################################################
function deploy()
{
#将命令行指定的密码机号和模块号映射成具体的模块ip,存放在executor变量中
local executor=$(eval echo \$executor$1_$2)
local language=$3
#echo $executor
if [[ $language = "java" ]];then
ssh -t root@$executor "systemctl stop scheduleClient"
if [[ $2 -eq 5 ]];then
scp -r $image_dir/java/deploy-java-com$2.sh root@$executor:/home/
scp -r $image_dir/java/de_ht_nt.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-java-com$2.sh && ./deploy-java-com$2.sh"
elif [[ $2 -eq 6 ]];then
scp -r $image_dir/java/deploy-java-com$2.sh root@$executor:/home/
scp -r $image_dir/java/ub_ng_nt.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-java-com$2.sh && ./deploy-java-com$2.sh"
elif [[ $2 -eq 7 ]];then
scp -r $image_dir/java/deploy-java-com$2.sh root@$executor:/home/
scp -r $image_dir/java/ce_ht_nt.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-java-com$2.sh && ./deploy-java-com$2.sh"
fi
ssh -t root@$executor "systemctl start scheduleClient"
elif [[ $language = "php" ]];then
ssh -t root@$executor "systemctl stop scheduleClient"
if [[ $2 -eq 5 ]];then
scp -r $image_dir/php/deploy-php-com$2.sh root@$executor:/home/
scp -r $image_dir/php/centos_apache_ci.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-php-com$2.sh && ./deploy-php-com$2.sh"
elif [[ $2 -eq 6 ]];then
scp -r $image_dir/php/deploy-php-com$2.sh root@$executor:/home/
scp -r $image_dir/php/debian_nginx_ci.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-php-com$2.sh && ./deploy-php-com$2.sh"
elif [[ $2 -eq 7 ]];then
scp -r $image_dir/php/deploy-php-com$2.sh root@$executor:/home/
scp -r $image_dir/php/ubuntu_apache_ci.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-php-com$2.sh && ./deploy-php-com$2.sh"
fi
ssh -t root@$executor "systemctl start scheduleClient"
elif [[ $language == "python" ]];then
ssh -t root@$executor "systemctl stop scheduleClient"
if [[ $2 -eq 5 ]];then
scp -r $image_dir/python/deploy-python-com$2.sh root@$executor:/home/
scp -r $image_dir/python/cm_un_env_v1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-python-com$2.sh && ./deploy-python-com$2.sh"
elif [[ $2 -eq 6 ]];then
scp -r $image_dir/python/deploy-python-com$2.sh root@$executor:/home/
scp -r $image_dir/python/cm_ca_env_v1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-python-com$2.sh && ./deploy-python-com$2.sh"
elif [[ $2 -eq 7 ]];then
scp -r $image_dir/python/deploy-python-com$2.sh root@$executor:/home/
scp -r $image_dir/python/cm_dn_env_v1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-python-com$2.sh && ./deploy-python-com$2.sh"
fi
ssh -t root@$executor "systemctl start scheduleClient"
elif [[ $language -eq 0 ]];then
ssh -t root@$executor "systemctl stop scheduleClient"
#部署php
if [[ $2 -eq 5 ]];then
scp -r $image_dir/php/deploy-php-com$2.sh root@$executor:/home/
scp -r $image_dir/php/centos_apache_ci.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-php-com$2.sh && ./deploy-php-com$2.sh"
elif [[ $2 -eq 6 ]];then
scp -r $image_dir/php/deploy-php-com$2.sh root@$executor:/home/
scp -r $image_dir/php/debian_nginx_ci.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-php-com$2.sh && ./deploy-php-com$2.sh"
elif [[ $2 -eq 7 ]];then
scp -r $image_dir/php/deploy-php-com$2.sh root@$executor:/home/
scp -r $image_dir/php/ubuntu_apache_ci.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-php-com$2.sh && ./deploy-php-com$2.sh"
fi
#部署python
if [[ $2 -eq 5 ]];then
scp -r $image_dir/python/deploy-python-com$2.sh root@$executor:/home/
scp -r $image_dir/python/cm_un_env_v1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-python-com$2.sh && ./deploy-python-com$2.sh"
elif [[ $2 -eq 6 ]];then
scp -r $image_dir/python/deploy-python-com$2.sh root@$executor:/home/
scp -r $image_dir/python/cm_ca_env_v1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-python-com$2.sh && ./deploy-python-com$2.sh"
elif [[ $2 -eq 7 ]];then
scp -r $image_dir/python/deploy-python-com$2.sh root@$executor:/home/
scp -r $image_dir/python/cm_dn_env_v1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-python-com$2.sh && ./deploy-python-com$2.sh"
fi
#部署java
if [[ $2 -eq 5 ]];then
scp -r $image_dir/java/deploy-java-com$2.sh root@$executor:/home/
scp -r $image_dir/java/de_ht_nt.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-java-com$2.sh && ./deploy-java-com$2.sh"
elif [[ $2 -eq 6 ]];then
scp -r $image_dir/java/deploy-java-com$2.sh root@$executor:/home/
scp -r $image_dir/java/ub_ng_nt.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-java-com$2.sh && ./deploy-java-com$2.sh"
elif [[ $2 -eq 7 ]];then
scp -r $image_dir/java/deploy-java-com$2.sh root@$executor:/home/
scp -r $image_dir/java/ce_ht_nt.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod u+x deploy-java-com$2.sh && ./deploy-java-com$2.sh"
fi
ssh -t root@$executor "systemctl start scheduleClient"
fi
}
#machine_num、com、lange
function exe()
{
local mchi_num=$1
local com_num=$2
local lang=$3
local all_mchi_num="2 3"
local mchi_isexist=`expr index "$all_mchi_num" $mchi_num` #isexit为0,不存在,非0存在
local all_com_num="5 6 7"
local com_isexist=`expr index "$all_com_num" "$com_num"`
if [[ $mchi_num == "all" ]];then
for i in $all_mchi_num
do
for((j=5;j<=7;j++))
do
deploy $i $j 0
#echo $i $j
done
done
elif [[ $mchi_isexist -ne 0 && $com_isexist -ne 0 ]];then
deploy $mchi_num $com_num $lange
#echo $mchi_num $com_num $lange
elif [[ $mchi_isexist -ne 0 && $com_num -eq 0 ]];then
#echo $mchi_num $com_num $lange
deploy $mchi_num 5 $lange
deploy $mchi_num 6 $lange
deploy $mchi_num 7 $lange
fi
}
function left()
{
echo "function left"
}
function sche()
{
echo "function sche"
}
function cmn()
{
local mchi_num=$1
local com_num=$2
local executor=$(eval echo \$executor$1_$2)
local left=$(eval echo \$left$1)
local left2=$(eval echo \$left$1_2)
local schedule=$(eval echo \$schedule$1)
local right=$(eval echo \$right$1)
local all_mchi_num="2 3"
local mchi_num_isexist=`expr index "$all_mchi_num" $mchi_num` #isexit为0,不存在,非0存在
if [[ $mchi_num_isexist -eq 0 ]];then
echo "nothing done!!!"
exit 127
fi
if [[ $com_num -eq 1 ]];then
scp -r $image_dir/cmnmod/web_left.tar root@$left:/home/
scp -r $image_dir/cmnmod/web_left.sh root@$left:/home/
ssh -t root@$left "cd /home/ && chmod a+x web_left.sh && ./web_left.sh"
elif [[ $com_num -eq 2 ]];then
scp -r $image_dir/cmnmod/csm_left.tar root@$left2:/home/
scp -r $image_dir/cmnmod/csm_left.sh root@$left2:/home/
ssh -t root@$left2 "cd /home/ && chmod a+x csm_left.sh && ./csm_left.sh"
elif [[ $com_num -eq 3 ]];then
scp -r $image_dir/cmnmod/dispatch.tar root@$schedule:/home/
scp -r $image_dir/cmnmod/dispatch.sh root@$schedule:/home/
ssh -t root@$schedule "cd /home/ && chmod a+x dispatch.sh && ./dispatch.sh"
elif [[ $com_num -eq 4 ]];then
scp -r $image_dir/cmnmod/right.tar root@$right:/home/
scp -r $image_dir/cmnmod/right.sh root@$right:/home/
ssh -t root@$right "cd /home/ && chmod a+x right.sh && ./right.sh"
elif [[ $com_num -eq 5 ]];then
scp -r $image_dir/cmnmod/executor1.sh root@$executor:/home/
scp -r $image_dir/cmnmod/executor1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod a+x executor1.sh && ./executor1.sh"
elif [[ $com_num -eq 6 ]];then
scp -r $image_dir/cmnmod/executor2.sh root@$executor:/home/
scp -r $image_dir/cmnmod/executor2.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod a+x executor2.sh && ./executor2.sh"
elif [[ $com_num -eq 7 ]];then
scp -r $image_dir/cmnmod/executor3.sh root@$executor:/home/
scp -r $image_dir/cmnmod/executor3.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod a+x executor3.sh && ./executor3.sh"
elif [[ $com -eq 0 ]];then
scp -r $image_dir/cmnmod/web_left.tar root@$left:/home/
scp -r $image_dir/cmnmod/web_left.sh root@$left:/home/
ssh -t root@$left "cd /home/ && chmod a+x web_left.sh && ./web_left.sh"
scp -r $image_dir/cmnmod/csm_left.tar root@$left2:/home/
scp -r $image_dir/cmnmod/csm_left.sh root@$left2:/home/
ssh -t root@$left2 "cd /home/ && chmod a+x csm_left.sh && ./csm_left.sh"
scp -r $image_dir/cmnmod/dispatch.tar root@$schedule:/home/
scp -r $image_dir/cmnmod/dispatch.sh root@$schedule:/home/
ssh -t root@$schedule "cd /home/ && chmod a+x dispatch.sh && ./dispatch.sh"
scp -r $image_dir/cmnmod/right.tar root@$right:/home/
scp -r $image_dir/cmnmod/right.sh root@$right:/home/
ssh -t root@$right "cd /home/ && chmod a+x right.sh && ./right.sh"
executor=$(eval echo \$executor$1_5)
scp -r $image_dir/cmnmod/executor1.sh root@$executor:/home/
scp -r $image_dir/cmnmod/executor1.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod a+x executor1.sh && ./executor1.sh"
executor=$(eval echo \$executor$1_6)
scp -r $image_dir/cmnmod/executor2.sh root@$executor:/home/
scp -r $image_dir/cmnmod/executor2.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod a+x executor2.sh && ./executor2.sh"
executor=$(eval echo \$executor$1_7)
scp -r $image_dir/cmnmod/executor3.sh root@$executor:/home/
scp -r $image_dir/cmnmod/executor3.tar root@$executor:/home/
ssh -t root@$executor "cd /home/ && chmod a+x executor3.sh && ./executor3.sh"
fi
}
if [[ $module == "left" ]];then
echo "function left"
elif [[ $module == "right" ]];then
echo "right"
elif [[ $module == "cmn" ]];then
cmn $machine_num $com
elif [[ $module == "sch" ]];then
echo "sche"
elif [[ $module == "exe" ]];then
exe $machine_num $com $lange
fi
评论