import sys import time import clb_utils import commonapi_config import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') def remove_instances(ecs_client, clb_client, instance_ids): """ 停止并释放机器 :param ecs_client: :param clb_client: :param instance_ids: instanceId type-list :return: None """ # 1. 摘流量 clb_utils.set_instance_weight_process_with_clbs(client=clb_client, clb_id_list=commonapi_config.clb_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.移除clb clb_utils.remove_backend_servers_with_clbs(client=clb_client, clb_id_list=commonapi_config.clb_id_list, instances=instance_ids) # 3. 停止机器 clb_utils.stop_instances(client=ecs_client, instance_ids=instance_ids) logging.info(f"instances stop finished, instances: {instance_ids}") # 4. 判断机器运行状态是否为Stopped while True: response = clb_utils.get_instances_status(client=ecs_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 = clb_utils.release_instances(client=ecs_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: clb_client = clb_utils.connect_client( access_key_id=commonapi_config.clb_client_params['access_key_id'], access_key_secret=commonapi_config.clb_client_params['access_key_secret'], region_id=commonapi_config.clb_client_params['region_id'] ) ecs_client = clb_utils.connect_client( access_key_id=commonapi_config.ecs_client_params['access_key_id'], access_key_secret=commonapi_config.ecs_client_params['access_key_secret'], region_id=commonapi_config.ecs_client_params['region_id'] ) # 获取指定释放的机器数量 reduce_count = int(sys.argv[1]) logging.info(f"reduce instances count: {reduce_count}") # 获取clb下所有机器 online_instance_ids = clb_utils.get_instance_ids(client=clb_client, clb_id=commonapi_config.clb_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}") # if online_instance_count - reduce_count < 4: # logging.error("缩容后服务器数量不能小于4台") # sys.exit() # 获取前count台机器进行释放 reduce_instance_ids = online_instance_ids[:reduce_count] logging.info(f"reduce instances: {reduce_instance_ids}") # 停止并释放机器 remove_instances(ecs_client=ecs_client, clb_client=clb_client, instance_ids=reduce_instance_ids) logging.info(f"stop & release instances end!") except Exception as e: logging.error(e) sys.exit() if __name__ == '__main__': main()