業(yè)務(wù)爸爸:小毛驢幫我實(shí)現(xiàn)一個(gè)功能,我在做業(yè)務(wù)版本更新的時(shí)候需要把該主機(jī)在clb上權(quán)限改為0,要求用shell腳本實(shí)現(xiàn)。
小毛驢:安排!
#!/bin/sh
#
#調(diào)用騰訊云的clb api,修改指定 clb, 指定 localtion id下指定 targets 主機(jī)的權(quán)限。
# author 小毛驢
#
clb_modify_target_api_url=${ApiUrl}
function installJq() {
if [ -f /etc/redhat-release ];then
_OSFLAG=`cat /etc/redhat-release | awk '{print tolower($1)}'`
if [[ ${_OSFLAG}x == 'centos'x ]] || [[ ${_OSFLAG}x == 'tencent'x ]] ; then
yum -y -q install jq
fi
elif [ -f /etc/lsb-release ];then
_OSFLAG=`cat /etc/lsb-release | grep "DISTRIB_ID" | awk -F"=" '{print tolower($2)}'`
if [ ${_OSFLAG}x == "ubuntu"x ];then
sudo apt-get -y -q install jq
fi
else
echo "This is no CentOS and Ubuntu , 腳本目前不支持此系統(tǒng) !"
exit 1
fi
}
which jq > /dev/null
if [ $? -gt 0 ]; then
installJq
fi
function modify_clb_targets() {
local region=$1
local load_balancer_id=$2
local listener_id=$3
local location_id=$4
local eni_ip=$5
local port=$6
local weight=$7
generate_get_clb_post_data() {
cat <<EOF
{
"region": "${region}",
"load_balancer_id": "${load_balancer_id}",
"listener_id": "${listener_id}",
"location_id": "${location_id}",
"targets": [{
"eni_ip": "${eni_ip}",
"port": ${port},
"weight": ${weight}
}]
}
EOF
}
respond=$(curl -s -X POST
${clb_modify_target_api_url}
-H 'cache-control: no-cache'
-H 'content-type: Application/json'
-d "$(generate_get_clb_post_data)")
echo ${respond}
http_code=$(echo ${respond} | jq '.code')
if [[ ${http_code} != 200 ]]; then
echo "$(echo ${respond} | jq '.message')"
exit 2
fi
echo ${respond} | jq '.message'
}
#如果 Port 是 -1 表示該LocationId下EniIp這地址只有一個(gè)端口
if [ ${Port} == -1 ];then
modify_clb_targets ${Region} ${LoadBalancerId} ${ListenerId} ${LocationId} ${EniIp} -1 ${Weight}
else
modify_clb_targets ${Region} ${LoadBalancerId} ${ListenerId} ${LocationId} ${EniIp} ${Port} ${Weight}
fi
知識(shí)點(diǎn)
- clb_modify_target_api_url 這個(gè)接口是基于騰訊云封裝后的接口(golang實(shí)現(xiàn),后面會(huì)把該代碼分享), 接收json數(shù)據(jù)能修改對(duì)應(yīng)clb下localtionId的主機(jī)權(quán)限。
- 用jq處理返回的json數(shù)據(jù)。