|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -o pipefail |
| 4 | +set -u |
| 5 | + |
| 6 | +function start_service { |
| 7 | + apache2ctl -D FOREGROUND || true |
| 8 | + echo "Error: Apache either terminated or would not start. Keeping container running for troubleshooting purposes." |
| 9 | + sleep infinity |
| 10 | +} |
| 11 | + |
| 12 | +function before_update { |
| 13 | + echo -e "[...] ${1}" |
| 14 | +} |
| 15 | + |
| 16 | +function progress_update { |
| 17 | + GREEN='\033[0;32m' |
| 18 | + RESET='\033[0m' |
| 19 | + echo -e "[ ${GREEN}\xE2\x9C\x94${RESET} ] ${1}" |
| 20 | +} |
| 21 | + |
| 22 | +check_port() { |
| 23 | + local host="$1" |
| 24 | + local port="$2" |
| 25 | + |
| 26 | + echo "Checking service availability at ${host}:${port}..." |
| 27 | + |
| 28 | + while true; do |
| 29 | + # Use nmap to scan the specified port on the given host |
| 30 | + nmap -p "${port}" "${host}" | grep -q "open" |
| 31 | + |
| 32 | + # If the port is open, exit the loop |
| 33 | + if [ $? -eq 0 ]; then |
| 34 | + echo "Server is available at ${host}:${port}" |
| 35 | + break |
| 36 | + else |
| 37 | + echo "Server at ${host}:${port} is not available. Retrying in 1 seconds..." |
| 38 | + sleep 1 |
| 39 | + fi |
| 40 | + done |
| 41 | +} |
| 42 | + |
| 43 | +## Check DB up |
| 44 | +check_port database 3306 |
| 45 | + |
| 46 | +set -e |
| 47 | + |
| 48 | +if ! [[ -f /container_initialized ]] |
| 49 | +then before_update "Setting up Apache vhost" |
| 50 | + perl -pi -e '$servername=$ENV{MATOMO_HOST}; s/SERVERNAME/$servername/' /etc/apache2/sites-available/001-matomo.conf |
| 51 | + before_update "Initializing Matomo." |
| 52 | + cd /var/www/html/matomo |
| 53 | + ./console matomo:install --no-interaction \ |
| 54 | + --db-username="$MATOMO_DATABASE_USERNAME" \ |
| 55 | + --db-pass="MATOMO_DATABASE_PASSWORD" \ |
| 56 | + --db-host="$MATOMO_DATABASE_HOST" \ |
| 57 | + --db-port=3306 \ |
| 58 | + --db-name="$MATOMO_DATABASE_DBNAME" \ |
| 59 | + --first-site-name="$MATOMO_FIRST_SITE_NAME" \ |
| 60 | + --first-site-url="$MATOMO_FIRST_SITE_URL" \ |
| 61 | + --first-user="$MATOMO_FIRST_USER_NAME" \ |
| 62 | + --first-user-email="$MATOMO_FIRST_USER_EMAIL" \ |
| 63 | + --first-user-pass="$MATOMO_FIRST_USER_PASSWORD" |
| 64 | + crudini --set /var/www/html/matomo/config/config.ini.php General force_ssl 1 |
| 65 | + crudini --set /var/www/html/matomo/config/config.ini.php General assume_secure_protocol 1 |
| 66 | + crudini --set /var/www/html/matomo/config/config.ini.php General proxy_client_headers[] HTTP_X_FORWARDED_FOR |
| 67 | + crudini --set /var/www/html/matomo/config/config.ini.php General proxy_host_headers[] HTTP_X_FORWARDED_HOST |
| 68 | + chmod 0644 /var/www/html/matomo/config/config.ini.php |
| 69 | + chown www-data:www-data /var/www/html/matomo/config/config.ini.php |
| 70 | + touch /container_initialized |
| 71 | +fi |
| 72 | + |
| 73 | +before_update "Initialization complete. Starting Apache" |
| 74 | +start_service |
0 commit comments