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)

    args = [
        payload['from_bundle_image'],
        payload.get('organization'),
        request.id,
        payload.get('registry_auths'),
    ]
    safe_args = _get_safe_args(args, payload)

    error_callback = failed_request_callback.s(request.id)
    try:
        handle_regenerate_bundle_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.º 2
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
    processed_request_ids = []
    build_and_requests = zip(payload['build_requests'], requests)
    try:
        for build_request, request in build_and_requests:
            args = [
                build_request['from_bundle_image'],
                build_request.get('organization'),
                request.id,
                build_request.get('registry_auths'),
            ]
            safe_args = _get_safe_args(args, build_request)
            error_callback = failed_request_callback.s(request.id)
            handle_regenerate_bundle_request.apply_async(
                args=args,
                link_error=error_callback,
                argsrepr=repr(safe_args),
                queue=_get_user_queue(),
            )

            request_jsons.append(request.to_json())
            processed_request_ids.append(str(request.id))
    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)

    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