def CreateInstance(ak, sk, region_tag, instance_name, security_id, vpc_id, instance_type, bandout): global instance_id client = AcsClient(ak, sk, region_tag) request = CreateInstanceRequest() request.set_accept_format('json') request.set_InstanceName(instance_name) request.set_SecurityGroupId(security_id) #安全组 request.set_InstanceType(instance_type) #型号 request.set_ImageId("m-bp1a8si7x4swoetbhtgn") #统一镜像 request.set_VSwitchId(vpc_id) #指定VPC的交换机 request.set_KeyPairName("电商平台秘钥") #秘钥 request.set_InstanceChargeType("PrePaid") #预付费 request.set_IoOptimized("optimized") #IO强化 request.set_SystemDiskSize(40) #磁盘容量 request.set_SystemDiskCategory("cloud_ssd") #高效云盘 request.set_PasswordInherit(True) request.set_InternetMaxBandwidthOut(bandout) #出网带宽1M request.set_Period(1) #购买资源的时长 request.set_AutoRenewPeriod(1) #按月续费 request.set_InternetChargeType("PayByTraffic") #按流量计费 request.set_AutoRenew(True) #自动计费 response = client.do_action_with_exception(request) result = str(response, encoding='utf-8') data = json.loads(result) instance_id = data["InstanceId"] print("生成新的ECS的ID号是=======>" + str(instance_id)) return instance_id #返回ID给下面使用
def CreateInstance(self, region_id, ZoneId, ImageId, InstanceType, InstanceName, InstanceChargeType, InternetChargeType, InternetMaxBandwidthOut, HostName, DataDisk, Password, VSwitchId, SecurityGroupId, UserData): """ 创建 实例 :param region_id: 地域 :param ZoneId: 可用区 :param ImageId: 镜像 :param InstanceType: 实例模板 :param InstanceName: 实例名字 :param InstanceChargeType: 计费方式 :param InternetChargeType: 网络计费方式 :param InternetMaxBandwidthOut: 出网带宽 :param HostName: 主机名字 :param DataDisk: 数据盘 :param Password: 密码 :param VSwitchId: 交换机 :param SecurityGroupId: 安全组 :param UserData: 机器初始化cloud_init加载脚本 :return: {'InstanceId': 'i-2ze210z0uiwyadm1m7x6'} """ createclt = AcsClient(self.AccessKeyId, self.AccessKeySecret, region_id) createreq = CreateInstanceRequest() createreq.set_action_name('CreateInstance') createreq.set_accept_format('json') createreq.set_ZoneId(ZoneId) createreq.set_ImageId(ImageId) createreq.set_InstanceType(InstanceType) createreq.set_InstanceName(InstanceName) createreq.set_InstanceChargeType(InstanceChargeType) if InstanceChargeType == 'PrePaid': createreq.set_Period('1') if InternetChargeType: createreq.set_InternetChargeType(InternetChargeType) createreq.set_InternetMaxBandwidthOut(InternetMaxBandwidthOut) createreq.set_HostName(HostName) createreq.set_Password(Password) createreq.set_VSwitchId(VSwitchId) createreq.set_SecurityGroupId(SecurityGroupId) '''通用配置 以下配固定不变配置''' createreq.set_KeyPairName('twl-vpc-ecs-key') # 初始化key createreq.set_SecurityEnhancementStrategy('Active') # 安全加固 createreq.set_IoOptimized('optimized') # IO优化 createreq.set_DataDisks(DataDisk) # 数据盘 ''' 通用配置 系统盘 ''' createreq.set_SystemDiskCategory('cloud_efficiency') # 高效云盘 createreq.set_SystemDiskSize(40) # 系统盘大小40G createreq.set_SystemDiskDiskName(HostName + "-sysdisk") # 系统盘命名 ''' 初始化要执行的脚本''' createreq.set_UserData(UserData) # 传入UserData数据,进行开机初始化 createre = json.loads(createclt.do_action_with_exception(createreq), encoding='utf-8') return createre
def create_prepay_instance(image_id, instance_type, security_group_id, vswitch_id): request = CreateInstanceRequest() request.set_ImageId(image_id) request.set_SecurityGroupId(security_group_id) request.set_InstanceType(instance_type) request.set_VSwitchId(vswitch_id) request.set_Period(1) request.set_InstanceChargeType('PrePaid') request.set_InstanceName(instance_name) request.set_KeyPairName("saltstack1") response = _send_request(request) instance_id = response.get('InstanceId') logging.info("instance %s created task submit successfully.", instance_id) return instance_id
def create_prepay_instance(image_id, instance_type, security_group_id): """ :param image_id: You can use public images or custom images. Image specify a OS and related software packages. :param instance_type: The option “one-core 2GiB n1.small” indicates that the input parameter is ecs.n1.small. :param security_group_id: Security group ID. A security group is similar to a firewall and uses security group rules to control network access requests of instances.We recommend that you configure access rules only according to the actual needs. :return: a instance id. """ request = CreateInstanceRequest() request.set_ImageId(image_id) request.set_SecurityGroupId(security_group_id) request.set_InstanceType(instance_type) request.set_IoOptimized('optimized') request.set_VSwitchId('vsw-vswitchid') request.set_SystemDiskCategory('cloud_ssd') request.set_Period(1) request.set_InstanceChargeType('PrePaid') response = _send_request(request) instance_id = response.get('InstanceId') logging.info("instance %s created task submit successfully.", instance_id) return instance_id