def modify_or_delete_users(): messages = [] data = [] try: validate_local_login_enabled() data_req = request.get_json() access_token = data_req["access_token"] login_required(access_token=access_token, admin=True) action = data_req["action"] user_selection = data_req["user_selection"] value = data_req["value"] if action == "delete": for username in user_selection: user_manager.delete(username) messages.append({ "time": get_time_string(), "type": "success", "text": "Successfully deleted users: \"" + ", ".join(user_selection) + "\"" }) if action == "set_status": for user in user_selection: change_user_status_or_level(get_user_by_username(user).id, new_status=value) messages.append({ "time": get_time_string(), "type": "success", "text": "Successfully set status on users: \"" + ", ".join(user_selection) + "\"" }) if action == "set_level": for user in user_selection: change_user_status_or_level(get_user_by_username(user).id, new_level=value) messages.append({ "time": get_time_string(), "type": "success", "text": "Successfully set level on users: \"" + ", ".join(user_selection) + "\"" }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})
def import_wf_by_trs_uri(): messages = [] data = [] try: data_req = request.get_json() access_token = data_req["access_token"] login_required(access_token=access_token) trs_uri = data_req["trs_uri"] import_name = data_req["import_name"] import_worflow_by_trs(uri=trs_uri, name=import_name, access_token=access_token) messages.append({ "time": get_time_string(), "type": "success", "text": import_name + " successfully imported." }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})
def register(): messages = [] data = {"success": False} try: validate_local_login_enabled() data_req = request.get_json() username = data_req["username"] email = data_req["email"] password = data_req["password"] rep_password = data_req["rep_password"] db_connector.user_manager.create(username=username, email=email, password=password, rep_password=rep_password, level="user", status="approval_needed") messages.append({ "time": get_time_string(), "type": "success", "text": "Successfully send. An administrator will need to approve your account." }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})
def change_password(): messages = [] data = {"success": False} try: validate_local_login_enabled() data_req = request.get_json() access_token = data_req["access_token"] username = data_req["username"] old_password = data_req["old_password"] new_password = data_req["new_password"] new_rep_password = data_req["new_rep_password"] login_required(access_token=access_token, username=username) db_connector.user_manager.change_password(username, old_password, new_password, new_rep_password) data = {"success": True} messages.append({ "time": get_time_string(), "type": "success", "text": "Successfully changed password. You will be logged out." }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})
def start_exec(): # returns all parmeter and its default mode (global/job specific) # for a given xls config messages = [] try: data_req = request.get_json() access_token = data_req["access_token"] username = data_req["username"] login_required(access_token=access_token, username=username) access_token = data_req["access_token"] job_name = data_req["job_name"] run_names = sorted(data_req["run_names"]) exec_profile_name = data_req["exec_profile"] max_parrallel_exec_user_def = int(data_req["parallel_exec"]) if "parallel_exec" in data_req.keys() else None started_runs, already_running_runs = exec_runs( job_name, run_names, exec_profile_name, username=username, max_parrallel_exec_user_def=max_parrallel_exec_user_def, access_token=access_token ) if len(started_runs) > 0: messages.append({ "time": get_time_string(), "type":"success", "text":"Successfully started execution for runs: " + ", ".join(started_runs) }) if len(already_running_runs) > 0: messages.append({ "time": get_time_string(), "type":"warning", "text":"Following runs are already running or have already finished: " + ", ".join(already_running_runs) + ". To restart them, reset them first." }) except AssertionError as e: messages.append( handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({ "data":{}, "messages":messages })
def upload_file(): messages = [] data={} try: # load metadata: metadata = json_loads(request.form.get("meta")) dir_path = metadata["dir_path"] job_name = metadata["job_name"] if "job_name" in metadata.keys() else None access_token = metadata["access_token"] login_required(access_token=access_token) assert 'file' in request.files, 'No file received.' import_file = request.files['file'] assert import_file.filename != '', "No file specified." filename = secure_filename(import_file.filename) # check if dir path allowed: allowed_dirs = get_allowed_base_dirs( job_name=job_name, allow_input=False, allow_upload=True, allow_download=False ) assert dir_path != "", "Path does not exist or you have no permission to enter it." dir_path = normalize_path(dir_path) assert os.path.exists(dir_path) and \ os.path.isdir(dir_path) and \ check_if_path_in_dirs(dir_path, allowed_dirs) is not None, \ "Path does not exist or you have no permission to enter it." import_filepath = os.path.join(dir_path, filename) import_file.save(import_filepath) data["file_path"] = import_filepath messages.append( { "time": get_time_string(), "type":"success", "text": "Successfully uploaded file." } ) except AssertionError as e: messages.append( handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({ "data": data, "messages": messages } )
def upload_cwl_zip(): messages = [] data = {} try: metadata = json_loads(request.form.get("meta")) access_token = metadata["access_token"] login_required(access_token=access_token) assert 'file' in request.files, 'No file received.' import_wf_file = request.files['file'] assert import_wf_file.filename != '', "No file specified." assert is_allowed_file(import_wf_file.filename, type="zip"), ( "Wrong file type. Only files with following extensions are allowed: " + ", ".join(allowed_extensions_by_type["CWL"])) # save the file to the CWL directory: import_wf_filename = secure_filename(import_wf_file.filename) temp_upload_dir = make_temp_dir() imported_filepath = os.path.join(temp_upload_dir, import_wf_filename) import_wf_file.save(imported_filepath) temp_extract_dir = make_temp_dir() unzip_dir(imported_filepath, temp_extract_dir) try: rmtree(temp_upload_dir) except Exception as e: pass data["temp_dir"] = temp_extract_dir messages.append({ "time": get_time_string(), "type": "success", "text": import_wf_file.filename + " was successfully uploaded and extracted." }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})
def delete_job(): messages = [] data = {} try: data_req = request.get_json() access_token = data_req["access_token"] login_required(access_token=access_token) job_name = data_req["job_name"] results = delete_job_by_name(job_name) if results["status"] == "success": pass elif results["status"] == "failed run termination": if len(results["could_not_be_terminated"]) > 0: messages.append({ "time": get_time_string(), "type":"error", "text":"Following runs could not be terminated: " + ", ".join(results["could_not_be_terminated"]) }) if len(results["could_not_be_cleaned"]) > 0: messages.append({ "time": get_time_string(), "type":"error", "text":"Following runs could not be cleaned: " + ", ".join(results["could_not_be_cleaned"]) }) else: messages.append({ "time": get_time_string(), "type":"error", "text":"Could not delete job dir for \"" + job_name + "\"." }) except AssertionError as e: messages.append( handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({ "data":data, "messages":messages })
def terminate_runs(): messages = [] data = {} try: data_req = request.get_json() access_token = data_req["access_token"] login_required(access_token=access_token) job_name = data_req["job_name"] run_names = sorted(data_req["run_names"]) mode = data_req["mode"] # one of terminate, reset, delete succeeded, could_not_be_terminated, could_not_be_cleaned = terminate_runs_by_name(job_name, run_names, mode) if len(succeeded) > 0: messages.append({ "time": get_time_string(), "type":"success", "text":"Successfully terminated/reset/deleted runs: " + ", ".join(succeeded) }) if len(could_not_be_terminated) > 0: messages.append({ "time": get_time_string(), "type":"warning", "text":"Following runs could not be terminated: " + ", ".join(could_not_be_terminated) }) if len(could_not_be_cleaned) > 0: messages.append({ "time": get_time_string(), "type":"warning", "text":"Following runs could not be cleaned: " + ", ".join(could_not_be_cleaned) }) except AssertionError as e: messages.append( handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({ "data":data, "messages":messages })
def import_wf_by_path_or_url(): messages = [] data = [] try: data_req = request.get_json() access_token = data_req["access_token"] login_required(access_token=access_token) wf_path = data_req["wf_path"] is_url = data_req["is_url"] if "is_url" in data_req.keys() else None import_name = data_req["import_name"] wf_type = data_req["wf_type"] if "wf_type" in data_req.keys() else None if is_url: validate_url(wf_path) else: allowed_dirs = get_allowed_base_dirs(allow_input=True, allow_upload=False, allow_download=False, include_tmp_dir=False) assert os.path.isfile(wf_path) and \ check_if_path_in_dirs(wf_path, allowed_dirs) is not None, \ f"Path does not exist or you have no permission to enter it.{allowed_dirs}" import_wf_(wf_path=wf_path, name=import_name, wf_type=wf_type) messages.append({ "time": get_time_string(), "type": "success", "text": import_name + " successfully imported." }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})
def upload_wf(): messages = [] data = [] try: metadata = json_loads(request.form.get("meta")) access_token = metadata["access_token"] login_required(access_token=access_token) # load metadata: wf_type = metadata["wf_type"] if "wf_type" in metadata.keys() else None # only relavant for janis: translate_to_cwl = metadata["translate_to_cwl"] \ if "translate_to_cwl" in metadata.keys() else True translate_to_wdl = metadata["translate_to_wdl"] \ if "translate_to_wdl" in metadata.keys() else True wf_name_in_script = metadata["wf_name_in_script"] \ if "wf_name_in_script" in metadata.keys() else None # save the file to the CWL directory: assert 'wf_file' in request.files, 'No file received.' import_wf_file = request.files['wf_file'] assert import_wf_file.filename != '', "No file specified." import_wf_filename = secure_filename(import_wf_file.filename) temp_dir = make_temp_dir() imported_wf_filepath = os.path.join(temp_dir, import_wf_filename) import_wf_file.save(imported_wf_filepath) # if existent, save imports.zip: wf_imports_zip_filepath = None if 'wf_imports_zip' in request.files.keys(): wf_imports_zip_file = request.files['wf_imports_zip'] wf_imports_zip_filepath = os.path.join(temp_dir, "imports.zip") wf_imports_zip_file.save(wf_imports_zip_filepath) # import workflow: import_name = secure_filename(metadata["import_name"]) \ if "import_name" in metadata.keys() and metadata["import_name"] != "" \ else import_wf_filename import_wf_(wf_path=imported_wf_filepath, name=os.path.splitext(import_name)[0], wf_type=wf_type, wf_imports_zip_path=wf_imports_zip_filepath, translate_to_cwl=translate_to_cwl, translate_to_wdl=translate_to_wdl, wf_name_in_script=wf_name_in_script) # cleanup temp: try: rmtree(temp_dir) except Exception as e: pass messages.append({ "time": get_time_string(), "type": "success", "text": import_wf_file.filename + " successfully imported." }) except AssertionError as e: messages.append(handle_known_error(e, return_front_end_message=True)) except Exception as e: messages.append(handle_unknown_error(e, return_front_end_message=True)) return jsonify({"data": data, "messages": messages})