|
@@ -10,7 +10,6 @@ import longvideo_config
|
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
|
-
|
|
|
health_instances = []
|
|
|
ess_instances = []
|
|
|
remove_container_instances = []
|
|
@@ -93,7 +92,6 @@ def remove_container_image(client, instance_id, container_name_list):
|
|
|
except Exception as e:
|
|
|
j += 1
|
|
|
|
|
|
-
|
|
|
def update_instance(create_client, alb_client, instance_id, version):
|
|
|
"""
|
|
|
线上机器更新
|
|
@@ -104,13 +102,15 @@ def update_instance(create_client, alb_client, instance_id, version):
|
|
|
:return:
|
|
|
"""
|
|
|
logging.info(f"update instance: {instance_id}")
|
|
|
- # 1. 摘流量
|
|
|
- for server_group_id in longvideo_config.server_group_id_list:
|
|
|
- utils.remove_servers_from_server_group(alb_client=alb_client,
|
|
|
- server_group_id=server_group_id,
|
|
|
- instance_id=instance_id)
|
|
|
- logging.info(f"remove from ALB finished, instance: {instance_id}")
|
|
|
- logging.info(f"set weight with 0 finished, instance: {instance_id}")
|
|
|
+ # 1. 摘流量,将权重降为0
|
|
|
+ health_instance_ids = [instance_id] # 这里假设只对当前实例进行操作
|
|
|
+ weight_list = [(0, 10)] # 设置权重为0,等待10秒
|
|
|
+ utils.set_instance_weight_process_with_alb(alb_client,
|
|
|
+ longvideo_config.server_group_id_list,
|
|
|
+ health_instance_ids,
|
|
|
+ weight_list)
|
|
|
+ logging.info(f"Set weight to 0 for instance: {instance_id}")
|
|
|
+
|
|
|
# 2. 移除旧容器并删除旧镜像
|
|
|
global remove_container_instances
|
|
|
remove_container_instances = []
|
|
@@ -121,26 +121,35 @@ def update_instance(create_client, alb_client, instance_id, version):
|
|
|
if len(remove_container_instances) == 0:
|
|
|
logging.error(f"remove container image failed|")
|
|
|
sys.exit()
|
|
|
+
|
|
|
# 3. 发送启动脚本到机器上
|
|
|
utils.send_file_to_ecs(client=create_client, instance_id_list=[instance_id], **longvideo_config.start_sh)
|
|
|
logging.info(f"send start shell file finished, instance: {instance_id}")
|
|
|
+
|
|
|
# 4. 启动服务
|
|
|
server_start_sh = os.path.join(longvideo_config.start_sh['target_dir'], longvideo_config.start_sh['name'])
|
|
|
server_start_command = f"sh {server_start_sh} {version}"
|
|
|
utils.run_command(client=create_client, instance_ids=[instance_id], command=server_start_command)
|
|
|
+
|
|
|
# 5. 探活
|
|
|
global health_instances
|
|
|
health_instances = []
|
|
|
server_health_check(alb_client, instance_id)
|
|
|
logging.info(f"health instances: {health_instances}, count: {len(health_instances)}")
|
|
|
+
|
|
|
# 6. 挂载流量
|
|
|
if len(health_instances) == 0:
|
|
|
logging.info(f"health instances: {health_instances}, count: {len(health_instances)}")
|
|
|
sys.exit()
|
|
|
# 机器探活成功
|
|
|
time.sleep(10)
|
|
|
- utils.add_backend_servers(alb_client, longvideo_config.server_group_id_list[0], health_instances)
|
|
|
+ # 使用 ALB 的函数挂载实例
|
|
|
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}.")
|
|
|
+
|
|
|
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,
|
|
@@ -155,11 +164,13 @@ def main():
|
|
|
alb_client = utils.connect_alb_client(access_key_id=longvideo_config.alb_client_params['access_key_id'],
|
|
|
access_key_secret=longvideo_config.alb_client_params['access_key_secret'])
|
|
|
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'],
|
|
|
+ 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=alb_client, server_group_id=longvideo_config.server_group_id_list[0])
|
|
|
+ # 1. 获取ALB下所有机器
|
|
|
+ online_instance_ids = utils.get_instance_ids(client=alb_client,
|
|
|
+ server_group_id=longvideo_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}")
|
|
@@ -177,3 +188,4 @@ def main():
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
main()
|
|
|
+
|