|
@@ -0,0 +1,128 @@
|
|
|
+
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import logging
|
|
|
+import asyncio
|
|
|
+from typing import List
|
|
|
+from concurrent.futures import ThreadPoolExecutor
|
|
|
+from alibabacloud_ecs20140526.client import Client as Ecs20140526Client
|
|
|
+from alibabacloud_alb20200616.client import Client as Alb20200616Client
|
|
|
+from alibabacloud_tea_openapi import models as open_api_models
|
|
|
+from alibabacloud_ecs20140526 import models as ecs_20140526_models
|
|
|
+from alibabacloud_alb20200616 import models as alb_20200616_models
|
|
|
+from alibabacloud_tea_util import models as util_models
|
|
|
+from alibabacloud_tea_util.client import Client as UtilClient
|
|
|
+import config
|
|
|
+import utils
|
|
|
+
|
|
|
+class Sample:
|
|
|
+ def __init__(self):
|
|
|
+ pass
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def create_ecs_client() -> Ecs20140526Client:
|
|
|
+ config = open_api_models.Config(
|
|
|
+ access_key_id=config.ALIBABA_CLOUD_ACCESS_KEY_ID,
|
|
|
+ access_key_secret=config.ALIBABA_CLOUD_ACCESS_KEY_SECRET
|
|
|
+ )
|
|
|
+ config.endpoint = 'ecs-cn-hangzhou.aliyuncs.com'
|
|
|
+ return Ecs20140526Client(config)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def create_alb_client() -> Alb20200616Client:
|
|
|
+ config = open_api_models.Config(
|
|
|
+ access_key_id=config.ALIBABA_CLOUD_ACCESS_KEY_ID,
|
|
|
+ access_key_secret=config.ALIBABA_CLOUD_ACCESS_KEY_SECRET
|
|
|
+ )
|
|
|
+ config.endpoint = 'alb.cn-hangzhou.aliyuncs.com'
|
|
|
+ return Alb20200616Client(config)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def create_ecs_instance(ecs_client: Ecs20140526Client, amount: int) -> List[str]:
|
|
|
+ instance_ids = []
|
|
|
+ for i in range(amount):
|
|
|
+ request = ecs_20140526_models.CreateInstanceRequest(
|
|
|
+ size=config.instance_config['size'],
|
|
|
+ category=config.instance_config['category'],
|
|
|
+ region_id=config.instance_config['region_id'],
|
|
|
+ image_id=config.instance_config['image_id'],
|
|
|
+ instance_type=config.instance_config['instance_type'],
|
|
|
+ security_group_id=config.instance_config['security_group_id'],
|
|
|
+ v_switch_id=config.instance_config['v_switch_id'],
|
|
|
+ instance_name=f"{config.instance_config['instance_name']}-{i+1}",
|
|
|
+ unique_suffix=config.instance_config['unique_suffix'],
|
|
|
+ password_inherit=config.instance_config['password_inherit'],
|
|
|
+ zone_id=config.instance_config['zone_id'],
|
|
|
+ key_pair_name=config.instance_config['key_pair_name']
|
|
|
+ )
|
|
|
+ response = ecs_client.create_instance(request)
|
|
|
+ instance_id = response.body.instance_id
|
|
|
+ instance_ids.append(instance_id)
|
|
|
+ logging.info(f"Created ECS instance with ID: {instance_id}")
|
|
|
+ return instance_ids
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ async def start_services_and_health_check(create_client: Ecs20140526Client, slb_client: Alb20200616Client, ess_instance_ids: List[str]):
|
|
|
+ try:
|
|
|
+ await utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **config.start_sh)
|
|
|
+ logging.info(f"send start shell file finished, instances: {ess_instance_ids}")
|
|
|
+
|
|
|
+ start_sh_param = "latest"
|
|
|
+ server_start_sh = os.path.join(config.start_sh['target_dir'], config.start_sh['name'])
|
|
|
+ server_start_commend = f"sh {server_start_sh} {start_sh_param}"
|
|
|
+ await utils.run_command(client=create_client, instance_ids=ess_instance_ids, command=server_start_commend)
|
|
|
+
|
|
|
+ global health_instances
|
|
|
+ health_instances = []
|
|
|
+ max_wait_time = 180
|
|
|
+ loop = asyncio.get_running_loop()
|
|
|
+ max_workers = 10
|
|
|
+ executor = ThreadPoolExecutor(max_workers=max_workers)
|
|
|
+ tasks = [
|
|
|
+ loop.run_in_executor(executor, utils.health_check, *args) for args in
|
|
|
+ [(slb_client, instance_id, max_wait_time) for instance_id in ess_instance_ids]
|
|
|
+ ]
|
|
|
+ await asyncio.wait(tasks)
|
|
|
+ logging.info(f"health instances count: {len(health_instances)}, {health_instances}")
|
|
|
+
|
|
|
+ if len(health_instances) > 0:
|
|
|
+ time.sleep(20)
|
|
|
+
|
|
|
+ Sample.mount_alb_to_instance(slb_client, ess_instance_ids)
|
|
|
+ except Exception as e:
|
|
|
+ logging.error(f"An error occurred: {e}")
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def mount_alb_to_instance(client: Alb20200616Client, instance_ids: List[str]) -> None:
|
|
|
+ try:
|
|
|
+ servers = [alb_20200616_models.AddServersToServerGroupRequestServers(
|
|
|
+ server_id=instance_id,
|
|
|
+ server_type=config.alb_config['server_type'],
|
|
|
+ weight=config.alb_config['weight']
|
|
|
+ ) for instance_id in instance_ids]
|
|
|
+ add_servers_to_server_group_request = alb_20200616_models.AddServersToServerGroupRequest(
|
|
|
+ server_group_id=config.alb_config['server_group_id'],
|
|
|
+ servers=servers
|
|
|
+ )
|
|
|
+ runtime = util_models.RuntimeOptions()
|
|
|
+ client.add_servers_to_server_group_with_options(add_servers_to_server_group_request, runtime)
|
|
|
+ except Exception as e:
|
|
|
+ logging.error(f"An error occurred: {e}")
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def main(args: List[str]) -> None:
|
|
|
+ try:
|
|
|
+ ecs_client = Sample.create_ecs_client()
|
|
|
+ alb_client = Sample.create_alb_client()
|
|
|
+ amount = int(args[0]) if len(args) > 0 else 1
|
|
|
+ instance_ids = Sample.create_ecs_instance(ecs_client, amount)
|
|
|
+
|
|
|
+ asyncio.run(Sample.start_services_and_health_check(ecs_client, alb_client, instance_ids))
|
|
|
+ except Exception as e:
|
|
|
+ logging.error(f"An error occurred: {e}")
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ Sample.main(sys.argv[1:])
|