def get_image_id(self, img_name): request = DescribeImagesRequest() request.set_accept_format('json') request.set_ImageOwnerAlias("self") request.set_PageSize(20) response = self.client.do_action_with_exception(request) response = json.loads(response) totalcount = response["TotalCount"] iteration = totalcount // 20 if iteration * 20 < totalcount: iteration += 1 for ii in range(1, iteration + 1): count = 0 flag = 0 request.set_PageNumber(ii) while count < 10: try: response = self.client.do_action_with_exception(request) response = json.loads(response) for img in response["Images"]["Image"]: if img["ImageName"] == img_name: return img["ImageId"] flag = 1 break except: count += 1 time.sleep(10) if not flag: dlog.info("get image failed, exit") sys.exit()
def describe_apg_instances(self): request = DescribeAutoProvisioningGroupInstancesRequest() request.set_accept_format('json') request.set_AutoProvisioningGroupId(self.cloud_resources["apg_id"]) request.set_PageSize(100) iteration = self.nchunks // 100 instance_list = [] for i in range(iteration + 1): request.set_PageNumber(i + 1) count = 0 flag = 0 err_msg = 0 while count < 10: try: response = self.client.do_action_with_exception(request) response = json.loads(response) for ins in response["Instances"]["Instance"]: instance_list.append(ins["InstanceId"]) flag = 1 break except ServerException as e: # dlog.info(e) err_msg = e count += 1 except ClientException as e: # dlog.info(e) err_msg = e count += 1 if not flag: dlog.info("describe_apg_instances failed, err msg: %s" % err_msg) sys.exit() return instance_list