Ejemplo n.º 1
0
def regenerate_bundle():
    """
    Submit a request to regenerate an operator bundle image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = RequestRegenerateBundle.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    error_callback = failed_request_callback.s(request.id)
    handle_regenerate_bundle_request.apply_async(
        args=[
            payload['from_bundle_image'],
            payload.get('organization'), request.id
        ],
        link_error=error_callback,
        queue=_get_user_queue(),
    )

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 2
0
Archivo: api_v1.py Proyecto: lcarva/iib
def add_bundles():
    """
    Submit a request to add operator bundles to an index image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = Request.from_add_json(payload)
    db.session.add(request)
    db.session.commit()

    args = [
        payload['bundles'],
        payload['binary_image'],
        request.id,
        payload.get('from_index'),
        payload.get('add_arches'),
        payload.get('cnr_token'),
        payload.get('organization'),
        _should_force_overwrite() or payload.get('overwrite_from_index'),
    ]
    safe_args = copy.copy(args)
    if payload.get('cnr_token'):
        safe_args[safe_args.index(payload['cnr_token'])] = '*****'

    error_callback = failed_request_callback.s(request.id)
    handle_add_request.apply_async(args=args, link_error=error_callback, argsrepr=repr(safe_args))

    flask.current_app.logger.debug('Successfully scheduled request %d', request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 3
0
def rm_operators():
    """
    Submit a request to remove operators from an index image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = RequestRm.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    overwrite_from_index = payload.get('overwrite_from_index')

    args = _get_rm_args(payload, request, overwrite_from_index)
    safe_args = _get_safe_args(args, payload)

    error_callback = failed_request_callback.s(request.id)
    try:
        handle_rm_request.apply_async(
            args=args,
            link_error=error_callback,
            argsrepr=repr(safe_args),
            queue=_get_user_queue(serial=overwrite_from_index),
        )
    except kombu.exceptions.OperationalError:
        handle_broker_error(request)

    flask.current_app.logger.debug('Successfully scheduled request %d', request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 4
0
Archivo: api_v1.py Proyecto: lcarva/iib
def rm_operators():
    """
    Submit a request to remove operators from an index image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = Request.from_remove_json(payload)
    db.session.add(request)
    db.session.commit()

    error_callback = failed_request_callback.s(request.id)
    handle_rm_request.apply_async(
        args=[
            payload['operators'],
            payload['binary_image'],
            request.id,
            payload['from_index'],
            payload.get('add_arches'),
            _should_force_overwrite() or payload.get('overwrite_from_index'),
        ],
        link_error=error_callback,
    )

    flask.current_app.logger.debug('Successfully scheduled request %d', request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 5
0
def regenerate_bundle_batch():
    """
    Submit a batch of requests to regenerate operator bundle images.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    Batch.validate_batch_request_params(payload)

    batch = Batch(annotations=payload.get('annotations'))
    db.session.add(batch)

    requests = []
    # Iterate through all the build requests and verify that the requests are valid before
    # committing them and scheduling the tasks
    for build_request in payload['build_requests']:
        try:
            request = RequestRegenerateBundle.from_json(build_request, batch)
        except ValidationError as e:
            # Rollback the transaction if any of the build requests are invalid
            db.session.rollback()
            raise ValidationError(
                f'{str(e).rstrip(".")}. This occurred on the build request in '
                f'index {payload["build_requests"].index(build_request)}.')
        db.session.add(request)
        requests.append(request)

    db.session.commit()
    messaging.send_messages_for_new_batch_of_requests(requests)

    request_jsons = []
    # This list will be used for the log message below and avoids the need of having to iterate
    # through the list of requests another time
    request_id_strs = []
    for build_request, request in zip(payload['build_requests'], requests):
        request_jsons.append(request.to_json())
        request_id_strs.append(str(request.id))

        error_callback = failed_request_callback.s(request.id)
        handle_regenerate_bundle_request.apply_async(
            args=[
                build_request['from_bundle_image'],
                build_request.get('organization'),
                request.id,
            ],
            link_error=error_callback,
            queue=_get_user_queue(),
        )

    flask.current_app.logger.debug(
        'Successfully scheduled the batch %d with requests: %s',
        batch.id,
        ', '.join(request_id_strs),
    )
    return flask.jsonify(request_jsons), 201
Ejemplo n.º 6
0
def add_bundles():
    """
    Submit a request to add operator bundles to an index image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = RequestAdd.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    overwrite_from_index = _should_force_overwrite() or payload.get(
        'overwrite_from_index')
    celery_queue = _get_user_queue(serial=overwrite_from_index)
    args = [
        payload['bundles'],
        payload['binary_image'],
        request.id,
        payload.get('from_index'),
        payload.get('add_arches'),
        payload.get('cnr_token'),
        payload.get('organization'),
        payload.get('force_backport'),
        overwrite_from_index,
        payload.get('overwrite_from_index_token'),
        flask.current_app.config['IIB_GREENWAVE_CONFIG'].get(celery_queue),
    ]
    safe_args = copy.copy(args)
    if payload.get('cnr_token'):
        safe_args[safe_args.index(payload['cnr_token'])] = '*****'
    if payload.get('overwrite_from_index_token'):
        safe_args[safe_args.index(
            payload['overwrite_from_index_token'])] = '*****'

    error_callback = failed_request_callback.s(request.id)

    try:
        handle_add_request.apply_async(args=args,
                                       link_error=error_callback,
                                       argsrepr=repr(safe_args),
                                       queue=celery_queue)
    except kombu.exceptions.OperationalError:
        handle_broker_error(request)

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 7
0
Archivo: api_v1.py Proyecto: zanssa/iib
def merge_index_image():
    """
    Submit a request to merge two index images.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')
    request = RequestMergeIndexImage.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    overwrite_target_index = payload.get('overwrite_target_index', False)
    celery_queue = _get_user_queue(serial=overwrite_target_index)
    args = [
        payload['source_from_index'],
        payload.get('deprecation_list', []),
        request.id,
        payload.get('binary_image'),
        payload.get('target_index'),
        overwrite_target_index,
        payload.get('overwrite_target_index_token'),
        request.distribution_scope,
        flask.current_app.config['IIB_BINARY_IMAGE_CONFIG'],
        payload.get('build_tags', []),
    ]
    safe_args = _get_safe_args(args, payload)

    error_callback = failed_request_callback.s(request.id)
    try:
        handle_merge_request.apply_async(args=args,
                                         link_error=error_callback,
                                         argsrepr=repr(safe_args),
                                         queue=celery_queue)
    except kombu.exceptions.OperationalError:
        handle_broker_error(request)

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 8
0
def rm_operators():
    """
    Submit a request to remove operators from an index image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = RequestRm.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    args = [
        payload['operators'],
        payload['binary_image'],
        request.id,
        payload['from_index'],
        payload.get('add_arches'),
        _should_force_overwrite() or payload.get('overwrite_from_index'),
        payload.get('overwrite_from_index_token'),
    ]

    safe_args = copy.copy(args)
    if payload.get('overwrite_from_index_token'):
        safe_args[safe_args.index(
            payload['overwrite_from_index_token'])] = '*****'

    error_callback = failed_request_callback.s(request.id)
    handle_rm_request.apply_async(
        args=args,
        link_error=error_callback,
        argsrepr=repr(safe_args),
        queue=_get_user_queue(),
    )

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 9
0
Archivo: api_v1.py Proyecto: zanssa/iib
def create_empty_index():
    """
    Submit a request to create an index image without bundles.

    Note: Any duplicate bundle will be removed from payload.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = RequestCreateEmptyIndex.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    args = [
        payload['from_index'],
        request.id,
        payload.get('output_fbc'),
        payload.get('binary_image'),
        payload.get('labels'),
        flask.current_app.config['IIB_BINARY_IMAGE_CONFIG'],
    ]
    safe_args = _get_safe_args(args, payload)
    error_callback = failed_request_callback.s(request.id)

    try:
        handle_create_empty_index_request.apply_async(
            args=args,
            link_error=error_callback,
            argsrepr=repr(safe_args),
            queue=_get_user_queue())
    except kombu.exceptions.OperationalError:
        handle_broker_error(request)

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 10
0
Archivo: api_v1.py Proyecto: zanssa/iib
def add_bundles():
    """
    Submit a request to add operator bundles to an index image.

    Note: Any duplicate bundle will be removed from payload.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    # Only run `_get_unique_bundles` if it is a list. If it's not, `from_json`
    # will raise an error to the user.
    if payload.get('bundles') and isinstance(payload['bundles'], list):
        payload['bundles'] = _get_unique_bundles(payload['bundles'])

    request = RequestAdd.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    overwrite_from_index = payload.get('overwrite_from_index')
    celery_queue = _get_user_queue(serial=overwrite_from_index)
    args = _get_add_args(payload, request, overwrite_from_index, celery_queue)
    safe_args = _get_safe_args(args, payload)
    error_callback = failed_request_callback.s(request.id)

    try:
        handle_add_request.apply_async(args=args,
                                       link_error=error_callback,
                                       argsrepr=repr(safe_args),
                                       queue=celery_queue)
    except kombu.exceptions.OperationalError:
        handle_broker_error(request)

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
Ejemplo n.º 11
0
def add_rm_batch():
    """
    Submit a batch of requests to add or remove operators from an index image.

    Note: Any duplicate bundle will be removed from payload when adding operators.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    Batch.validate_batch_request_params(payload)

    batch = Batch(annotations=payload.get('annotations'))
    db.session.add(batch)

    requests = []
    # Iterate through all the build requests and verify that the requests are valid before
    # committing them and scheduling the tasks
    for build_request in payload['build_requests']:
        try:
            if build_request.get('operators'):
                # Check for the validity of a RM request
                request = RequestRm.from_json(build_request, batch)
            elif build_request.get('bundles'):
                build_request_uniq = copy.deepcopy(build_request)
                build_request_uniq['bundles'] = _get_unique_bundles(build_request_uniq['bundles'])
                # Check for the validity of an Add request
                request = RequestAdd.from_json(build_request_uniq, batch)
            else:
                raise ValidationError('Build request is not a valid Add/Rm request.')
        except ValidationError as e:
            raise ValidationError(
                f'{str(e).rstrip(".")}. This occurred on the build request in '
                f'index {payload["build_requests"].index(build_request)}.'
            )
        db.session.add(request)
        requests.append(request)

    db.session.commit()
    messaging.send_messages_for_new_batch_of_requests(requests)

    request_jsons = []
    # This list will be used for the log message below and avoids the need of having to iterate
    # through the list of requests another time
    processed_request_ids = []
    for build_request, request in zip(payload['build_requests'], requests):
        request_jsons.append(request.to_json())

        overwrite_from_index = build_request.get('overwrite_from_index')
        celery_queue = _get_user_queue(serial=overwrite_from_index)
        if isinstance(request, RequestAdd):
            args = _get_add_args(build_request, request, overwrite_from_index, celery_queue)
        elif isinstance(request, RequestRm):
            args = _get_rm_args(build_request, request, overwrite_from_index)

        safe_args = _get_safe_args(args, build_request)

        error_callback = failed_request_callback.s(request.id)
        try:
            if isinstance(request, RequestAdd):
                handle_add_request.apply_async(
                    args=args,
                    link_error=error_callback,
                    argsrepr=repr(safe_args),
                    queue=celery_queue,
                )
            else:
                handle_rm_request.apply_async(
                    args=args,
                    link_error=error_callback,
                    argsrepr=repr(safe_args),
                    queue=celery_queue,
                )
        except kombu.exceptions.OperationalError:
            unprocessed_requests = [r for r in requests if str(r.id) not in processed_request_ids]
            handle_broker_batch_error(unprocessed_requests)

        processed_request_ids.append(str(request.id))

    flask.current_app.logger.debug(
        'Successfully scheduled the batch %d with requests: %s',
        batch.id,
        ', '.join(processed_request_ids),
    )
    return flask.jsonify(request_jsons), 201