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 @   ascertain  阅读(166)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示