-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·445 lines (382 loc) · 12.4 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·445 lines (382 loc) · 12.4 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env bash
set -euo pipefail
RUN_DOCKER=true
INTERACTIVE_ENV=false
# ------------------------------------------
# Helpers
# ------------------------------------------
read_default() {
local prompt="$1"
local default="$2"
local reply=""
read -r -p "$prompt [$default]: " reply
echo "${reply:-$default}"
}
gen_password() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -base64 24 | tr -d '/+=' | head -c 24
else
tr -dc 'A-Za-z0-9' </dev/urandom | head -c 24
fi
}
detect_host_ip() {
hostname -I 2>/dev/null | awk '{print $1}' || echo "<VM_IP>"
}
generate_env_interactive() {
echo
if [ "${1:-}" = "overwrite" ]; then
echo "📝 Reconfiguring .env (overwrite)"
else
echo "📝 Starting interactive setup"
fi
echo " (Press Enter to accept defaults shown in brackets)"
echo
local data_base app_user root_pw app_pw
data_base="$(read_default "Data directory base" "/var/lib/database-docker")"
app_user="$(read_default "Application username" "momod")"
echo
read -r -p "Auto-generate secure passwords? [Y/n]: " auto_pw
if [[ ! "$auto_pw" =~ ^[Nn]$ ]]; then
root_pw="$(gen_password)"
app_pw="$(gen_password)"
echo " ✓ Passwords generated automatically"
else
root_pw="$(read_default "Root/superuser password" "changeme-root")"
app_pw="$(read_default "Application user password" "changeme-app")"
fi
local mysql_port=3306 mariadb_port=3307 postgres_port=5432 pgbouncer_port=6432
local mysql_tz="Asia/Jakarta" mariadb_tz="Asia/Jakarta" postgres_tz="UTC"
if [ "$USE_MYSQL" = true ]; then
echo
echo "--- MySQL ---"
mysql_port="$(read_default "MySQL host port (accessible from other VMs)" "$mysql_port")"
mysql_tz="$(read_default "MySQL timezone" "$mysql_tz")"
fi
if [ "$USE_MARIADB" = true ]; then
echo
echo "--- MariaDB ---"
mariadb_port="$(read_default "MariaDB host port (accessible from other VMs)" "$mariadb_port")"
mariadb_tz="$(read_default "MariaDB timezone" "$mariadb_tz")"
fi
if [ "$USE_POSTGRES" = true ]; then
echo
echo "--- PostgreSQL + PgBouncer ---"
postgres_port="$(read_default "PostgreSQL direct port" "$postgres_port")"
pgbouncer_port="$(read_default "PgBouncer port (recommended for apps)" "$pgbouncer_port")"
postgres_tz="$(read_default "PostgreSQL timezone" "$postgres_tz")"
fi
cat > .env <<EOF
# Generated by setup.sh on $(date -Iseconds)
# Host ports below are exposed on this VM for remote access from other machines.
# ===============================
# MYSQL
# ===============================
MYSQL_HOST_PORT=${mysql_port}
MYSQL_ROOT_PASSWORD=${root_pw}
MYSQL_USER=${app_user}
MYSQL_PASSWORD=${app_pw}
MYSQL_TZ=${mysql_tz}
MYSQL_DATA_PATH=${data_base}/mysql/data
# ===============================
# MARIADB
# ===============================
MARIADB_HOST_PORT=${mariadb_port}
MARIADB_ROOT_PASSWORD=${root_pw}
MARIADB_USER=${app_user}
MARIADB_PASSWORD=${app_pw}
MARIADB_TZ=${mariadb_tz}
MARIADB_DATA_PATH=${data_base}/mariadb/data
# ===============================
# POSTGRESQL
# ===============================
POSTGRES_HOST_PORT=${postgres_port}
POSTGRES_USER_APP=${app_user}
POSTGRES_PASSWORD_APP=${app_pw}
POSTGRES_ROOT_PASSWORD=${root_pw}
POSTGRES_TZ=${postgres_tz}
POSTGRES_DATA_PATH=${data_base}/postgre/data
# ===============================
# PGBOUNCER
# ===============================
PGBOUNCER_PORT=${pgbouncer_port}
PGBOUNCER_POOL_MODE=transaction
PGBOUNCER_MAX_CLIENT_CONN=200
PGBOUNCER_DEFAULT_POOL_SIZE=20
PGBOUNCER_AUTH_USER=postgres
PGBOUNCER_AUTH_PASSWORD=${root_pw}
PGBOUNCER_APP_USER=${app_user}
PGBOUNCER_APP_PASSWORD=${app_pw}
EOF
chmod 600 .env
INTERACTIVE_ENV=true
echo
echo "✅ .env created at $(pwd)/.env"
}
select_engines_interactive() {
echo
echo "Which database engines do you want?"
echo " 1) PostgreSQL only (+ PgBouncer)"
echo " 2) MySQL only"
echo " 3) MariaDB only"
echo " 4) All engines"
echo " 5) Custom (enter numbers separated by space, e.g. 1 2)"
local engine_choice
engine_choice="$(read_default "Choice" "4")"
USE_POSTGRES=false
USE_MYSQL=false
USE_MARIADB=false
case "$engine_choice" in
1) USE_POSTGRES=true ;;
2) USE_MYSQL=true ;;
3) USE_MARIADB=true ;;
4) USE_POSTGRES=true; USE_MYSQL=true; USE_MARIADB=true ;;
*)
for n in $engine_choice; do
case $n in
1) USE_POSTGRES=true ;;
2) USE_MYSQL=true ;;
3) USE_MARIADB=true ;;
esac
done
;;
esac
if [ "$USE_POSTGRES" = false ] && [ "$USE_MYSQL" = false ] && [ "$USE_MARIADB" = false ]; then
echo "❌ No engine selected"
exit 1
fi
}
ensure_env() {
if [ -f .env ]; then
echo
echo "⚠️ .env already exists at $(pwd)/.env"
read -r -p "Overwrite and reconfigure? [y/N]: " overwrite_reply
if [[ "$overwrite_reply" =~ ^[Yy]$ ]]; then
cp .env ".env.bak.$(date +%Y%m%d%H%M%S)"
echo " ℹ️ Previous .env backed up"
if [ "$ENGINE_FLAGS" = false ]; then
select_engines_interactive
fi
generate_env_interactive overwrite
else
echo "ℹ️ Keeping existing .env"
fi
else
if [ "$ENGINE_FLAGS" = false ]; then
select_engines_interactive
fi
generate_env_interactive
fi
}
print_remote_access_info() {
local host_ip
host_ip="$(detect_host_ip)"
echo
echo "========================================="
echo " Remote Access (from other VMs)"
echo "========================================="
echo " VM IP: ${host_ip}"
echo
if [ "$USE_MYSQL" = true ]; then
echo " MySQL: ${host_ip}:${MYSQL_HOST_PORT} user=${MYSQL_USER}"
fi
if [ "$USE_MARIADB" = true ]; then
echo " MariaDB: ${host_ip}:${MARIADB_HOST_PORT} user=${MARIADB_USER}"
fi
if [ "$USE_POSTGRES" = true ]; then
echo " PostgreSQL: ${host_ip}:${POSTGRES_HOST_PORT} user=${POSTGRES_USER_APP}"
echo " PgBouncer: ${host_ip}:${PGBOUNCER_PORT} user=${POSTGRES_USER_APP} (recommended for apps)"
fi
echo
echo " Open firewall on this VM (example):"
[ "$USE_MYSQL" = true ] && echo " sudo ufw allow ${MYSQL_HOST_PORT}/tcp"
[ "$USE_MARIADB" = true ] && echo " sudo ufw allow ${MARIADB_HOST_PORT}/tcp"
[ "$USE_POSTGRES" = true ] && echo " sudo ufw allow ${POSTGRES_HOST_PORT}/tcp"
[ "$USE_POSTGRES" = true ] && echo " sudo ufw allow ${PGBOUNCER_PORT}/tcp"
echo
echo " Edit .env anytime, then: docker compose up -d"
}
# ------------------------------------------
# Engine selection flags
# ------------------------------------------
USE_POSTGRES=false
USE_MYSQL=false
USE_MARIADB=false
ENGINE_FLAGS=false
for arg in "$@"; do
case $arg in
--postgres|--mysql|--mariadb)
ENGINE_FLAGS=true
;;
esac
done
if [ "$ENGINE_FLAGS" = false ] && [ "$#" -eq 0 ] && [ -f .env ]; then
# Keeping existing .env without CLI flags — start all engines
USE_POSTGRES=true
USE_MYSQL=true
USE_MARIADB=true
else
for arg in "$@"; do
case $arg in
--no-run)
RUN_DOCKER=false
;;
--postgres)
USE_POSTGRES=true
;;
--mysql)
USE_MYSQL=true
;;
--mariadb)
USE_MARIADB=true
;;
--help|-h)
cat <<'HELP'
Usage: ./setup.sh [options]
Options:
--postgres Install PostgreSQL (+ PgBouncer)
--mysql Install MySQL
--mariadb Install MariaDB
--no-run Prepare directories only, skip Docker
-h, --help Show this help
If .env does not exist, an interactive wizard runs automatically.
If .env already exists, you will be asked whether to overwrite it (engine selection reappears on overwrite).
Examples:
./setup.sh --postgres # PostgreSQL only, interactive .env
./setup.sh --mysql --mariadb # MySQL + MariaDB
./setup.sh # All engines (default)
HELP
exit 0
;;
*)
echo "❌ Unknown option: $arg"
echo " Run ./setup.sh --help"
exit 1
;;
esac
done
fi
echo "========================================="
echo " Database Stack Initial Setup"
echo "========================================="
# ------------------------------------------
# Ensure .env exists (interactive if missing or on overwrite)
# ------------------------------------------
ensure_env
if [ "$USE_POSTGRES" = true ]; then echo " • PostgreSQL + PgBouncer"; fi
if [ "$USE_MYSQL" = true ]; then echo " • MySQL"; fi
if [ "$USE_MARIADB" = true ]; then echo " • MariaDB"; fi
set -a
source .env
set +a
# ------------------------------------------
# Validate required variables dynamically
# ------------------------------------------
REQUIRED_VARS=()
[ "$USE_MYSQL" = true ] && REQUIRED_VARS+=(MYSQL_DATA_PATH)
[ "$USE_MARIADB" = true ] && REQUIRED_VARS+=(MARIADB_DATA_PATH)
[ "$USE_POSTGRES" = true ] && REQUIRED_VARS+=(POSTGRES_DATA_PATH)
for VAR in "${REQUIRED_VARS[@]}"; do
if [ -z "${!VAR:-}" ]; then
echo "❌ Environment variable $VAR is not set"
exit 1
fi
done
# ------------------------------------------
# Create directories
# ------------------------------------------
echo
echo "📁 Creating persistent data directories..."
[ "$USE_MYSQL" = true ] && mkdir -p "$MYSQL_DATA_PATH"
[ "$USE_MARIADB" = true ] && mkdir -p "$MARIADB_DATA_PATH"
[ "$USE_POSTGRES" = true ] && mkdir -p "$POSTGRES_DATA_PATH"
# ------------------------------------------
# Fix ownership
# ------------------------------------------
echo
echo "🔐 Fixing ownership..."
if [ "$USE_MYSQL" = true ]; then
sudo chown -R 999:999 "$(dirname "$MYSQL_DATA_PATH")"
sudo chmod 750 "$MYSQL_DATA_PATH"
fi
if [ "$USE_MARIADB" = true ]; then
sudo chown -R 999:999 "$(dirname "$MARIADB_DATA_PATH")"
sudo chmod 750 "$MARIADB_DATA_PATH"
fi
if [ "$USE_POSTGRES" = true ]; then
sudo chown -R 999:999 "$(dirname "$POSTGRES_DATA_PATH")"
sudo chmod 700 "$POSTGRES_DATA_PATH"
fi
# ------------------------------------------
# Ensure executable scripts
# ------------------------------------------
echo
echo "🔧 Ensuring executable scripts..."
[ -d "./mysql/init" ] && chmod +x ./mysql/init/*.sh 2>/dev/null || true
[ -d "./mariadb/init" ] && chmod +x ./mariadb/init/*.sh 2>/dev/null || true
[ -d "./postgres/init" ] && chmod +x ./postgres/init/*.sh 2>/dev/null || true
[ -f "./pgbouncer/entrypoint.sh" ] && chmod +x ./pgbouncer/entrypoint.sh
[ -f "./reset-databases.sh" ] && chmod +x ./reset-databases.sh
[ -f "./scripts/mysql-make-superadmin.sh" ] && chmod +x ./scripts/mysql-make-superadmin.sh
[ -f "./scripts/mariadb-make-superadmin.sh" ] && chmod +x ./scripts/mariadb-make-superadmin.sh
# ------------------------------------------
# Fix stale shared-net (manual create vs Compose-managed)
# ------------------------------------------
fix_shared_net() {
if ! docker network inspect shared-net >/dev/null 2>&1; then
return 0
fi
local label containers
label="$(docker network inspect shared-net --format '{{index .Labels "com.docker.compose.network"}}' 2>/dev/null || true)"
containers="$(docker network inspect shared-net --format '{{len .Containers}}' 2>/dev/null || echo "0")"
if [ "$label" = "shared-net" ]; then
return 0
fi
echo
echo "⚠️ Network 'shared-net' exists but was created outside Compose (label mismatch)."
echo " This can happen if the network was created manually with 'docker network create'."
if [ "$containers" != "0" ]; then
echo
echo " Containers are still attached. Stop them first, then recreate the network:"
echo " docker compose down"
echo " docker network rm shared-net"
echo " ./setup.sh"
exit 1
fi
echo " No containers attached — removing old network (Compose will recreate it)..."
docker network rm shared-net
}
# ------------------------------------------
# Docker
# ------------------------------------------
if [ "$RUN_DOCKER" = true ]; then
fix_shared_net
echo
echo "🐳 Building selected services..."
SERVICES=()
if [ "$USE_MYSQL" = true ]; then
SERVICES+=(mysql)
fi
if [ "$USE_MARIADB" = true ]; then
SERVICES+=(mariadb)
fi
if [ "$USE_POSTGRES" = true ]; then
SERVICES+=(postgres pgbouncer) # auto include pgbouncer
fi
docker compose build "${SERVICES[@]}"
docker compose up -d "${SERVICES[@]}"
sleep 5
# Post start scripts
if [ "$USE_MYSQL" = true ]; then
docker exec mysql_db bash /scripts/mysql-make-superadmin.sh || true
fi
if [ "$USE_MARIADB" = true ]; then
docker exec mariadb_db bash /scripts/mariadb-make-superadmin.sh || true
fi
echo
echo "✅ Setup complete."
print_remote_access_info
else
echo
echo "ℹ️ Setup complete. Docker skipped (--no-run)."
print_remote_access_info
fi