Beispiel #1
0
def update_request_with_deps(request_id, deps):
    """
    Update the Cachito request with the resolved dependencies.

    :param int request_id: the ID of the Cachito request
    :param list deps: the list of dependency dictionaries to record
    :raise CachitoError: if the request to the Cachito API fails
    """
    # Import this here to avoid a circular import
    from cachito.workers.requests import requests_auth_session
    config = get_worker_config()
    request_url = f'{config.cachito_api_url.rstrip("/")}/requests/{request_id}'

    log.info('Adding %d dependencies to request %d', len(deps), request_id)
    payload = {'dependencies': deps}
    try:
        rv = requests_auth_session.patch(request_url, json=payload, timeout=30)
    except requests.RequestException:
        msg = f'The connection failed when setting the dependencies on request {request_id}'
        log.exception(msg)
        raise CachitoError(msg)

    if not rv.ok:
        log.error(
            'The worker failed to set the dependencies on request %d. The status was %d. '
            'The text was:\n%s',
            request_id, rv.status_code, rv.text,
        )
        raise CachitoError(f'Setting the dependencies on request {request_id} failed')
Beispiel #2
0
def set_request_state(request_id, state, state_reason):
    """
    Set the state of the request using the Cachito API.

    :param int request_id: the ID of the Cachito request
    :param str state: the state to set the Cachito request to
    :param str state_reason: the state reason to set the Cachito request to
    :raise CachitoError: if the request to the Cachito API fails
    """
    # Import this here to avoid a circular import
    from cachito.workers.requests import requests_auth_session

    config = get_worker_config()
    request_url = f'{config.cachito_api_url.rstrip("/")}/requests/{request_id}'

    log.info(
        'Setting the state of request %d to "%s" with the reason "%s"',
        request_id, state, state_reason
    )
    payload = {'state': state, 'state_reason': state_reason}
    try:
        rv = requests_auth_session.patch(
            request_url, json=payload, timeout=config.cachito_api_timeout)
    except requests.RequestException:
        msg = f'The connection failed when setting the state to "{state}" on request {request_id}'
        log.exception(msg)
        raise CachitoError(msg)

    if not rv.ok:
        log.error(
            'The worker failed to set request %d to the "%s" state. The status was %d. '
            'The text was:\n%s',
            request_id, state, rv.status_code, rv.text,
        )
        raise CachitoError(f'Setting the state to "{state}" on request {request_id} failed')
Beispiel #3
0
def update_request_with_deps(request_id,
                             deps,
                             env_vars=None,
                             pkg_manager=None,
                             packages=None):
    """
    Update the Cachito request with the resolved dependencies.

    :param int request_id: the ID of the Cachito request
    :param list deps: the list of dependency dictionaries to record
    :param dict env_vars: mapping of environment variables to record
    :param str pkg_manager: a package manager to add to the request if auto-detection was used
    :param list packages: the list of packages that were resolved
    :raise CachitoError: if the request to the Cachito API fails
    """
    # Import this here to avoid a circular import
    from cachito.workers.requests import requests_auth_session
    config = get_worker_config()
    request_url = f'{config.cachito_api_url.rstrip("/")}/requests/{request_id}'

    log.info('Adding %d dependencies to request %d', len(deps), request_id)
    for index in range(0, len(deps), config.cachito_deps_patch_batch_size):
        batch_upper_limit = index + config.cachito_deps_patch_batch_size
        payload = {'dependencies': deps[index:batch_upper_limit]}
        if index == 0:
            if env_vars:
                log.info('Adding environment variables to the request %d: %s',
                         request_id, env_vars)
                payload['environment_variables'] = env_vars
            if pkg_manager:
                log.info(
                    'Adding the package manager "%s" to the request %d',
                    pkg_manager,
                    request_id,
                )
                payload['pkg_managers'] = [pkg_manager]
            if packages:
                log.info('Adding the packages "%s" to the request %d',
                         packages, request_id)
                payload['packages'] = packages
        try:
            log.info('Patching deps {} through {} out of {}'.format(
                index + 1, min(batch_upper_limit, len(deps)), len(deps)))
            rv = requests_auth_session.patch(
                request_url, json=payload, timeout=config.cachito_api_timeout)
        except requests.RequestException:
            msg = f'The connection failed when setting the dependencies on request {request_id}'
            log.exception(msg)
            raise CachitoError(msg)

        if not rv.ok:
            log.error(
                'The worker failed to set the dependencies on request %d. The status was %d. '
                'The text was:\n%s',
                request_id,
                rv.status_code,
                rv.text,
            )
            raise CachitoError(
                f'Setting the dependencies on request {request_id} failed')
