Beispiel #1
0
def calculate_url(host: str,
                  url: str = None,
                  model_route: str = None,
                  model_deployment: str = None,
                  url_prefix: str = None,
                  mr_client: ModelRouteClient = None):
    """
    Calculate url for model

    :param host: edge host
    :param url: full url to model api
    :param model_route: model route name
    :param model_deployment: model deployment name
    :param url_prefix: model prefix
    :return: model url
    """
    if url:
        return url

    if url_prefix:
        LOGGER.debug('')
        return f'{host}{url_prefix}'

    model_route = model_route or model_deployment
    if model_route:
        if mr_client is None:
            mr_client = ModelRouteClient()

        model_route = mr_client.get(model_route)

        LOGGER.debug('Found model route: %s', model_route)
        return model_route.status.edge_url

    raise NotImplementedError("Cannot create a model url")
Beispiel #2
0
def create(client: ModelRouteClient, mr_id: str, file: str, wait: bool,
           timeout: int):
    """
    Create a model route.\n
    You should specify a path to file with a model route. The file must contain only one model route.
    For now, CLI supports yaml and JSON file formats.
    If you want to create multiples routes then you should use "odahuflowctl bulk apply" instead.
    If you provide the model route id parameter than it will be overridden before sending to API server.\n
    Usage example:\n
        * odahuflowctl route create -f route.yaml --id examples-git
    \f
    :param timeout: timeout in seconds. for wait (if no-wait is off)
    :param wait: no wait until operation will be finished
    :param client: ModelRoute HTTP client
    :param mr_id: ModelRoute ID
    :param file: Path to the file with only one model route
    """
    route_resource = parse_resources_file_with_one_item(file).resource
    if not isinstance(route_resource, ModelRoute):
        raise ValueError(
            f'ModelRoute expected, but {type(route_resource)} provided')

    if mr_id:
        route_resource.id = mr_id

    click.echo(client.create(route_resource))

    wait_operation_finish(timeout, wait, mr_id, client)
def build_client(resource: OdahuflowCloudResourceUpdatePair,
                 api_client: RemoteAPIClient) -> typing.Optional[object]:
    """
    Build client for particular resource (e.g. it builds ModelTrainingClient for ModelTraining resource)

    :param resource: target resource
    :param api_client: base API client to extract connection options from
    :return: remote client or None
    """
    if isinstance(resource.resource, ModelTraining):
        return ModelTrainingClient.construct_from_other(api_client)
    elif isinstance(resource.resource, ModelDeployment):
        return ModelDeploymentClient.construct_from_other(api_client)
    elif isinstance(resource.resource, Connection):
        return ConnectionClient.construct_from_other(api_client)
    elif isinstance(resource.resource, ToolchainIntegration):
        return ToolchainIntegrationClient.construct_from_other(api_client)
    elif isinstance(resource.resource, ModelRoute):
        return ModelRouteClient.construct_from_other(api_client)
    elif isinstance(resource.resource, ModelPackaging):
        return ModelPackagingClient.construct_from_other(api_client)
    elif isinstance(resource.resource, PackagingIntegration):
        return PackagingIntegrationClient.construct_from_other(api_client)
    else:
        raise InvalidResourceType('{!r} is invalid resource '.format(
            resource.resource))
Beispiel #4
0
def delete(client: ModelRouteClient, mr_id: str, file: str,
           ignore_not_found: bool):
    """
    Delete a model route.\n
    For this command, you must provide a model route ID or path to file with one model route.
    The file must contain only one model route.
    If you want to delete multiples routes than you should use "odahuflowctl res delete" instead.
    For now, CLI supports yaml and JSON file formats.
    The command will be failed if you provide both arguments.\n
    Usage example:\n
        * odahuflowctl route delete --id examples-git\n
        * odahuflowctl route delete -f route.yaml
    \f
    :param client: ModelRoute HTTP client
    :param mr_id: ModelRoute ID
    :param file: Path to the file with only one model route
    :param ignore_not_found: ignore if Model Deployment is not found
    """
    check_id_or_file_params_present(mr_id, file)

    if file:
        route_resource = parse_resources_file_with_one_item(file).resource
        if not isinstance(route_resource, ModelRoute):
            raise ValueError(
                f'ModelRoute expected, but {type(route_resource)} provided')

        mr_id = route_resource.id

    try:
        click.echo(client.delete(mr_id))
    except WrongHttpStatusCode as e:
        if e.status_code != 404 or not ignore_not_found:
            raise e

        click.echo(f'Model route {mr_id} was not found. Ignore')
