-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlauncher
More file actions
607 lines (494 loc) · 16.8 KB
/
launcher
File metadata and controls
607 lines (494 loc) · 16.8 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/usr/bin/env bash
#
# October CMS Docker Launcher
# A wrapper around docker-compose for managing October CMS containers
#
set -e
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONTAINERS_DIR="$SCRIPT_DIR/containers"
##############################################################################
# Helper Functions
##############################################################################
# Colors (only if terminal supports it)
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
else
RED=''
GREEN=''
YELLOW=''
CYAN=''
BOLD=''
NC=''
fi
# Debug mode
DEBUG=${DEBUG:-0}
info() {
printf "${CYAN}INFO:${NC} %b\n" "$1"
}
success() {
printf "${GREEN}SUCCESS:${NC} %b\n" "$1"
}
warning() {
printf "${YELLOW}WARNING:${NC} %b\n" "$1"
}
error() {
printf "${RED}ERROR:${NC} %b\n" "$1"
}
fatal() {
error "$1"
exit 1
}
debug() {
if [ "$DEBUG" = "1" ]; then
printf "${CYAN}DEBUG:${NC} %b\n" "$1"
fi
}
show_help() {
printf "${BOLD}October CMS Docker Launcher${NC}\n"
cat << 'EOF'
Usage: ./launcher <command> <site_name>
Commands:
start <site> Start the site containers
stop <site> Stop the site containers
restart <site> Restart the site containers
rebuild <site> Rebuild and restart containers (after Dockerfile changes)
destroy <site> Stop and remove containers (preserves data)
wipe <site> Completely remove a site (containers, files, config)
enter <site> Open a shell in the web container
logs <site> View container logs (follow mode)
code <site> Open VS Code attached to the web container
status Show status of all sites
Windows (WSL only):
mount Share sites via Samba and mount as a Windows drive
unmount Disconnect the Windows drive and stop Samba
Examples:
./launcher start app
./launcher logs app
./launcher enter app
./launcher rebuild app
Site configurations are stored in containers/<site>.yml
Site data is stored at the path specified in the configuration.
EOF
}
##############################################################################
# Configuration Parsing
##############################################################################
# Parse YAML value (simple key: value parsing)
parse_yaml_value() {
local file="$1"
local key="$2"
grep -E "^${key}:" "$file" 2>/dev/null | sed 's/^[^:]*:[[:space:]]*//' | sed 's/^"\(.*\)"$/\1/' | sed "s/^'\(.*\)'$/\1/" | tr -d '\r'
}
# Parse nested YAML value (e.g., services.mariadb)
parse_yaml_nested() {
local file="$1"
local parent="$2"
local key="$3"
# Simple approach: look for key under parent section
awk -v parent="$parent" -v key="$key" '
$0 ~ "^"parent":" { in_parent=1; next }
in_parent && /^[a-z]/ { in_parent=0 }
in_parent && $0 ~ "^ "key":" { gsub(/^ [^:]*:[[:space:]]*/, ""); print; exit }
' "$file" | tr -d '\r'
}
# Expand ~ to home directory
expand_path() {
local path="$1"
echo "${path/#\~/$HOME}"
}
# Load site configuration
load_config() {
local site_name="$1"
local config_file="$CONTAINERS_DIR/${site_name}.yml"
if [ ! -f "$config_file" ]; then
fatal "Configuration not found: $config_file\nRun './october-setup $site_name' to create it."
fi
# Parse configuration
SITE_NAME="$site_name"
SITE_PATH=$(expand_path "$(parse_yaml_value "$config_file" "site_path")")
ENVIRONMENT=$(parse_yaml_value "$config_file" "environment")
HTTP_PORT=$(parse_yaml_value "$config_file" "http_port")
HTTPS_PORT=$(parse_yaml_value "$config_file" "https_port")
SSL_MODE=$(parse_yaml_value "$config_file" "ssl_mode")
# Service toggles
USE_MARIADB=$(parse_yaml_nested "$config_file" "services" "mariadb")
USE_REDIS=$(parse_yaml_nested "$config_file" "services" "redis")
# External service hosts (if not using bundled)
EXT_DB_HOST=$(parse_yaml_value "$config_file" "db_host")
EXT_REDIS_HOST=$(parse_yaml_value "$config_file" "redis_host")
# Defaults
HTTP_PORT="${HTTP_PORT:-80}"
if [ "$ENVIRONMENT" = "production" ]; then
HTTPS_PORT="${HTTPS_PORT:-443}"
fi
ENVIRONMENT="${ENVIRONMENT:-development}"
USE_MARIADB="${USE_MARIADB:-true}"
USE_REDIS="${USE_REDIS:-true}"
# Set environment-specific defaults
if [ "$ENVIRONMENT" = "production" ]; then
APP_ENV="production"
APP_DEBUG="false"
XDEBUG_MODE="off"
OPCACHE_VALIDATE_TIMESTAMPS="0"
OPCACHE_REVALIDATE_FREQ="0"
else
APP_ENV="local"
APP_DEBUG="true"
XDEBUG_MODE="debug"
OPCACHE_VALIDATE_TIMESTAMPS="1"
OPCACHE_REVALIDATE_FREQ="2"
fi
# Validate
if [ -z "$SITE_PATH" ]; then
fatal "site_path not defined in $config_file"
fi
# Export for docker-compose
export SITE_NAME SITE_PATH HTTP_PORT HTTPS_PORT ENVIRONMENT
export APP_ENV APP_DEBUG XDEBUG_MODE
export OPCACHE_VALIDATE_TIMESTAMPS OPCACHE_REVALIDATE_FREQ
export OPCACHE_ENABLE=1
}
##############################################################################
# Docker Compose Helpers
##############################################################################
# Build docker-compose command with appropriate files
compose_cmd() {
local cmd="docker compose -p ${SITE_NAME} -f ${SCRIPT_DIR}/docker-compose.base.yml"
# Include HTTPS port mapping for production
if [ "$ENVIRONMENT" = "production" ]; then
cmd="$cmd -f ${SCRIPT_DIR}/docker-compose.ssl.yml"
fi
# Exclude services if using external
if [ "$USE_MARIADB" != "true" ]; then
export DB_HOST="${EXT_DB_HOST}"
else
export DB_HOST="mariadb"
fi
if [ "$USE_REDIS" != "true" ]; then
export REDIS_HOST="${EXT_REDIS_HOST}"
else
export REDIS_HOST="redis"
fi
echo "$cmd"
}
# Get list of services to start
get_services() {
local services="web"
if [ "$USE_MARIADB" = "true" ]; then
services="$services mariadb"
fi
if [ "$USE_REDIS" = "true" ]; then
services="$services redis"
fi
echo "$services"
}
##############################################################################
# Commands
##############################################################################
cmd_start() {
local site_name="$1"
load_config "$site_name"
info "Starting $site_name..."
info " Site path: $SITE_PATH"
info " Environment: $ENVIRONMENT"
info " HTTP port: $HTTP_PORT"
# Ensure site directory exists
if [ ! -d "$SITE_PATH" ]; then
fatal "Site directory not found: $SITE_PATH\nRun './october-setup $site_name' first."
fi
# Ensure data directories exist with correct ownership
# MariaDB/Redis run as UID 999 and need to own their data dirs
if [ "$USE_MARIADB" = "true" ]; then
docker run --rm -v "$SITE_PATH/.data":/data alpine sh -c "mkdir -p /data/mysql && chown -R 999:999 /data/mysql"
fi
if [ "$USE_REDIS" = "true" ]; then
docker run --rm -v "$SITE_PATH/.data":/data alpine sh -c "mkdir -p /data/redis && chown -R 999:999 /data/redis"
fi
local services=$(get_services)
$(compose_cmd) up -d $services
success "Site $site_name started!"
echo ""
echo "Access your site at: http://localhost:${HTTP_PORT}"
if [ "$ENVIRONMENT" = "production" ] && [ -n "$HTTPS_PORT" ]; then
echo " https://localhost:${HTTPS_PORT}"
fi
}
cmd_stop() {
local site_name="$1"
load_config "$site_name"
info "Stopping $site_name..."
$(compose_cmd) down
success "Site $site_name stopped."
}
cmd_restart() {
local site_name="$1"
cmd_stop "$site_name"
cmd_start "$site_name"
}
cmd_rebuild() {
local site_name="$1"
load_config "$site_name"
info "Rebuilding $site_name..."
$(compose_cmd) down
$(compose_cmd) build --no-cache
local services=$(get_services)
$(compose_cmd) up -d $services
success "Site $site_name rebuilt and started!"
}
cmd_destroy() {
local site_name="$1"
load_config "$site_name"
warning "This will stop and remove containers for $site_name."
warning "Site data in $SITE_PATH will be preserved."
read -p "Are you sure? [y/N]: " confirm
if [[ ! "$confirm" =~ ^[Yy] ]]; then
info "Cancelled."
exit 0
fi
info "Destroying containers for $site_name..."
$(compose_cmd) down -v --remove-orphans
success "Containers removed. Data preserved in $SITE_PATH"
}
cmd_wipe() {
local site_name="$1"
load_config "$site_name"
local expanded_path="${SITE_PATH/#\~/$HOME}"
local config_file="$CONTAINERS_DIR/${site_name}.yml"
echo ""
error "This will permanently delete everything for $site_name:"
echo " - Docker containers and volumes"
echo " - Site files: $expanded_path"
echo " - Configuration: $config_file"
echo ""
read -p "Type the site name to confirm: " confirm
if [ "$confirm" != "$site_name" ]; then
info "Cancelled."
exit 0
fi
info "Destroying containers..."
$(compose_cmd) down -v --remove-orphans 2>/dev/null || true
if [ -d "$expanded_path" ]; then
info "Removing site files: $expanded_path"
rm -rf "$expanded_path"
fi
if [ -f "$config_file" ]; then
info "Removing configuration: $config_file"
rm -f "$config_file"
fi
success "Site $site_name has been completely removed."
}
cmd_enter() {
local site_name="$1"
load_config "$site_name"
info "Entering $site_name web container..."
$(compose_cmd) exec web bash
}
cmd_logs() {
local site_name="$1"
load_config "$site_name"
$(compose_cmd) logs -f
}
cmd_code() {
local site_name="$1"
load_config "$site_name"
local container_name="${SITE_NAME}_web"
# Check if container is running
if ! docker ps --format '{{.Names}}' | grep -q "^${container_name}$"; then
fatal "Container $container_name is not running.\nStart it first: ./launcher start $site_name"
fi
local container_id
container_id=$(docker inspect --format '{{.Id}}' "$container_name")
info "Opening VS Code attached to $container_name..."
local hex_id
hex_id=$(printf '%s' "$container_name" | od -A n -t x1 | tr -d ' \n')
code --folder-uri "vscode-remote://attached-container+${hex_id}/var/www/html"
}
cmd_status() {
printf "${BOLD}October CMS Sites${NC}\n"
echo "================="
echo ""
if [ ! -d "$CONTAINERS_DIR" ] || [ -z "$(ls -A "$CONTAINERS_DIR" 2>/dev/null)" ]; then
info "No sites configured. Run './october-setup <name>' to create one."
exit 0
fi
for config in "$CONTAINERS_DIR"/*.yml; do
[ -f "$config" ] || continue
local name=$(basename "$config" .yml)
local path=$(expand_path "$(parse_yaml_value "$config" "site_path")")
local env=$(parse_yaml_value "$config" "environment")
local port=$(parse_yaml_value "$config" "http_port")
# Check if running
local status_text
if docker ps --format '{{.Names}}' | grep -q "^${name}-web"; then
status_text="${GREEN}RUNNING${NC}"
else
status_text="${RED}stopped${NC}"
fi
printf "%-15s %-12s %-8s %b\n" "$name" "[$env]" ":$port" "$status_text"
echo " $path"
echo ""
done
}
##############################################################################
# Windows Drive Mount (WSL only)
##############################################################################
SITES_BASE_DIR="$HOME/october-sites"
SAMBA_CONF="/etc/samba/smb.conf"
SAMBA_SHARE_NAME="sites"
is_wsl() {
grep -qi microsoft /proc/version 2>/dev/null
}
get_wsl_ip() {
hostname -I | awk '{print $1}'
}
ensure_samba_installed() {
if ! command -v smbd > /dev/null 2>&1; then
info "Installing Samba (one-time setup)..."
sudo apt-get update -qq && sudo apt-get install -y -qq samba > /dev/null 2>&1
if [ $? -ne 0 ]; then
fatal "Failed to install Samba. Run 'sudo apt-get install samba' manually."
fi
success "Samba installed."
fi
}
configure_samba() {
# Create system user and samba user with known password
if ! id october > /dev/null 2>&1; then
sudo useradd -r -s /usr/sbin/nologin october
fi
if ! sudo pdbedit -L 2>/dev/null | grep -q "^october:"; then
(echo "october"; echo "october") | sudo smbpasswd -a -s october > /dev/null 2>&1
fi
# Check if our share already exists in config
if grep -q "\[${SAMBA_SHARE_NAME}\]" "$SAMBA_CONF" 2>/dev/null; then
return 0
fi
info "Configuring Samba share..."
# Append our share config
sudo tee -a "$SAMBA_CONF" > /dev/null << EOF
[${SAMBA_SHARE_NAME}]
path = ${SITES_BASE_DIR}
browseable = yes
read only = no
valid users = october
force user = root
create mask = 0664
directory mask = 0775
EOF
}
cmd_mount() {
# Check we're in WSL
if ! is_wsl; then
fatal "The mount command is only available in WSL (Windows Subsystem for Linux)."
fi
# Ensure sites directory exists
if [ ! -d "$SITES_BASE_DIR" ]; then
fatal "Sites directory not found: $SITES_BASE_DIR"
fi
# Check if Samba is already running
if pgrep -x smbd > /dev/null 2>&1; then
local wsl_ip=$(get_wsl_ip)
warning "Samba is already running."
echo ""
info "To mount, run this in PowerShell on Windows:"
echo " net use <DRIVE>: \\\\${wsl_ip}\\${SAMBA_SHARE_NAME}"
exit 0
fi
# Ask for drive letter
local drive_letter=""
while true; do
read -p "Drive letter to mount [Z]: " drive_letter
drive_letter="${drive_letter:-Z}"
drive_letter=$(echo "$drive_letter" | tr '[:lower:]' '[:upper:]')
# Validate single letter
if [[ "$drive_letter" =~ ^[A-Z]$ ]]; then
break
else
error "Please enter a single letter (A-Z)."
fi
done
# Install and configure Samba if needed
ensure_samba_installed
configure_samba
# Start Samba
info "Starting Samba..."
sudo smbd --no-process-group > /dev/null 2>&1 &
sleep 2
if ! pgrep -x smbd > /dev/null 2>&1; then
fatal "Failed to start Samba."
fi
local wsl_ip=$(get_wsl_ip)
info "Mounting drive ${drive_letter}: via PowerShell..."
powershell.exe -Command "net use ${drive_letter}: \\\\${wsl_ip}\\${SAMBA_SHARE_NAME} /user:october october" 2>/dev/null
if [ $? -eq 0 ]; then
success "Mounted ${drive_letter}:\\ -> ${SITES_BASE_DIR}"
echo ""
echo " Your sites are available at ${drive_letter}:\\"
echo " Use Tortoise Git and other Windows tools normally."
echo ""
echo " Run './launcher unmount' to disconnect."
else
warning "Auto-mount failed. You can mount manually in PowerShell:"
echo " net use ${drive_letter}: \\\\${wsl_ip}\\${SAMBA_SHARE_NAME} /user:october october"
fi
}
cmd_unmount() {
if ! is_wsl; then
fatal "The unmount command is only available in WSL (Windows Subsystem for Linux)."
fi
# Disconnect drive while Samba is still running (cleaner disconnect)
info "Disconnecting Windows drive..."
local wsl_ip=$(get_wsl_ip)
powershell.exe -Command "net use | Select-String '${SAMBA_SHARE_NAME}' | ForEach-Object { if (\$_ -match '([A-Z]:)') { net use \$Matches[1] /delete /yes } }" 2>/dev/null || true
# Clean up any remembered connections in the registry
powershell.exe -Command "'X','Z','Y','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E' | ForEach-Object { Remove-Item -Path \"HKCU:\\Network\\\$_\" -Recurse -ErrorAction SilentlyContinue }" 2>/dev/null || true
# Stop Samba
if pgrep -x smbd > /dev/null 2>&1; then
info "Stopping Samba..."
sudo pkill smbd 2>/dev/null
fi
success "Samba stopped and drive disconnected."
}
##############################################################################
# Main
##############################################################################
main() {
if [ $# -lt 1 ]; then
show_help
exit 1
fi
local command="$1"
local site_name="$2"
case "$command" in
start|stop|restart|rebuild|destroy|wipe|enter|logs|code)
if [ -z "$site_name" ]; then
fatal "Site name required. Usage: ./launcher $command <site_name>"
fi
cmd_$command "$site_name"
;;
status)
cmd_status
;;
mount)
cmd_mount
;;
unmount)
cmd_unmount
;;
help|--help|-h)
show_help
;;
*)
error "Unknown command: $command"
show_help
exit 1
;;
esac
}
main "$@"