Esempio n. 1
0
    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()
Esempio n. 2
0
 def create_template(self, image_id, sg_id, vpc_id):
     request = CreateLaunchTemplateRequest()
     request.set_accept_format('json')
     request.set_LaunchTemplateName(''.join(
         random.choice(string.ascii_uppercase) for _ in range(20)))
     request.set_ImageId(image_id)
     request.set_ImageOwnerAlias("self")
     request.set_PasswordInherit(True)
     if "address" in self.cloud_resources and self.cloud_resources[
             'address'] == "public":
         request.set_InternetMaxBandwidthIn(100)
         request.set_InternetMaxBandwidthOut(100)
     request.set_InstanceType("ecs.c6.large")
     request.set_InstanceName(self.cloud_resources["instance_name"])
     request.set_SecurityGroupId(sg_id)
     request.set_VpcId(vpc_id)
     request.set_SystemDiskCategory("cloud_efficiency")
     request.set_SystemDiskSize(70)
     request.set_IoOptimized("optimized")
     request.set_InstanceChargeType("PostPaid")
     request.set_NetworkType("vpc")
     request.set_SpotStrategy("SpotWithPriceLimit")
     request.set_SpotPriceLimit(100)
     try:
         response = self.client.do_action_with_exception(request)
         response = json.loads(response)
         return response["LaunchTemplateId"]
     except ServerException as e:
         dlog.info(e)
         sys.exit()
     except ClientException as e:
         dlog.info(e)
         sys.exit()
Esempio n. 3
0
def modify_record_name(domain_name, name, record_id, new_ip_addr):
    request = UpdateDomainRecordRequest()
    request.set_accept_format('json')
    request.set_RecordId(record_id)
    request.set_RR(name)
    request.set_Type("A")
    request.set_Value(new_ip_addr)
    with open(log_file, "a") as logfile:
        try:
            response = client.do_action_with_exception(request)
            sms.dns_record_update_sms(
                '.'.join([name, domain_name]).replace(".", "-"),
                new_ip_addr.replace('.', '-'))
            serverchan_ip_change_msg(name, "-".join(new_ip_addr.split(".")))
            logfile.write(
                "%s \t %s \n" %
                (str(time.asctime(time.localtime(time.time()))), response))
            r_j = json.loads(response.decode())
            request_id = r_j["RequestId"]
            record_id = r_j["RecordId"]
            insert_change(request_id=request_id,
                          record_id=record_id,
                          domain=domain_name,
                          name=name,
                          ip_addr=new_ip_addr)
            exist_and_count(domain_name, name, record_id)
            OK_response("200.3 OK")
        except Exception as e:
            logfile.write("%s \t %s \n" %
                          (str(time.asctime(time.localtime(
                              time.time()))), traceback.print_exc()))
            code = "503.4"
            serverchan_error_msg(domain_name, code, e)
            error_response(code)
Esempio n. 4
0
    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()
Esempio n. 5
0
 def get_vsw_id(self, vpc_id):
     request = DescribeVpcsRequest()
     request.set_accept_format('json')
     request.set_VpcId(vpc_id)
     response = self.client.do_action_with_exception(request)
     response = json.loads(response)
     for vpc in response["Vpcs"]["Vpc"]:
         if vpc["VpcId"] == vpc_id:
             vswitchids = vpc["VSwitchIds"]["VSwitchId"]
             break
     vswitchid_option = []
     if "zone" in self.cloud_resources and self.cloud_resources['zone']:
         for zone in self.cloud_resources['zone']:
             for vswitchid in vswitchids:
                 request = DescribeVSwitchesRequest()
                 request.set_accept_format('json')
                 request.set_VSwitchId(vswitchid)
                 zoneid = self.cloud_resources['regionID'] + "-" + zone
                 request.set_ZoneId(zoneid)
                 response = self.client.do_action_with_exception(request)
                 response = json.loads(response)
                 if (response["TotalCount"] == 1):
                     vswitchid_option.append(vswitchid)
                     continue
     if (vswitchid_option):
         return vswitchid_option
     else:
         return vswitchids
