-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,33 +1,96 @@ | ||
import json | ||
from enum import Enum | ||
|
||
from fastapi import FastAPI | ||
from pydantic import BaseModel, create_model | ||
|
||
app = FastAPI() | ||
app = FastAPI(title="OL Remote Operator", version="0.0.1") | ||
|
||
|
||
@app.get("/") | ||
def root(): | ||
return {"Hello": "0L Operator!"} | ||
|
||
|
||
@app.post("/restart") | ||
def restart(): | ||
return {"not": "implemented"} | ||
class OperationType(str, Enum): | ||
""" | ||
Operation types | ||
cmd: run a command and return its output | ||
patch: given a filename, update part of its content | ||
""" | ||
CMD = 'cmd' | ||
PATCH = 'patch' | ||
|
||
|
||
class BaseOperation(BaseModel): | ||
name: str | ||
description: str | ||
method: str | ||
type: OperationType | ||
checks: list[str] = [] | ||
|
||
|
||
class CmdOperation(BaseOperation): | ||
cmd: list[str] = None | ||
filename: str = None | ||
payload: str = None | ||
|
||
|
||
class PatchOperation(BaseOperation): | ||
cmd: list[str] = None | ||
filename: str = None | ||
payload: str = None | ||
|
||
|
||
class BaseRequest(BaseModel): | ||
reason: str | ||
|
||
|
||
# Load operations schema | ||
with open("operations.json") as f: | ||
operations = json.load(f) | ||
|
||
for op_dict in operations: | ||
op: BaseOperation = BaseOperation.parse_obj(op_dict) | ||
|
||
# Create request model | ||
request_model_attrs = { | ||
'__base__': BaseRequest, | ||
} | ||
# Create response model | ||
|
||
if op.type == OperationType.CMD: | ||
op: CmdOperation = CmdOperation.parse_obj(op) | ||
|
||
@app.post("/cron/on") | ||
def cron_on(): | ||
return {"not": "implemented"} | ||
elif op.type == OperationType.PATCH: | ||
op: PatchOperation = PatchOperation.parse_obj(op) | ||
# create payload model based on schema from op.payload | ||
|
||
OperationRequestModel = create_model(f"Request{op.name.capitalize()}", **request_model_attrs) | ||
|
||
@app.post("/cron/off") | ||
def cron_off(): | ||
return {"not": "implemented"} | ||
# Create endpoint | ||
|
||
def endpoint(data: OperationRequestModel): | ||
if op.type == OperationType.CMD: | ||
output = 1 | ||
elif op.type == OperationType.PATCH: | ||
output = 2 | ||
|
||
@app.patch("/ol-toml") | ||
def patch_ol_toml(): | ||
return {"not": "implemented"} | ||
return { | ||
"operation": op.name, | ||
"description": op.description, | ||
"not": "implemented", | ||
"output": output | ||
} | ||
|
||
|
||
@app.patch("/validator-yaml") | ||
def patch_validator_yaml(): | ||
return {"not": "implemented"} | ||
# Add endpoint to api | ||
app.add_api_route( | ||
path=f"/{op.name}", | ||
methods=[op.method], | ||
endpoint=endpoint, | ||
name=f"{op.name.capitalize()} Endpoint", | ||
description=op.description | ||
) |
This file contains 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,60 @@ | ||
[ | ||
{ | ||
"name": "restart", | ||
"description": "Restarts node", | ||
"method": "post", | ||
"type": "cmd", | ||
"checks": [ | ||
"echo checking..." | ||
], | ||
"cmd": [ | ||
"echo restarting..." | ||
] | ||
}, | ||
{ | ||
"name": "cron_on", | ||
"description": "Setup cron", | ||
"method": "post", | ||
"type": "cmd", | ||
"checks": [ | ||
"echo checking..." | ||
], | ||
"cmd": [ | ||
"echo setting cron..." | ||
] | ||
}, | ||
{ | ||
"name": "cron_off", | ||
"description": "Disable cron", | ||
"method": "post", | ||
"type": "cmd", | ||
"checks": [ | ||
"echo checking..." | ||
], | ||
"cmd": [ | ||
"echo disabling cron..." | ||
] | ||
}, | ||
{ | ||
"name": "patch_validator_yaml", | ||
"description": "Patch validator.node.yaml", | ||
"method": "patch", | ||
"type": "patch", | ||
"checks": [ | ||
"echo checking..." | ||
], | ||
"filename": "validator.node.yaml", | ||
"payload": "str" | ||
}, | ||
{ | ||
"name": "patch_ol_toml", | ||
"description": "Patch 0L.toml", | ||
"method": "patch", | ||
"type": "patch", | ||
"checks": [ | ||
"echo checking..." | ||
], | ||
"filename": "0L.toml", | ||
"payload": "str" | ||
} | ||
] |