Skip to content

Commit

Permalink
core: services: commander: Add command endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Mar 15, 2022
1 parent a948362 commit d554d19
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions core/services/commander/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ShutdownType(str, Enum):
POWEROFF = "poweroff"


def run_command(command: str) -> "subprocess.CompletedProcess['bytes']":
def run_command(command: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
user = "pi"
password = "raspberry"

Expand All @@ -50,7 +50,10 @@ def run_command(command: str) -> "subprocess.CompletedProcess['bytes']":
f"{user}@localhost",
command,
],
check=True,
check=check,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)


Expand All @@ -62,6 +65,28 @@ def check_what_i_am_doing(i_know_what_i_am_doing: bool = False) -> None:
)


@app.post("/command/host", status_code=status.HTTP_200_OK)
@version(1, 0)
async def command_host(response: Response, command: str, i_know_what_i_am_doing: bool = False) -> Any:
check_what_i_am_doing(i_know_what_i_am_doing)
logger.debug(f"Running command: {command}")

try:
output = run_command(command, False)
logger.debug(f"Output: {output}")
response.status_code = status.HTTP_200_OK
message = {
"stdout": f"{output.stdout!r}",
"stderr": f"{output.stderr!r}",
"return_code": output.returncode,
}
return message
except Exception as error:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
logger.exception(error)
return {"message": f"{error}"}


# TODO: Update commander to work with openapi modules and improve modularity and code organization
@app.post("/shutdown", status_code=status.HTTP_200_OK)
@version(1, 0)
Expand Down

0 comments on commit d554d19

Please sign in to comment.