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 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 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 lan_connect(): target_id = request.values.get('id') find: ModelServer = ModelServer.get_or_none(id=target_id) if find is None: return make_faild(msg='操作失败') service_lan.start_lan_play(ip=find.host) return make_success()
def server_remove(): target_id = request.values['id'] find: ModelServer = ModelServer.get_or_none(id=target_id) if find is None: return make_faild(msg='操作失败') if find.host == service_lan.lan_address: return make_faild(msg='删除失败,服务器正在连接中') find.delete_instance() return make_success()
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_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_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 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 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 server_add(): name = request.values.get('name') host = request.values['host'].replace(' ', '') if name is None or len(name) == 0: name = host find = ModelServer.get_or_none(ModelServer.host == host) if find is not None: return make_faild(msg='该服务器已存在', data=find) find = ModelServer.create(host=host, name=name) return make_success(msg='添加服务器成功', data=find)
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 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_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 lan_info(): runing = service_lan.runing() address = service_lan.lan_address update_timestamp = service_lan.update_timestamp server_info = None if runing: find = ModelServer.get_or_none( ModelServer.host == service_lan.lan_address ) if find is not None: server_info = find.dict() return make_success(data={ 'runing': runing, 'address': address, 'update_timestamp': update_timestamp, 'server': server_info })
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 server_refresh(): find_all = ModelServer.select().order_by(-ModelServer.id) for item in find_all: try: uri = format(item.host) if uri.startswith('http') == False: uri = 'http://' + uri response = requests.get( '{0}/info'.format(uri), timeout=(0.5, 2), ) if response.status_code != 200: item.version = '错误: {0}'.format(response.status_code) item.save() continue data = response.json() item.online = data['online'] item.version = data['version'] item.ping = int(response.elapsed.total_seconds() * 1000) except requests.exceptions.ReadTimeout as e: item.version = '服务器响应超时' item.ping = -1 except requests.exceptions.ConnectTimeout as e: item.version = '连接到服务器超时' item.ping = -1 except requests.exceptions.ConnectionError: item.version = '地址访问失败' item.ping = -1 except: item.version = '服务器访问失败' item.ping = -1 item.save() result = list(map(lambda v: v.dict(), find_all)) return make_success(data=result)
def server_list(): find_all = ModelServer.select().execute() result = list(map(lambda v: v.dict(), find_all)) return make_success(data=result)
def lan_disconnect(): service_lan.stop_lan_play() return make_success(msg='已断开与服务器的连接')
def hello_world(): return make_success()