Fixes in plane
This commit is contained in:
132
app/sbin/api
132
app/sbin/api
@@ -13,9 +13,10 @@ import uvicorn
|
||||
from typing import Optional
|
||||
|
||||
# Constants
|
||||
DB_PATH = "/4server/data/contracts/contracts.db"
|
||||
DB_PATH = "/4server/data/contracts.db"
|
||||
BIN_PATH = "/4server/sbin"
|
||||
API_KEY = os.getenv("API_KEY", "your-secret-api-key")
|
||||
VERSION = "API: 0.0.5"
|
||||
|
||||
# FastAPI app
|
||||
app = FastAPI()
|
||||
@@ -28,7 +29,6 @@ def verify_api_key(key: str = Depends(api_key_header)):
|
||||
if key != API_KEY:
|
||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||
|
||||
|
||||
# ---------------------- Database ----------------------
|
||||
def init_db():
|
||||
"""Initialize the database with containers table."""
|
||||
@@ -38,12 +38,18 @@ def init_db():
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS containers (
|
||||
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
UUID CHAR(36),
|
||||
location CHAR(100),
|
||||
UUID CHAR(50),
|
||||
email CHAR(100),
|
||||
expires DATE,
|
||||
tags TEXT,
|
||||
env TEXT
|
||||
env TEXT,
|
||||
affiliate char(30),
|
||||
image char(50),
|
||||
history text,
|
||||
comment text,
|
||||
domains text,
|
||||
status char (20).
|
||||
created DATE
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
@@ -53,11 +59,17 @@ def init_db():
|
||||
# ---------------------- Models ----------------------
|
||||
class ContainerModel(BaseModel):
|
||||
UUID: str
|
||||
location: str
|
||||
email: str
|
||||
expires: str
|
||||
tags: Optional[str] = None
|
||||
env: Optional[str] = None
|
||||
affiliate: Optional[str] = None
|
||||
image: Optional[str} = None
|
||||
history: Optional[str] = None
|
||||
comment: Optional[str] = None
|
||||
domains:Optional[str] = None
|
||||
status: str
|
||||
created: str
|
||||
|
||||
|
||||
class StartContainerRequest(BaseModel):
|
||||
@@ -65,29 +77,57 @@ class StartContainerRequest(BaseModel):
|
||||
email: str
|
||||
|
||||
|
||||
# ---------------------- Routes ----------------------
|
||||
# ---------------------- CONTAINER Routes ----------------------
|
||||
@app.get("/", include_in_schema=False)
|
||||
def redirect_to_odoo():
|
||||
return RedirectResponse(url="https://ODOO4PROJECTS.com")
|
||||
|
||||
@app.post("/container/update", dependencies=[Depends(verify_api_key)])
|
||||
def update_container(request: UpdateContainerRequest):
|
||||
--> Insert the new container into the database. Create, if container does not exist
|
||||
|
||||
@app.post("/startContainer", dependencies=[Depends(verify_api_key)])
|
||||
@app.post("/container/start", dependencies=[Depends(verify_api_key)])
|
||||
def start_container(request: StartContainerRequest):
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[os.path.join(BIN_PATH, "startContainer"), request.uuid, request.email],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
return {"status": "success", "output": result.stdout}
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error in /startContainer: {e.stderr}", file=sys.stderr)
|
||||
raise HTTPException(status_code=500, detail=f"Command failed: {e.stderr}")
|
||||
--> refactor, so that only the container is is given in the request. the shell script BIN_PATH/startContainer is called withtge containerid as parameter
|
||||
|
||||
@app.post("/container/stop", dependencies=[Depends(verify_api_key)])
|
||||
def stop_container(request: StopContainerRequest):
|
||||
--> refactor, so that only the container is is given in the request. the shell script BIN_PATH/stopContainer is called withtge containerid as parameter
|
||||
|
||||
@app.post("/container/nuke", dependencies=[Depends(verify_api_key)])
|
||||
def nuke_container(request: StopContainerRequest):
|
||||
--> refactor, so that only the container is is given in the request.
|
||||
when the status of the database is "nuke" then
|
||||
the shell script BIN_PATH/nukeContainer is called withtge containerid as parameter
|
||||
|
||||
@app.post("/container/info", dependencies=[Depends(verify_api_key)])
|
||||
def info_container(request: InfoContainerRequest):
|
||||
--> refactor: When no container id is given, query all containers from the database. when a containeris is given, just select that one. Return the result
|
||||
|
||||
|
||||
@app.get("/system", dependencies=[Depends(verify_api_key)])
|
||||
|
||||
--> add /container/quota return the disk and ram usage of this container. I think you can obtain this from docker
|
||||
|
||||
|
||||
|
||||
# ------------------------ SYSTEM Routes
|
||||
|
||||
|
||||
@app.get("/system/containers", dependencies=[Depends(verify_api_key)])
|
||||
def get_containers():
|
||||
result = subprocess.run([BIN_PATH+'/getContainers'], capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
return Response(content='{"error": "Script failed"}', media_type="application/json", status_code=500)
|
||||
return Response(content=result.stdout, media_type="application/json")
|
||||
|
||||
|
||||
@app.get("/system/info", dependencies=[Depends(verify_api_key)])
|
||||
def get_system_info():
|
||||
return all INFOas JSON
|
||||
in this function add last_update and return the content of /4server/data/update
|
||||
if this file does not exist return NONE
|
||||
|
||||
return the VERSION as well.
|
||||
try:
|
||||
with open("/etc/alpine-release") as f:
|
||||
version = f.read().strip()
|
||||
@@ -98,8 +138,11 @@ def get_system_info():
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@app.get("/resources", dependencies=[Depends(verify_api_key)])
|
||||
@app.get("/system/resources", dependencies=[Depends(verify_api_key)])
|
||||
def get_resources():
|
||||
|
||||
--> consolidate this API into /system/info
|
||||
|
||||
mem = psutil.virtual_memory()
|
||||
disk = psutil.disk_usage("/")
|
||||
return {
|
||||
@@ -109,55 +152,10 @@ def get_resources():
|
||||
}
|
||||
|
||||
|
||||
@app.get("/containers", dependencies=[Depends(verify_api_key)])
|
||||
def get_containers():
|
||||
result = subprocess.run([BIN_PATH+'/getContainers'], capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
return Response(content='{"error": "Script failed"}', media_type="application/json", status_code=500)
|
||||
return Response(content=result.stdout, media_type="application/json")
|
||||
|
||||
|
||||
@app.post("/container", dependencies=[Depends(verify_api_key)])
|
||||
def upsert_container(container: ContainerModel):
|
||||
try:
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("SELECT 1 FROM containers WHERE UUID = ?", (container.UUID,))
|
||||
exists = cursor.fetchone()
|
||||
|
||||
if exists:
|
||||
cursor.execute("""
|
||||
UPDATE containers SET
|
||||
location = ?, email = ?, expires = ?, tags = ?, env = ?
|
||||
WHERE UUID = ?
|
||||
""", (
|
||||
container.location, container.email, container.expires,
|
||||
container.tags, container.env, container.UUID
|
||||
))
|
||||
operation = "update"
|
||||
else:
|
||||
cursor.execute("""
|
||||
INSERT INTO containers (UUID, location, email, expires, tags, env)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
container.UUID, container.location, container.email,
|
||||
container.expires, container.tags, container.env
|
||||
))
|
||||
operation = "insert"
|
||||
|
||||
conn.commit()
|
||||
return {"status": "success", "operation": operation}
|
||||
except Exception as e:
|
||||
print(f"Error in /container: {e}", file=sys.stderr)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
# ---------------------- Entry Point ----------------------
|
||||
if __name__ == "__main__":
|
||||
print("Version 0.1")
|
||||
print(VERSION)
|
||||
init_db()
|
||||
uvicorn.run(app, host="10.5.0.1", port=8888)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user