Ejemplo n.º 1
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()
Ejemplo n.º 2
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()