1. 程式人生 > 實用技巧 >python & shell實現業務高峰期臨時動態增加伺服器

python & shell實現業務高峰期臨時動態增加伺服器

  

啟停示例:

startInstance.py

#!/bin/env python3

# StartInstance
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest
import sys

id
=sys.argv[1]
 client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')


request = StartInstanceRequest()
request.set_accept_format('json')

request.set_InstanceId(id)

try:
        response = client.do_action_with_exception(request)
        print('StartInstance Exited Normally: 
',str(response, encoding='utf-8')) except Exception as e: print('Exception occured: {}'.format(e)) sys.exit(5) ~

stopInstance.py

#!/bin/env python3

# StopInstance
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.StopInstanceRequest import StopInstanceRequest
import sys

id
=sys.argv[1]
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

request = StopInstanceRequest()
request.set_accept_format('json')

request.set_InstanceId(id)
request.set_StoppedMode("stopcharging")

try:
        response = client.do_action_with_exception(request)
        print('StopInstance Exited Normally: ',str(response, encoding='utf-8'))
except Exception as e:
        print('Exception ocurred: {}'.format(e))
        sys.exit(10)
~

操作SLB:

addBackendServer.py

#!/bin/env python3

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.AddBackendServersRequest import AddBackendServersRequest
import sys

slb=sys.argv[1]
server=sys.argv[2]

client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

request = AddBackendServersRequest()
request.set_accept_format('json')

request.set_LoadBalancerId(slb)
request.set_BackendServers(
    [{"ServerId": server, "Weight": "100", "Type": "ecs", "Port":"80","Description":"add-alternate-server" }]
)

try:
        response = client.do_action_with_exception(request)
        print('AddBackendServer Exited Normally',str(response, encoding='utf-8'))
except Exception as e:
        print('Exception occured: {}'.format(e))
        sys.exit(55)

removeBackendServer.py

#!/bin/env python3
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.RemoveBackendServersRequest import RemoveBackendServersRequest
import sys

slb=sys.argv[1]
server=sys.argv[2]

client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')


request = RemoveBackendServersRequest()
request.set_accept_format('json')

request.set_BackendServers(
    [{"ServerId":server}]
)
request.set_LoadBalancerId(slb)

try:
        response = client.do_action_with_exception(request)
        print('Remove Backend Server Exited Normally:',str(response, encoding='utf-8'))
except Exception as e:
        print('Exception occured: {}'.format(e))
        sys.exit(88)

調整權重

#!/bin/env python3
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.SetBackendServersRequest import SetBackendServersRequest
import sys

slb_id=sys.argv[1]
id=sys.argv[2]
weight=sys.argv[3]

#配置阿里雲子賬號的SLB管理賬號密碼
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')


#給變數賦值
request = SetBackendServersRequest()
request.set_accept_format('json')

#設定需要修改的ecs權重
request.set_BackendServers([
{"ServerId":id,"Weight":weight}
])

#指定負載均衡器
request.set_LoadBalancerId(slb_id)

#發起請求執行slb操作
response = client.do_action_with_exception(request)

#列印結果
print(str(response, encoding='utf-8'))

定時同步shell

#!/bin/env sh

severe=172.16.30.96
cygnus=172.16.20.106

declare -A sew

sew=([severe-1]=i-uf60l46im3efecfjux4o [cygnus-1]=i-uf6f3z1uc58nhmmxzcu8)

for b in "${!sew[@]}";do
        /opt/scripts/startInstance.py ${sew[$b]}

        v=$?
        figlet "startInstance Exit status : $v"
        if [[ $v -eq 0 ]];then
                echo sleep 60 seconds
                sleep 60
        fi

        echo ########################################################################################################################
        echo Execute $b synchronization
        echo ########################################################################################################################
        if [[ $b =~ severe ]];then
                ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$severe/rod /cygdrive/d/website"
        else
                ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$cygnus/rod /cygdrive/d/website"
        fi

        /opt/scripts/stopInstance.py ${sew[$b]}
        figlet "stopInstance Exit status : $?"
done

Jenkins呼叫指令碼

#!/bin/env sh

severe=172.16.30.96
cygnus=172.16.20.106

declare -A sew

sew=([severe-1]=i-uf60l46im3efecfjux4o [cygnus-1]=i-uf6f3z1uc58nhmmxzcu8)

for b in "${!sew[@]}";do
        if [[ $1 =~ api ]];then
                if [[ $b =~ cygnus ]];then
                        continue
                fi
        elif [[ $1 =~ web ]];then
                if [[ $b =~ severe ]];then
                        continue
                fi
        fi


        /opt/scripts/startInstance.py ${sew[$b]}

        v=$?
        figlet "startInstance Exit status : $v"
        if [[ $v -eq 0 ]];then
                echo sleep 60 seconds
                sleep 30
        fi

        echo ########################################################################################################################
        echo Add Backend Server $b
        echo ########################################################################################################################

        if [[ $1 =~ api ]];then
                slb_api=lb-uf6lw0it0b1if0kt6r6zd
                #slb_api=lb-uf6c1xnu22i6jkm6m3rpe # for test
                /opt/scripts/addBackendServer.py $slb_api ${sew[$b]}
        elif [[ $1 =~ web ]];then
                #slb_api=lb-uf6c1xnu22i6jkm6m3rpe # for test
                #/opt/scripts/addBackendServer.py $slb_api ${sew[$b]} # for test
                slb_cjml=lb-uf611bih0o3phcwzivxfe
                /opt/scripts/addBackendServer.py $slb_cjml ${sew[$b]}
                slb_zhipei=lb-uf6sljbd851v9pgrng6nk
                /opt/scripts/addBackendServer.py $slb_zhipei ${sew[$b]}
        fi

        if [[ $2 == false ]];then
                continue
        fi

        echo ########################################################################################################################
        echo Execute $b synchronization
        echo ########################################################################################################################
        if [[ $b =~ severe ]];then
                ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$severe/rod /cygdrive/d/website"
        else
                ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$cygnus/rod /cygdrive/d/website"
        fi

done

Jenkins project

觸發後續Jenkins project

#!/bin/env sh

declare -A sew

sew=([severe-1]=i-uf60l46im3efecfjux4o [cygnus-1]=i-uf6f3z1uc58nhmmxzcu8)

for b in "${!sew[@]}";do


        echo $b
        echo ${sew[$b]}
        echo ########################################################################################################################
        echo Remove Backend Server $b
        echo ########################################################################################################################

        if [[ $b =~ severe ]];then
                slb_api=lb-uf6lw0it0b1if0kt6r6zd
                #slb_api=lb-uf6c1xnu22i6jkm6m3rpe # for test
                /opt/scripts/removeBackendServer.py $slb_api ${sew[$b]}
        elif [[ $b =~ cygnus ]];then
                #slb_api=lb-uf6c1xnu22i6jkm6m3rpe # for test
                #/opt/scripts/removeBackendServer.py $slb_api ${sew[$b]}
                slb_cjml=lb-uf611bih0o3phcwzivxfe
                /opt/scripts/removeBackendServer.py $slb_cjml ${sew[$b]}
                slb_zhipei=lb-uf6sljbd851v9pgrng6nk
                /opt/scripts/removeBackendServer.py $slb_zhipei ${sew[$b]}
        fi

        remove established connections
        sleep $[60*10]


        echo ########################################################################################################################
        echo Stop Instance $b
        echo ########################################################################################################################

        /opt/scripts/stopInstance.py ${sew[$b]}
done

Jenkins project