Created
March 21, 2025 07:34
-
-
Save TanjinAlam/309a13e003f602576690ee695d2fdbff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# setup.sh - Main setup script | |
exec > >(tee /var/log/setup.log) 2>&1 | |
echo "Starting Node.js application setup..." | |
# Update system and install dependencies | |
apt-get update | |
apt-get upgrade -y | |
apt-get install -y netcat-openbsd mysql-client | |
# Install Node.js | |
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - | |
apt-get install -y nodejs | |
# Create script directory | |
mkdir -p /usr/local/bin | |
# Copy the MySQL check script to proper location | |
cat > /usr/local/bin/check-mysql.sh << 'EOL' | |
#!/bin/bash | |
DB_HOST="$DB_PRIVATE_IP" | |
DB_PORT=3306 | |
MAX_RETRIES=30 | |
RETRY_INTERVAL=10 | |
check_mysql() { | |
nc -z "$DB_HOST" "$DB_PORT" | |
return $? | |
} | |
retry_count=0 | |
while [ $retry_count -lt $MAX_RETRIES ]; do | |
if check_mysql; then | |
echo "Successfully connected to MySQL at $DB_HOST:$DB_PORT" | |
exit 0 | |
fi | |
echo "Attempt $((retry_count + 1))/$MAX_RETRIES: Cannot connect to MySQL at $DB_HOST:$DB_PORT. Retrying in $RETRY_INTERVAL seconds..." | |
sleep $RETRY_INTERVAL | |
retry_count=$((retry_count + 1)) | |
done | |
echo "Failed to connect to MySQL after $MAX_RETRIES attempts" | |
exit 1 | |
EOL | |
chmod +x /usr/local/bin/check-mysql.sh | |
# Wait for environment variable to be set | |
max_attempts=30 | |
attempt=0 | |
while [ -z "$DB_PRIVATE_IP" ]; do | |
if [ $attempt -ge $max_attempts ]; then | |
echo "Timeout waiting for DB_PRIVATE_IP to be set" | |
exit 1 | |
fi | |
echo "Waiting for DB_PRIVATE_IP environment variable..." | |
attempt=$((attempt + 1)) | |
sleep 10 | |
# Source the environment file only once per iteration | |
source /etc/environment | |
done | |
echo "DB_PRIVATE_IP is set to: $DB_PRIVATE_IP" | |
# Wait for MySQL server to be ready | |
echo "Waiting for MySQL server to be ready..." | |
sleep 120 | |
echo "Creating MySQL Connectivity Check Service" | |
# Install systemd service | |
cat > /etc/systemd/system/mysql-check.service << 'EOL' | |
[Unit] | |
Description=MySQL Connectivity Check Service | |
After=network.target | |
Wants=network.target | |
[Service] | |
Type=simple | |
EnvironmentFile=/etc/environment | |
ExecStart=/usr/local/bin/check-mysql.sh | |
Restart=on-failure | |
RestartSec=30 | |
StandardOutput=append:/var/log/mysql-check.log | |
StandardError=append:/var/log/mysql-check.log | |
[Install] | |
WantedBy=multi-user.target | |
EOL | |
# Reload systemd and start service | |
systemctl daemon-reload | |
systemctl enable mysql-check | |
systemctl start mysql-check | |
echo "MySQL check service has been started. You can check the status with: systemctl status mysql-check" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment