-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-env.sh
More file actions
executable file
·65 lines (54 loc) · 2.62 KB
/
Copy pathinit-env.sh
File metadata and controls
executable file
·65 lines (54 loc) · 2.62 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
#!/bin/bash
# Array of services and their respective database names
SERVICES=("auth-service" "inventory-service" "order-service" "payment-service" "shipping-service")
DB_NAMES=("auth_db" "inventory_db" "order_db" "payment_db" "shipping_db")
echo "=================================================="
echo "Initializing environment files for microservices..."
echo "=================================================="
for i in "${!SERVICES[@]}"; do
SERVICE="${SERVICES[$i]}"
DB_NAME="${DB_NAMES[$i]}"
if [ -d "$SERVICE" ]; then
echo "Processing $SERVICE..."
# Copy .env.example if .env does not exist
if [ ! -f "$SERVICE/.env" ]; then
cp "$SERVICE/.env.example" "$SERVICE/.env"
echo " ✔ Created .env file from .env.example"
else
echo " ℹ .env file already exists. Updating configurations..."
fi
# Configure Database connection
# Replace DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD
sed -i 's/^DB_HOST=.*/DB_HOST=db/g' "$SERVICE/.env"
sed -i "s/^DB_DATABASE=.*/DB_DATABASE=$DB_NAME/g" "$SERVICE/.env"
sed -i 's/^DB_USERNAME=.*/DB_USERNAME=root/g' "$SERVICE/.env"
sed -i 's/^DB_PASSWORD=.*/DB_PASSWORD=rootpassword/g' "$SERVICE/.env"
# Configure Redis connection (for caching/queues)
sed -i 's/^REDIS_HOST=.*/REDIS_HOST=redis/g' "$SERVICE/.env"
# Set APP_ENV to local and APP_DEBUG to true
sed -i 's/^APP_ENV=.*/APP_ENV=local/g' "$SERVICE/.env"
sed -i 's/^APP_DEBUG=.*/APP_DEBUG=true/g' "$SERVICE/.env"
# Generate application key if it is empty or default
APP_KEY_VAL=$(grep "^APP_KEY=" "$SERVICE/.env" | cut -d= -f2-)
if [ -z "$APP_KEY_VAL" ] || [ "$APP_KEY_VAL" = '""' ] || [ "$APP_KEY_VAL" = "none" ]; then
echo " Generating application key..."
if command -v php >/dev/null 2>&1 && [ -f "$SERVICE/vendor/autoload.php" ]; then
php "$SERVICE/artisan" key:generate --ansi
echo " ✔ Generated Laravel app key using host PHP"
else
# Fallback to generating key using openssl
RAND_KEY=$(openssl rand -base64 32)
sed -i "s|^APP_KEY=.*|APP_KEY=base64:$RAND_KEY|g" "$SERVICE/.env"
echo " ✔ Generated app key using openssl (base64:$RAND_KEY)"
fi
else
echo " ℹ APP_KEY already exists. Skipping key generation."
fi
echo " ✔ Configuration completed for $SERVICE"
echo "--------------------------------------------------"
else
echo " ⚠ Directory $SERVICE not found. Skipping."
echo "--------------------------------------------------"
fi
done
echo "Environment initialization complete! Run 'docker compose up -d --build' to start services."