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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律