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
wait
# 改为并发执行

 

Jenkins project

 

 

 

脚本改进版:

Jenkins.sh

 

#!/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 Execute $b synchronization
        echo ########################################################################################################################
        if [[ $2 == true ]];then
                if [[ $b =~ severe ]];then
                        ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$severe/rod /cygdrive/d/website"
                        ssh administrator@$b "rsync -avzP --delete --include='AppConnStr/' --include='AppConnStr/*' --include='RedisClientConfig/' --include='RedisClientConfig/*' --exclude='/*' --exclude='*/' rsync://$severe/wax /cygdrive/d"
                else
                        ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$cygnus/rod /cygdrive/d/website"
                        ssh administrator@$b "rsync -avzP --delete --include='AppConnStr/' --include='AppConnStr/*' --include='RedisClientConfig/' --include='RedisClientConfig/*' --exclude='/*' --exclude='*/' rsync://$cygnus/wax /cygdrive/d"
                fi
        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
done

 

 

cronie.sh

#!/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"
                ssh administrator@$b "rsync -avzP --delete --include='AppConnStr/' --include='AppConnStr/*' --include='RedisClientConfig/' --include='RedisClientConfig/*' --exclude='/*' --exclude='*/' rsync://$severe/wax /cygdrive/d"
        else
                ssh administrator@$b "rsync -avzP --delete --exclude="*.rar" --exclude="*.zip" rsync://$cygnus/rod /cygdrive/d/website"
                ssh administrator@$b "rsync -avzP --delete --include='AppConnStr/' --include='AppConnStr/*' --include='RedisClientConfig/' --include='RedisClientConfig/*' --exclude='/*' --exclude='*/' rsync://$cygnus/wax /cygdrive/d"
        fi

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

 

rsyncd.conf

use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
uid = 0
gid = 0
max connections = 0
ignore nonreadable = yes
exclude = lost+found
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
read only = false
write only = false
list = true
transfer logging = yes
ignore errors

[rod]
path = /cygdrive/d/website
comment = website synchronization

[wax]
path = /cygdrive/d/
comment = config synchronization

 

posted @ 2020-10-23 19:27  ascertain  阅读(164)  评论(0编辑  收藏  举报