xuekailun пре 4 месеци
родитељ
комит
8302504612

+ 22 - 18
alb_utils.py

@@ -384,29 +384,33 @@ def add_servers_to_server_group(alb_client, server_group_id, instance_id, weight
 
 
 
-def remove_servers_from_server_group(alb_client, server_group_id, instance_id, port):
+def remove_servers_from_server_group(alb_client, server_group_id, instance_ids, port):
     """
     从ALB服务器组中移除服务器
     :param alb_client: ALB客户端连接
     :param server_group_id: 服务器组ID
-    :param instance_id: 实例ID
+    :param instance_ids: 实例ID
     :param port: 后端服务器使用的端口
     """
-    server = alb_models.RemoveServersFromServerGroupRequestServers(
-        port=port,
-        server_id=instance_id,
-        server_type='ecs'
-    )
-    request = alb_models.RemoveServersFromServerGroupRequest(
-        server_group_id=server_group_id,
-        servers=[server]
-    )
-    runtime = util_models.RuntimeOptions()
-    try:
-        alb_client.remove_servers_from_server_group_with_options(request, runtime)
-        logging.info(f"Successfully removed server {instance_id} from server group {server_group_id}.")
-    except Exception as e:
-        logging.error(f"Failed to remove server {instance_id} from server group {server_group_id}: {str(e)}")
+    for instance_id in instance_ids:
+        server = alb_models.RemoveServersFromServerGroupRequestServers(
+            port=port,
+            server_id=instance_id,
+            server_type='ecs'
+        )
+        request = alb_models.RemoveServersFromServerGroupRequest(
+            server_group_id=server_group_id,
+            servers=[server]
+        )
+        runtime = util_models.RuntimeOptions()
+        try:
+            alb_client.remove_servers_from_server_group_with_options(request, runtime)
+            logging.info(f"Successfully removed server {instance_id} from server group {server_group_id}.")
+        except Exception as e:
+            logging.error(f"Failed to remove server {instance_id} from server group {server_group_id}: {str(e)}")
+
+
+
 
 def list_server_group_servers(alb_client, server_group_id):
     """
@@ -454,7 +458,7 @@ async def list_server_group_servers_async(alb_client, server_group_id):
 
 def update_server_group_server_weight(alb_client, server_group_id, instance_id, weight, port):
     """
-    更新特定服务器在服务器组中的权重
+    更定服务器在服务器组中的权重
     :param alb_client: ALB客户端
     :param server_group_id: 服务器组ID
     :param instance_id: 实例ID

+ 4 - 8
gateway/gateway_update.py

@@ -43,7 +43,6 @@ def checkHealth(ipadd):
 
 
 def update(instance_id, port):
-
     time.sleep(10)  # 等待10秒钟
     global success_count
 
@@ -69,11 +68,8 @@ def update(instance_id, port):
         print("开始健康检查")
         checkHealth(ipadd)
         print("%s :权重修改中......" % (ipadd))
-
         weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
-        for server_group_id in gateway_config.server_group_id_list:
-            alb_utils.update_server_group_servers_attribute(alb_client, [server_group_id],   [instance_id], weight_list, port)
-
+        alb_utils.update_server_group_servers_attribute(alb_client,  gateway_config.server_group_id_list, instance_id_list=instance_id, weight_list=weight_list, port=port)
         success_count += 1
         print("更新进度" + "%s/%s" % (success_count, total))
     except Exception as e:
@@ -137,6 +133,6 @@ if __name__ == '__main__':
     # 更新每个实例
     for instanceID in InstanceIDs:
         for server_group_id in gateway_config.server_group_id_list:
-            alb_utils.update_server_group_servers_attribute(alb_client, [server_group_id], [instanceID],
-                                                       [(0, 20)], port=gateway_config.port)  # 设置初始权重为0
-        update(instanceID)
+            alb_utils.update_server_group_server_weight(alb_client, server_group_id, instanceID,
+                                                       weight=0, port=gateway_config.port)  # 设置初始权重为0
+        update(instanceID, port=gateway_config.port)

+ 7 - 7
longvideoapi/longvideoapi_grayscale_update.py

@@ -104,13 +104,13 @@ def update_instance(ecs_client, alb_client, instance_id, version, port):
     """
     logging.info(f"update instance: {instance_id}")
     # 1. 摘流量,将权重降为0
-    health_instance_ids = [instance_id]
+    # health_instance_ids = [instance_id]
     weight_list = [(0, 20)]  # 设置权重为0,等待20秒
     alb_utils.update_server_group_servers_attribute(alb_client,
                                                longvideoapi_config.server_group_id_list,
-                                               health_instance_ids,
-                                               weight_list,
-                                                port)
+                                               instance_id_list=instance_id,
+                                               weight_list=weight_list,
+                                                port=port)
     logging.info(f"Set weight to 0 for instance: {instance_id}")
 
     # 2. 移除旧容器并删除旧镜像
@@ -149,9 +149,9 @@ def update_instance(ecs_client, alb_client, instance_id, version, port):
     add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
     alb_utils.update_server_group_servers_attribute(alb_client,
                                                longvideoapi_config.server_group_id_list,
-                                               health_instance_ids,
-                                               add_weight_list,
-                                                port)
+                                               instance_id_list=health_instances,
+                                               weight_list=add_weight_list,
+                                                port=port)
     logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
 
 

+ 4 - 4
longvideoapi/longvideoapi_reduce_with_count.py

@@ -23,9 +23,9 @@ def remove_instances(ecs_client, alb_client, instance_ids, port):
     try:
         alb_utils.update_server_group_servers_attribute(alb_client,
                                                    longvideoapi_config.server_group_id_list,
-                                                   instance_ids,
-                                                   weight_list,
-                                                    port)
+                                                   instance_id_list=instance_ids,
+                                                   weight_list=weight_list,
+                                                   port=port)
     except Exception as e:
         logging.error(f"Failed to set instance weight: {e}")
         sys.exit()
@@ -37,7 +37,7 @@ def remove_instances(ecs_client, alb_client, instance_ids, port):
     for server_group_id in longvideoapi_config.server_group_id_list:
         try:
             alb_utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
-                                                   instance_id=instance_ids, port=port)
+                                                   instance_ids=instance_ids, port=port)
             logging.info(f"Successfully removed instances from server group {server_group_id}.")
         except Exception as e:
             logging.error(f"Failed to remove instances from server group {server_group_id}: {e}")

+ 4 - 4
longvideoapi/longvideoapi_restart.py

@@ -38,10 +38,10 @@ def server_restart(ecs_client, alb_client, instance_id, image_name, port):
         # 设置权重
         add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
         alb_utils.update_server_group_servers_attribute(alb_client,
-                                                   longvideoapi_config.server_group_id_list,
-                                                   instance_id,
-                                                   add_weight_list,
-                                                   port)
+                                                   server_group_id_list=longvideoapi_config.server_group_id_list,
+                                                   instance_id_list=instance_id,
+                                                   weight_list=add_weight_list,
+                                                   port=port)
         logging.info(f"Server restart finished, instance: {instance_id}/{ip_address}")
     except Exception as e:
         logging.error(f"Server restart failed, instance: {instance_id}")

+ 25 - 16
longvideoapi/longvideoapi_update_k.py

@@ -10,6 +10,7 @@ import longvideoapi_config
 
 from concurrent.futures import ThreadPoolExecutor
 
+from gateway.gateway_config import server_group_id_list
 
 health_instances = []
 ess_instances = []
@@ -90,12 +91,20 @@ async def ess_instance(ecs_client, alb_client, ess_count, max_workers, version,
 
         add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
         alb_utils.update_server_group_servers_attribute(alb_client,
-                                                   longvideoapi_config.server_group_id_list,
-                                                   health_instance_ids,
-                                                   add_weight_list,
-                                                   longvideoapi_config.port)
-        logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
-
+                                                   server_group_id_list=longvideoapi_config.server_group_id_list,
+                                                   instance_id_list=health_instance_ids,
+                                                   weight_list=add_weight_list,
+                                                   port=port)
+        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(ecs_client, instance_id, container_name_list):
@@ -217,10 +226,10 @@ async def update_instance(ecs_client, alb_client, instance_ids, max_workers, ver
 
             add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
             alb_utils.update_server_group_servers_attribute(alb_client,
-                                                       longvideoapi_config.server_group_id_list,
-                                                       health_instance_ids,
-                                                       add_weight_list,
-                                                       port)
+                                                       server_group_id_list=longvideoapi_config.server_group_id_list,
+                                                       instance_id_list=health_instance_ids,
+                                                       weight_list=add_weight_list,
+                                                       port=port)
             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)}")
@@ -242,10 +251,10 @@ def remove_instances(ecs_client, alb_client, instance_ids, port):
     weight_list = [(0, 20)]  # 设置权重为0,等待20秒
     try:
         alb_utils.update_server_group_servers_attribute(alb_client,
-                                                   longvideoapi_config.server_group_id_list,
-                                                   instance_ids,
-                                                   weight_list,
-                                                   port)
+                                                   server_group_id_list=longvideoapi_config.server_group_id_list,
+                                                   instance_id_list=instance_ids,
+                                                   weight_list=weight_list,
+                                                   port=port)
     except Exception as e:
         logging.error(f"Failed to set instance weight: {e}")
         sys.exit()
@@ -255,7 +264,7 @@ def remove_instances(ecs_client, alb_client, instance_ids, port):
     for server_group_id in longvideoapi_config.server_group_id_list:
         try:
             alb_utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
-                                                   instance_id=instance_ids, port=port)
+                                                   instance_ids=instance_ids, port=port)
             logging.info(f"Successfully removed instances from server group {server_group_id}.")
         except Exception as e:
             logging.error(f"Failed to remove instances from server group {server_group_id}: {e}")
@@ -329,7 +338,7 @@ def main():
 
         # 4. 停止并释放扩容机器
         logging.info(f"stop & release instances start ...")
-        remove_instances(ecs_client=ecs_client, alb_client=alb_client, instance_ids=ess_instances)
+        remove_instances(ecs_client=ecs_client, alb_client=alb_client, instance_ids=ess_instances, port=longvideoapi_config.port)
         logging.info(f"stop & release instances end!")
     except Exception as e:
         logging.error(e)