Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def submit_job(
request: Request, db_session: Session = Depends(deps.get_db_session)
):
data = None
try:
data = await request.json()
except ValueError:
log_and_raise(HTTPStatus.BAD_REQUEST, reason="bad JSON body")
logger.info("submit_job: {}".format(data))
response = await run_in_threadpool(submit, db_session, data)
return response
def __init__(self, request: Request):
self.username = None
self.password = None
self.token = None
cfg = config.httpdb
header = request.headers.get('Authorization', '')
if self._basic_auth_required(cfg):
if not header.startswith(self._basic_prefix):
log_and_raise(HTTPStatus.UNAUTHORIZED, reason="missing basic auth")
user, password = self._parse_basic_auth(header)
if user != cfg.user or password != cfg.password:
log_and_raise(HTTPStatus.UNAUTHORIZED, reason="bad basic auth")
self.username = user
self.password = password
elif self._bearer_auth_required(cfg):
if not header.startswith(self._bearer_prefix):
log_and_raise(HTTPStatus.UNAUTHORIZED, reason="missing bearer auth")
token = header[len(self._bearer_prefix) :]
if token != cfg.token:
log_and_raise(HTTPStatus.UNAUTHORIZED, reason="bad basic auth")
self.token = token
async def submit_pipeline(
request: Request,
namespace: str = config.namespace,
experiment_name: str = Query("Default", alias="experiment"),
run_name: str = Query("", alias="run"),
):
run_name = run_name or experiment_name + " " + datetime.now().strftime(
"%Y-%m-%d %H-%M-%S"
)
data = await request.body()
if not data:
log_and_raise(HTTPStatus.BAD_REQUEST, reason="post data is empty")
run = await run_in_threadpool(
_submit_pipeline, request, data, namespace, experiment_name, run_name
)
return {
"id": run.id,
"name": run.name,
}
def _get_function_status(data):
logger.info("function_status:\n{}".format(data))
selector = data.get("selector")
kind = data.get("kind")
if not selector or not kind:
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: selector or runtime kind not specified")
resource = runtime_resources_map.get(kind)
if "status" not in resource:
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: 'status' not supported by this runtime")
resp = None
try:
resp = resource["status"](selector)
logger.info("status: %s", resp)
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: {}".format(err))
fn = new_function(runtime=runtime)
resource = runtime_resources_map.get(fn.kind)
if "start" not in resource:
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: 'start' not supported by this runtime")
try:
run_db = get_run_db_instance(db_session)
fn.set_db_connection(run_db)
# resp = resource["start"](fn) # TODO: handle resp?
resource["start"](fn)
fn.save(versioned=False)
logger.info("Fn:\n %s", fn.to_yaml())
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: {}".format(err))
return fn
def _build_function(db_session, function, with_mlrun):
fn = None
ready = None
try:
fn = new_function(runtime=function)
run_db = get_run_db_instance(db_session)
fn.set_db_connection(run_db)
fn.save(versioned=False)
ready = build_runtime(fn, with_mlrun)
fn.save(versioned=False)
logger.info("Fn:\n %s", fn.to_yaml())
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: {}".format(err))
return fn, ready
async def store_artifact(
request: Request,
project: str,
uid: str,
key: str,
tag: str = "",
iter: int = 0,
db_session: Session = Depends(deps.get_db_session),
):
data = None
try:
data = await request.json()
except ValueError:
log_and_raise(HTTPStatus.BAD_REQUEST, reason="bad JSON body")
logger.debug(data)
await run_in_threadpool(
get_db().store_artifact,
db_session,
key,
data,
uid,
iter=iter,
tag=tag,
project=project,
)
return {}
def delete_runtime(
kind: str,
label_selector: str = None,
force: bool = False,
db_session: Session = Depends(deps.get_db_session),
):
if kind not in RuntimeKinds.runtime_with_handlers():
log_and_raise(
status.HTTP_400_BAD_REQUEST, kind=kind, err='Invalid runtime kind'
)
runtime_handler = get_runtime_handler(kind)
runtime_handler.delete_resources(get_db(), db_session, label_selector, force)
return Response(status_code=status.HTTP_204_NO_CONTENT)
secrets = get_secrets(request)
body = None
try:
stores = StoreManager(secrets)
obj = stores.object(url=objpath)
if objpath.endswith("/"):
listdir = obj.listdir()
return {
"listdir": listdir,
}
body = obj.get(size, offset)
except FileNotFoundError as e:
log_and_raise(HTTPStatus.NOT_FOUND, path=objpath, err=str(e))
if body is None:
log_and_raise(HTTPStatus.NOT_FOUND, path=objpath)
ctype, _ = mimetypes.guess_type(objpath)
if not ctype:
ctype = "application/octet-stream"
return Response(
content=body, media_type=ctype, headers={"x-suggested-filename": filename}
)
def get_runtime(kind: str, label_selector: str = None):
if kind not in RuntimeKinds.runtime_with_handlers():
log_and_raise(
status.HTTP_400_BAD_REQUEST, kind=kind, err='Invalid runtime kind'
)
runtime_handler = get_runtime_handler(kind)
resources = runtime_handler.list_resources(label_selector)
return {
'kind': kind,
'resources': resources,
}