File size: 3,403 Bytes
a9e8b86
 
 
 
 
 
 
5270984
 
 
a9e8b86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5270984
 
 
 
 
a9e8b86
 
 
 
 
 
5270984
a9e8b86
 
 
 
 
 
 
 
 
 
 
5270984
a9e8b86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
# Aether HF Space entrypoint
#
# Steps:
#   1. Restore SQLite db from HF Dataset (if backup exists)
#   2. Start backup cron (every AETHER_BACKUP_INTERVAL_MIN minutes)
#   3. Start auto-update cron (every AETHER_AUTO_UPDATE_INTERVAL_HOURS hours)
#   4. Run a non-blocking auto-update check at startup (HF free Spaces sleep, so cron alone is not reliable)
#   5. Trap SIGTERM/SIGINT to flush a final backup before exit
#   6. exec aether-gateway in the foreground

set -euo pipefail

log() {
  printf '[%s] aether-entrypoint: %s\n' "$(date -u +%FT%TZ)" "$*"
}

# ---------- step 1: restore ----------
log "step 1: restore SQLite db from HF Dataset (if exists)"
if [[ -n "${HF_TOKEN:-}" && -n "${AETHER_BACKUP_REPO:-}" ]]; then
  python3 /usr/local/bin/aether_restore.py || \
    log "restore failed (will start with fresh db)"
else
  log "HF_TOKEN or AETHER_BACKUP_REPO unset; skip restore"
fi
mkdir -p "${AETHER_BACKUP_DIR}"

# ---------- step 2: backup cron ----------
log "step 2: setup backup cron (every ${AETHER_BACKUP_INTERVAL_MIN}m)"
mkdir -p /var/log/aether /etc/cron.d
cat > /etc/cron.d/aether-backup <<EOFCRON
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HF_TOKEN=${HF_TOKEN:-}
AETHER_BACKUP_REPO=${AETHER_BACKUP_REPO}
AETHER_BACKUP_DIR=${AETHER_BACKUP_DIR}
*/${AETHER_BACKUP_INTERVAL_MIN} * * * * root /usr/local/bin/aether-backup.sh >> /var/log/aether/backup.log 2>&1
EOFCRON
chmod 0644 /etc/cron.d/aether-backup
touch /var/log/aether/backup.log

# ---------- step 3: auto-update cron ----------
if [[ "${AETHER_AUTO_UPDATE:-true}" == "true" ]]; then
  log "step 3: setup auto-update cron (every ${AETHER_AUTO_UPDATE_INTERVAL_HOURS}h)"
  cat > /etc/cron.d/aether-auto-update <<EOFCRON
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HF_TOKEN=${HF_TOKEN:-}
AETHER_HF_SPACE_REPO=${AETHER_HF_SPACE_REPO:-iiioooo1/aether-hf}
AETHER_IMAGE=${AETHER_IMAGE:-ghcr.io/fawney19/aether:latest}
0 */${AETHER_AUTO_UPDATE_INTERVAL_HOURS} * * * root /usr/local/bin/aether-auto-update.sh >> /var/log/aether/auto-update.log 2>&1
EOFCRON
  chmod 0644 /etc/cron.d/aether-auto-update
  touch /var/log/aether/auto-update.log

  log "step 4: start non-blocking auto-update check"
  (
    /usr/local/bin/aether-auto-update.sh || true
  ) >> /var/log/aether/auto-update.log 2>&1 &
else
  log "auto-update disabled"
fi

cron

# ---------- step 5: trap shutdown for final backup ----------
shutdown_backup() {
  log "received signal, flushing final backup"
  /usr/local/bin/aether-backup.sh || log "final backup failed"
  if [[ -n "${AETHER_PID:-}" ]] && kill -0 "$AETHER_PID" 2>/dev/null; then
    kill -TERM "$AETHER_PID" || true
    wait "$AETHER_PID" || true
  fi
  exit 0
}
trap shutdown_backup TERM INT

# ---------- step 6: launch aether-gateway ----------
log "step 5: launch aether-gateway on port ${APP_PORT}"
cd /opt/aether

# distroless layout: /opt/aether/current -> /opt/aether/releases/image
# binary: /opt/aether/current/bin/aether-gateway
exec_bin="/opt/aether/current/bin/aether-gateway"
if [[ ! -x "$exec_bin" ]]; then
  log "ERROR: $exec_bin not found or not executable"
  ls -la /opt/aether/current/bin/ || true
  exit 1
fi

"$exec_bin" &
AETHER_PID=$!
log "aether-gateway started (pid=${AETHER_PID})"
wait "$AETHER_PID"
EXIT_CODE=$?
log "aether-gateway exited (code=${EXIT_CODE})"
shutdown_backup
exit $EXIT_CODE