How to use the mlrun.run.new_function function in mlrun

To help you get started, we’ve selected a few mlrun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mlrun / mlrun / mlrun / __main__.py View on Github external
set_item(runobj.spec, out_path, run_keys.output_path)
    set_item(runobj.spec, outputs, run_keys.outputs, list(outputs))
    set_item(
        runobj.spec, secrets, run_keys.secrets, line2keylist(secrets, 'kind', 'source')
    )

    if kfp or runobj.spec.verbose or verbose:
        print('MLRun version: {}'.format(get_version()))
        print('Runtime:')
        pprint(runtime)
        print('Run:')
        pprint(runobj.to_dict())

    try:
        update_in(runtime, 'metadata.name', name, replace=False)
        fn = new_function(runtime=runtime, kfp=kfp, mode=mode)
        if workdir:
            fn.spec.workdir = workdir
        fn.is_child = from_env and not kfp
        resp = fn.run(runobj, watch=watch, schedule=schedule)
        if resp and dump:
            print(resp.to_yaml())
    except RunError as err:
        print('runtime error: {}'.format(err))
        exit(1)
github mlrun / mlrun / mlrun / __main__.py View on Github external
skip,
):
    """Build a container image from code and requirements."""

    if db:
        mlconf.dbpath = db

    if runtime:
        runtime = py_eval(runtime)
        if not isinstance(runtime, dict):
            print('runtime parameter must be a dict, not {}'.format(type(runtime)))
            exit(1)
        if kfp:
            print('Runtime:')
            pprint(runtime)
        func = new_function(runtime=runtime)
    elif func_url.startswith('db://'):
        func_url = func_url[5:]
        func = import_function(func_url)
    elif func_url:
        func_url = 'function.yaml' if func_url == '.' else func_url
        func = import_function(func_url)
    else:
        print('please specify the function path or url')
        exit(1)

    meta = func.metadata
    meta.project = project or meta.project or mlconf.default_project
    meta.name = name or meta.name
    meta.tag = tag or meta.tag

    b = func.spec.build
github mlrun / mlrun / mlrun / api / api / utils.py View on Github external
url = data.get("functionUrl")
    if not url and task:
        url = get_in(task, "spec.function")
    if not (function or url) or not task:
        log_and_raise(
            HTTPStatus.BAD_REQUEST,
            reason="bad JSON, need to include function/url and task objects",
        )

    # TODO: block exec for function["kind"] in ["", "local]  (must be a
    # remote/container runtime)

    response = None
    try:
        if function and not url:
            fn = new_function(runtime=function)
        else:
            if "://" in url:
                fn = import_function(url=url)
            else:
                project, name, tag, hash_key = parse_function_uri(url)
                runtime = get_db().get_function(
                    db_session, name, project, tag, hash_key
                )
                if not runtime:
                    log_and_raise(
                        HTTPStatus.BAD_REQUEST,
                        reason="runtime error: function {} not found".format(url),
                    )
                fn = new_function(runtime=runtime)

            if function:
github mlrun / mlrun / mlrun / projects / project.py View on Github external
if with_repo and not project.source:
        raise ValueError('project source must be specified when cloning context')

    in_context = False
    if not url and 'spec' not in f:
        raise ValueError('function missing a url or a spec')

    if url and '://' not in url:
        if project.context and not url.startswith('/'):
            url = path.join(project.context, url)
            in_context = True
        if not path.isfile(url):
            raise OSError('{} not found'.format(url))

    if 'spec' in f:
        func = new_function(name, runtime=f['spec'])
    elif url.endswith('.yaml') or url.startswith('db://') or url.startswith('hub://'):
        func = import_function(url)
        if image:
            func.spec.image = image
    elif url.endswith('.ipynb'):
        func = code_to_function(name, filename=url, image=image, kind=kind)
    elif url.endswith('.py'):
        if not image:
            raise ValueError(
                'image must be provided with py code files, '
                'use function object for more control/settings'
            )
        if in_context and with_repo:
            func = new_function(name, command=url, image=image, kind=kind or 'job')
        else:
            func = code_to_function(name, filename=url, image=image, kind=kind or 'job')
