def check_spot_callback(self, instance_id): request = DescribeInstancesRequest() request.set_accept_format('json') request.set_InstanceIds([instance_id]) status = False count = 0 while count < 10: try: response = self.client.do_action_with_exception(request) response = json.loads(response) if len(response["Instances"]["Instance"] ) == 1 and "Recycling" in response["Instances"][ "Instance"][0]["OperationLocks"]["LockReason"]: status = True if instance_id not in self.describe_apg_instances(): status = True break except ServerException as e: # dlog.info(e) count += 1 time.sleep(10) except ClientException as e: # dlog.info(e) count += 1 time.sleep(10) return status
def delete(self, ii): '''delete one machine''' request = DeleteInstancesRequest() request.set_accept_format('json') request.set_InstanceIds( [self.dispatcher_list[ii]["entity"].instance_id]) request.set_Force(True) count = 0 flag = 0 while count < 10: try: response = self.client.do_action_with_exception(request) flag = 1 break except ServerException as e: time.sleep(10) count += 1 if flag: status_list = [ item["dispatcher_status"] for item in self.dispatcher_list ] running_num = status_list.count("running") running_num += status_list.count("unsubmitted") self.change_apg_capasity(running_num) else: dlog.info("delete failed, exit") sys.exit()
def get_ip(self, instance_list): request = DescribeInstancesRequest() request.set_accept_format('json') ip_list = [] if len(instance_list) == 0: return ip_list try: if len(instance_list) <= 10: for i in range(len(instance_list)): request.set_InstanceIds([instance_list[i]]) response = self.client.do_action_with_exception(request) response = json.loads(response) if "address" in self.cloud_resources and self.cloud_resources[ 'address'] == "public": ip_list.append(response["Instances"]["Instance"][0] ["PublicIpAddress"]["IpAddress"][0]) else: ip_list.append(response["Instances"]["Instance"][0] ["VpcAttributes"]["PrivateIpAddress"] ['IpAddress'][0]) # ip_list.append(response["Instances"]["Instance"][0]["PublicIpAddress"]["IpAddress"][0]) else: iteration = len(instance_list) // 10 for i in range(iteration): for j in range(10): request.set_InstanceIds([instance_list[i * 10 + j]]) response = self.client.do_action_with_exception( request) response = json.loads(response) if "address" in self.cloud_resources and self.cloud_resources[ 'address'] == "public": ip_list.append(response["Instances"]["Instance"][0] ["PublicIpAddress"]["IpAddress"][0]) else: ip_list.append( response["Instances"]["Instance"][0] ["VpcAttributes"]["PrivateIpAddress"] ['IpAddress'][0]) if len(instance_list) - iteration * 10 != 0: for j in range(len(instance_list) - iteration * 10): request.set_InstanceIds( [instance_list[iteration * 10 + j]]) response = self.client.do_action_with_exception( request) response = json.loads(response) if "address" in self.cloud_resources and self.cloud_resources[ 'address'] == "public": ip_list.append(response["Instances"]["Instance"][0] ["PublicIpAddress"]["IpAddress"][0]) else: ip_list.append( response["Instances"]["Instance"][0] ["VpcAttributes"]["PrivateIpAddress"] ['IpAddress'][0]) return ip_list except: return []