forked from InvoiceShelf/InvoiceShelf
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevenv
More file actions
executable file
·596 lines (508 loc) · 17.8 KB
/
devenv
File metadata and controls
executable file
·596 lines (508 loc) · 17.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
#!/bin/bash
# InvoiceShelf Development Environment Startup Script
# Supports Linux and macOS
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " (no command) Start the development environment (interactive setup)"
echo " start Start the development environment with existing configuration"
echo " stop Stop the development environment"
echo " destroy Stop and remove containers, networks, and images"
echo " logs Show logs from all services"
echo " rebuild Rebuild and restart the development environment"
echo " shell Enter the PHP container's shell"
echo " run [CMD] Run a command inside the PHP container (e.g., ./artisan)"
echo " test Run the test suite (Pest)"
echo " format Format the code (Pint)"
echo ""
echo "Examples:"
echo " $0 # Interactive setup and start"
echo " $0 start # Start with last used configuration"
echo " $0 stop # Stop all services"
echo " $0 logs # View logs"
echo " $0 shell # Get a shell inside the PHP container"
echo " $0 run ./artisan --help"
}
# Function to detect OS
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux";;
Darwin*) echo "mac";;
*) echo "unsupported";;
esac
}
# Function to get docker compose command
get_docker_compose_cmd() {
if command -v docker-compose &> /dev/null; then
echo "docker-compose"
else
echo "docker compose"
fi
}
# Function to find the most recent compose file used
find_compose_file() {
local config_file=".devenvconfig"
if [ -f "$config_file" ]; then
cat "$config_file"
else
echo ""
fi
}
# Function to save compose file configuration
save_config() {
local compose_file=$1
echo "$compose_file" > .devenvconfig
}
# Function to check if host entry exists
check_host_entry() {
if grep -q "invoiceshelf.test" /etc/hosts; then
return 0
else
return 1
fi
}
# Function to add host entry
add_host_entry() {
local os_type=$1
print_info "Adding invoiceshelf.test to /etc/hosts..."
if [ "$os_type" = "linux" ] || [ "$os_type" = "mac" ]; then
# Check if we can write to /etc/hosts
if [ ! -w /etc/hosts ]; then
print_warning "Root permissions required to modify /etc/hosts"
if sudo sh -c 'echo "127.0.0.1 invoiceshelf.test" >> /etc/hosts'; then
print_success "Host entry added successfully"
else
print_error "Failed to add host entry"
exit 1
fi
else
echo "127.0.0.1 invoiceshelf.test" >> /etc/hosts
print_success "Host entry added successfully"
fi
fi
}
# Function to select database type
select_database() {
echo "" >&2
print_info "Select database type:" >&2
echo "1) MySQL/MariaDB" >&2
echo "2) PostgreSQL" >&2
echo "3) SQLite" >&2
while true; do
read -p "Enter your choice (1-3): " db_choice
case $db_choice in
1) echo "mysql"; break;;
2) echo "pgsql"; break;;
3) echo "sqlite"; break;;
*) print_error "Invalid choice. Please enter 1, 2, or 3." >&2;;
esac
done
}
# Function to ask about gotenberg
ask_gotenberg() {
echo "" >&2
print_info "Do you want to use Gotenberg for PDF generation?" >&2
print_info "Gotenberg provides better PDF generation capabilities but requires more resources." >&2
while true; do
read -p "Use Gotenberg? (y/n): " gotenberg_choice
case $gotenberg_choice in
[Yy]* ) echo "yes"; break;;
[Nn]* ) echo "no"; break;;
* ) print_error "Please answer yes (y) or no (n)." >&2;;
esac
done
}
# Function to validate environment
validate_environment() {
# Detect OS
local os_type=$(detect_os)
if [ "$os_type" = "unsupported" ]; then
print_error "Unsupported operating system. This script only supports Linux and macOS."
exit 1
fi
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
echo ""
print_info "Installation instructions:"
if [ "$os_type" = "linux" ]; then
echo " • Ubuntu/Debian: https://docs.docker.com/engine/install/ubuntu/"
echo " • CentOS/RHEL: https://docs.docker.com/engine/install/centos/"
echo " • Fedora: https://docs.docker.com/engine/install/fedora/"
else
echo " • macOS: https://docs.docker.com/desktop/mac/install/"
fi
exit 1
fi
# Check if Docker daemon is running
print_info "Checking Docker daemon status..."
if ! docker info &> /dev/null; then
print_error "Docker daemon is not running."
echo ""
print_info "To start Docker:"
if [ "$os_type" = "linux" ]; then
echo " • systemctl start docker"
echo " • sudo systemctl start docker (if not in docker group)"
echo " • Or start Docker Desktop if using it"
else
echo " • Start Docker Desktop application"
echo " • Or run: open -a Docker"
fi
exit 1
fi
# Check Docker daemon connectivity and permissions
if ! docker ps &> /dev/null; then
print_error "Cannot connect to Docker daemon. Permission denied."
echo ""
print_info "This might be a permissions issue. Try:"
if [ "$os_type" = "linux" ]; then
echo " • sudo usermod -aG docker \$USER"
echo " • Log out and log back in"
echo " • Or run the script with sudo (not recommended)"
else
echo " • Restart Docker Desktop"
echo " • Check Docker Desktop settings"
fi
exit 1
fi
# Check if Docker Compose is available
local compose_available=false
local compose_cmd=""
if command -v docker-compose &> /dev/null; then
if docker-compose version &> /dev/null; then
compose_available=true
compose_cmd="docker-compose"
fi
fi
if ! $compose_available && docker compose version &> /dev/null 2>&1; then
compose_available=true
compose_cmd="docker compose"
fi
if ! $compose_available; then
print_error "Docker Compose is not available or not working properly."
echo ""
print_info "Installation instructions:"
if [ "$os_type" = "linux" ]; then
echo " • Install docker-compose: sudo apt-get install docker-compose"
echo " • Or use Docker Compose V2: https://docs.docker.com/compose/install/"
else
echo " • Docker Compose should be included with Docker Desktop"
echo " • Try restarting Docker Desktop"
fi
exit 1
fi
# Check available system resources
print_info "Checking system resources..."
# Check available disk space (require at least 2GB free)
local available_space
if [ "$os_type" = "linux" ]; then
available_space=$(df / | awk 'NR==2 {print $4}')
else
available_space=$(df / | awk 'NR==2 {print $4}')
fi
# Convert KB to GB (approximate)
local available_gb=$((available_space / 1024 / 1024))
if [ $available_gb -lt 2 ]; then
print_warning "Low disk space detected: ${available_gb}GB available"
print_warning "Docker images and containers require significant disk space"
read -p "Continue anyway? (y/N): " continue_choice
case $continue_choice in
[Yy]* ) ;;
* ) print_info "Operation cancelled."; exit 1;;
esac
fi
# Check if Docker has enough resources allocated (for Docker Desktop)
if [ "$os_type" = "mac" ] || ([ "$os_type" = "linux" ] && command -v docker-desktop &> /dev/null); then
print_info "Detected Docker Desktop - ensure adequate resources are allocated"
print_info "Recommended: 4GB RAM, 2 CPU cores, 20GB disk space"
fi
print_success "Docker environment validation completed"
}
# Function to get compose file for subcommands
get_compose_file_for_cmd() {
local compose_file=$(find_compose_file)
if [ -z "$compose_file" ] || [ ! -f "$compose_file" ]; then
print_error "No previous configuration found. Please run the script without arguments first to set up the environment."
exit 1
fi
echo "$compose_file"
}
# Subcommand functions
cmd_start() {
print_info "Starting development environment..."
local compose_file=$(get_compose_file_for_cmd)
local docker_compose=$(get_docker_compose_cmd)
print_info "Using compose file: $compose_file"
if $docker_compose -f "$compose_file" up -d; then
print_success "Development environment started successfully!"
show_service_info
else
print_error "Failed to start development environment"
exit 1
fi
}
cmd_stop() {
print_info "Stopping development environment..."
local compose_file=$(get_compose_file_for_cmd)
local docker_compose=$(get_docker_compose_cmd)
if $docker_compose -f "$compose_file" down --remove-orphans; then
print_success "Development environment stopped successfully!"
else
print_error "Failed to stop development environment"
exit 1
fi
}
cmd_destroy() {
print_info "Destroying development environment (removing containers, networks, and images)..."
local compose_file=$(get_compose_file_for_cmd)
local docker_compose=$(get_docker_compose_cmd)
print_warning "This will remove all containers, networks, and images for this project."
read -p "Are you sure? (y/N): " confirm
case $confirm in
[Yy]* )
if $docker_compose -f "$compose_file" down --rmi all --volumes --remove-orphans; then
print_success "Development environment destroyed successfully!"
# Remove the config file since everything is destroyed
rm -f .devenvconfig
else
print_error "Failed to destroy development environment"
exit 1
fi
;;
* )
print_info "Operation cancelled."
;;
esac
}
cmd_logs() {
print_info "Showing logs from development environment..."
local compose_file=$(get_compose_file_for_cmd)
local docker_compose=$(get_docker_compose_cmd)
# Check if any additional arguments were passed for specific services
shift # Remove 'logs' from arguments
if [ $# -gt 0 ]; then
print_info "Showing logs for services: $*"
$docker_compose -f "$compose_file" logs -f "$@"
else
print_info "Showing logs for all services (Press Ctrl+C to exit)"
$docker_compose -f "$compose_file" logs -f
fi
}
cmd_rebuild() {
print_info "Rebuilding development environment..."
local compose_file=$(get_compose_file_for_cmd)
local docker_compose=$(get_docker_compose_cmd)
print_info "Stopping services..."
$docker_compose -f "$compose_file" down
print_info "Rebuilding images..."
if $docker_compose -f "$compose_file" build --no-cache; then
print_info "Starting services..."
if $docker_compose -f "$compose_file" up -d; then
print_success "Development environment rebuilt and started successfully!"
show_service_info
else
print_error "Failed to start development environment after rebuild"
exit 1
fi
else
print_error "Failed to rebuild development environment"
exit 1
fi
}
cmd_shell() {
print_info "Entering PHP container shell..."
# Check if the container is running first
if ! docker ps --format "{{.Names}}" | grep -q "invoiceshelf-dev-php"; then
print_error "PHP container 'invoiceshelf-dev-php' is not running."
print_info "Start the development environment first with: $0 start"
exit 1
fi
print_info "Connecting to invoiceshelf-dev-php container... Type 'exit' to leave."
# Try /bin/bash first, fall back to /bin/sh if bash is not available
if ! docker exec -it invoiceshelf-dev-php /bin/bash; then
docker exec -it invoiceshelf-dev-php /bin/sh
fi
print_success "Exited from container shell."
}
cmd_run() {
shift # Remove 'run' from arguments
if [ $# -eq 0 ]; then
print_error "No command provided to 'run'."
show_usage
exit 1
fi
print_info "Running command inside PHP container: $@"
# Check if the container is running first
if ! docker ps --format "{{.Names}}" | grep -q "invoiceshelf-dev-php"; then
print_error "PHP container 'invoiceshelf-dev-php' is not running."
print_info "Start the development environment first with: $0 start"
exit 1
fi
docker exec -it -w /var/www/html invoiceshelf-dev-php "$@"
}
# Function to run tests
cmd_test() {
print_info "Running tests (Pest)..."
# Check if the container is running first
if ! docker ps --format "{{.Names}}" | grep -q "invoiceshelf-dev-php"; then
print_error "PHP container 'invoiceshelf-dev-php' is not running."
print_info "Start the development environment first with: $0 start"
exit 1
fi
shift # Remove 'test' from arguments
docker exec -it -w /var/www/html invoiceshelf-dev-php /var/www/html/vendor/bin/pest "$@"
}
# Function to format code
cmd_format() {
print_info "Formatting code (Pint)..."
# Check if the container is running first
if ! docker ps --format "{{.Names}}" | grep -q "invoiceshelf-dev-php"; then
print_error "PHP container 'invoiceshelf-dev-php' is not running."
print_info "Start the development environment first with: $0 start"
exit 1
fi
shift # Remove 'format' from arguments
docker exec -it -w /var/www/html invoiceshelf-dev-php /var/www/html/vendor/bin/pint "$@"
}
# Function to show service information
show_service_info() {
echo ""
print_info "Available services:"
echo " • Application: http://invoiceshelf.test"
echo " • Adminer (DB): http://localhost:8080"
echo " • Mailpit: http://localhost:8025"
echo ""
print_info "Useful commands:"
echo " • Stop: $0 stop"
echo " • View logs: $0 logs"
echo " • Rebuild: $0 rebuild"
echo " • Shell: $0 shell"
echo " • Run command: $0 run php artisan help"
}
# Main setup function (original functionality)
setup_environment() {
print_info "InvoiceShelf Development Environment Setup"
echo "=========================================="
local os_type=$(detect_os)
print_success "Detected OS: $os_type"
validate_environment
local docker_compose=$(get_docker_compose_cmd)
print_success "Docker and Docker Compose are available"
# Check host entry
if check_host_entry; then
print_success "invoiceshelf.test host entry already exists"
else
print_warning "invoiceshelf.test host entry not found"
add_host_entry "$os_type"
fi
# Select database type
local db_type=$(select_database)
print_success "Selected database: $db_type"
# Ask about Gotenberg
local use_gotenberg=$(ask_gotenberg)
# Build compose file name
local compose_file
if [ "$use_gotenberg" = "yes" ]; then
compose_file="docker/development/docker-compose.${db_type}.gotenberg.yml"
print_success "Using Gotenberg-enabled compose file"
else
compose_file="docker/development/docker-compose.${db_type}.yml"
print_success "Using standard compose file"
fi
# Check if compose file exists
if [ ! -f "$compose_file" ]; then
print_error "Compose file not found: $compose_file"
exit 1
fi
# Save configuration for future use
save_config "$compose_file"
print_info "Starting development environment..."
print_info "Compose file: $compose_file"
# Start the development environment
if $docker_compose -f "$compose_file" up -d; then
echo ""
print_success "Development environment started successfully!"
show_service_info
else
print_error "Failed to start development environment"
exit 1
fi
}
# Main script execution
main() {
local command=${1:-""}
case "$command" in
"start")
validate_environment > /dev/null
cmd_start
;;
"stop")
validate_environment > /dev/null
cmd_stop
;;
"destroy")
validate_environment > /dev/null
cmd_destroy
;;
"logs")
validate_environment > /dev/null
cmd_logs "$@"
;;
"rebuild")
validate_environment > /dev/null
cmd_rebuild
;;
"shell")
validate_environment > /dev/null
cmd_shell
;;
"run")
validate_environment > /dev/null
cmd_run "$@"
;;
"test")
validate_environment > /dev/null
cmd_test "$@"
;;
"format")
validate_environment > /dev/null
cmd_format "$@"
;;
"help"|"-h"|"--help")
show_usage
;;
"")
setup_environment
;;
*)
print_error "Unknown command: $command"
echo ""
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"