Beispiel #5
0
def get(client: ModelRouteClient, mr_id: str, output_format: str):
    """
    Get routes.\n
    The command without id argument retrieve all routes.\n
    Get all routes in json format:\n
        odahuflowctl route get --output-format json\n
    Get model route with "git-repo" id:\n
        odahuflowctl route get --id git-repo\n
    Using jsonpath:\n
        odahuflowctl route get -o 'jsonpath=[*].spec.reference'
    \f
    :param client: ModelRoute HTTP client
    :param mr_id: ModelRoute ID
    :param output_format: Output format
    :return:
    """
    routes = [client.get(mr_id)] if mr_id else client.get_all()

    format_output(routes, output_format)
Beispiel #6
0
def wait_operation_finish(timeout: int, wait: bool, mr_id: str,
                          mr_client: ModelRouteClient):
    """
    Wait route to finish according command line arguments

    :param timeout: timeout in seconds. for wait (if no-wait is off)
    :param wait: no wait until operation will be finished
    :param mr_id: Model Route id
    :param mr_client: Model Route Client

    :return: None
    """
    if not wait:
        return

    start = time.time()
    if timeout <= 0:
        raise Exception(
            'Invalid --timeout argument: should be positive integer')

    while True:
        elapsed = time.time() - start
        if elapsed > timeout:
            raise Exception(TIMEOUT_ERROR_MESSAGE)

        try:
            mr = mr_client.get(mr_id)
            if mr.status.state == READY_STATE:
                print(f'Model Route {mr_id} is ready')
                return
            elif mr.status.state == "":
                print(f"Can't determine the state of {mr.id}. Sleeping...")
            else:
                print(f'Current route state is {mr.status.state}. Sleeping...')
        except WrongHttpStatusCode:
            LOGGER.info(
                'Callback have not confirmed completion of the operation')

        LOGGER.debug('Sleep before next request')
        time.sleep(DEFAULT_WAIT_TIMEOUT)
Beispiel #7
0
 def route_delete(route_id: str):
     return ModelRouteClient().delete(route_id)
Beispiel #8
0
 def route_post(payload_file):
     api_object = parse_resources_file_with_one_item(payload_file).resource
     return ModelRouteClient().create(api_object)
Beispiel #9
0
 def route_get_id(route_id: str):
     return ModelRouteClient().get(route_id)
Beispiel #10
0
 def route_get():
     return ModelRouteClient().get_all()
Beispiel #11
0
def route(ctx: click.core.Context, url: str, token: str):
    """
    Allow you to perform actions on routes
    """
    ctx.obj = ModelRouteClient(url, token)
Beispiel #12
0
    PackagingIntegration(
        id=ENTITY_ID,
        spec=PackagingIntegrationSpec(default_image="odahu:image",
                                      entrypoint="some_entrypoint"),
    ),
    packaging_integration.packaging_integration,
    'PackagingIntegration',
)

DEPLOYMENT = EntityTestData(
    ModelDeploymentClient(),
    ModelDeployment(id=ENTITY_ID,
                    spec=ModelDeploymentSpec(image="odahu:image",
                                             min_replicas=0),
                    status=ModelDeploymentStatus(
                        state=READY_STATE,
                        available_replicas=1,
                    )),
    deployment.deployment,
    'ModelDeployment',
)

ROUTER = EntityTestData(
    ModelRouteClient(),
    ModelRoute(id=ENTITY_ID,
               spec=ModelRouteSpec(mirror="test", ),
               status=ModelRouteStatus(state=READY_STATE, )),
    route.route,
    'ModelRoute',
)