def create_new_repo(): """ Creates a new repo under the authenticated user. Example HTTP POST Request body (JSON format): { "RepoName": "repo_name", "RepoDescription": "Some description here." } """ # Get parameters # TODO: Sanitize inputs. params = request.get_json() if "RepoName" not in params: return make_error("Missing repo name from request.") if "RepoDescription" not in params: return make_error("Missing repo description from request.") repo_name = params["RepoName"][:20] repo_description = params["RepoDescription"][:80] # TODO: Check repo doesn't already exist. repo_name = re.sub('[^a-zA-Z0-9-]', '-', repo_name) username = current_identity.username repo_id, api_key, token = _create_repo_id_and_api_key_and_token() try: _create_new_repo(username, repo_id, api_key, repo_name, \ repo_description, token, False) except Exception as e: # TODO: Revert things. return make_error("Error creating new repo: " + str(e)) return make_success({"RepoId": repo_id, "ApiKey": api_key})
def download_model(): """ Returns the download url of a model. Example HTTP POST Request body (JSON format): { "RepoId": "test", "SessionId": "c257efa4-791d-4130-bdc6-b0d3cee5fa25", "Round": 5 } """ # Get parameters params = request.get_json() if "RepoId" not in params: return make_error("Missing repo id from request.") if "SessionId" not in params: return make_error("Missing session id from request.") if "Round" not in params: return make_error("Missing round from request.") repo_id = params.get('RepoId', None) session_id = params.get('SessionId', None) round = params.get('Round', None) bucket_name = "updatestore" object_name = "{0}/{1}/{2}/model.h5".format(repo_id, session_id, round) # Get presigned URL username = current_identity.username try: _assert_user_can_read_repo(username, repo_id) url = _create_presigned_url(bucket_name, object_name) except Exception as e: return make_error("Error getting download URL: " + str(e)) # Return url return make_success(url)
def register(): """ Process registration request, assign user a demo repo, and start creating the next demo repo. """ params = request.get_json() if "email" not in params: return make_error("Missing email from request.") if "password1" not in params: return make_error("Missing password from request.") if "password2" not in params: return make_error("Missing password from request.") for arg in ["first_name", "last_name", "company", "occupation"]: params[arg] = params.get(arg, "N/A") try: registration_details = make_auth_call(params, "registration") if "token" not in registration_details: return make_success(registration_details) user_id = registration_details["user"]["pk"] repo_id = _assign_user_repo(user_id) registration_details["demo_repo_id"] = repo_id _create_new_demo_repo_async() while not _user_can_read_repo(user_id, repo_id): time.sleep(0.1) except Exception as e: return make_error("Error setting up demo node: " + str(e)) return make_success(registration_details)
def inner(*args, **kw): all_tokens = User.token_dict() token = request.args.get('token') if not token: return make_error(403, "Acess forbidden") if restrict and not any(all_tokens.get(user)== token for user in restrict): return make_error(403, "Acess forbidden") if not any(all_tokens[user] for user in all_tokens): return make_error(403, "Acess forbidden") return f(*args, **kw)
def clone_repo(self): if self.address: try: git.Git(f"{dir_file}").clone(self.address) except git.GitCommandError as e: self.errors = make_error( 400, 'Problems with the specified GIT repository', e.stderr) else: self.errors = make_error(400, 'Incorrect repo selected ', '')
def inner(*args, **kw): all_tokens = User.token_dict() token = request.args.get('token') if not token: return make_error(403, "Acess forbidden") if restrict and not any( all_tokens.get(user) == token for user in restrict): return make_error(403, "Acess forbidden") if not any(all_tokens[user] for user in all_tokens): return make_error(403, "Acess forbidden") return f(*args, **kw)
def remove_repo(): repo = request.args.get('repo') if repo and repo in REPOSITORIES: remove = subprocess.run(f"rm -rf {dir_file}/{repo}", shell=True, capture_output=True, text=True, timeout=60) if remove.returncode != 0: return make_error(500, f'Remove repo error', remove.stderr) return jsonify(success=True) else: return make_error(400, 'Incorrect repo selected ', '')
def login(): """ Process login request and return results. """ params = request.get_json() if "email" not in params: return make_error("Missing email from request.") if "password" not in params: return make_error("Missing password from request.") try: login_details = make_auth_call(params, "login") except Exception as e: return make_error("Error logging in user: " + str(e)) return make_success(login_details)
def get_all_repos(): """ Returns all repos the user has access to. """ username = current_identity.username try: repo_list = _get_all_repos(username) except Exception as e: return make_error("Error while getting list of repos: " + str(e)) return make_success(repo_list)
def get_repo(repo_id): """ Returns a Repo's information (if the user has access to it). """ username = current_identity.username try: repo_details = _get_repo_details(username, repo_id) except Exception as e: return make_error("Error while getting the details for this repo: " + str(e)) return make_success(repo_details)
def get_logs(repo_id): """ Returns all logs for a Repo the user has access to. """ username = current_identity.username try: _assert_user_can_read_repo(username, repo_id) logs = _get_logs(repo_id) except Exception as e: return make_error("Error getting logs: " + str(e)) return make_success(logs)
def get_user_data(): """ Returns the authenticated user's data. """ username = current_identity.username try: user_data = _get_user_data(username) repos_remaining = user_data['ReposRemaining'] except Exception as e: return make_error("Error getting user data: " + str(e)) return make_success({"ReposRemaining": True if repos_remaining > 0 else False})
def delete_repo(repo_id): """ Deletes a repo under the authenticated user. """ username = current_identity.username try: _delete_repo(username, repo_id) except Exception as e: # TODO: Revert things. return make_error("Error deleting repo: " + str(e)) return make_success(repo_id)
def build(self): if not self.errors: for a in self.actions: action = subprocess.run(f"npm {a}", shell=True, cwd=f'{dir_file}/{self.name}', capture_output=True, text=True, timeout=60) if action.returncode != 0: self.errors = make_error(500, f'Npm {a} error', action.stderr) break
def reset_state(repo_id): """ Authorize request, then reset state for cloud node corresponding to given repo_id """ username = current_identity.username try: repo_details = _get_repo_details(username, repo_id) is_demo = repo_details["IsDemo"] reset_cloud_node(repo_id, is_demo) except Exception as e: return make_error("Error resetting state: " + str(e)) return make_success(repo_id)
def get_task_status(repo_id): """ Returns the status of the Cloud Node associated to a Repo the user has access to. """ username = current_identity.username try: repo_details = _get_repo_details(username, repo_id) task_arns = [repo_details["CloudTaskArn"], repo_details["ExploraTaskArn"]] is_demo = repo_details["IsDemo"] status = get_status(task_arns, repo_id, is_demo) except Exception as e: return make_error("Error checking status: " + str(e)) return make_success(status)
def get_all_repos(): """ Returns all repos the user has access to. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: repo_list = _get_all_repos(user_id) except Exception as e: return make_error("Error while getting list of repos: " + str(e)) return make_success(repo_list)
def reset_state(repo_id): """ Authorize request, then reset state for cloud node corresponding to given repo_id """ claims = authorize_user(request) if claims is None: return make_unauthorized_error() user_id = claims["pk"] try: repo_details = _get_repo_details(user_id, repo_id) is_demo = repo_details["IsDemo"] reset_cloud_node(repo_id, is_demo) except Exception as e: return make_error("Error resetting state: " + str(e)) return make_success(repo_id)
def get_logs(repo_id): """ Returns all logs for a Repo the user has access to. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: _assert_user_can_read_repo(user_id, repo_id) logs = _get_logs(repo_id) except Exception as e: return make_error("Error getting logs: " + str(e)) return make_success(logs)
def delete_repo(repo_id): """ Deletes a repo under the authenticated user. """ # Check authorization claims = authorize_user(request) if not claims: return make_unauthorized_error() user_id = claims["pk"] try: _delete_repo(user_id, repo_id) except Exception as e: # TODO: Revert things. return make_error("Error deleting repo: " + str(e)) return make_success(repo_id)
def get_repo(repo_id): """ Returns a Repo's information (if the user has access to it). """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: repo_details = _get_repo_details(user_id, repo_id) except Exception as e: return make_error("Error while getting the details for this repo: " + str(e)) return make_success(repo_details)
def get_user_data(): """ Returns the authenticated user's data. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: user_data = _get_user_data(user_id) repos_remaining = user_data['ReposRemaining'] except Exception as e: return make_error("Error getting user data: " + str(e)) return make_success( {"ReposRemaining": True if repos_remaining > 0 else False})
def get_task_status(repo_id): """ Returns the status of the Cloud Node associated to a Repo the user has access to. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: repo_details = _get_repo_details(user_id, repo_id) task_arns = [ repo_details["CloudTaskArn"], repo_details["ExploraTaskArn"] ] is_demo = repo_details["IsDemo"] status = get_status(task_arns, repo_id, is_demo) except Exception as e: return make_error("Error checking status: " + str(e)) return make_success(status)
def miss(e): return make_error(msg='Not Found'), 404
def error(e): return make_error(msg='服务器异常'), 500
def bad(e): return make_error(msg='Bad Request'), 400
def not_allowed(e): return make_error(msg='Method Not Allowed'), 405