Beispiel #4
0
def update_request_env_vars(request_id: int,
                            env_vars: Dict[str, Dict[str, str]]) -> None:
    """Update environment variables of a request.

    :param int request_id: the id of a request to update the environment variables.
    :param dict env_vars: mapping of environment variables to record. The keys represent
        the environment variable name, and its value should be another map with the "value" and
        "kind" attributes, e.g. {"NAME": {"value": "VALUE", "kind": "KIND"}}.
    :raise CachitoError: if the request to the Cachito API fails
    """
    config = get_worker_config()
    request_url = _get_request_url(request_id)
    payload = {"environment_variables": env_vars}
    try:
        rv = requests_auth_session.patch(request_url,
                                         json=payload,
                                         timeout=config.cachito_api_timeout)
    except requests.RequestException:
        msg = (
            f"The connection failed when updating environment variables on the request {request_id}"
        )
        log.exception(msg)
        raise CachitoError(msg)
    if not rv.ok:
        log.error(
            "The worker failed to update environment variables on the request %d. "
            "The status was %d. The text was:\n%s",
            request_id,
            rv.status_code,
            rv.text,
        )
        raise CachitoError(
            f"Updating environment variables on request {request_id} failed")
Beispiel #5
0
def _patch_request_or_fail(request_id: int, payload: dict,
                           connect_error_msg: str,
                           status_error_msg: str) -> None:
    """
    Try to update the specified request using the Cachito PATCH API.

    Both error messages can contain the {exc} placeholder which will be replaced by the actual
    exception.

    :param request_id: ID of the request to get
    :param payload: the JSON data to send to the PATCH endpoint
    :param connect_error_msg: error message to raise if the connection fails
    :param status_error_msg: error message to raise if the response status is 4xx or 5xx
    :raises CachitoError: if the connection fails or the API returns an error response
    """
    config = get_worker_config()
    request_url = f'{config.cachito_api_url.rstrip("/")}/requests/{request_id}'

    try:
        rv = requests_auth_session.patch(request_url,
                                         json=payload,
                                         timeout=config.cachito_api_timeout)
        rv.raise_for_status()
    except requests.HTTPError as e:
        msg = status_error_msg.format(exc=e)
        log.exception(msg)
        raise CachitoError(msg)
    except requests.RequestException as e:
        msg = connect_error_msg.format(exc=e)
        log.exception(msg)
        raise CachitoError(msg)
Beispiel #6
0
def update_request_with_package(request_id, package, env_vars=None, package_subpath=None):
    """
    Update the request with the resolved packages and corresponding metadata.

    :param dict package: the package that was resolved
    :param dict env_vars: mapping of environment variables to record. The keys represent
        the environment variable name, and its value should be another map wth the "value" and
        "kind" attributes, e.g. {"NAME": {"value": "VALUE", "kind": "KIND"}}.
    :param str package_subpath: relative path from root of repository to package directory
    :raise CachitoError: if the request to the Cachito API fails
    """
    log.info('Adding the package "%r" to the request %d', package, request_id)
    payload = {"package": package}

    if env_vars:
        log.info("Also adding environment variables to the request %d: %s", request_id, env_vars)
        payload["environment_variables"] = env_vars

    if package_subpath and package_subpath != os.curdir:
        log.info("Also setting a subpath for the package %r: %r", package["name"], package_subpath)
        payload["package_subpath"] = package_subpath

    # Import this here to avoid a circular import
    from cachito.workers.requests import requests_auth_session

    config = get_worker_config()
    request_url = _get_request_url(request_id)

    try:
        rv = requests_auth_session.patch(
            request_url, json=payload, timeout=config.cachito_api_timeout
        )
    except requests.RequestException:
        msg = f"The connection failed when adding a package to the request {request_id}"
        log.exception(msg)
        raise CachitoError(msg)

    if not rv.ok:
        log.error(
            "The worker failed to add a package to the request %d. The status was %d. "
            "The text was:\n%s",
            request_id,
            rv.status_code,
            rv.text,
        )
        raise CachitoError(f"Setting a package on request {request_id} failed")
Beispiel #7
0
def update_request_with_deps(request_id, package, deps):
    """
    Update the Cachito request with the resolved dependencies.

    :param int request_id: the ID of the Cachito request
    :param dict package: the package these dependencies are associated with
    :param list deps: the list of dependency dictionaries to record
    :raise CachitoError: if the request to the Cachito API fails
    """
    # Import this here to avoid a circular import
    from cachito.workers.requests import requests_auth_session

    config = get_worker_config()
    request_url = _get_request_url(request_id)

    log.info("Adding %d dependencies to request %d", len(deps), request_id)
    for index in range(0, len(deps), config.cachito_deps_patch_batch_size):
        batch_upper_limit = index + config.cachito_deps_patch_batch_size
        payload = {"dependencies": deps[index:batch_upper_limit], "package": package}
        try:
            log.info(
                "Patching deps {} through {} out of {}".format(
                    index + 1, min(batch_upper_limit, len(deps)), len(deps)
                )
            )
            rv = requests_auth_session.patch(
                request_url, json=payload, timeout=config.cachito_api_timeout
            )
        except requests.RequestException:
            msg = f"The connection failed when setting the dependencies on request {request_id}"
            log.exception(msg)
            raise CachitoError(msg)

        if not rv.ok:
            log.error(
                "The worker failed to set the dependencies on request %d. The status was %d. "
                "The text was:\n%s",
                request_id,
                rv.status_code,
                rv.text,
            )
            raise CachitoError(f"Setting the dependencies on request {request_id} failed")