Exemplo n.º 1
0
    def handle(self, *args, **options):
        """
        add a VMWare instance into console db

        :param args:
        :param options:
        :return:
        """
        info = self.get_fake_info()
        info.update(self.get_real_info(options))
        instance, err = InstancesModel.save_instance(**info)
        if instance:
            logger.info("import instance %s success: %s" %
                        (instance.to_dict(), err))
        else:
            logger.info("import instance %s error: %s" %
                        (info.get('uuid'), err))
Exemplo n.º 2
0
def create_instance_from_backup(payload):
    backup_id = payload.pop("backup_id")
    version = payload.pop("version")
    resource_name = payload.pop("resource_name")
    owner = payload.get("owner")
    zone = payload.get("zone")
    charge_mode = payload.get("charge_mode")
    pool_name = payload.pop("pool_name")
    nets = payload.pop("nets")

    image_uuid = InstanceBackupModel.get_backup_by_id(backup_id).uuid
    instance_name = resource_name

    flavor_resp = get_flavor_info_through_backup(payload, image_uuid)
    if flavor_resp.get("ret_code") != 0:
        return flavor_resp
    ins_image_info = flavor_resp["ret_set"][0]
    flavor = int(ins_image_info.get("flavor_id"))
    instance_type = InstanceTypeModel. \
        get_instance_type_by_flavor_id(str(flavor))
    if instance_type is None:
        logger.error("cannot find instance type with flavor id " + str(flavor))
        return console_response(
            BackupErrorCode.ASSOCIATE_INSTANCE_NOT_FOUND,
            "cannot find instance type with flavor" + flavor)

    instance_id = make_instance_id()

    security_group_resp = get_default_security_group(payload, logger)
    if security_group_resp.get("ret_code") != 0:
        return security_group_resp

    default_security_group_uuid = security_group_resp["ret_set"][0]

    nets_info = format_payload_nets(nets, False)

    payload.update({
        "action": "CreateInstance",
        "name": instance_id,
        "flavor": flavor,
        "version": version,
        "image": image_uuid,
        "secgroup": default_security_group_uuid,
        "net_info": nets_info,
        "availability_zone": pool_name
    })

    urlparams = [
        "name", "flavor", "image", "secgroup", "zone", "owner",
        "availability_zone"
    ]
    # resp = api.post(payload=payload, urlparams=urlparams, timeout=10)
    resp = api.post(payload=payload, urlparams=urlparams)
    if resp.get("code") != 0:
        return console_response(CommonErrorCode.REQUEST_API_ERROR,
                                resp.get("msg"))
    instance_ret = resp["data"]["ret_set"]
    if isinstance(instance_ret, list):
        instance_ret = instance_ret[0]
    instance_uuid = instance_ret.get("id")

    instance, err = InstancesModel.save_instance(
        uuid=instance_uuid,
        instance_name=instance_name,
        instance_id=instance_id,
        zone=zone,
        owner=owner,
        instance_type=instance_type.instance_type_id,
        charge_mode=charge_mode,
        backup_id=backup_id,
        seen_flag=1)
    if err is not None:
        logger.error("save instance error, %s" % str(err))
        return console_response(InstanceErrorCode.RUN_INSTANCES_FAILED,
                                str(err))

    return console_response(0, "Success", 1, [{
        "instance_id": instance_id
    }], {"resource_id": instance_id})