Esempio n. 6
0
 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
Esempio n. 7
0
 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
Esempio n. 8
0
    def create_apg(self):
        request = CreateAutoProvisioningGroupRequest()
        request.set_accept_format('json')
        request.set_TotalTargetCapacity(str(self.nchunks_limit))
        request.set_LaunchTemplateId(self.cloud_resources["template_id"])
        request.set_AutoProvisioningGroupName(
            self.cloud_resources["instance_name"] +
            ''.join(random.choice(string.ascii_uppercase) for _ in range(20)))
        request.set_AutoProvisioningGroupType("maintain")
        request.set_SpotAllocationStrategy("lowest-price")
        request.set_SpotInstanceInterruptionBehavior("terminate")
        request.set_SpotInstancePoolsToUseCount(1)
        request.set_ExcessCapacityTerminationPolicy("termination")
        request.set_TerminateInstances(True)
        request.set_PayAsYouGoTargetCapacity("0")
        request.set_SpotTargetCapacity(str(self.nchunks_limit))
        config = self.generate_config()
        request.set_LaunchTemplateConfigs(config)

        try:
            response = self.client.do_action_with_exception(request)
            response = json.loads(response)
            with open('apg_id.json', 'w') as fp:
                json.dump({'apg_id': response["AutoProvisioningGroupId"]},
                          fp,
                          indent=4)
            return response["AutoProvisioningGroupId"]
        except ServerException as e:
            dlog.info("create apg failed, err msg: %s" % e)
            sys.exit()
        except ClientException as e:
            dlog.info("create apg failed, err msg: %s" % e)
            sys.exit()
Esempio n. 9
0
 def get_sg_vpc_id(self):
     request = DescribeSecurityGroupsRequest()
     request.set_accept_format('json')
     response = self.client.do_action_with_exception(request)
     response = json.loads(response)
     for sg in response["SecurityGroups"]["SecurityGroup"]:
         if sg["SecurityGroupName"] == "sg":
             return sg["SecurityGroupId"], sg["VpcId"]
Esempio n. 10
0
 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 []
Esempio n. 11
0
def describe_full_records(domain_name):
    request = DescribeDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain_name)
    try:
        response = client.do_action_with_exception(request)
        return json.loads(response.decode())
    except Exception as e:
        code = "503.1"
        serverchan_error_msg(domain_name, code, e)
        error_response(code)
def send_request(request, region):
    """Send request to Alibaba cloud."""
    aliyunsdkcore.request.set_default_protocol_type("https")
    request.set_protocol_type("https")
    request.set_accept_format('json')
    try:
        client = get_acs_client(region)
        response_str = client.do_action(request)
        response_detail = json.loads(response_str)
        return response_detail
    except (ClientException, ServerException) as exc:
        print('exception in send_request: %s, exit.' % exc)
        sys.exit(1)
Esempio n. 13
0
def describe_name_record(record_id):
    request = DescribeDomainRecordInfoRequest()
    request.set_accept_format('json')
    request.set_RecordId(record_id)
    with open(log_file, "a") as logfile:
        try:
            response = client.do_action_with_exception(request).decode()
            ip_value_in_record = json.loads(response)['Value']
            return ip_value_in_record
        except Exception as e:
            logfile.write("%s \t %s \n" % (str(time.asctime(time.localtime(time.time()))), traceback.print_exc()))
            code ="503.3"
            serverchan_error_msg(record_id, code, e)
            error_response(code)
Esempio n. 14
0
 def delete_template(self):
     request = DeleteLaunchTemplateRequest()
     request.set_accept_format('json')
     count = 0
     flag = 0
     while count < 10:
         try:
             request.set_LaunchTemplateId(
                 self.cloud_resources["template_id"])
             response = self.client.do_action_with_exception(request)
             flag = 1
             break
         except:
             count += 1
