-
Notifications
You must be signed in to change notification settings - Fork 1
Replace telnet shell with http api #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| - Replace prettytable with rich | ||
|
|
||
| - Replace telnet shell with HTTP API | ||
|
|
||
| - Migrate `backy check` to `backy client check` and use the new HTTP API |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import datetime | ||
| import re | ||
| from json import JSONEncoder | ||
| from typing import Any, List, Tuple | ||
|
|
||
| from aiohttp import hdrs, web | ||
| from aiohttp.web_exceptions import HTTPAccepted, HTTPNotFound, HTTPUnauthorized | ||
| from aiohttp.web_middlewares import middleware | ||
| from aiohttp.web_runner import AppRunner, TCPSite | ||
| from structlog.stdlib import BoundLogger | ||
|
|
||
| import backy.daemon | ||
|
|
||
|
|
||
| class BackyJSONEncoder(JSONEncoder): | ||
| def default(self, o: Any) -> Any: | ||
| if hasattr(o, "to_dict"): | ||
| return o.to_dict() | ||
| elif isinstance(o, datetime.datetime): | ||
| return o.isoformat() | ||
| else: | ||
| super().default(o) | ||
|
|
||
|
|
||
| class BackyAPI: | ||
| daemon: "backy.daemon.BackyDaemon" | ||
| sites: dict[Tuple[str, int], TCPSite] | ||
| runner: AppRunner | ||
| tokens: dict | ||
| log: BoundLogger | ||
|
|
||
| def __init__(self, daemon, log): | ||
| self.log = log.bind(subsystem="api") | ||
| self.daemon = daemon | ||
| self.sites = {} | ||
| self.app = web.Application( | ||
| middlewares=[self.log_conn, self.require_auth, self.to_json] | ||
| ) | ||
| self.app.add_routes( | ||
| [ | ||
| web.get("/v1/status", self.get_status), | ||
| web.post("/v1/reload", self.reload_daemon), | ||
| web.get("/v1/jobs", self.get_jobs), | ||
| web.post("/v1/jobs/{job_name}/run", self.run_job), | ||
| ] | ||
| ) | ||
|
|
||
| async def start(self): | ||
| self.runner = AppRunner(self.app) | ||
| await self.runner.setup() | ||
|
|
||
| async def stop(self): | ||
| await self.runner.cleanup() | ||
| self.sites = {} | ||
|
|
||
| async def reconfigure( | ||
| self, tokens: dict[str, str], addrs: List[str], port: int | ||
| ): | ||
| self.log.debug("reconfigure") | ||
| self.tokens = tokens | ||
| bind_addrs = [(addr, port) for addr in addrs if addr and port] | ||
| for bind_addr in bind_addrs: | ||
| if bind_addr in self.sites: | ||
| continue | ||
| self.sites[bind_addr] = site = TCPSite( | ||
| self.runner, bind_addr[0], bind_addr[1] | ||
| ) | ||
| await site.start() | ||
| self.log.info("added-site", site=site.name) | ||
| for bind_addr, site in self.sites.items(): | ||
| if bind_addr in bind_addrs: | ||
| continue | ||
| await site.stop() | ||
| del self.sites[bind_addr] | ||
| self.log.info("deleted-site", site=site.name) | ||
|
|
||
| @middleware | ||
| async def log_conn(self, request: web.Request, handler): | ||
| request["log"] = self.log.bind( | ||
| path=request.path, query=request.query_string | ||
| ) | ||
| request["log"].debug("new-conn") | ||
| try: | ||
| resp = await handler(request) | ||
| except Exception as e: | ||
| if not isinstance(e, web.HTTPException): | ||
| request["log"].exception("error-handling-request") | ||
| else: | ||
| request["log"].debug( | ||
| "request-result", status_code=e.status_code | ||
| ) | ||
| raise | ||
| request["log"].debug( | ||
| "request-result", status_code=resp.status, response=resp.body | ||
| ) | ||
| return resp | ||
|
|
||
| @middleware | ||
| async def require_auth(self, request: web.Request, handler): | ||
| token = request.headers.get(hdrs.AUTHORIZATION, "") | ||
| if not token.startswith("Bearer "): | ||
| request["log"].info("auth-invalid-token") | ||
| raise HTTPUnauthorized() | ||
| token = token.removeprefix("Bearer ") | ||
| client = self.tokens.get(token, None) | ||
| if not client: | ||
| request["log"].info("auth-token-unknown") | ||
| raise HTTPUnauthorized() | ||
| request["client"] = client | ||
| request["log"] = request["log"].bind(client=client) | ||
| request["log"].debug("auth-passed") | ||
| return await handler(request) | ||
|
|
||
| @middleware | ||
| async def to_json(self, request: web.Request, handler): | ||
| resp = await handler(request) | ||
| if isinstance(resp, web.Response): | ||
| return resp | ||
| elif resp is None: | ||
| raise web.HTTPNoContent() | ||
| else: | ||
| return web.json_response(resp, dumps=BackyJSONEncoder().encode) | ||
|
|
||
| async def get_status(self, request: web.Request): | ||
| filter = request.query.get("filter", "") | ||
| if filter: | ||
| filter = re.compile(filter) | ||
| return self.daemon.status(filter) | ||
|
|
||
| async def reload_daemon(self, request: web.Request): | ||
| self.daemon.reload() | ||
|
|
||
| async def get_jobs(self, request: web.Request): | ||
| return list(self.daemon.jobs.values()) | ||
|
|
||
| async def get_job(self, request: web.Request): | ||
| try: | ||
| name = request.match_info.get("job_name", None) | ||
| return self.daemon.jobs[name] | ||
| except KeyError: | ||
| raise HTTPNotFound() | ||
|
|
||
| async def run_job(self, request: web.Request): | ||
| j = await self.get_job(request) | ||
| j.run_immediately.set() | ||
| raise HTTPAccepted() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.