Пример #1
0
def createInstance(params):
    request = CreateInstanceRequest()
    request.set_accept_format('json')

    if "SecurityGroupId" in params:
        request.set_SecurityGroupId(params["SecurityGroupId"])
    if "ZoneId" in params:
        request.set_ZoneId(params["ZoneId"])
    if "VSwitchId" in params:
        request.set_VSwitchId(params["VSwitchId"])

    request.set_ImageId(params["ImageId"])
    request.set_InstanceType(params["InstanceType"])

    request.set_InstanceName(params["InstanceName"])
    request.set_InternetChargeType(params["InternetChargeType"])
    request.set_AutoRenew(params["AutoRenew"])
    request.set_InternetMaxBandwidthOut(params["InternetMaxBandwidthOut"])
    request.set_Password(params["Password"])

    request.set_SystemDiskSize(params["SystemDiskSize"])
    request.set_SystemDiskCategory(params["SystemDiskCategory"])

    request.set_IoOptimized(params["IoOptimized"])
    request.set_InstanceChargeType(params["InstanceChargeType"])

    response = client.do_action_with_exception(request)
    response = json.loads(str(response, encoding='utf-8'))

    return response["InstanceId"]
Пример #2
0
    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
Пример #3
0
    def _boot_image(self, zone_id, image_id, instance_type, vswitch_id,
                    instance_name, ssh_keyname, security_group_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_ZoneId(zone_id)
        request.set_InstanceChargeType('PostPaid')
        request.set_KeyPairName(ssh_keyname)
        request.set_InternetMaxBandwidthOut(1)
        response = self._send_request(request)
        instance_id = response.get('InstanceId')
        if instance_id is None:
            logger.info(response)
            raise Exception("Instance could not be created.")
        self.instance_id = instance_id
        logger.info("Instance %s created" % instance_id)

        logger.info(
            "Wait for the instance to appear as stopped so we can continue")
        max_tries = 10
        status = None
        while max_tries > 0 and status != 'Stopped':
            max_tries -= 1
            time.sleep(5)
            status = self._get_instance_status_by_id(instance_id)
        if status != 'Stopped':
            raise Exception("VM %s not in desired state" % instance_id)

        logger.info("Allocating public ip for instance %s" % instance_id)
        request = AllocatePublicIpAddressRequest()
        request.set_InstanceId(instance_id)
        ip_address = self._send_request(request)
        logger.info("Public IP address asssigned to instance: %s" % ip_address)
        logger.info(json.dumps(ip_address, indent=4))

        logger.info("Starting instance %s" % instance_id)
        request = StartInstanceRequest()
        request.set_InstanceId(instance_id)
        self._send_request(request)

        # wait for instance to be started
        max_tries = 60
        status = "initial"
        while max_tries > 0 and status != 'Running':
            max_tries -= 1
            time.sleep(10)
            status = self._get_instance_status_by_id(instance_id)
            logger.info("Status is %s" % status)

        if status != 'Running':
            raise Exception("Instance %s status is %s: failed to start." %
                            (instance_id, status))

        return (instance_id, ip_address)
Пример #4
0
def create_after_pay_instance(image_id, instance_type, security_group_id,
                              vsw_vswitch_id, zone_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_vswitch_id)
    request.set_SystemDiskCategory('cloud_ssd')
    request.set_ZoneId(zone_id)
    request.set_HostName("flink-console-%s" % REGION_ID)
    request.set_Password(ECS_PAW)
    response = _send_request(request)
    instance_id = response.get('InstanceId')
    if instance_id:
        logging.info("instance %s created task submit successfully.",
                     instance_id)
    else:
        raise Exception("Invalid instance_id: %s ", instance_id)
    return instance_id