|
@@ -1,325 +0,0 @@
|
|
|
-import asyncio
|
|
|
-import sys
|
|
|
-import time
|
|
|
-import requests
|
|
|
-import utils
|
|
|
-import logging
|
|
|
-import os
|
|
|
-import docker
|
|
|
-import longvideo_config
|
|
|
-
|
|
|
-from concurrent.futures import ThreadPoolExecutor
|
|
|
-
|
|
|
-
|
|
|
-health_instances = []
|
|
|
-ess_instances = []
|
|
|
-remove_container_instances = []
|
|
|
-
|
|
|
-
|
|
|
-def server_health_check(client, instance_id):
|
|
|
- """
|
|
|
- 服务健康检查
|
|
|
- :param client: 客户端连接
|
|
|
- :param instance_id: instanceId
|
|
|
- :return:
|
|
|
- """
|
|
|
- global health_instances
|
|
|
- ip_address = utils.get_ip_address(client=client, instance_id=instance_id)
|
|
|
- while True:
|
|
|
- health_check_url = f"http://{ip_address}:8080/longvideoapi/test"
|
|
|
- try:
|
|
|
- http_code = requests.get(health_check_url).status_code
|
|
|
- except:
|
|
|
- logging.info(f"images is downloading ip:{ip_address}")
|
|
|
- http_code = 0
|
|
|
-
|
|
|
- if http_code == 200:
|
|
|
- health_instances.append((instance_id, ip_address))
|
|
|
- logging.info(f"health check success, instance: {instance_id}/{ip_address}")
|
|
|
- break
|
|
|
- else:
|
|
|
- time.sleep(10)
|
|
|
-
|
|
|
-
|
|
|
-async def ess_instance(create_client, slb_client, ess_count, max_workers, version):
|
|
|
- """
|
|
|
- 扩容机器并运行新服务
|
|
|
- :param create_client: 购买机器客户端连接
|
|
|
- :param slb_client: 修改负载均衡权限
|
|
|
- :param ess_count: 扩容数量
|
|
|
- :param max_workers: 线程数
|
|
|
- :param version: 版本标记
|
|
|
- :return:
|
|
|
- """
|
|
|
- # 1. 购买机器并启动
|
|
|
- ess_instance_ids = utils.create_multiple_instances(
|
|
|
- amount=ess_count,
|
|
|
- client=create_client,
|
|
|
- **longvideo_config.instance_config_f,
|
|
|
- )
|
|
|
- time.sleep(60)
|
|
|
-
|
|
|
- # 2. 发送启动脚本到机器上
|
|
|
- utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **longvideo_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_commend = f"sh {server_start_sh} {version}"
|
|
|
- utils.run_command(client=create_client, instance_ids=ess_instance_ids, command=server_start_commend)
|
|
|
- # 4. 异步探活
|
|
|
- global health_instances
|
|
|
- health_instances = []
|
|
|
- loop = asyncio.get_running_loop()
|
|
|
- executor = ThreadPoolExecutor(max_workers=max_workers)
|
|
|
- tasks = [
|
|
|
- loop.run_in_executor(executor, server_health_check, *args) for args in
|
|
|
- [(slb_client, instance_id) for instance_id in ess_instance_ids]
|
|
|
- ]
|
|
|
- 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(10)
|
|
|
- utils.add_backend_servers_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instances=health_instances)
|
|
|
- health_instance_ids = [instance_id for instance_id, _ in health_instances]
|
|
|
- add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
|
|
|
- utils.set_instance_weight_process_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instance_id_list=health_instance_ids,
|
|
|
- weight_list=add_weight_list)
|
|
|
- global ess_instances
|
|
|
- ess_instances.extend(health_instance_ids)
|
|
|
- logging.info(f"ess count: {ess_count}, "
|
|
|
- f"create count: {len(ess_instance_ids)}, "
|
|
|
- f"finished count: {len(health_instance_ids)}")
|
|
|
- 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_list):
|
|
|
- """
|
|
|
- 移除旧容器并删除旧镜像
|
|
|
- :param 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)
|
|
|
- logging.info(f"服务器信息:{instance_id}/{ip_address}")
|
|
|
- client = docker.DockerClient(base_url=f'tcp://{ip_address}:2375', timeout=60)
|
|
|
- # 移除旧的容器
|
|
|
- container_remove_retry = 3
|
|
|
- i = 0
|
|
|
- while True:
|
|
|
- if i >= container_remove_retry:
|
|
|
- logging.error(f"容器不存在或者无法删除当前容器, instance = {instance_id}/{ip_address}")
|
|
|
- sys.exit()
|
|
|
- try:
|
|
|
- flag = False
|
|
|
- for container_name in container_name_list:
|
|
|
- try:
|
|
|
- container_id = client.containers.get(container_name)
|
|
|
- container_id.remove(force=True)
|
|
|
- flag = True
|
|
|
- break
|
|
|
- except:
|
|
|
- continue
|
|
|
- if flag:
|
|
|
- break
|
|
|
- except Exception as e:
|
|
|
- i += 1
|
|
|
-
|
|
|
- # 删除旧镜像
|
|
|
- images_remove_retry = 3
|
|
|
- j = 0
|
|
|
- while True:
|
|
|
- if j >= images_remove_retry:
|
|
|
- logging.error(f"镜像不存在,无法获取到镜像ID, instance = {instance_id}/{ip_address}")
|
|
|
- sys.exit()
|
|
|
- try:
|
|
|
- images = client.images.list()
|
|
|
- for image in images:
|
|
|
- client.images.remove(force=True, image=image.tags[0])
|
|
|
- time.sleep(2)
|
|
|
- global remove_container_instances
|
|
|
- remove_container_instances.append(instance_id)
|
|
|
- break
|
|
|
- except Exception as e:
|
|
|
- i += 1
|
|
|
-
|
|
|
-
|
|
|
-async def update_instance(create_client, slb_client, instance_ids, max_workers, version):
|
|
|
- """
|
|
|
- 线上机器更新
|
|
|
- :param create_client:
|
|
|
- :param slb_client: slb客户端连接
|
|
|
- :param instance_ids: instanceId type-list
|
|
|
- :param max_workers:
|
|
|
- :param version: 版本标记
|
|
|
- :return:
|
|
|
- """
|
|
|
- media_index = len(instance_ids)//2
|
|
|
- instance_ids_group = [instance_ids[:media_index], instance_ids[media_index:]]
|
|
|
- update_finished_count = 0
|
|
|
- for instance_id_list in instance_ids_group:
|
|
|
- logging.info(f"update instances: {instance_id_list}")
|
|
|
- # 1. 摘流量
|
|
|
- utils.set_instance_weight_process_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instance_id_list=instance_id_list,
|
|
|
- weight_list=[(0, 15)])
|
|
|
- logging.info(f"set weight with 0 finished, instances: {instance_id_list}")
|
|
|
- # 2. 异步移除旧容器并删除旧镜像
|
|
|
- global remove_container_instances
|
|
|
- remove_container_instances = []
|
|
|
- # container_name = 'longvideoapi'
|
|
|
- container_name_list = ['vlogapi', 'longvideoapi']
|
|
|
- loop = asyncio.get_running_loop()
|
|
|
- executor = ThreadPoolExecutor(max_workers=max_workers)
|
|
|
- tasks = [
|
|
|
- loop.run_in_executor(executor, remove_container_image, *args) for args in
|
|
|
- [(slb_client, instance_id, container_name_list) for instance_id in instance_id_list]
|
|
|
- ]
|
|
|
- await asyncio.wait(tasks)
|
|
|
- logging.info(f"remove container & images finished, instances: {remove_container_instances},"
|
|
|
- f" count: {len(remove_container_instances)}")
|
|
|
- if len(remove_container_instances) < len(instance_id_list):
|
|
|
- logging.error(f"remove container image failed| "
|
|
|
- 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)
|
|
|
- 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_commend = f"sh {server_start_sh} {version}"
|
|
|
- utils.run_command(client=create_client, instance_ids=instance_id_list, command=server_start_commend)
|
|
|
- # 5. 异步探活
|
|
|
- global health_instances
|
|
|
- health_instances = []
|
|
|
- loop = asyncio.get_running_loop()
|
|
|
- executor = ThreadPoolExecutor(max_workers=max_workers)
|
|
|
- tasks = [
|
|
|
- loop.run_in_executor(executor, server_health_check, *args) for args in
|
|
|
- [(slb_client, instance_id) for instance_id in instance_id_list]
|
|
|
- ]
|
|
|
- await asyncio.wait(tasks)
|
|
|
- logging.info(f"health instances: {health_instances}, count: {len(health_instances)}")
|
|
|
- # 6. 挂载流量
|
|
|
- if len(health_instances) == len(instance_id_list):
|
|
|
- # 所有机器探活成功
|
|
|
- time.sleep(10)
|
|
|
- utils.add_backend_servers_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instances=health_instances)
|
|
|
- health_instance_ids = [instance_id for instance_id, _ in health_instances]
|
|
|
- add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
|
|
|
- utils.set_instance_weight_process_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instance_id_list=health_instance_ids,
|
|
|
- weight_list=add_weight_list)
|
|
|
- logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
|
|
|
- update_finished_count += len(health_instances)
|
|
|
- logging.info(f"update finished: {update_finished_count}/{len(instance_ids)}")
|
|
|
- else:
|
|
|
- logging.info(f"health instances: {health_instances}, count: {len(health_instances)}")
|
|
|
- sys.exit()
|
|
|
-
|
|
|
-
|
|
|
-def remove_instances(create_client, slb_client, instance_ids):
|
|
|
- """
|
|
|
- 停止并释放机器
|
|
|
- :param create_client:
|
|
|
- :param slb_client:
|
|
|
- :param instance_ids: instanceId type-list
|
|
|
- :return: None
|
|
|
- """
|
|
|
- # 1. 摘流量
|
|
|
- utils.set_instance_weight_process_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instance_id_list=instance_ids,
|
|
|
- weight_list=[(0, 20)])
|
|
|
- logging.info(f"set weight = 0 finished, instances: {instance_ids}")
|
|
|
- time.sleep(10)
|
|
|
- # 2.移除slb
|
|
|
- utils.remove_backend_servers_with_slbs(client=slb_client,
|
|
|
- slb_id_list=longvideo_config.slb_id_list,
|
|
|
- instances=instance_ids)
|
|
|
- # 3. 停止机器
|
|
|
- utils.stop_instances(client=create_client, instance_ids=instance_ids)
|
|
|
- logging.info(f"instances stop finished, instances: {instance_ids}")
|
|
|
- # 4. 判断机器运行状态是否为Stopped
|
|
|
- while True:
|
|
|
- response = utils.get_instances_status(client=create_client, instance_ids=instance_ids)
|
|
|
- if response.get('Code') is None:
|
|
|
- instances_list = response.get('InstanceStatuses').get('InstanceStatus')
|
|
|
- # logging.info(instances_list)
|
|
|
- stopped_instances = [instance.get('InstanceId') for instance in instances_list
|
|
|
- if instance.get('Status') == 'Stopped']
|
|
|
- if len(stopped_instances) == len(instance_ids):
|
|
|
- logging.info(f"instances stopped status set success, instances: {stopped_instances}")
|
|
|
- break
|
|
|
- else:
|
|
|
- logging.info(f"stopped instances count = {len(stopped_instances)}, instances: {stopped_instances}")
|
|
|
- time.sleep(5)
|
|
|
- else:
|
|
|
- logging.error(response)
|
|
|
- sys.exit()
|
|
|
- # 5. 释放机器
|
|
|
- response = utils.release_instances(client=create_client, instance_ids=stopped_instances)
|
|
|
- if response.get('Code') is None:
|
|
|
- logging.info(f"release instances finished, instances: {stopped_instances}")
|
|
|
- else:
|
|
|
- logging.error(f"release instances fail!!!")
|
|
|
- sys.exit()
|
|
|
-
|
|
|
-
|
|
|
-def main():
|
|
|
- try:
|
|
|
- version = sys.argv[1]
|
|
|
- slb_client = utils.connect_client(access_key_id=longvideo_config.slb_client_params['access_key_id'],
|
|
|
- access_key_secret=longvideo_config.slb_client_params['access_key_secret'],
|
|
|
- region_id=longvideo_config.slb_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'])
|
|
|
-
|
|
|
- # 1. 获取slb下所有机器
|
|
|
- online_instance_ids = utils.get_instance_ids(client=slb_client, slb_id=longvideo_config.slb_id_list[0])
|
|
|
- # online_instance_ids = online_instance_ids[:2]
|
|
|
- # online_instance_ids = online_instance_ids[2:]
|
|
|
- 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}")
|
|
|
-
|
|
|
- # 2. 扩容机器并启动新服务 扩容数量:线上机器数量//2
|
|
|
- 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, slb_client=slb_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, slb_client=slb_client,
|
|
|
- instance_ids=online_instance_ids, max_workers=8, version=version))
|
|
|
- logging.info(f"update online instances end!")
|
|
|
-
|
|
|
- # 4. 停止并释放扩容机器
|
|
|
- logging.info(f"stop & release instances start ...")
|
|
|
- remove_instances(create_client=create_client, slb_client=slb_client, instance_ids=ess_instances)
|
|
|
- logging.info(f"stop & release instances end!")
|
|
|
- except Exception as e:
|
|
|
- logging.error(e)
|
|
|
- sys.exit()
|
|
|
-
|
|
|
-
|
|
|
-if __name__ == '__main__':
|
|
|
- main()
|