Delete apps from database
This commit is contained in:
+35
-46
@@ -1,24 +1,23 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
CONFIG_FILE="/opt/appfactory/config/appfactory.env"
|
||||||
|
source "$CONFIG_FILE"
|
||||||
|
|
||||||
APP_ID="${1:-}"
|
APP_ID="${1:-}"
|
||||||
|
|
||||||
|
DB_FILE="/opt/appfactory/data/appfactory/appfactory.db"
|
||||||
|
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
|
||||||
|
|
||||||
if [ -z "$APP_ID" ]; then
|
if [ -z "$APP_ID" ]; then
|
||||||
echo "Usage: ./delete-app.sh <app-id>"
|
echo "Usage: ./delete-app.sh <app-id>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CONFIG_FILE="/opt/appfactory/config/appfactory.env"
|
|
||||||
source "$CONFIG_FILE"
|
|
||||||
|
|
||||||
COMPOSE_FILE="$APPFACTORY_DIR/deploy/apps-compose.yml"
|
|
||||||
CATALOG_FILE="$APPFACTORY_DIR/apps/catalog.yml"
|
|
||||||
CADDY_FILE="$APPFACTORY_DIR/gateway/Caddyfile"
|
|
||||||
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
|
|
||||||
|
|
||||||
echo "Deleting app: $APP_ID"
|
echo "Deleting app: $APP_ID"
|
||||||
|
|
||||||
echo "Deleting Gitea repository..."
|
echo "Deleting Gitea repository..."
|
||||||
|
|
||||||
curl -sS -X DELETE \
|
curl -sS -X DELETE \
|
||||||
"$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP_ID" \
|
"$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP_ID" \
|
||||||
-H "Authorization: token $GITEA_TOKEN" || true
|
-H "Authorization: token $GITEA_TOKEN" || true
|
||||||
@@ -27,60 +26,50 @@ echo "Stopping container..."
|
|||||||
docker rm -f "$APP_ID" || true
|
docker rm -f "$APP_ID" || true
|
||||||
|
|
||||||
echo "Removing image..."
|
echo "Removing image..."
|
||||||
docker rmi -f "$APP_ID:latest" || true
|
docker image rm "$APP_ID:latest" || true
|
||||||
|
|
||||||
echo "Removing workspace..."
|
echo "Removing workspace..."
|
||||||
rm -rf "$WORKSPACE_DIR/$APP_ID"
|
rm -rf "$WORKSPACE_DIR/$APP_ID"
|
||||||
|
|
||||||
python3 - "$APP_ID" "$COMPOSE_FILE" "$CATALOG_FILE" "$CADDY_FILE" <<'PY'
|
echo "Removing app from database..."
|
||||||
|
|
||||||
|
python3 - "$DB_FILE" "$APP_ID" <<'PY'
|
||||||
|
import sqlite3
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
app_id = sys.argv[1]
|
db_file = sys.argv[1]
|
||||||
compose_file = Path(sys.argv[2])
|
app_id = sys.argv[2]
|
||||||
catalog_file = Path(sys.argv[3])
|
|
||||||
caddy_file = Path(sys.argv[4])
|
|
||||||
|
|
||||||
def remove_block(lines, predicate):
|
con = sqlite3.connect(db_file)
|
||||||
out = []
|
|
||||||
i = 0
|
|
||||||
while i < len(lines):
|
|
||||||
line = lines[i]
|
|
||||||
if predicate(line):
|
|
||||||
indent = len(line) - len(line.lstrip())
|
|
||||||
i += 1
|
|
||||||
while i < len(lines):
|
|
||||||
next_line = lines[i]
|
|
||||||
if next_line.strip():
|
|
||||||
next_indent = len(next_line) - len(next_line.lstrip())
|
|
||||||
if next_indent <= indent:
|
|
||||||
break
|
|
||||||
i += 1
|
|
||||||
continue
|
|
||||||
out.append(line)
|
|
||||||
i += 1
|
|
||||||
return out
|
|
||||||
|
|
||||||
if compose_file.exists():
|
con.execute(
|
||||||
lines = compose_file.read_text().splitlines()
|
"DELETE FROM deployments WHERE app_id = ?",
|
||||||
lines = remove_block(lines, lambda l: l.startswith(f" {app_id}:"))
|
(app_id,)
|
||||||
compose_file.write_text("\n".join(lines).rstrip() + "\n")
|
)
|
||||||
|
|
||||||
if catalog_file.exists():
|
con.execute(
|
||||||
lines = catalog_file.read_text().splitlines()
|
"DELETE FROM apps WHERE id = ?",
|
||||||
lines = remove_block(lines, lambda l: l.strip() == f"- id: {app_id}")
|
(app_id,)
|
||||||
catalog_file.write_text("\n".join(lines).rstrip() + "\n")
|
)
|
||||||
|
|
||||||
if caddy_file.exists():
|
con.commit()
|
||||||
lines = caddy_file.read_text().splitlines()
|
con.close()
|
||||||
lines = remove_block(lines, lambda l: f"/apps/{app_id}/*" in l)
|
|
||||||
caddy_file.write_text("\n".join(lines).rstrip() + "\n")
|
|
||||||
PY
|
PY
|
||||||
|
|
||||||
echo "Regenerating generated files..."
|
echo "Regenerating generated files..."
|
||||||
|
|
||||||
|
"$TOOLS_DIR/scripts/generate-catalog.sh"
|
||||||
"$TOOLS_DIR/scripts/generate-apps-compose.sh"
|
"$TOOLS_DIR/scripts/generate-apps-compose.sh"
|
||||||
"$TOOLS_DIR/scripts/generate-caddyfile.sh"
|
"$TOOLS_DIR/scripts/generate-caddyfile.sh"
|
||||||
"$TOOLS_DIR/scripts/generate-apps-page.sh"
|
"$TOOLS_DIR/scripts/generate-apps-page.sh"
|
||||||
|
|
||||||
|
echo "Reloading Caddy..."
|
||||||
|
|
||||||
|
docker exec appfactory-caddy caddy validate \
|
||||||
|
--config /etc/caddy/Caddyfile
|
||||||
|
|
||||||
|
docker exec appfactory-caddy caddy reload \
|
||||||
|
--config /etc/caddy/Caddyfile
|
||||||
|
|
||||||
|
echo ""
|
||||||
echo "DONE"
|
echo "DONE"
|
||||||
|
|||||||
Reference in New Issue
Block a user