def get_image_infos(): imageclient = AcsClient(accessKeyId, accessSecret, regionId) request = DescribeImagesRequest() request.set_accept_format('json') request.set_PageSize(100) request.set_OSType("linux") request.set_Architecture("x86_64") # request.set_Usage("instance") request.set_ActionType("CreateEcs") response = imageclient.do_action_with_exception(request) res_image = json.loads(str(response, encoding='utf-8')) image_list = [] for image in res_image['Images']['Image']: image_dict = {} image_dict['imageid'] = image['ImageId'] image_dict['imageowner'] = image['ImageOwnerAlias'] image_dict['name'] = image['ImageName'] image_list.append(image_dict) # print(image_list) return image_list
def make(): security_group_dict = acdb.db.load( "security_group_dict") # map[region_id]security_group_id region_instance_id_list = [] # Check image id is avaliable for node_conf in conf.config["ecs"]["node"]: print("Get request", node_conf) client = AcsClient( conf.secret["aliyun"]["access_key"], conf.secret["aliyun"]["access_secret"], node_conf["region_id"], ) req = DescribeImagesRequest() req.set_Status("Available") req.set_ImageOwnerAlias("system") req.set_InstanceType(node_conf["instance_type"]) req.set_OSType("linux") req.set_PageSize(100) req.set_ActionType("CreateEcs") res = json.loads(client.do_action_with_exception(req)) all_avaliable_image = [e["ImageId"] for e in res["Images"]["Image"]] assert node_conf["image"] in all_avaliable_image # Create ECS instance for node_conf in conf.config["ecs"]["node"]: client = AcsClient( conf.secret["aliyun"]["access_key"], conf.secret["aliyun"]["access_secret"], node_conf["region_id"], ) req = CreateInstanceRequest() req.set_ImageId(node_conf["image"]) req.set_SecurityGroupId(security_group_dict[node_conf["region_id"]]) req.set_Password(conf.secret["aliyun"]["ecs_password"]) req.set_InstanceChargeType("PostPaid") req.set_SystemDiskCategory("cloud_efficiency") req.set_SystemDiskSize(c_system_disk_size) req.set_InstanceType(node_conf["instance_type"]) req.set_InternetMaxBandwidthOut( node_conf["internet_max_bandwidth_out"]) req.set_KeyPairName(c_keypair_name) res = json.loads(client.do_action_with_exception(req)) instance_id = res["InstanceId"] print("Create", node_conf["region_id"], instance_id) region_instance_id_list.append([node_conf["region_id"], instance_id]) print("Wait") for _ in range(1 << 32): if sum([ e["Status"] == "Stopped" for e in info(region_instance_id_list) ]) == len(region_instance_id_list): break for (region_id, instance_id) in region_instance_id_list: client = AcsClient( conf.secret["aliyun"]["access_key"], conf.secret["aliyun"]["access_secret"], region_id, ) # Allocate public ip address req = AllocatePublicIpAddressRequest() req.set_accept_format("json") req.set_InstanceId(instance_id) client.do_action_with_exception(req) print("Allocate public ip address", region_id, instance_id) # Start ECS instance req = StartInstanceRequest() req.set_accept_format("json") req.set_InstanceId(instance_id) res = json.loads(client.do_action_with_exception(req)) print("Start", region_id, instance_id) print("Wait") for _ in range(1 << 32): if sum([ e["Status"] == "Running" for e in info(region_instance_id_list) ]) == len(region_instance_id_list): break # Get ECS info acdb.db.save("instance_list", info(region_instance_id_list)) print( f"Instance info saved at {conf.config['db']['path']}/instance_list.json" )