This commit is contained in:
+74
-61
@@ -872,81 +872,94 @@ case "$COMMAND" in
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
remove)
|
remove)
|
||||||
FORCE_REMOVE=false
|
FORCE_REMOVE=false
|
||||||
TENANT_ARG="${2:-}"
|
|
||||||
|
|
||||||
if [ "$TENANT_ARG" = "--yes" ] || [ "$TENANT_ARG" = "-y" ]; then
|
# Fix: Prüfe sowohl Position 2 als auch 3 auf das -y Flag
|
||||||
FORCE_REMOVE=true
|
if [ "${2:-}" = "--yes" ] || [ "${2:-}" = "-y" ]; then
|
||||||
TENANT_ID="${3:-}"
|
FORCE_REMOVE=true
|
||||||
else
|
TENANT_ID="${3:-}"
|
||||||
TENANT_ID="$TENANT_ARG"
|
elif [ "${3:-}" = "--yes" ] || [ "${3:-}" = "-y" ]; then
|
||||||
|
FORCE_REMOVE=true
|
||||||
|
TENANT_ID="${2:-}"
|
||||||
|
else
|
||||||
|
TENANT_ID="${2:-}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$TENANT_ID" ]; then
|
||||||
|
echo "Error: Please provide a tenant_id to remove."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$FORCE_REMOVE" != true ]; then
|
||||||
|
echo -n "WARNING: Are you sure you want to permanently delete all data for tenant '$TENANT_ID'? (y/N) "
|
||||||
|
read confirm
|
||||||
|
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
||||||
|
echo "Removal canceled."
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -z "$TENANT_ID" ]; then
|
echo "Removing tenant '$TENANT_ID'..."
|
||||||
echo "Error: Please provide a tenant_id to remove."
|
APP_CONTAINER=$(docker ps -qf "name=app" | head -n 1)
|
||||||
exit 1
|
port_to_remove=""
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$FORCE_REMOVE" != true ]; then
|
if [ -n "$APP_CONTAINER" ]; then
|
||||||
echo -n "WARNING: Are you sure you want to permanently delete all data for tenant '$TENANT_ID'? (y/N) "
|
# MongoDB-Datenbank via PyMongo direkt im Container droppen
|
||||||
read confirm
|
# Fix 1: Wir hängen '|| true' an, damit das Skript nicht abstürzt, falls der Befehl fehlschlägt.
|
||||||
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
docker exec -i "$APP_CONTAINER" python3 -c '
|
||||||
echo "Removal canceled."
|
import sys, os
|
||||||
exit 0
|
try:
|
||||||
fi
|
import pymongo
|
||||||
fi
|
tenant_id = sys.argv[1]
|
||||||
|
# In Docker Compose heißt der Host oft "mongodb" statt "localhost"
|
||||||
|
mongo_uri = os.environ.get("MONGO_URI", "mongodb://mongodb:27017/")
|
||||||
|
client = pymongo.MongoClient(mongo_uri, serverSelectionTimeoutMS=2000)
|
||||||
|
|
||||||
echo "Removing tenant '$TENANT_ID'..."
|
# Fix 2: Wir versuchen beide Formate zu löschen, da "list" zeigt, dass sie "test" und nicht "inventar_test" heißen.
|
||||||
APP_CONTAINER=$(docker ps -qf "name=app" | head -n 1)
|
dbs_to_drop = [f"inventar_{tenant_id}", tenant_id]
|
||||||
port_to_remove=""
|
existing_dbs = client.list_database_names()
|
||||||
|
|
||||||
if [ -n "$APP_CONTAINER" ]; then
|
for db_name in dbs_to_drop:
|
||||||
# MongoDB-Datenbank via PyMongo direkt im Container droppen
|
if db_name in existing_dbs:
|
||||||
docker exec -i "$APP_CONTAINER" python3 -c '
|
|
||||||
import sys, os
|
|
||||||
try:
|
|
||||||
import pymongo
|
|
||||||
tenant_id = sys.argv[1]
|
|
||||||
mongo_uri = os.environ.get("MONGO_URI", "mongodb://localhost:27017/")
|
|
||||||
client = pymongo.MongoClient(mongo_uri, serverSelectionTimeoutMS=2000)
|
|
||||||
db_name = f"inventar_{tenant_id}"
|
|
||||||
if db_name in client.list_database_names():
|
|
||||||
client.drop_database(db_name)
|
client.drop_database(db_name)
|
||||||
print(f"Dropped database: {db_name}")
|
print(f"Dropped database: {db_name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error dropping database: {e}", file=sys.stderr)
|
print(f"Error dropping database: {e}", file=sys.stderr)
|
||||||
' "$TENANT_ID" > /dev/null 2>&1
|
' "$TENANT_ID" > /dev/null 2>&1 || true
|
||||||
|
|
||||||
# Konfiguration und Port via Host-Funktion bereinigen und Port ermitteln
|
# Konfiguration und Port via Host-Funktion bereinigen und Port ermitteln
|
||||||
if port_to_remove="$(remove_tenant_port "$TENANT_ID" 2>/dev/null)"; then
|
if port_to_remove="$(remove_tenant_port "$TENANT_ID" 2>/dev/null)"; then
|
||||||
:
|
:
|
||||||
else
|
|
||||||
port_to_remove=""
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Tenant '$TENANT_ID' database and config removed."
|
|
||||||
else
|
else
|
||||||
echo "Warning: Application container not running. Tenant database may still exist in MongoDB."
|
port_to_remove=""
|
||||||
if port_to_remove="$(remove_tenant_port "$TENANT_ID" 2>/dev/null)"; then
|
|
||||||
:
|
|
||||||
else
|
|
||||||
echo "Warning: tenant '$TENANT_ID' was not configured in config.json or could not be removed."
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "$port_to_remove" ]; then
|
echo "Tenant '$TENANT_ID' database and config removed."
|
||||||
remove_runtime_port "$port_to_remove"
|
else
|
||||||
fi
|
echo "Warning: Application container not running. Tenant database may still exist in MongoDB."
|
||||||
remove_tenant_nginx_config "$TENANT_ID"
|
if port_to_remove="$(remove_tenant_port "$TENANT_ID" 2>/dev/null)"; then
|
||||||
sync_tenant_port_map
|
:
|
||||||
if [ -n "$(docker ps -qf 'name=app' | head -n 1)" ]; then
|
|
||||||
restart_app_container
|
|
||||||
fi
|
|
||||||
if [ -n "$port_to_remove" ]; then
|
|
||||||
echo "Removed tenant '$TENANT_ID' and cleaned runtime port $port_to_remove."
|
|
||||||
else
|
else
|
||||||
echo "Removed tenant '$TENANT_ID'. No port mapping was present."
|
echo "Warning: tenant '$TENANT_ID' was not configured in config.json or could not be removed."
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$port_to_remove" ]; then
|
||||||
|
remove_runtime_port "$port_to_remove" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
remove_tenant_nginx_config "$TENANT_ID"
|
||||||
|
sync_tenant_port_map
|
||||||
|
|
||||||
|
if [ -n "$(docker ps -qf 'name=app' | head -n 1)" ]; then
|
||||||
|
restart_app_container || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$port_to_remove" ]; then
|
||||||
|
echo "Removed tenant '$TENANT_ID' and cleaned runtime port $port_to_remove."
|
||||||
|
else
|
||||||
|
echo "Removed tenant '$TENANT_ID'. No port mapping was present."
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
|
|
||||||
restart-tenant)
|
restart-tenant)
|
||||||
|
|||||||
Reference in New Issue
Block a user