forked from aditya-adiga/live_conversational_threads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-once.command
More file actions
executable file
·136 lines (112 loc) · 3.71 KB
/
setup-once.command
File metadata and controls
executable file
·136 lines (112 loc) · 3.71 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"
ROOT_DIR="$PWD"
VENV_PY="$ROOT_DIR/.venv/bin/python3"
ENV_FILE="$ROOT_DIR/lct_python_backend/.env"
PG_DATA="$ROOT_DIR/.postgres_data"
PG_LOG="$ROOT_DIR/.postgres.log"
PG_PORT="${PG_PORT:-5433}"
DB_URL_DEFAULT="postgresql://lct_user:lct_password@localhost:${PG_PORT}/lct_dev"
POSTGRES_BIN_ARM="/opt/homebrew/opt/postgresql@15/bin"
POSTGRES_BIN_INTEL="/usr/local/opt/postgresql@15/bin"
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
}
ensure_postgres_path() {
if [ -d "$POSTGRES_BIN_ARM" ]; then
export PATH="$POSTGRES_BIN_ARM:$PATH"
return
fi
if [ -d "$POSTGRES_BIN_INTEL" ]; then
export PATH="$POSTGRES_BIN_INTEL:$PATH"
return
fi
if ! command -v brew >/dev/null 2>&1; then
log "Homebrew not found. Installing Homebrew first..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
if ! brew list postgresql@15 >/dev/null 2>&1; then
log "Installing postgresql@15 with Homebrew..."
brew install postgresql@15
fi
if [ -d "$POSTGRES_BIN_ARM" ]; then
export PATH="$POSTGRES_BIN_ARM:$PATH"
elif [ -d "$POSTGRES_BIN_INTEL" ]; then
export PATH="$POSTGRES_BIN_INTEL:$PATH"
else
log "postgresql@15 binaries were not found after install."
exit 1
fi
}
ensure_python_env() {
if [ ! -x "$VENV_PY" ]; then
log "Creating Python virtual environment (.venv)..."
python3 -m venv "$ROOT_DIR/.venv"
fi
log "Installing Python dependencies..."
"$VENV_PY" -m pip install --upgrade pip setuptools wheel
"$VENV_PY" -m pip install -r "$ROOT_DIR/requirements.txt"
"$VENV_PY" -m pip install -r "$ROOT_DIR/lct_python_backend/requirements.txt"
}
ensure_frontend_deps() {
if ! command -v npm >/dev/null 2>&1; then
log "npm is required but not installed."
exit 1
fi
log "Installing frontend dependencies..."
npm --prefix "$ROOT_DIR/lct_app" install
}
ensure_postgres_cluster() {
if [ ! -d "$PG_DATA" ]; then
log "Initializing local PostgreSQL data directory at .postgres_data..."
initdb -D "$PG_DATA" --username=lct_user --pwfile=<(printf 'lct_password\n')
fi
if ! pg_ctl -D "$PG_DATA" status >/dev/null 2>&1; then
log "Starting local PostgreSQL on port ${PG_PORT}..."
pg_ctl -D "$PG_DATA" -l "$PG_LOG" -o "-p ${PG_PORT}" start >/dev/null
sleep 2
fi
if ! pg_ctl -D "$PG_DATA" status >/dev/null 2>&1; then
log "PostgreSQL failed to start. Check $PG_LOG"
exit 1
fi
if ! PGPASSWORD=lct_password psql -h localhost -p "$PG_PORT" -U lct_user -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='lct_dev'" | grep -q 1; then
log "Creating database lct_dev..."
createdb -h localhost -p "$PG_PORT" -U lct_user lct_dev
fi
}
ensure_env_file() {
if [ -f "$ENV_FILE" ]; then
log "Existing lct_python_backend/.env found. Leaving it unchanged."
return
fi
if [ -f "$ROOT_DIR/lct_python_backend/.env.example" ]; then
cp "$ROOT_DIR/lct_python_backend/.env.example" "$ENV_FILE"
else
touch "$ENV_FILE"
fi
if grep -q '^DATABASE_URL=' "$ENV_FILE"; then
sed -i.bak "s|^DATABASE_URL=.*|DATABASE_URL=${DB_URL_DEFAULT}|" "$ENV_FILE"
rm -f "$ENV_FILE.bak"
else
printf '\nDATABASE_URL=%s\n' "$DB_URL_DEFAULT" >> "$ENV_FILE"
fi
log "Created lct_python_backend/.env with local database defaults."
}
run_migrations() {
log "Running Alembic migrations..."
(
cd "$ROOT_DIR/lct_python_backend"
DATABASE_URL="$DB_URL_DEFAULT" "$VENV_PY" -m alembic upgrade head
)
}
log "Starting one-time setup for Live Conversational Threads..."
ensure_postgres_path
ensure_python_env
ensure_frontend_deps
ensure_postgres_cluster
ensure_env_file
run_migrations
log "Setup complete."
log "Next: run ./start.command"