async def update_settings(self, repository_url, repository_branch, ssh_private_key, *ignored): app_id = self.context.get("app_id") environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources builds = self.application.builds try: await environment_client.get_app_info(app_id) except AppNotFound as e: raise a.ActionError("App was not found.") if not (await builds.validate_repository_url(repository_url, ssh_private_key)): raise a.ActionError( "Error: \"{0}\" is not a valid Git repository URL, or " "the repository does not exist, or the ssh key is wrong.". format(repository_url)) await sources.update_project(self.gamespace, app_id, repository_url, repository_branch, ssh_private_key) raise a.Redirect("app_settings", message="Application settings have been updated.", app_id=app_id)
async def switch_commit(self, commit): sources = self.application.sources builds = self.application.builds try: project_settings = await sources.get_server_project(self.gamespace) except NoSuchProjectError as e: raise a.Redirect("server_settings", message="Please define project settings first") try: project = builds.get_server_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError: raise a.ActionError("Repository has not updated itself yet.") try: commit_exists = await project.check_commit(commit) except SourceCodeError as e: raise a.ActionError(e.message) if not commit_exists: raise a.ActionError("No such commit") try: await sources.update_server_commit(self.gamespace, commit) except SourceCodeError as e: raise a.ActionError(e.message) raise a.Redirect("server", message="Server code commit has been updated")
async def get(self, blog_id, clone=None): try: blog = await self.application.blogs.get_blog( self.gamespace, blog_id) except BlogNotFound: raise a.ActionError("No such blog") if clone is None: enabled = "true" data = {} else: try: entry = await self.application.blogs.get_blog_entry( self.gamespace, blog_id, clone) except BlogNotFound: raise a.ActionError("No such blog") else: enabled = "true" if entry.enabled else "false" data = entry.data return { "blog_name": blog.name, "blog_schema": blog.schema, "enabled": enabled, "data": data }
async def detach_version(self): app_id = self.context.get("app_id") app_version = self.context.get("app_version") environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources builds = self.application.builds try: await environment_client.get_app_info(app_id) except AppNotFound: raise a.ActionError("App was not found.") try: deleted = await sources.delete_commit(self.gamespace, app_id, app_version) except SourceCodeError as e: raise a.ActionError(e.message) if deleted: raise a.Redirect("app_version", message="Version has been disabled", app_id=app_id, app_version=app_version) raise a.Redirect("app_version", message="Version was already disabled", app_id=app_id, app_version=app_version)
async def pull_updates(self): sources = self.application.sources builds = self.application.builds try: project_settings = await sources.get_server_project(self.gamespace) except NoSuchProjectError: raise a.Redirect("server_settings", message="Please define project settings first") try: project = builds.get_server_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError: raise a.ActionError("Repository has not updated itself yet.") try: pulled = await project.pull() except SourceCodeError as e: raise a.ActionError(e.message) if not pulled: raise a.ActionError("Failed to pull updates") raise a.Redirect("server", message="Updates has been pulled.")
async def get(self, app_id, app_version): environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources builds = self.application.builds try: app = await environment_client.get_app_info(app_id) except AppNotFound: raise a.ActionError("App was not found.") try: project_settings = await sources.get_project( self.gamespace, app_id) except NoSuchProjectError as e: raise a.Redirect("app_settings", message="Please define project settings first", app_id=app_id) try: commit = await sources.get_version_commit(self.gamespace, app_id, app_version) except NoSuchSourceError: current_commit = None else: current_commit = commit.repository_commit try: project = builds.get_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError as e: commits_history = None else: try: commits_history = await project.get_commits_history(50) except SourceCodeError as e: raise a.ActionError(e.message) result = { "app_id": app_id, "app_record_id": app.id, "app_name": app.title, "versions": app.versions, "current_commit": current_commit, "commits_history": commits_history } return result
async def get(self, blog_id, page=1): try: blog = await self.application.blogs.get_blog( self.gamespace, blog_id) except BlogNotFound: raise a.ActionError("No such blog") try: pages, entries = await self.application.blogs.list_blog_entries_pages( self.gamespace, blog_id, BlogAdminController.ENTRIES_PER_PAGE, page) except BlogError as e: raise a.ActionError(e.args[0]) return {"blog_name": blog.name, "pages": pages, "entries": entries}
async def get(self, app_id): environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources try: app = await environment_client.get_app_info(app_id) except AppNotFound: raise a.ActionError("App was not found.") try: await sources.get_project(self.gamespace, app_id) except NoSuchProjectError: has_no_settings = True commits = {} else: has_no_settings = False try: commits = await sources.list_versions(self.gamespace, app_id) except SourceCodeError: commits = {} result = { "app_id": app_id, "app_record_id": app.id, "app_name": app.title, "versions": app.versions, "commits": commits, "has_no_settings": has_no_settings } return result
async def get(self, app_id): environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources try: app = await environment_client.get_app_info(app_id) except AppNotFound as e: raise a.ActionError("App was not found.") try: project = await sources.get_project(self.gamespace, app_id) except NoSuchProjectError as e: repository_url = "" ssh_private_key = "" repository_branch = SourceCodeRoot.DEFAULT_BRANCH else: repository_url = project.repository_url repository_branch = project.repository_branch ssh_private_key = project.ssh_private_key result = { "app_id": app_id, "app_record_id": app.id, "app_name": app.title, "repository_url": repository_url, "repository_branch": repository_branch, "ssh_private_key": ssh_private_key } return result
async def get(self): try: settings = await self.application.deployment_settings.get_settings(self.gamespace) except NoSuchSettingsError: deployment_method = "" deployment_data = {} except SettingsError as e: raise a.ActionError(e.message) else: deployment_method = settings.deployment_method deployment_data = settings.deployment_data deployment_methods = {t: t for t in DeploymentMethods.types()} if not deployment_method: deployment_methods[""] = "< SELECT >" result = { "deployment_methods": deployment_methods, "deployment_method": deployment_method, "deployment_data": deployment_data } return result
async def get(self): try: blogs = await self.application.blogs.list_blogs(self.gamespace) except BlogError as e: raise a.ActionError(e.args[0]) return {"blogs": blogs}
async def get(self): sources = self.application.sources builds = self.application.builds try: project_settings = await sources.get_server_project(self.gamespace) except NoSuchProjectError: raise a.Redirect("server_settings", message="Please define project settings first") try: commit = await sources.get_server_commit(self.gamespace) except NoSuchSourceError: current_commit = None else: current_commit = commit.repository_commit server_fetch_error = None try: project = builds.get_server_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError: commits_history = None except SourceCodeError as e: commits_history = None server_fetch_error = str(e.message) except Exception as e: commits_history = None server_fetch_error = str(e) else: try: commits_history = await project.get_commits_history(50) except SourceCodeError as e: raise a.ActionError(e.message) result = { "current_commit": current_commit, "commits_history": commits_history, "server_fetch_error": server_fetch_error, } return result
async def update(self, data, enabled=False, **ignored): blog_id = self.context.get("blog_id") entry_id = self.context.get("entry_id") try: await self.application.blogs.get_blog(self.gamespace, blog_id) except BlogNotFound: raise a.ActionError("No such blog") try: await self.application.blogs.update_blog_entry( self.gamespace, blog_id, entry_id, data, enabled) except BlogError as e: raise a.ActionError(e.args[0]) raise a.Redirect("blog", message="Blog entry has been updated", blog_id=blog_id)
async def delete(self): blog_id = self.context.get("blog_id") try: await self.application.blogs.delete_blog(self.gamespace, blog_id) except BlogError: raise a.ActionError("Failed to delete blog") raise a.Redirect("index", message="Blog has been deleted")
async def pull_updates(self): app_id = self.context.get("app_id") app_version = self.context.get("app_version") environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources builds = self.application.builds try: await environment_client.get_app_info(app_id) except AppNotFound: raise a.ActionError("App was not found.") try: project_settings = await sources.get_project( self.gamespace, app_id) except NoSuchProjectError as e: raise a.Redirect("app_settings", message="Please define project settings first", app_id=app_id) try: project = builds.get_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError: raise a.ActionError("Repository has not updated itself yet.") try: pulled = await project.pull() except SourceCodeError as e: raise a.ActionError(e.message) if not pulled: raise a.ActionError("Failed to pull updates") raise a.Redirect("app_version", message="Updates has been pulled.", app_id=app_id, app_version=app_version)
async def get(self, blog_id, entry_id): try: blog = await self.application.blogs.get_blog( self.gamespace, blog_id) except BlogNotFound: raise a.ActionError("No such blog") try: entry = await self.application.blogs.get_blog_entry( self.gamespace, blog_id, entry_id) except BlogNotFound: raise a.ActionError("No such blog") return { "blog_name": blog.name, "blog_schema": blog.schema, "data": entry.data, "enabled": "true" if entry.enabled else "false", }
async def create(self, name, schema, enabled): try: blog_id = await self.application.blogs.create_blog( self.gamespace, name, schema, enabled) except BlogError as e: raise a.ActionError(e.args[0]) raise a.Redirect("blog", message="New blog has been created", blog_id=blog_id)
async def update_deployment_method(self, deployment_method): if not DeploymentMethods.valid(deployment_method): raise a.ActionError("Not a valid deployment method") try: stt = await self.application.deployment_settings.get_settings(self.gamespace) except NoSuchSettingsError: deployment_data = {} else: deployment_data = stt.deployment_data try: await self.application.deployment_settings.update_settings( self.gamespace, deployment_method, deployment_data) except SettingsError as e: raise a.ActionError(e.message) raise a.Redirect("settings", message="Deployment method has been updated")
async def create(self, data, enabled=False, **ignored): blog_id = self.context.get("blog_id") try: entry_id = await self.application.blogs.create_blog_entry( self.gamespace, blog_id, data, enabled) except BlogError as e: raise a.ActionError(e.args[0]) raise a.Redirect("blog", message="New blog entry has been created", blog_id=blog_id)
async def get(self, blog_id): try: blog = await self.application.blogs.get_blog( self.gamespace, blog_id) except BlogNotFound: raise a.ActionError("No such blog") return { "name": blog.name, "schema": blog.schema, "enabled": "true" if blog.enabled else "false" }
async def update(self, name, schema, enabled=False, **ignored): blog_id = self.context.get("blog_id") try: await self.application.blogs.update_blog(self.gamespace, blog_id, name, schema, enabled) except BlogError: raise a.ActionError("Failed to update blog") raise a.Redirect("blog_settings", message="Blog has been updated", blog_id=blog_id)
async def detach_version(self): sources = self.application.sources try: deleted = await sources.delete_server_commit(self.gamespace) except SourceCodeError as e: raise a.ActionError(e.message) if deleted: raise a.Redirect("server", message="Server code has been disabled") raise a.Redirect("server", message="Server code was already disabled")
async def switch_to_latest_commit(self): sources = self.application.sources builds = self.application.builds try: project_settings = await sources.get_server_project(self.gamespace) except NoSuchProjectError: raise a.Redirect("server_settings", message="Please define project settings first") try: project = builds.get_server_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError: raise a.ActionError("Repository has not updated itself yet.") try: latest_commit = await project.pull_and_get_latest_commit() except SourceCodeError as e: raise a.ActionError(e.message) if not latest_commit: raise a.ActionError("Failed to check the latest commit") try: updated = await sources.update_server_commit( self.gamespace, latest_commit) except SourceCodeError as e: raise a.ActionError(e.message) if updated: raise a.Redirect("server", message="Server Code has been updated") raise a.Redirect("server", message="Already up-to-date.")
async def update_deployment(self, **kwargs): try: settings = await self.application.deployment_settings.get_settings(self.gamespace) except NoSuchSettingsError: raise a.ActionError("Please select deployment method first") except SettingsError as e: raise a.ActionError(e.message) else: deployment_method = settings.deployment_method deployment_data = settings.deployment_data m = DeploymentMethods.get(deployment_method)() m.load(deployment_data) await m.update(**kwargs) try: await self.application.deployment_settings.update_settings( self.gamespace, deployment_method, m.dump()) except SettingsError as e: raise a.ActionError(e.message) raise a.Redirect("settings", message="Deployment settings have been updated")
async def switch_to_latest_commit(self): app_id = self.context.get("app_id") app_version = self.context.get("app_version") environment_client = EnvironmentClient(self.application.cache) sources = self.application.sources builds = self.application.builds try: await environment_client.get_app_info(app_id) except AppNotFound: raise a.ActionError("App was not found.") try: project_settings = await sources.get_project( self.gamespace, app_id) except NoSuchProjectError as e: raise a.Redirect("app_settings", message="Please define project settings first", app_id=app_id) try: project = builds.get_project(project_settings) await with_timeout(timedelta(seconds=10), project.init()) except JavascriptBuildError as e: raise a.ActionError(e.message) except TimeoutError: raise a.ActionError("Repository has not updated itself yet.") try: latest_commit = await project.pull_and_get_latest_commit() except SourceCodeError as e: raise a.ActionError(e.message) if not latest_commit: raise a.ActionError("Failed to check the latest commit") try: updated = await sources.update_commit(self.gamespace, app_id, app_version, latest_commit) except SourceCodeError as e: raise a.ActionError(e.message) if updated: raise a.Redirect("app_version", message="Version has been updated", app_id=app_id, app_version=app_version) raise a.Redirect("app_version", message="Already up-to-date.", app_id=app_id, app_version=app_version)
async def update_settings(self, repository_url, repository_branch, ssh_private_key, *ignored): sources = self.application.sources builds = self.application.builds if not (await builds.validate_repository_url(repository_url, ssh_private_key)): raise a.ActionError( "Error: \"{0}\" is not a valid Git repository URL, or " "the repository does not exist, or the ssh key is wrong.". format(repository_url)) await sources.update_server_project(self.gamespace, repository_url, repository_branch, ssh_private_key) raise a.Redirect("server", message="Server Code settings have been updated.")
async def get(self, report_id, download=False): environment_client = EnvironmentClient(self.application.cache) reports = self.application.reports try: report = await reports.get_report(self.gamespace, report_id) except ReportError as e: raise a.ActionError(e) app_name = report.application_name app_version = report.application_version if download: if report.format == ReportFormat.TEXT: ext = ".txt" elif report.format == ReportFormat.JSON: ext = ".json" else: ext = ".data" raise a.BinaryFile(report.payload, name="report_" + str(report.report_id) + "_" + str(app_name) + "_" + str(app_version) + ext) try: app = await environment_client.get_app_info(app_name) except AppNotFound as e: app_title = app_name else: app_title = app.title return { "account_id": report.account_id, "app_name": app_name, "app_version": app_version, "app_title": app_title, "category": report.category, "message": report.message, "info": report.info, "time": str(report.time), "format": report.format, "format_title": str(report.format).upper(), "payload": report.payload }
async def get(self, app_name): environment_client = EnvironmentClient(self.application.cache) try: app = await environment_client.get_app_info(app_name) except AppNotFound as e: raise a.ActionError("App was not found.") app_versions = list(app.versions.keys()) app_versions.sort() result = { "app_name": app_name, "app_title": app.title, "app_record_id": app.id, "versions": app_versions } return result
async def get(self, app_name, app_version, page=1, info=None, account_id=None, report_message=None, category=None, export=False): environment_client = EnvironmentClient(self.application.cache) reports = self.application.reports try: app = await environment_client.get_app_info(app_name) except AppNotFound: raise a.ActionError("App was not found.") versions = app.versions if app_version not in versions: raise a.ActionError("No such app version") query = reports.reports_query(self.gamespace, app_name, app_version) if export: query.offset = 0 query.limit = 10000 query.include_payload = True else: query.offset = (int(page) - 1) * ApplicationVersionController.REPORTS_PER_PAGE query.limit = ApplicationVersionController.REPORTS_PER_PAGE if account_id: query.account_id = str(account_id) if report_message: query.message = str(report_message) if category: query.category = str(category) if info: try: info = ujson.loads(info) except (KeyError, ValueError): raise a.ActionError("Corrupted info") try: cond = format_conditions_json('report_info', info) except ConditionError as e: raise a.ActionError(str(e)) query.add_conditions(cond) else: info = {} if export: reports = await query.query(one=False, count=False) output = StringIO() data = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC) data.writerow([ "Report ID", "Category", "Message", "Sender", "Info", "Time", "Format", "Contents" ]) for report in reports: if report.format == ReportFormat.BINARY: contents = base64.b64encode(report.payload) else: contents = str(report.payload) data.writerow([ str(report.report_id), str(report.category), str(report.message.encode("ascii", "ignore")), str(report.account_id), str(ujson.dumps(report.info)), str(report.time), str(report.format), contents ]) raise a.BinaryFile(output.getvalue(), "reports.csv") reports, count = await query.query(one=False, count=True) pages = int( math.ceil( float(count) / float(ApplicationVersionController.REPORTS_PER_PAGE))) return { "app_name": app_name, "app_title": app.title, "app_record_id": app.id, "reports": reports, "account_id": account_id, "report_message": report_message, "category": category, "info": info, "pages_count": pages, "total_count": count }