github mlrun / mlrun / mlrun / httpd / functions.py View on Github external
url = data.get('functionUrl')
    if not url:
        return app.json_error(
            HTTPStatus.BAD_REQUEST,
            reason='runtime error: functionUrl not specified',
        )

    project, name, tag = parse_function_uri(url)
    runtime = app.db.get_function(name, project, tag)
    if not runtime:
        return app.json_error(
            HTTPStatus.BAD_REQUEST,
            reason='runtime error: function {} not found'.format(url),
        )

    fn = new_function(runtime=runtime)
    resource = runtime_resources_map.get(fn.kind)
    if 'start' not in resource:
        return app.json_error(
            HTTPStatus.BAD_REQUEST,
            reason='runtime error: "start" not supported by this runtime',
        )

    try:
        fn.set_db_connection(app.db)
        #  resp = resource['start'](fn)  # TODO: handle resp?
        resource['start'](fn)
        fn.save(versioned=False)
        app.logger.info('Fn:\n %s', fn.to_yaml())
    except Exception as err:
        app.logger.error(traceback.format_exc())
        return app.json_error(
github mlrun / mlrun / mlrun / db / httpd.py View on Github external
reason='bad JSON, need to include function/url and task objects',
        )

    # TODO: block exec for function['kind'] in ['', 'local]  (must be a
    # remote/container runtime)

    try:
        if function:
            fn = new_function(runtime=function)
        else:
            if '://' in url:
                fn = import_function(url=url)
            else:
                project, name, tag = parse_function_uri(url)
                runtime = _db.get_function(name, project, tag)
                fn = new_function(runtime=runtime)

        fn.set_db_connection(_db, True)
        # fn.spec.rundb = 'http://mlrun-api:8080'
        resp = fn.run(task)

        logger.info('resp: %s', resp.to_yaml())
    except Exception as err:
        print(traceback.format_exc())
        return json_error(
            HTTPStatus.BAD_REQUEST,
            reason='runtime error: {}'.format(err),
        )

    return jsonify(ok=True, data=resp.to_dict())
github mlrun / mlrun / mlrun / projects / project.py View on Github external
if 'spec' in f:
        func = new_function(name, runtime=f['spec'])
    elif url.endswith('.yaml') or url.startswith('db://') or url.startswith('hub://'):
        func = import_function(url)
        if image:
            func.spec.image = image
    elif url.endswith('.ipynb'):
        func = code_to_function(name, filename=url, image=image, kind=kind)
    elif url.endswith('.py'):
        if not image:
            raise ValueError(
                'image must be provided with py code files, '
                'use function object for more control/settings'
            )
        if in_context and with_repo:
            func = new_function(name, command=url, image=image, kind=kind or 'job')
        else:
            func = code_to_function(name, filename=url, image=image, kind=kind or 'job')
    else:
        raise ValueError('unsupported function url {} or no spec'.format(url))

    if with_repo:
        func.spec.build.source = './'

    return _init_function_from_obj(func, project, name)
github mlrun / mlrun / mlrun / httpd / functions.py View on Github external
def build_function():
    try:
        data = request.get_json(force=True)
    except ValueError:
        return app.json_error(HTTPStatus.BAD_REQUEST, reason='bad JSON body')

    app.logger.info('build_function:\n{}'.format(data))
    function = data.get('function')
    with_mlrun = strtobool(data.get('with_mlrun', 'on'))
    ready = False

    try:
        fn = new_function(runtime=function)
        fn.set_db_connection(app.db)
        fn.save(versioned=False)

        ready = build_runtime(fn, with_mlrun)
        fn.save(versioned=False)
        app.logger.info('Fn:\n %s', fn.to_yaml())
    except Exception as err:
        app.logger.error(traceback.format_exc())
        return app.json_error(
            HTTPStatus.BAD_REQUEST,
            reason='runtime error: {}'.format(err),
        )

    return jsonify(ok=True, data=fn.to_dict(), ready=ready)