Úprava skriptu delete-orphan-services.sh (portál: jiri.uhlir59@gmail.com)

This commit is contained in:
AppFactory Portal
2026-07-16 09:36:01 +00:00
parent bdb584a38c
commit 95503bd4d4
+37 -52
View File
@@ -1,37 +1,35 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
APPFACTORY_ROOT="/opt/appfactory" APPFACTORY_ROOT="/opt/appfactory"
DB_FILE="$APPFACTORY_ROOT/data/appfactory/appfactory.db" DB_FILE="$APPFACTORY_ROOT/data/appfactory/appfactory.db"
WORKSPACE_DIR="$APPFACTORY_ROOT/workspace" WORKSPACE_DIR="$APPFACTORY_ROOT/workspace"
DELETE_SCRIPT="$WORKSPACE_DIR/appfactory-tools/scripts/delete-app.sh" DELETE_SCRIPT="$WORKSPACE_DIR/appfactory-tools/scripts/delete-app.sh"
# Výchozí režim pouze vypisuje kandidáty.
DRY_RUN="${DRY_RUN:-1}" DRY_RUN="${DRY_RUN:-1}"
# Čerstvé adresáře nemažeme. Selhávající provisioning může ještě dobíhat.
MIN_AGE_HOURS="${MIN_AGE_HOURS:-24}" MIN_AGE_HOURS="${MIN_AGE_HOURS:-24}"
MIN_AGE_MINUTES=$((MIN_AGE_HOURS * 60)) MIN_AGE_MINUTES=$((MIN_AGE_HOURS * 60))
CORE_SERVICES=( PROTECTED_WORKSPACES=(
appfactory-tools
appfactory-portal
appfactory-worker
appfactory-monitor
appfactory-webhook
appfactory-infrastructure appfactory-infrastructure
appfactory-monitor
appfactory-portal
appfactory-tools
appfactory-webhook
appfactory-worker
python-fastapi-template
) )
log() { log() {
printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
} }
is_core_service() { is_protected() {
local candidate="$1" local candidate="$1"
local core local protected
for core in "${CORE_SERVICES[@]}"; do for protected in "${PROTECTED_WORKSPACES[@]}"; do
if [ "$candidate" = "$core" ]; then if [ "$candidate" = "$protected" ]; then
return 0 return 0
fi fi
done done
@@ -39,6 +37,14 @@ is_core_service() {
return 1 return 1
} }
is_registered() {
local candidate="$1"
sqlite3 "$DB_FILE" \
"SELECT 1 FROM apps WHERE id = '$candidate' LIMIT 1;" |
grep -qx 1
}
if [ ! -f "$DB_FILE" ]; then if [ ! -f "$DB_FILE" ]; then
log "ERROR: Databáze neexistuje: $DB_FILE" log "ERROR: Databáze neexistuje: $DB_FILE"
exit 1 exit 1
@@ -49,65 +55,40 @@ if [ ! -x "$DELETE_SCRIPT" ]; then
exit 1 exit 1
fi fi
# Bez důvěryhodného zdroje pravdy nic nemažeme.
if ! sqlite3 "$DB_FILE" \
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='apps';" |
grep -qx 1; then
log "ERROR: Tabulka apps nebyla nalezena. Automatické mazání zastaveno."
exit 1
fi
mapfile -t REGISTERED_APPS < <(
sqlite3 "$DB_FILE" \
"SELECT id FROM apps WHERE id IS NOT NULL AND trim(id) <> '' ORDER BY id;"
)
is_registered_app() {
local candidate="$1"
local registered
for registered in "${REGISTERED_APPS[@]}"; do
if [ "$candidate" = "$registered" ]; then
return 0
fi
done
return 1
}
found=0 found=0
deleted=0
failed=0
while IFS= read -r app_dir; do while IFS= read -r app_dir; do
app_id="$(basename "$app_dir")" app_id="$(basename "$app_dir")"
if is_core_service "$app_id"; then if is_protected "$app_id"; then
continue continue
fi fi
if is_registered_app "$app_id"; then if is_registered "$app_id"; then
continue continue
fi fi
# Nedotýkat se čerstvého workspace. if ! find "$app_dir" -maxdepth 0 -mmin "+$MIN_AGE_MINUTES" | grep -q .; then
if ! find "$app_dir" -maxdepth 0 -mmin "+$MIN_AGE_MINUTES" | log "SKIP fresh orphan candidate: $app_id"
grep -q .; then
continue continue
fi fi
found=$((found + 1)) found=$((found + 1))
log "ORPHAN candidate: $app_id" log "ORPHAN: $app_id"
if [ "$DRY_RUN" = "1" ]; then if [ "$DRY_RUN" = "1" ]; then
log "DRY-RUN: byl by spuštěn delete-app.sh $app_id" log "DRY-RUN: spustil by se delete-app.sh $app_id"
continue continue
fi fi
log "Deleting orphan: $app_id"
if "$DELETE_SCRIPT" "$app_id"; then if "$DELETE_SCRIPT" "$app_id"; then
log "Deleted: $app_id" deleted=$((deleted + 1))
log "DELETED: $app_id"
else else
log "ERROR: Mazání selhalo: $app_id" failed=$((failed + 1))
log "FAILED: $app_id"
fi fi
done < <( done < <(
find "$WORKSPACE_DIR" \ find "$WORKSPACE_DIR" \
@@ -118,4 +99,8 @@ done < <(
sort sort
) )
log "Finished. Orphan candidates: $found; DRY_RUN=$DRY_RUN" log "Finished. Found=$found Deleted=$deleted Failed=$failed DryRun=$DRY_RUN"
if [ "$failed" -gt 0 ]; then
exit 1
fi