File size: 2,132 Bytes
f3bcb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
set -e

echo "=== TensorWall Initialization ==="
echo ""

# Wait for database
echo "Waiting for database..."
python -c "
import asyncio
import sys
from sqlalchemy import text
from backend.db.session import AsyncSessionLocal

async def wait_db():
    for i in range(30):
        try:
            async with AsyncSessionLocal() as session:
                await session.execute(text('SELECT 1'))
            return True
        except Exception:
            print(f'  Attempt {i+1}/30...')
            await asyncio.sleep(1)
    return False

if not asyncio.run(wait_db()):
    print('ERROR: Database not available')
    sys.exit(1)
print('Database ready.')
"

# Check if already initialized
echo ""
echo "Checking setup state..."
SETUP_DONE=$(python -c "
import asyncio
from sqlalchemy import text
from backend.db.session import AsyncSessionLocal

async def check():
    try:
        async with AsyncSessionLocal() as session:
            result = await session.execute(
                text(\"SELECT value FROM setup_state WHERE key = 'setup_completed'\")
            )
            row = result.fetchone()
            return 'true' if row and row[0] == 'true' else 'false'
    except Exception:
        return 'false'

print(asyncio.run(check()))
" 2>/dev/null || echo "false")

if [ "$SETUP_DONE" = "true" ]; then
    echo "Setup already completed. Skipping initialization."
    exit 0
fi

# Run setup wizard in non-interactive mode
echo ""
echo "Running setup wizard..."

# Generate password if not provided
if [ -z "$ADMIN_PASSWORD" ]; then
    ADMIN_PASSWORD=$(python -c "import secrets; print(secrets.token_urlsafe(16))")
    echo ""
    echo "=================================================="
    echo "  GENERATED ADMIN PASSWORD (SAVE THIS!)"
    echo "  $ADMIN_PASSWORD"
    echo "=================================================="
    echo ""
fi

# Run the setup
python -m backend.cli setup wizard \
    --non-interactive \
    --admin-email "${ADMIN_EMAIL:-admin@example.com}" \
    --admin-password "$ADMIN_PASSWORD" \
    --seed "${SEED_DATA:-development}"

echo ""
echo "=== Initialization Complete ==="