2024-11-03 16:55:10 +00:00
|
|
|
from os import getenv, path
|
2024-11-03 21:57:12 +00:00
|
|
|
from typing import Annotated
|
2024-11-03 16:55:10 +00:00
|
|
|
|
2024-11-03 19:42:55 +00:00
|
|
|
from docker import errors, from_env
|
2024-11-03 20:24:49 +00:00
|
|
|
from docker.models.containers import Container
|
2024-11-03 16:55:10 +00:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
from fastapi import Depends, FastAPI, HTTPException, Request
|
2024-11-04 15:55:23 +00:00
|
|
|
from fastapi.responses import PlainTextResponse
|
2024-11-03 16:55:10 +00:00
|
|
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
2024-11-03 21:57:12 +00:00
|
|
|
from pydantic import BaseModel
|
2024-11-03 16:55:10 +00:00
|
|
|
from starlette import status, types
|
|
|
|
from uvicorn import run
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
client = from_env()
|
|
|
|
security = HTTPBasic()
|
|
|
|
|
|
|
|
|
|
|
|
def http_401() -> HTTPException:
|
|
|
|
return HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail="Incorrect username or password",
|
|
|
|
headers={"WWW-Authenticate": "Basic"},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def check_auth(
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> HTTPBasicCredentials:
|
|
|
|
usernames = getenv("USERNAMES", "").split(",")
|
|
|
|
passwords = getenv("PASSWORDS", "").split(",")
|
|
|
|
|
|
|
|
if credentials.username not in usernames or credentials.password not in passwords:
|
|
|
|
raise http_401()
|
|
|
|
|
|
|
|
user_index = usernames.index(credentials.username)
|
|
|
|
password = passwords[user_index]
|
|
|
|
|
|
|
|
if credentials.password != password:
|
|
|
|
raise http_401()
|
|
|
|
|
|
|
|
return credentials
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI(dependencies=[Depends(check_auth)])
|
|
|
|
|
|
|
|
|
2024-11-03 21:57:12 +00:00
|
|
|
class SerializedContainer(BaseModel):
|
|
|
|
id: str
|
|
|
|
name: str | None
|
|
|
|
image: str | None
|
|
|
|
labels: dict[str, str]
|
|
|
|
status: str
|
|
|
|
health: str
|
2024-11-04 10:04:25 +00:00
|
|
|
engine: str | None
|
2024-11-03 21:57:12 +00:00
|
|
|
owner: str | None
|
|
|
|
environment: list[str]
|
2024-11-04 15:55:23 +00:00
|
|
|
logs: str | None
|
|
|
|
|
|
|
|
|
|
|
|
def container_logs(container: Container, tail: int) -> str | None:
|
|
|
|
try:
|
|
|
|
return container.logs(tail=tail).decode()
|
|
|
|
except errors.APIError:
|
|
|
|
return None
|
2024-11-03 21:57:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def serialize_container(container: Container) -> SerializedContainer:
|
|
|
|
return SerializedContainer(
|
|
|
|
id=container.short_id,
|
|
|
|
name=container.name,
|
|
|
|
image=container.image.tags[0] if container.image else None,
|
|
|
|
labels=container.labels,
|
|
|
|
status=container.status,
|
|
|
|
health=container.health,
|
2024-11-04 10:04:25 +00:00
|
|
|
engine=container.labels.get("engine"),
|
2024-11-03 21:57:12 +00:00
|
|
|
owner=container.labels.get("owner"),
|
|
|
|
environment=container.attrs["Config"]["Env"],
|
2024-11-04 15:55:23 +00:00
|
|
|
logs=container_logs(container, 100),
|
2024-11-03 21:57:12 +00:00
|
|
|
)
|
2024-11-03 20:24:49 +00:00
|
|
|
|
|
|
|
|
2024-11-04 10:04:25 +00:00
|
|
|
def select_container(
|
|
|
|
container_name: str, credentials: Annotated[HTTPBasicCredentials, Depends(security)]
|
|
|
|
) -> Container:
|
|
|
|
try:
|
|
|
|
container = client.containers.get(container_name)
|
|
|
|
except errors.APIError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
if (
|
|
|
|
credentials.username != "admin"
|
|
|
|
and container.labels.get("engine") != "pilotwings"
|
|
|
|
and container.labels.get("owner") != credentials.username
|
|
|
|
):
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
|
|
|
return container
|
|
|
|
|
|
|
|
|
2024-11-03 16:55:10 +00:00
|
|
|
@app.get("/api/containers")
|
|
|
|
def get_containers(
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
2024-11-03 21:57:12 +00:00
|
|
|
) -> list[SerializedContainer]:
|
2024-11-03 16:55:10 +00:00
|
|
|
if credentials.username == "admin":
|
2024-11-03 20:24:49 +00:00
|
|
|
return [
|
2024-11-04 10:04:25 +00:00
|
|
|
serialize_container(container)
|
|
|
|
for container in client.containers.list(
|
2024-11-05 22:45:23 +00:00
|
|
|
all=True,
|
2024-11-04 10:04:25 +00:00
|
|
|
filters={"label": ["engine=pilotwings"]}
|
|
|
|
)
|
2024-11-03 20:24:49 +00:00
|
|
|
]
|
2024-11-03 16:55:10 +00:00
|
|
|
|
|
|
|
return [
|
2024-11-03 20:24:49 +00:00
|
|
|
serialize_container(container)
|
2024-11-03 16:55:10 +00:00
|
|
|
for container in client.containers.list(
|
2024-11-05 22:45:23 +00:00
|
|
|
all=True,
|
2024-11-04 10:04:25 +00:00
|
|
|
filters={"label": ["engine=pilotwings", f"owner={credentials.username}"]},
|
2024-11-03 16:55:10 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-11-03 19:42:55 +00:00
|
|
|
@app.get("/api/container/{container_name}")
|
|
|
|
def get_container(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
2024-11-03 21:57:12 +00:00
|
|
|
) -> SerializedContainer:
|
2024-11-04 10:04:25 +00:00
|
|
|
return serialize_container(select_container(container_name, credentials))
|
2024-11-03 21:57:12 +00:00
|
|
|
|
|
|
|
|
2024-11-04 10:04:25 +00:00
|
|
|
class ContainerRequest(BaseModel):
|
2024-11-03 21:57:12 +00:00
|
|
|
image: str
|
2024-11-04 15:55:23 +00:00
|
|
|
environment: list[str]
|
2024-11-03 21:57:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/container/{container_name}")
|
2024-11-04 10:04:25 +00:00
|
|
|
def create_or_update_container(
|
2024-11-03 21:57:12 +00:00
|
|
|
container_name: str,
|
2024-11-04 10:04:25 +00:00
|
|
|
request_body: ContainerRequest,
|
2024-11-03 21:57:12 +00:00
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> SerializedContainer:
|
2024-11-04 10:04:25 +00:00
|
|
|
networks = client.networks.list(names=["pilotwings"])
|
2024-11-03 21:57:12 +00:00
|
|
|
|
2024-11-04 10:04:25 +00:00
|
|
|
if not networks:
|
|
|
|
client.networks.create("pilotwings")
|
2024-11-03 21:57:12 +00:00
|
|
|
|
2024-11-04 15:55:23 +00:00
|
|
|
client.images.pull(request_body.image)
|
|
|
|
|
2024-11-03 21:57:12 +00:00
|
|
|
try:
|
2024-11-04 15:55:23 +00:00
|
|
|
delete_container(container_name, credentials)
|
|
|
|
except HTTPException:
|
2024-11-04 10:04:25 +00:00
|
|
|
pass
|
2024-11-03 21:57:12 +00:00
|
|
|
|
|
|
|
return serialize_container(
|
|
|
|
client.containers.run(
|
2024-11-04 10:04:25 +00:00
|
|
|
request_body.image,
|
2024-11-03 21:57:12 +00:00
|
|
|
detach=True,
|
|
|
|
environment=request_body.environment,
|
2024-11-04 10:04:25 +00:00
|
|
|
labels={"engine": "pilotwings", "owner": credentials.username},
|
2024-11-03 21:57:12 +00:00
|
|
|
name=container_name,
|
2024-11-04 10:04:25 +00:00
|
|
|
network="pilotwings",
|
2024-11-03 21:57:12 +00:00
|
|
|
restart_policy={"Name": "always"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-11-04 15:55:23 +00:00
|
|
|
@app.post("/api/container/{container_name}/pull")
|
|
|
|
def pull_container(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> SerializedContainer:
|
|
|
|
container = select_container(container_name, credentials)
|
|
|
|
|
|
|
|
if not container.image:
|
|
|
|
raise HTTPException(status_code=status.HTTP_410_GONE)
|
|
|
|
|
|
|
|
request_body = ContainerRequest(
|
|
|
|
image=container.image.tags[0], environment=container.attrs["Config"]["Env"]
|
|
|
|
)
|
|
|
|
|
|
|
|
return create_or_update_container(container_name, request_body, credentials)
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/container/{container_name}/restart")
|
|
|
|
def restart_container(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> SerializedContainer:
|
|
|
|
container = select_container(container_name, credentials)
|
|
|
|
container.restart()
|
2024-11-05 22:45:23 +00:00
|
|
|
container.reload()
|
|
|
|
return serialize_container(container)
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/container/{container_name}/start")
|
|
|
|
def start_container(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> SerializedContainer:
|
|
|
|
container = select_container(container_name, credentials)
|
|
|
|
container.start()
|
|
|
|
container.reload()
|
|
|
|
return serialize_container(container)
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/container/{container_name}/stop")
|
|
|
|
def stop_container(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> SerializedContainer:
|
|
|
|
container = select_container(container_name, credentials)
|
|
|
|
container.stop()
|
|
|
|
container.reload()
|
2024-11-04 15:55:23 +00:00
|
|
|
return serialize_container(container)
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/container/{container_name}/logs", response_class=PlainTextResponse)
|
|
|
|
def get_container_logs(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> str:
|
|
|
|
container = select_container(container_name, credentials)
|
|
|
|
return container.logs().decode()
|
|
|
|
|
|
|
|
|
|
|
|
@app.delete("/api/container/{container_name}")
|
|
|
|
def delete_container(
|
|
|
|
container_name: str,
|
|
|
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
|
|
|
) -> None:
|
|
|
|
container = select_container(container_name, credentials)
|
|
|
|
container.stop()
|
|
|
|
container.remove(v=True, force=True)
|
|
|
|
|
|
|
|
|
2024-11-03 22:10:21 +00:00
|
|
|
class AuthStaticFiles(StaticFiles):
|
|
|
|
async def __call__(
|
|
|
|
self, scope: types.Scope, receive: types.Receive, send: types.Send
|
|
|
|
) -> None:
|
|
|
|
request = Request(scope, receive)
|
|
|
|
credentials = await security(request)
|
|
|
|
|
|
|
|
if not credentials:
|
|
|
|
raise http_401()
|
|
|
|
|
|
|
|
await check_auth(credentials)
|
|
|
|
await super().__call__(scope, receive, send)
|
|
|
|
|
|
|
|
|
2024-11-03 16:55:10 +00:00
|
|
|
app.mount(
|
|
|
|
"/",
|
|
|
|
AuthStaticFiles(
|
|
|
|
directory=f"{path.dirname(path.realpath(__file__))}/dist", html=True
|
|
|
|
),
|
|
|
|
name="static",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-11-03 20:24:49 +00:00
|
|
|
def launch() -> None:
|
2024-11-03 16:55:10 +00:00
|
|
|
run(app, host="0.0.0.0")
|