Esempio n. 15
0
 def send(self, identity, code):
     request = aliyunsdkcore.request.CommonRequest()
     request.set_accept_format('json')
     request.set_domain('dysmsapi.aliyuncs.com')
     request.set_method('POST')
     request.set_protocol_type('https')
     request.set_version('2017-05-25')
     request.set_action_name('SendSms')
     request.add_query_param('RegionId', 'default')
     request.add_query_param('PhoneNumbers', identity)
     request.add_query_param('SignName', 'Hackergame')
     request.add_query_param('TemplateCode', 'SMS_168560438')
     request.add_query_param('TemplateParam', json.dumps({'code': code}))
     response = json.loads(client.do_action_with_exception(request))
     if response['Code'] != 'OK':
         raise ValueError(response['Code'])
Esempio n. 16
0
    def __send_request(self, request):
        """ Send a request to Alibaba cloud services """
        aliyunsdkcore.request.set_default_protocol_type('https')
        request.set_protocol_type('https')
        request.set_accept_format('json')
        client = self.__get_acs_client()
        try:
            response_str = client.do_action_with_exception(request)
        except ClientException as exc:
            LOGGER.exception(exc)
            raise RuntimeError(
                'Check correctness of ALIBABA_REGION configuration variable')
        except ServerException as exc:
            LOGGER.exception(exc)
            if exc.get_error_code() == 'InvalidAccessKeyId.NotFound' and \
                    exc.get_error_msg() == 'Specified access key is not found.':
                raise RuntimeError(
                    'InvalidAccessKeyId.NotFound: Check correctness of ' +
                    'ALIBABA_ACCESS_KEY_ID configuration variable')
            if exc.get_error_code() == 'IncompleteSignature' and \
                    exc.get_error_msg().startswith('The request signature does not conform to ' +
                                                   'Aliyun standards'):
                raise RuntimeError(
                    'IncompleteSignature: Check correctness of ' +
                    'ALIBABA_ACCESS_KEY_SECRET configuration variable')
            if exc.get_error_code() == 'InvalidAccessKeySecret' and \
                    exc.get_error_msg() == 'The AccessKeySecret is incorrect. Please check ' + \
                                           'your AccessKeyId and AccessKeySecret.':
                raise RuntimeError(
                    'InvalidAccessKeySecret: Check correctness of ' +
                    'ALIBABA_ACCESS_KEY_ID and ALIBABA_ACCESS_KEY_SECRET ' +
                    'configuration variables')
            raise exc

        response = json.loads(response_str)
        if 'Code' in response.keys():
            LOGGER.warning(
                'Request to Alibaba has \'Code\' attribute. Full Alibaba response:'
            )
            LOGGER.warning(
                json.dumps(response,
                           sort_keys=True,
                           indent=4,
                           separators=(',', ': ')))
        return response
Esempio n. 17
0
 def delete_apg(self):
     request = DeleteAutoProvisioningGroupRequest()
     request.set_accept_format('json')
     request.set_AutoProvisioningGroupId(self.cloud_resources["apg_id"])
     request.set_TerminateInstances(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 not flag:
         dlog.info("delete apg failed, exit")
         sys.exit()
Esempio n. 18
0
 def change_apg_capasity(self, capasity):
     request = ModifyAutoProvisioningGroupRequest()
     request.set_accept_format('json')
     request.set_AutoProvisioningGroupId(self.cloud_resources["apg_id"])
     request.set_TotalTargetCapacity(str(capasity))
     request.set_SpotTargetCapacity(str(capasity))
     request.set_PayAsYouGoTargetCapacity("0")
     count = 0
     flag = 0
     while count < 10:
         try:
             response = self.client.do_action_with_exception(request)
             flag = 1
             break
         except:
             count += 1
             time.sleep(10)
     if not flag:
         dlog.info("change_apg_capasity failed, exit")
         sys.exit()
Esempio n. 19
0
 def get_image_id(self, img_name):
     request = DescribeImagesRequest()
     request.set_accept_format('json')
     request.set_ImageOwnerAlias("self")
     request.set_PageSize(100)
     count = 0
     flag = 0
     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()