rename ass

This commit is contained in:
Oliver
2025-08-10 17:43:04 -03:00
parent 73d9587e1d
commit 4c93fa132f
20 changed files with 447 additions and 29 deletions

163
app/sbin/api Executable file
View File

@@ -0,0 +1,163 @@
#!/usr/bin/env python3
from fastapi import FastAPI, HTTPException, Depends, Response
from fastapi.security.api_key import APIKeyHeader
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
import psutil
import sqlite3
import subprocess
import os
import sys
import uvicorn
from typing import Optional
# Constants
DB_PATH = "/4server/data/contracts/contracts.db"
BIN_PATH = "/4server/sbin"
API_KEY = os.getenv("API_KEY", "your-secret-api-key")
# FastAPI app
app = FastAPI()
# Security
api_key_header = APIKeyHeader(name="X-API-Key")
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."""
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS containers (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
UUID CHAR(36),
location CHAR(100),
email CHAR(100),
expires DATE,
tags TEXT,
env TEXT
)
''')
conn.commit()
conn.close()
# ---------------------- Models ----------------------
class ContainerModel(BaseModel):
UUID: str
location: str
email: str
expires: str
tags: Optional[str] = None
env: Optional[str] = None
class StartContainerRequest(BaseModel):
uuid: str
email: str
# ---------------------- Routes ----------------------
@app.get("/", include_in_schema=False)
def redirect_to_odoo():
return RedirectResponse(url="https://ODOO4PROJECTS.com")
@app.post("/startContainer", 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}")
@app.get("/system", dependencies=[Depends(verify_api_key)])
def get_system_info():
try:
with open("/etc/alpine-release") as f:
version = f.read().strip()
return {"alpine_version": version}
except FileNotFoundError:
raise HTTPException(status_code=404, detail="File not found. Press play on tape")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/resources", dependencies=[Depends(verify_api_key)])
def get_resources():
mem = psutil.virtual_memory()
disk = psutil.disk_usage("/")
return {
"memory": {"total": mem.total, "available": mem.available, "used": mem.used},
"disk": {"total": disk.total, "used": disk.used, "free": disk.free},
"cpu_count": psutil.cpu_count(logical=True),
}
@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")
init_db()
uvicorn.run(app, host="10.5.0.1", port=8888)

61
app/sbin/getContainers Executable file
View File

@@ -0,0 +1,61 @@
#!/bin/bash
# Collect running container IDs
container_ids=$(docker ps -q)
# Start JSON array
echo "["
first=true
for cid in $container_ids; do
# Get basic info
name=$(docker inspect --format '{{.Name}}' "$cid" | sed 's/^\/\(.*\)/\1/')
image=$(docker inspect --format '{{.Config.Image}}' "$cid")
container_id=$(docker inspect --format '{{.Id}}' "$cid")
# RAM usage in bytes (from docker stats)
mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$cid")
# mem_usage looks like "12.34MiB / 1.944GiB" — extract the used part
mem_used=$(echo "$mem_usage" | awk '{print $1}')
# Convert mem_used to MiB as a number
# Support KiB, MiB, GiB units
ram_usage=$(echo "$mem_used" | awk '
{
val = substr($0, 1, length($0)-3)
unit = substr($0, length($0)-2, 3)
if (unit == "KiB") print val / 1024
else if (unit == "MiB") print val
else if (unit == "GiB") print val * 1024
else print 0
}')
# Traefik labels (extract labels JSON)
labels=$(docker inspect --format '{{json .Config.Labels}}' "$cid" | jq -r 'to_entries | map(select(.key | test("^traefik.*"))) | map({(.key): .value}) | add')
# Comma between JSON objects
if [ "$first" = false ]; then
echo ","
fi
first=false
# Output JSON object
jq -n \
--arg name "$name" \
--arg id "$container_id" \
--arg image "$image" \
--argjson ram "$ram_usage" \
--argjson tags "$labels" \
'{
name: $name,
id: $id,
image: $image,
ram_mb: $ram,
traefik_tags: $tags
}'
done
# End JSON array
echo "]"

52
app/sbin/startContainer Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env bash
exec > /4server/data/startContainer.log 2>&1
echo "$(date '+%Y-%m-%d %H:%M') - startContainer $1"
CONTAINER_NAME="$1"
# Get the hostname of the machine
HOSTNAME=$(hostname)
mkdir -p /4server/data/${CONTAINER_NAME}/n8n
mkdir -p /4server/data/${CONTAINER_NAME}/data
sudo chmod 777 /4server/data/${CONTAINER_NAME}/n8n
sudo chmod 777 /4server/data/${CONTAINER_NAME}/data
# Stop the container if it exists
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "$(date '+%Y-%m-%d %H:%M') - stopping existing container $CONTAINER_NAME"
docker stop "$CONTAINER_NAME"
docker rm "$CONTAINER_NAME"
fi
docker run -d \
--name "$CONTAINER_NAME" \
-p 5678 \
--cap-add=SYS_ADMIN \
--security-opt seccomp=unconfined \
--restart=always \
-e N8N_HOST="${CONTAINER_NAME}.od8n.com" \
-e N8N_PORT=5678 \
-e N8N_PROTOCOL=https \
-e NODE_ENV=production \
-e WEBHOOK_URL="https://${CONTAINER_NAME}.od8n.com/" \
-e GENERIC_TIMEZONE="UTC-3" \
-v "/4server/data/${CONTAINER_NAME}/n8n:/home/node/.n8n" \
-v "/4server/data/${CONTAINER_NAME}/data:/data" \
--label "traefik.enable=true" \
--label "traefik.http.routers.${CONTAINER_NAME}.rule=Host(\`${CONTAINER_NAME}.od8n.com\`)" \
--label "traefik.http.routers.${CONTAINER_NAME}.entrypoints=web,websecure" \
--label "traefik.http.routers.${CONTAINER_NAME}.tls=true" \
--label "traefik.http.routers.${CONTAINER_NAME}.tls.certresolver=production" \
--label "traefik.http.services.${CONTAINER_NAME}.loadbalancer.server.port=5678" \
--network docker-compose_4projects \
n8nio/n8n:latest
echo "Done $1"