new structure
This commit is contained in:
8
app/.ssh/boston
Normal file
8
app/.ssh/boston
Normal file
@@ -0,0 +1,8 @@
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACAOKT5um7fzviD27odH4GTW5oos0xAeP3DW7OhTqbEyAgAAAJhBIAtnQSAL
|
||||
ZwAAAAtzc2gtZWQyNTUxOQAAACAOKT5um7fzviD27odH4GTW5oos0xAeP3DW7OhTqbEyAg
|
||||
AAAEAk4EqWEVc34Dht1e/vD0ajtgSCo6xq30YPtvnrNuL3PQ4pPm6bt/O+IPbuh0fgZNbm
|
||||
iizTEB4/cNbs6FOpsTICAAAADm9saXZlckBtYXRlLTE2AQIDBAUGBw==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
|
||||
2
app/.ssh/boston.pub
Normal file
2
app/.ssh/boston.pub
Normal file
@@ -0,0 +1,2 @@
|
||||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA4pPm6bt/O+IPbuh0fgZNbmiizTEB4/cNbs6FOpsTIC oliver@mate-16
|
||||
|
||||
8
app/.ssh/london
Normal file
8
app/.ssh/london
Normal file
@@ -0,0 +1,8 @@
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACD64E32udubuP0PEhxgoxR35jgHFUzlSRONXfGgsxNPswAAAJhgli31YJYt
|
||||
9QAAAAtzc2gtZWQyNTUxOQAAACD64E32udubuP0PEhxgoxR35jgHFUzlSRONXfGgsxNPsw
|
||||
AAAEC+wOOEwsAkPIhD02f+Yw5MYaCZZ5hhU9ZG62CcdH4+XvrgTfa525u4/Q8SHGCjFHfm
|
||||
OAcVTOVJE41d8aCzE0+zAAAADm9saXZlckBtYXRlLTE2AQIDBAUGBw==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
|
||||
2
app/.ssh/london.pub
Normal file
2
app/.ssh/london.pub
Normal file
@@ -0,0 +1,2 @@
|
||||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPrgTfa525u4/Q8SHGCjFHfmOAcVTOVJE41d8aCzE0+z oliver@mate-16
|
||||
|
||||
7
app/.ssh/mumbai
Normal file
7
app/.ssh/mumbai
Normal file
@@ -0,0 +1,7 @@
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACDEN4FtZTrBFBkDDy7hTptxeoyPvpHDbSKuJxEGWusaMgAAAJjCrJXhwqyV
|
||||
4QAAAAtzc2gtZWQyNTUxOQAAACDEN4FtZTrBFBkDDy7hTptxeoyPvpHDbSKuJxEGWusaMg
|
||||
AAAEAUwrtarcT6FifBEUGdm8F32i2lZ3rEeYyKQo5n0wRyxcQ3gW1lOsEUGQMPLuFOm3F6
|
||||
jI++kcNtIq4nEQZa6xoyAAAADm9saXZlckBtYXRlLTE2AQIDBAUGBw==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
2
app/.ssh/mumbai.pub
Normal file
2
app/.ssh/mumbai.pub
Normal file
@@ -0,0 +1,2 @@
|
||||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ3gW1lOsEUGQMPLuFOm3F6jI++kcNtIq4nEQZa6xoy ansible@mumbai
|
||||
|
||||
8
app/.ssh/saopaulo
Normal file
8
app/.ssh/saopaulo
Normal file
@@ -0,0 +1,8 @@
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACDAXv2aFH8xzLWB5zvtJI1SkgDnB/OMhmkFQQa/vzhk8gAAAJjM4MTdzODE
|
||||
3QAAAAtzc2gtZWQyNTUxOQAAACDAXv2aFH8xzLWB5zvtJI1SkgDnB/OMhmkFQQa/vzhk8g
|
||||
AAAEDVHiNzHN0zUBHU5ui2U00xxXs4UIVGVRW1PCEhxpD/iMBe/ZoUfzHMtYHnO+0kjVKS
|
||||
AOcH84yGaQVBBr+/OGTyAAAADm9saXZlckBtYXRlLTE2AQIDBAUGBw==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
|
||||
2
app/.ssh/saopaulo.pub
Normal file
2
app/.ssh/saopaulo.pub
Normal file
@@ -0,0 +1,2 @@
|
||||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMBe/ZoUfzHMtYHnO+0kjVKSAOcH84yGaQVBBr+/OGTy oliver@mate-16
|
||||
|
||||
163
app/bin/OD8N/sbin/api
Executable file
163
app/bin/OD8N/sbin/api
Executable 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 = "/OD8N/data/contracts/contracts.db"
|
||||
BIN_PATH = "/OD8N/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://OD8N.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(['/OD8N/sbin/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/bin/OD8N/sbin/getContainers
Executable file
61
app/bin/OD8N/sbin/getContainers
Executable 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/bin/OD8N/sbin/startContainer
Executable file
52
app/bin/OD8N/sbin/startContainer
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env bash
|
||||
exec > /OD8N/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 /OD8N/data/${CONTAINER_NAME}/n8n
|
||||
mkdir -p /OD8N/data/${CONTAINER_NAME}/data
|
||||
|
||||
sudo chmod 777 /OD8N/data/${CONTAINER_NAME}/n8n
|
||||
sudo chmod 777 /OD8N/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 "/OD8N/data/${CONTAINER_NAME}/n8n:/home/node/.n8n" \
|
||||
-v "/OD8N/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"
|
||||
1
app/host_vars/boston
Normal file
1
app/host_vars/boston
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY=4lnZRkRB7ke0A2zkX0T
|
||||
1
app/host_vars/dev
Normal file
1
app/host_vars/dev
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY=4h6lDzAOVksuCqmhEB3
|
||||
1
app/host_vars/london
Normal file
1
app/host_vars/london
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY=4h6lDzAOVksuCqmhEB3
|
||||
1
app/host_vars/mumbai
Normal file
1
app/host_vars/mumbai
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY=4SSJxWKmuwblhzd3F5L
|
||||
1
app/host_vars/saopaulo
Normal file
1
app/host_vars/saopaulo
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY=7WxFrFAvQjVIJF1sLzl
|
||||
4
app/hosts.all
Executable file
4
app/hosts.all
Executable file
@@ -0,0 +1,4 @@
|
||||
saopaulo
|
||||
mumbai
|
||||
boston
|
||||
london
|
||||
1
app/hosts.dev
Executable file
1
app/hosts.dev
Executable file
@@ -0,0 +1 @@
|
||||
dev
|
||||
4
app/restart
Executable file
4
app/restart
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/ass
|
||||
|
||||
doas apk update
|
||||
doas apk upgrade
|
||||
1
app/templates/od8n
Normal file
1
app/templates/od8n
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY={API_KEY}
|
||||
25
app/templates/od8n-api
Normal file
25
app/templates/od8n-api
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/sbin/openrc-run
|
||||
|
||||
name="od8n-api"
|
||||
description="OD8N API Service"
|
||||
|
||||
command="/OD8N/sbin/api"
|
||||
command_args=""
|
||||
pidfile="/run/${RC_SVCNAME}.pid"
|
||||
command_background="yes"
|
||||
|
||||
if [ -f /etc/od8n ]; then
|
||||
. /etc/od8n
|
||||
export $(cut -d= -f1 /etc/od8n)
|
||||
fi
|
||||
|
||||
output_log="/OD8N/data/api.log"
|
||||
error_log="/OD8N/data/api.log"
|
||||
|
||||
depend() {
|
||||
need net
|
||||
use logger dns
|
||||
after firewall
|
||||
}
|
||||
|
||||
|
||||
25
app/update
Executable file
25
app/update
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
ass doas mkdir -p /OD8N
|
||||
ass doas chmod 777 /OD8N
|
||||
ass mkdir -p /OD8N/data
|
||||
|
||||
|
||||
ass doas apk update
|
||||
ass doas apk upgrade
|
||||
ass doas apk add jq rsync mc vim
|
||||
|
||||
prsync -h "$hosts_file" -avz ./bin/OD8N/sbin/ /OD8N/sbin/
|
||||
|
||||
#INSTALL API KEYS
|
||||
template templates/od8n /OD8N/od8n ./host_vars
|
||||
ass doas mv /OD8N/od8n /etc/od8n
|
||||
|
||||
#INSTALL API SERVICE
|
||||
template templates/od8n-api /OD8N/od8n-api ./host_vars/
|
||||
ass doas mv /OD8N/od8n-api /etc/init.d/od8n-api
|
||||
ass doas chmod 0755 /etc/init.d/od8n-api
|
||||
ass doas chown root:root /etc/init.d/od8n-api
|
||||
ass doas rc-update add od8n-api default
|
||||
ass doas rc-service od8n-api restart
|
||||
ass doas rc-update add od8n-api default
|
||||
Reference in New Issue
Block a user