def run_instances(self): """ 调用创建实例的API,得到实例ID后继续查询实例状态 :return:instance_ids 需要检查的实例ID """ request = RunInstancesRequest() request.set_DryRun(self.dry_run) request.set_InstanceChargeType(self.instance_charge_type) request.set_Period(self.period) request.set_PeriodUnit(self.period_unit) request.set_ZoneId(self.zone_id) request.set_InternetChargeType(self.internet_charge_type) request.set_ImageId(self.image_id) request.set_SecurityGroupId(self.security_group_id) request.set_InstanceName(self.instance_name) request.set_PasswordInherit(self.password_inherit) request.set_Amount(self.amount) request.set_InternetMaxBandwidthOut(self.internet_max_bandwidth_out) request.set_UniqueSuffix(self.unique_suffix) request.set_IoOptimized(self.io_optimized) request.set_SecurityEnhancementStrategy( self.security_enhancement_strategy) request.set_InstanceType(self.instance_type) request.set_SystemDiskSize(self.system_disk_size) request.set_SystemDiskCategory(self.system_disk_category) request.set_DataDisks(self.data_disks) body = self.client.do_action_with_exception(request) data = json.loads(body) instance_ids = data['InstanceIdSets']['InstanceIdSet'] print('Success. Instance creation succeed. InstanceIds: {}'.format( ', '.join(instance_ids))) return instance_ids
def create_machine(self): AccessKey_ID = self.adata["AccessKey_ID"] AccessKey_Secret = self.adata["AccessKey_Secret"] strategy = self.adata["pay_strategy"] pwd = self.adata["password"] regionID = self.mdata_machine['regionID'] template_name = '%s_%s_%s' % (self.mdata_resources['partition'], self.mdata_resources['numb_gpu'], strategy) instance_name = self.adata["instance_name"] client = AcsClient(AccessKey_ID,AccessKey_Secret, regionID) request = RunInstancesRequest() request.set_accept_format('json') request.set_UniqueSuffix(True) request.set_Password(pwd) request.set_InstanceName(instance_name) request.set_Amount(self.nchunks) request.set_LaunchTemplateName(template_name) response = client.do_action_with_exception(request) response = json.loads(response) self.instance_list = response["InstanceIdSets"]["InstanceIdSet"] time.sleep(50) request = DescribeInstancesRequest() request.set_accept_format('json') request.set_InstanceIds(self.instance_list) response = client.do_action_with_exception(request) response = json.loads(response) ip = [] for i in range(len(response["Instances"]["Instance"])): ip.append(response["Instances"]["Instance"][i]["PublicIpAddress"]['IpAddress'][0]) self.ip_list = ip
def run_instances(self): """ 调用创建实例的API,得到实例ID后继续查询实例状态 :return:instance_ids 需要检查的实例ID """ request = RunInstancesRequest() # true:发送检查请求,不会创建实例。检查项包括是否填写了必需参数、请求格式、业务限制和ECS库存。如果检查不通过,则返回对应错误。如果检查通过,则返回错误码DryRunOperation # false(默认):发送正常请求,通过检查后直接创建实例。 request.set_DryRun(self.dry_run) request.set_InstanceType(self.instance_type) request.set_InstanceChargeType(self.instance_charge_type) request.set_ImageId(self.image_id) request.set_SecurityGroupId(self.security_group_id) request.set_Period(self.period) request.set_PeriodUnit(self.period_unit) request.set_ZoneId(self.zone_id) request.set_InternetChargeType(self.internet_charge_type) request.set_VSwitchId(self.vswitch_id) request.set_InstanceName(self.instance_name) request.set_Password(self.password) request.set_Amount(self.amount) request.set_InternetMaxBandwidthOut(self.internet_max_bandwidth_out) request.set_UniqueSuffix(self.unique_suffix) request.set_IoOptimized(self.io_optimized) request.set_SecurityEnhancementStrategy( self.security_enhancement_strategy) if self.auto_release_time: request.set_AutoReleaseTime(self.auto_release_time) request.set_SystemDiskSize(self.system_disk_size) request.set_SystemDiskCategory(self.system_disk_category) request.set_DataDisks(self.data_disks) request.set_Tags(self.tags) body = self.client.do_action_with_exception(request) data = json.loads(body) instance_ids = data['InstanceIdSets']['InstanceIdSet'] print('Success. Instance creation succeed. InstanceIds: {}'.format( ', '.join(instance_ids))) # instance_ids 输出为一个list return instance_ids
def create(user_data): request = RunInstancesRequest() request.set_accept_format('json') request.set_ImageId("ubuntu_18_04_64_20G_alibase_20190624.vhd") request.set_InstanceType("ecs.t5-lc2m1.nano") request.set_SecurityGroupId("sg-j6c0z0xdiqgujaby8hu4") request.set_VSwitchId("vsw-j6cpqku7fl7zjcej0fj1b") request.set_InstanceName("ss-slave") request.set_Description("ss-slave") request.set_InternetMaxBandwidthOut(100) request.set_HostName("ss-slave") request.set_UniqueSuffix(True) request.set_InternetChargeType("PayByTraffic") request.set_UserData(user_data) request.set_KeyPairName("ss-slave") request.set_Amount(1) response = client.do_action_with_exception(request) res = json.loads(response) return res["InstanceIdSets"]["InstanceIdSet"]