|
@@ -6,7 +6,7 @@ import utils
|
|
|
import logging
|
|
|
import os
|
|
|
import docker
|
|
|
-import longvideo_config
|
|
|
+import longvideoapi_config
|
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
@@ -24,7 +24,7 @@ def server_health_check(client, instance_id):
|
|
|
:return:
|
|
|
"""
|
|
|
global health_instances
|
|
|
- ip_address = utils.get_ip_address(client=client, instance_id=instance_id)
|
|
|
+ ip_address = utils.get_ip_address(ecs_client=ecs_client, instance_id=instance_id)
|
|
|
while True:
|
|
|
health_check_url = f"http://{ip_address}:8080/longvideoapi/test"
|
|
|
try:
|
|
@@ -41,11 +41,11 @@ def server_health_check(client, instance_id):
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
|
-async def ess_instance(create_client, alb_client, ess_count, max_workers, version):
|
|
|
+async def ess_instance(ecs_client, alb_client, ess_count, max_workers, version):
|
|
|
"""
|
|
|
扩容机器并运行新服务
|
|
|
- :param create_client: 购买机器客户端连接
|
|
|
- :param slb_client: 修改负载均衡权限
|
|
|
+ :param ecs_client: 购买机器客户端连接
|
|
|
+ :param alb_client: 修改负载均衡权限
|
|
|
:param ess_count: 扩容数量
|
|
|
:param max_workers: 线程数
|
|
|
:param version: 版本标记
|
|
@@ -54,18 +54,18 @@ async def ess_instance(create_client, alb_client, ess_count, max_workers, versio
|
|
|
# 1. 购买机器并启动
|
|
|
ess_instance_ids = utils.create_multiple_instances(
|
|
|
amount=ess_count,
|
|
|
- client=create_client,
|
|
|
- **longvideo_config.instance_config_k,
|
|
|
+ ecs_client=ecs_client,
|
|
|
+ **longvideoapi_config.instance_config_k,
|
|
|
)
|
|
|
time.sleep(60)
|
|
|
|
|
|
# 2. 发送启动脚本到机器上
|
|
|
- utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **longvideo_config.start_sh)
|
|
|
+ utils.send_file_to_ecs(ecs_client=ecs_client, instance_id_list=ess_instance_ids, **longvideoapi_config.start_sh)
|
|
|
logging.info(f"send start shell file finished, instances: {ess_instance_ids}")
|
|
|
# 3. 启动服务
|
|
|
- server_start_sh = os.path.join(longvideo_config.start_sh['target_dir'], longvideo_config.start_sh['name'])
|
|
|
+ server_start_sh = os.path.join(longvideoapi_config.start_sh['target_dir'], longvideoapi_config.start_sh['name'])
|
|
|
server_start_commend = f"sh {server_start_sh} {version}"
|
|
|
- utils.run_command(client=create_client, instance_ids=ess_instance_ids, command=server_start_commend)
|
|
|
+ utils.run_command(ecs_client=ecs_client, instance_ids=ess_instance_ids, command=server_start_commend)
|
|
|
# 4. 异步探活
|
|
|
global health_instances
|
|
|
health_instances = []
|
|
@@ -82,29 +82,29 @@ async def ess_instance(create_client, alb_client, ess_count, max_workers, versio
|
|
|
# 所有机器探活成功
|
|
|
time.sleep(10)
|
|
|
health_instance_ids = [instance_id for instance_id, _ in health_instances]
|
|
|
- for server_group_id in longvideo_config.server_group_id_list:
|
|
|
+ for server_group_id in longvideoapi_config.server_group_id_list:
|
|
|
for instance_id in health_instance_ids:
|
|
|
utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=0)
|
|
|
logging.info(f"Successfully added instance {instance_id} to server group {server_group_id}.")
|
|
|
|
|
|
add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
|
|
|
utils.set_instance_weight_process_with_alb(alb_client,
|
|
|
- longvideo_config.server_group_id_list,
|
|
|
+ longvideoapi_config.server_group_id_list,
|
|
|
health_instance_ids,
|
|
|
add_weight_list)
|
|
|
logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
|
|
|
|
|
|
|
|
|
|
|
|
-def remove_container_image(client, instance_id, container_name_list):
|
|
|
+def remove_container_image(ecs_client, instance_id, container_name_list):
|
|
|
"""
|
|
|
移除旧容器并删除旧镜像
|
|
|
- :param client: 客户端连接
|
|
|
+ :param ecs_client: 客户端连接
|
|
|
:param instance_id: instanceId type-string
|
|
|
:param container_name: 容器名称 type-string
|
|
|
:return:
|
|
|
"""
|
|
|
- ip_address = utils.get_ip_address(client=client, instance_id=instance_id)
|
|
|
+ ip_address = utils.get_ip_address(ecs_client=ecs_client, instance_id=instance_id)
|
|
|
logging.info(f"服务器信息:{instance_id}/{ip_address}")
|
|
|
client = docker.DockerClient(base_url=f'tcp://{ip_address}:2375', timeout=60)
|
|
|
# 移除旧的容器
|
|
@@ -148,10 +148,10 @@ def remove_container_image(client, instance_id, container_name_list):
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
-async def update_instance(create_client, alb_client, instance_ids, max_workers, version):
|
|
|
+async def update_instance(ecs_client, alb_client, instance_ids, max_workers, version):
|
|
|
"""
|
|
|
线上机器更新
|
|
|
- :param create_client:
|
|
|
+ :param ecs_client:
|
|
|
:param alb_client: slb客户端连接
|
|
|
:param instance_ids: instanceId type-list
|
|
|
:param max_workers:
|
|
@@ -165,7 +165,7 @@ async def update_instance(create_client, alb_client, instance_ids, max_workers,
|
|
|
logging.info(f"update instances: {instance_id_list}")
|
|
|
# 1. 摘流量
|
|
|
utils.set_instance_weight_process_with_alb(alb_client=alb_client,
|
|
|
- server_group_id_list=longvideo_config.server_group_id_list,
|
|
|
+ server_group_id_list=longvideoapi_config.server_group_id_list,
|
|
|
instance_id_list=instance_id_list,
|
|
|
weight_list=[(0, 15)])
|
|
|
logging.info(f"set weight with 0 finished, instances: {instance_id_list}")
|
|
@@ -188,12 +188,12 @@ async def update_instance(create_client, alb_client, instance_ids, max_workers,
|
|
|
f"request count: {len(instance_id_list)}, removed count: {len(remove_container_instances)}")
|
|
|
sys.exit()
|
|
|
# 3. 发送启动脚本到机器上
|
|
|
- utils.send_file_to_ecs(client=create_client, instance_id_list=instance_id_list, **longvideo_config.start_sh)
|
|
|
+ utils.send_file_to_ecs(ecs_client=ecs_client, instance_id_list=instance_id_list, **longvideoapi_config.start_sh)
|
|
|
logging.info(f"send start shell file finished, instances: {instance_id_list}, count: {len(instance_id_list)}")
|
|
|
# 4. 启动服务
|
|
|
- server_start_sh = os.path.join(longvideo_config.start_sh['target_dir'], longvideo_config.start_sh['name'])
|
|
|
+ server_start_sh = os.path.join(longvideoapi_config.start_sh['target_dir'], longvideoapi_config.start_sh['name'])
|
|
|
server_start_commend = f"sh {server_start_sh} {version}"
|
|
|
- utils.run_command(client=create_client, instance_ids=instance_id_list, command=server_start_commend)
|
|
|
+ utils.run_command(ecs_client=ecs_client, instance_ids=instance_id_list, command=server_start_commend)
|
|
|
# 5. 异步探活
|
|
|
global health_instances
|
|
|
health_instances = []
|
|
@@ -210,14 +210,14 @@ async def update_instance(create_client, alb_client, instance_ids, max_workers,
|
|
|
# 所有机器探活成功
|
|
|
time.sleep(10)
|
|
|
health_instance_ids = [instance_id for instance_id, _ in health_instances]
|
|
|
- for server_group_id in longvideo_config.server_group_id_list:
|
|
|
- for instance_id in health_instance_ids:
|
|
|
- utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=100) # 设定权重为100
|
|
|
- logging.info(f"Successfully added instance {instance_id} to server group {server_group_id}.")
|
|
|
+ # for server_group_id in longvideoapi_config.server_group_id_list:
|
|
|
+ # for instance_id in health_instance_ids:
|
|
|
+ # utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=0)
|
|
|
+ # logging.info(f"Successfully added instance {instance_id} to server group {server_group_id}.")
|
|
|
|
|
|
add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
|
|
|
utils.set_instance_weight_process_with_alb(alb_client,
|
|
|
- longvideo_config.server_group_id_list,
|
|
|
+ longvideoapi_config.server_group_id_list,
|
|
|
health_instance_ids,
|
|
|
add_weight_list)
|
|
|
logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
|
|
@@ -228,7 +228,7 @@ async def update_instance(create_client, alb_client, instance_ids, max_workers,
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
-def remove_instances(create_client, alb_client, instance_ids):
|
|
|
+def remove_instances(ecs_client, alb_client, instance_ids):
|
|
|
"""
|
|
|
停止并释放机器
|
|
|
:param create_client:
|
|
@@ -238,9 +238,10 @@ def remove_instances(create_client, alb_client, instance_ids):
|
|
|
"""
|
|
|
# 1. 摘流量
|
|
|
weight_list = [(0, 10)] # 设置权重为0,等待10秒
|
|
|
+ # todo
|
|
|
try:
|
|
|
utils.set_instance_weight_process_with_alb(alb_client,
|
|
|
- longvideo_config.server_group_id_list,
|
|
|
+ longvideoapi_config.server_group_id_list,
|
|
|
instance_ids,
|
|
|
weight_list)
|
|
|
except Exception as e:
|
|
@@ -249,7 +250,7 @@ def remove_instances(create_client, alb_client, instance_ids):
|
|
|
|
|
|
time.sleep(10)
|
|
|
# 2.移除alb
|
|
|
- for server_group_id in longvideo_config.server_group_id_list:
|
|
|
+ for server_group_id in longvideoapi_config.server_group_id_list:
|
|
|
try:
|
|
|
utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
|
|
|
instance_id=instance_ids)
|
|
@@ -260,7 +261,7 @@ def remove_instances(create_client, alb_client, instance_ids):
|
|
|
logging.info(f"Remove from ALB finished, instances: {instance_ids}")
|
|
|
|
|
|
# 3. 停止机器
|
|
|
- stop_response = utils.stop_instances(client=create_client, instance_ids=instance_ids)
|
|
|
+ stop_response = utils.stop_instances(ecs_client=ecs_client, instance_ids=instance_ids)
|
|
|
if stop_response.get('Code') is None:
|
|
|
logging.info(f"Instances stop finished, instances: {instance_ids}")
|
|
|
else:
|
|
@@ -269,7 +270,7 @@ def remove_instances(create_client, alb_client, instance_ids):
|
|
|
|
|
|
# 4. 判断机器运行状态是否为Stopped
|
|
|
while True:
|
|
|
- response = utils.get_instances_status(client=create_client, instance_ids=instance_ids)
|
|
|
+ response = utils.get_instances_status(ecs_client=ecs_client, instance_ids=instance_ids)
|
|
|
if response.get('Code') is None:
|
|
|
instances_list = response.get('InstanceStatuses').get('InstanceStatus')
|
|
|
stopped_instances = [instance.get('InstanceId') for instance in instances_list if
|
|
@@ -284,7 +285,7 @@ def remove_instances(create_client, alb_client, instance_ids):
|
|
|
logging.error(response)
|
|
|
sys.exit()
|
|
|
# 5. 释放机器
|
|
|
- release_response = utils.release_instances(client=create_client, instance_ids=stopped_instances)
|
|
|
+ release_response = utils.release_instances(ecs_client=ecs_client, instance_ids=stopped_instances)
|
|
|
if release_response.get('Code') is None:
|
|
|
logging.info(f"Release instances finished, instances: {stopped_instances}")
|
|
|
else:
|
|
@@ -296,15 +297,15 @@ def remove_instances(create_client, alb_client, instance_ids):
|
|
|
def main():
|
|
|
try:
|
|
|
version = sys.argv[1]
|
|
|
- alb_client = utils.connect_client(access_key_id=longvideo_config.alb_client_params['access_key_id'],
|
|
|
- access_key_secret=longvideo_config.alb_client_params['access_key_secret'],
|
|
|
- region_id=longvideo_config.alb_client_params['region_id'])
|
|
|
- create_client = utils.connect_client(access_key_id=longvideo_config.create_client_params['access_key_id'],
|
|
|
- access_key_secret=longvideo_config.create_client_params['access_key_secret'],
|
|
|
- region_id=longvideo_config.create_client_params['region_id'])
|
|
|
+ alb_client = utils.connect_client(access_key_id=longvideoapi_config.alb_client_params['access_key_id'],
|
|
|
+ access_key_secret=longvideoapi_config.alb_client_params['access_key_secret'],
|
|
|
+ region_id=longvideoapi_config.alb_client_params['region_id'])
|
|
|
+ create_client = utils.connect_client(access_key_id=longvideoapi_config.ecs_client_params['access_key_id'],
|
|
|
+ access_key_secret=longvideoapi_config.ecs_client_params['access_key_secret'],
|
|
|
+ region_id=longvideoapi_config.ecs_client_params['region_id'])
|
|
|
|
|
|
# 1. 获取slb下所有机器
|
|
|
- online_instance_ids = utils.get_instance_ids(client=alb_client, server_group_id=longvideo_config.server_group_id_list[0])
|
|
|
+ online_instance_ids = utils.get_instance_ids(alb_client=alb_client, server_group_id=longvideoapi_config.server_group_id_list[0])
|
|
|
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}")
|
|
@@ -313,13 +314,13 @@ def main():
|
|
|
logging.info(f"ess instances start ...")
|
|
|
ess_instance_count = online_instance_count // 2
|
|
|
logging.info(f"ess instance count: {ess_instance_count}")
|
|
|
- asyncio.run(ess_instance(create_client=create_client, alb_client=alb_client,
|
|
|
+ asyncio.run(ess_instance(ecs_client=ecs_client, alb_client=alb_client,
|
|
|
ess_count=ess_instance_count, max_workers=2, version=version))
|
|
|
logging.info(f"ess instances end!")
|
|
|
|
|
|
# 3. 原有机器进行更新
|
|
|
logging.info(f"update online instances start ...")
|
|
|
- asyncio.run(update_instance(create_client=create_client, alb_client=alb_client,
|
|
|
+ asyncio.run(update_instance(ecs_client=ecs_client, alb_client=alb_client,
|
|
|
instance_ids=online_instance_ids, max_workers=8, version=version))
|
|
|
logging.info(f"update online instances end!")
|
|
|
|