def retrieve_from_path(self, path: str) -> Response: directory_items = self.get_directory_tree() # Otherwise, find the file if it exists and return it. path_parts = path.split("/") # TODO: Deal with slashes in file names while len(path_parts) > 0: part = path_parts[0] path_parts = path_parts[1:] if part not in {item["name"] for item in directory_items}: return flask_not_found_error("Nothing found at specified path") try: node = next(item for item in directory_items if item["name"] == part) if "contents" not in node: if len(path_parts) > 0: return flask_bad_request_error( "Cannot retrieve a directory") return send_file(node["path"], mimetype="application/octet-stream", as_attachment=True, download_name=node["name"]) directory_items = node["contents"] except StopIteration: return flask_not_found_error("Nothing found at specified path")
def object_info(object_id: str): drs_bundle = DrsBundle.query.filter_by(id=object_id).first() drs_object = None if not drs_bundle: # Only try hitting the database for an object if no bundle was found drs_object = DrsObject.query.filter_by(id=object_id).first() if not drs_object: return flask_errors.flask_not_found_error( "No object found for this ID") # Are we inside the bento singularity container? if so, provide local accessmethod inside_container = request.headers.get("X-CHORD-Internal", "0") == "1" # Log X-CHORD-Internal header current_app.logger.info( f"[{SERVICE_NAME}] object_info X-CHORD-Internal: {request.headers.get('X-CHORD-Internal', 'not set')}" ) if drs_bundle: response = build_bundle_json(drs_bundle, inside_container=inside_container) else: response = build_object_json(drs_object, inside_container=inside_container) return jsonify(response)
def service_by_id(service_id): services_by_id = {s["id"]: s for s in service_info_cache.values()} if service_id not in services_by_id: return flask_not_found_error( f"Service with ID {service_id} was not found in registry") service_artifact = services_by_id[service_id]["type"].split(":")[1] return get_service(service_artifact)
def notification_read(n_id: uuid.UUID): notification = Notification.query.filter_by(id=str(n_id)).first() if not notification: return flask_not_found_error(f"Notification {n_id} not found") notification.is_read() db.session.commit() return current_app.response_class(status=204)
def run_status(run_id): c = get_db().cursor() c.execute("SELECT * FROM runs WHERE id = ?", (str(run_id),)) run = c.fetchone() if run is None: return flask_not_found_error(f"Run {run_id} not found") return jsonify({ "run_id": run["id"], "state": run["state"] })
def retrieve_from_path(self, path: str) -> Response: try: obj = self.bucket.Object(path) content = obj.get() except ClientError: return flask_not_found_error("Nothing found at specified path") filename = path.split('/')[-1] return send_file( content['Body'], mimetype="application/octet-stream", as_attachment=True, download_name=filename )
def run_cancel(run_id): # TODO: Check if already completed # TODO: Check if run log exists # TODO: from celery.task.control import revoke; revoke(celery_id, terminate=True) db = get_db() c = db.cursor() event_bus = get_flask_event_bus() c.execute("SELECT * FROM runs WHERE id = ?", (str(run_id),)) run = c.fetchone() if run is None: return flask_not_found_error(f"Run {run_id} not found") if run["state"] in (states.STATE_CANCELING, states.STATE_CANCELED): return flask_bad_request_error("Run already canceled") if run["state"] in states.FAILURE_STATES: return flask_bad_request_error("Run already terminated with error") if run["state"] in states.SUCCESS_STATES: return flask_bad_request_error("Run already completed") c.execute("SELECT * FROM run_logs WHERE id = ?", (run["run_log"],)) run_log = c.fetchone() if run_log is None: return flask_internal_server_error(f"No run log present for run {run_id}") if run_log["celery_id"] is None: # Never made it into the queue, so "cancel" it return flask_internal_server_error(f"No Celery ID present for run {run_id}") # TODO: terminate=True might be iffy update_run_state_and_commit(db, c, event_bus, run["id"], states.STATE_CANCELING) celery.control.revoke(run_log["celery_id"], terminate=True) # Remove from queue if there, terminate if running # TODO: wait for revocation / failure and update status... # TODO: Generalize clean-up code / fetch from back-end run_dir = os.path.join(current_app.config["SERVICE_TEMP"], run["run_id"]) shutil.rmtree(run_dir, ignore_errors=True) update_run_state_and_commit(db, c, event_bus, run["id"], states.STATE_CANCELED) return current_app.response_class(status=204) # TODO: Better response
def object_download(object_id): try: drs_object = DrsObject.query.filter_by(id=object_id).one() except NoResultFound: return flask_errors.flask_not_found_error( "No object found for this ID") minio_obj = drs_object.return_minio_object() if not minio_obj: return send_file(drs_object.location) # TODO: kinda greasy, not really sure we want to support such a feature later on response = make_response( send_file(minio_obj["Body"], mimetype="application/octet-stream", as_attachment=True, attachment_filename=drs_object.name)) response.headers["Content-length"] = minio_obj["ContentLength"] return response
def _logs_service_endpoint(log_dict: dict, service: str, log_base_path: str): if not service or service not in log_dict: return flask_not_found_error(f"Could not find service '{service}'") return _log_to_endpoint_value(log_dict[service], log_base_path)
def notification_detail(n_id: uuid.UUID): notification = Notification.query.filter_by(id=str(n_id)).first() return jsonify( notification.serialize) if notification else flask_not_found_error( f"Notification {n_id} not found")
def get_stream(c, stream, run_id): c.execute("SELECT * FROM runs AS r, run_logs AS rl WHERE r.id = ? AND r.run_log = rl.id", (str(run_id),)) run = c.fetchone() return (current_app.response_class(response=run[stream], mimetype="text/plain", status=200) if run is not None else flask_not_found_error(f"Stream {stream} not found for run {run_id}"))
def run_detail(run_id): run_details, err = get_run_details(get_db().cursor(), run_id) return jsonify(run_details) if run_details is not None else flask_not_found_error(f"Run {run_id} not found ({err})")