|
@@ -6,6 +6,7 @@ import utils
|
|
|
import logging
|
|
|
import os
|
|
|
import docker
|
|
|
+import rov_server_config
|
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
@@ -13,52 +14,52 @@ from concurrent.futures import ThreadPoolExecutor
|
|
|
health_instances = []
|
|
|
ess_instances = []
|
|
|
|
|
|
-slb_id = 'lb-bp1werfophtsjzfr76njm'
|
|
|
-# 修改负载均衡权限
|
|
|
-slb_client_params = {
|
|
|
- 'access_key_id': 'LTAIuPbTPL3LDDKN',
|
|
|
- 'access_key_secret': 'ORcNedKwWuwVtcq4IRFtUDZgS0b1le',
|
|
|
- 'region_id': 'cn-hangzhou'
|
|
|
-}
|
|
|
-# 购买机器权限
|
|
|
-create_client_params = {
|
|
|
- 'access_key_id': 'LTAI4GBWbFvvXoXsSVBe1o9f',
|
|
|
- 'access_key_secret': 'kRAikWitb4kDxaAyBqNrmLmllMEDO3',
|
|
|
- 'region_id': 'cn-hangzhou'
|
|
|
-}
|
|
|
-
|
|
|
-# 机器配置
|
|
|
-instance_config = {
|
|
|
- # 使用的镜像信息
|
|
|
- 'image_id': 'm-bp1e5jx8eqhq22l91xw7',
|
|
|
- # 设置实例规格
|
|
|
- 'instance_type': 'ecs.ic5.xlarge',
|
|
|
- # 选择的交换机
|
|
|
- 'vswitch_id': 'vsw-bp19lpjwtc6j0p0m9mdc2',
|
|
|
- # 当前VPC类型的安全组
|
|
|
- 'security_group_id': 'sg-bp1irhrkr4vfj272hk4y',
|
|
|
- # 硬盘的大小,单位:G
|
|
|
- 'disk_size': '200',
|
|
|
- # 服务器命名
|
|
|
- 'instance_name': 'ESS-rov-server-[1,2]',
|
|
|
- # 服务器所在区域
|
|
|
- 'zone_id': 'cn-hangzhou-h',
|
|
|
- # 磁盘类型:云盘
|
|
|
- 'disk_category': 'cloud_efficiency',
|
|
|
- # 密钥
|
|
|
- 'key_pair_name': 'stuuudy'
|
|
|
-}
|
|
|
-
|
|
|
-# 服务启动脚本
|
|
|
-start_sh_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
-start_sh_filename = 'rov_server_start.sh'
|
|
|
-with open(file=os.path.join(start_sh_dir, start_sh_filename), mode='r', encoding='utf-8') as rf:
|
|
|
- file_content = rf.read()
|
|
|
-start_sh = {
|
|
|
- 'target_dir': '/home/piaoquan_server_sh',
|
|
|
- 'name': start_sh_filename,
|
|
|
- 'content': file_content,
|
|
|
-}
|
|
|
+# slb_id = 'lb-bp1werfophtsjzfr76njm'
|
|
|
+# # 修改负载均衡权限
|
|
|
+# slb_client_params = {
|
|
|
+# 'access_key_id': 'LTAIuPbTPL3LDDKN',
|
|
|
+# 'access_key_secret': 'ORcNedKwWuwVtcq4IRFtUDZgS0b1le',
|
|
|
+# 'region_id': 'cn-hangzhou'
|
|
|
+# }
|
|
|
+# # 购买机器权限
|
|
|
+# create_client_params = {
|
|
|
+# 'access_key_id': 'LTAI4GBWbFvvXoXsSVBe1o9f',
|
|
|
+# 'access_key_secret': 'kRAikWitb4kDxaAyBqNrmLmllMEDO3',
|
|
|
+# 'region_id': 'cn-hangzhou'
|
|
|
+# }
|
|
|
+#
|
|
|
+# # 机器配置
|
|
|
+# instance_config = {
|
|
|
+# # 使用的镜像信息
|
|
|
+# 'image_id': 'm-bp1e5jx8eqhq22l91xw7',
|
|
|
+# # 设置实例规格
|
|
|
+# 'instance_type': 'ecs.ic5.xlarge',
|
|
|
+# # 选择的交换机
|
|
|
+# 'vswitch_id': 'vsw-bp19lpjwtc6j0p0m9mdc2',
|
|
|
+# # 当前VPC类型的安全组
|
|
|
+# 'security_group_id': 'sg-bp1irhrkr4vfj272hk4y',
|
|
|
+# # 硬盘的大小,单位:G
|
|
|
+# 'disk_size': '200',
|
|
|
+# # 服务器命名
|
|
|
+# 'instance_name': 'ESS-rov-server-[1,2]',
|
|
|
+# # 服务器所在区域
|
|
|
+# 'zone_id': 'cn-hangzhou-h',
|
|
|
+# # 磁盘类型:云盘
|
|
|
+# 'disk_category': 'cloud_efficiency',
|
|
|
+# # 密钥
|
|
|
+# 'key_pair_name': 'stuuudy'
|
|
|
+# }
|
|
|
+#
|
|
|
+# # 服务启动脚本
|
|
|
+# start_sh_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
+# start_sh_filename = 'rov_server_start.sh'
|
|
|
+# with open(file=os.path.join(start_sh_dir, start_sh_filename), mode='r', encoding='utf-8') as rf:
|
|
|
+# file_content = rf.read()
|
|
|
+# start_sh = {
|
|
|
+# 'target_dir': '/home/piaoquan_server_sh',
|
|
|
+# 'name': start_sh_filename,
|
|
|
+# 'content': file_content,
|
|
|
+# }
|
|
|
|
|
|
|
|
|
def server_health_check(client, instance_id):
|
|
@@ -82,8 +83,7 @@ def server_health_check(client, instance_id):
|
|
|
health_instances.append((instance_id, ip_address))
|
|
|
break
|
|
|
else:
|
|
|
- # time.sleep(20)
|
|
|
- await asyncio.sleep(10)
|
|
|
+ time.sleep(10)
|
|
|
|
|
|
|
|
|
def set_instance_weight_process(client, instance_id_list, weight_list):
|
|
@@ -99,7 +99,7 @@ def set_instance_weight_process(client, instance_id_list, weight_list):
|
|
|
while flag:
|
|
|
try:
|
|
|
utils.set_weight_for_instances(client=client,
|
|
|
- slb_id=slb_id,
|
|
|
+ slb_id=rov_server_config.slb_id,
|
|
|
instance_id_list=instance_id_list,
|
|
|
weight=weight)
|
|
|
time.sleep(sleep_time)
|
|
@@ -111,10 +111,10 @@ def set_instance_weight_process(client, instance_id_list, weight_list):
|
|
|
|
|
|
async def run_server(create_client, slb_client, instance_ids, max_workers):
|
|
|
# 1. 发送启动脚本到机器上
|
|
|
- utils.send_file_to_ecs(client=create_client, instance_id_list=instance_ids, **start_sh)
|
|
|
+ utils.send_file_to_ecs(client=create_client, instance_id_list=instance_ids, **rov_server_config.start_sh)
|
|
|
logging.info(f"send start shell file finished, instances: {instance_ids}")
|
|
|
# 2. 启动服务
|
|
|
- server_start_sh = os.path.join(start_sh['target_dir'], start_sh['name'])
|
|
|
+ server_start_sh = os.path.join(rov_server_config.start_sh['target_dir'], rov_server_config.start_sh['name'])
|
|
|
server_start_commend = f"sh {server_start_sh}"
|
|
|
utils.run_command(client=create_client, instance_ids=instance_ids, command=server_start_commend)
|
|
|
# 3. 异步探活
|
|
@@ -153,15 +153,15 @@ async def ess_instance(create_client, slb_client, ess_count, max_workers):
|
|
|
ess_instance_ids = utils.create_multiple_instances(
|
|
|
amount=ess_count,
|
|
|
client=create_client,
|
|
|
- **instance_config,
|
|
|
+ **rov_server_config.instance_config,
|
|
|
)
|
|
|
time.sleep(60)
|
|
|
|
|
|
# 2. 发送启动脚本到机器上
|
|
|
- utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **start_sh)
|
|
|
+ utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **rov_server_config.start_sh)
|
|
|
logging.info(f"send start shell file finished, instances: {ess_instance_ids}")
|
|
|
# 3. 启动服务
|
|
|
- server_start_sh = os.path.join(start_sh['target_dir'], start_sh['name'])
|
|
|
+ server_start_sh = os.path.join(rov_server_config.start_sh['target_dir'], rov_server_config.start_sh['name'])
|
|
|
server_start_commend = f"sh {server_start_sh}"
|
|
|
utils.run_command(client=create_client, instance_ids=ess_instance_ids, command=server_start_commend)
|
|
|
# 4. 异步探活
|
|
@@ -175,22 +175,22 @@ async def ess_instance(create_client, slb_client, ess_count, max_workers):
|
|
|
]
|
|
|
await asyncio.wait(tasks)
|
|
|
logging.info(f"health instances count: {len(health_instances)}, {health_instances}")
|
|
|
- # 5. 挂载流量
|
|
|
- if len(health_instances) == len(ess_instance_ids):
|
|
|
- # 所有机器探活成功
|
|
|
- time.sleep(60)
|
|
|
- add_weight_list = [(10, 30), (20, 20), (40, 10), (60, 10), (80, 10), (100, 10)]
|
|
|
- set_instance_weight_process(client=slb_client, instance_id_list=ess_instance_ids, weight_list=add_weight_list)
|
|
|
- global ess_instances
|
|
|
- ess_instances.extend(ess_instance_ids)
|
|
|
- logging.info(f"ess count: {ess_count}, "
|
|
|
- f"create count: {len(ess_instance_ids)}, "
|
|
|
- f"finished count: {len(health_instances)}")
|
|
|
- else:
|
|
|
- logging.info(f"ess count: {ess_count}, "
|
|
|
- f"create count: {len(ess_instance_ids)}, "
|
|
|
- f"health count: {len(health_instances)}")
|
|
|
- sys.exit()
|
|
|
+ # # 5. 挂载流量
|
|
|
+ # if len(health_instances) == len(ess_instance_ids):
|
|
|
+ # # 所有机器探活成功
|
|
|
+ # time.sleep(60)
|
|
|
+ # add_weight_list = [(10, 30), (20, 20), (40, 10), (60, 10), (80, 10), (100, 10)]
|
|
|
+ # set_instance_weight_process(client=slb_client, instance_id_list=ess_instance_ids, weight_list=add_weight_list)
|
|
|
+ # global ess_instances
|
|
|
+ # ess_instances.extend(ess_instance_ids)
|
|
|
+ # logging.info(f"ess count: {ess_count}, "
|
|
|
+ # f"create count: {len(ess_instance_ids)}, "
|
|
|
+ # f"finished count: {len(health_instances)}")
|
|
|
+ # else:
|
|
|
+ # logging.info(f"ess count: {ess_count}, "
|
|
|
+ # f"create count: {len(ess_instance_ids)}, "
|
|
|
+ # f"health count: {len(health_instances)}")
|
|
|
+ # sys.exit()
|
|
|
|
|
|
|
|
|
def remove_container_image(client, instance_id, container_name):
|
|
@@ -255,10 +255,10 @@ async def update_instance(create_client, slb_client, instance_ids, max_workers):
|
|
|
await asyncio.wait(tasks)
|
|
|
logging.info(f"remove container & images finished, instances: {instance_id_list}")
|
|
|
# 3. 发送启动脚本到机器上
|
|
|
- utils.send_file_to_ecs(client=create_client, instance_id_list=instance_id_list, **start_sh)
|
|
|
+ utils.send_file_to_ecs(client=create_client, instance_id_list=instance_id_list, **rov_server_config.start_sh)
|
|
|
logging.info(f"send start shell file finished, instances: {instance_id_list}")
|
|
|
# 4. 启动服务
|
|
|
- server_start_sh = os.path.join(start_sh['target_dir'], start_sh['name'])
|
|
|
+ server_start_sh = os.path.join(rov_server_config.start_sh['target_dir'], rov_server_config.start_sh['name'])
|
|
|
server_start_commend = f"sh {server_start_sh}"
|
|
|
utils.run_command(client=create_client, instance_ids=instance_id_list, command=server_start_commend)
|
|
|
# 5. 异步探活
|
|
@@ -324,15 +324,15 @@ def remove_instances(create_client, slb_client, instance_ids):
|
|
|
|
|
|
|
|
|
def main():
|
|
|
- slb_client = utils.connect_client(access_key_id=slb_client_params['access_key_id'],
|
|
|
- access_key_secret=slb_client_params['access_key_secret'],
|
|
|
- region_id=slb_client_params['region_id'])
|
|
|
- create_client = utils.connect_client(access_key_id=create_client_params['access_key_id'],
|
|
|
- access_key_secret=create_client_params['access_key_secret'],
|
|
|
- region_id=create_client_params['region_id'])
|
|
|
+ slb_client = utils.connect_client(access_key_id=rov_server_config.slb_client_params['access_key_id'],
|
|
|
+ access_key_secret=rov_server_config.slb_client_params['access_key_secret'],
|
|
|
+ region_id=rov_server_config.slb_client_params['region_id'])
|
|
|
+ create_client = utils.connect_client(access_key_id=rov_server_config.create_client_params['access_key_id'],
|
|
|
+ access_key_secret=rov_server_config.create_client_params['access_key_secret'],
|
|
|
+ region_id=rov_server_config.create_client_params['region_id'])
|
|
|
|
|
|
# 1. 获取slb下所有机器
|
|
|
- online_instance_ids = utils.get_instance_ids(client=slb_client, slb_id=slb_id)
|
|
|
+ online_instance_ids = utils.get_instance_ids(client=slb_client, slb_id=rov_server_config.slb_id)
|
|
|
online_instance_count = len(online_instance_ids)
|
|
|
logging.info(f"online instance count: {online_instance_count}.")
|
|
|
logging.info(f"online instance ids: {online_instance_ids}")
|
|
@@ -345,11 +345,11 @@ def main():
|
|
|
ess_count=ess_instance_count, max_workers=2))
|
|
|
logging.info(f"ess instances end!")
|
|
|
|
|
|
- # 3. 原有机器进行更新
|
|
|
- logging.info(f"update online instances start ...")
|
|
|
- asyncio.run(update_instance(create_client=create_client, slb_client=slb_client,
|
|
|
- instance_ids=online_instance_ids, max_workers=2))
|
|
|
- logging.info(f"update online instances end!")
|
|
|
+ # # 3. 原有机器进行更新
|
|
|
+ # logging.info(f"update online instances start ...")
|
|
|
+ # asyncio.run(update_instance(create_client=create_client, slb_client=slb_client,
|
|
|
+ # instance_ids=online_instance_ids, max_workers=2))
|
|
|
+ # logging.info(f"update online instances end!")
|
|
|
|
|
|
# 4. 停止并释放扩容机器
|
|
|
logging.info(f"stop & release instances start ...")
|