entrypoint.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/sh
  2. set -eu
  3. cd /var/www/html
  4. PORT="${PORT:-80}"
  5. # Whole-line patterns that take any port, not only the image's 80: a restart that keeps the
  6. # container's filesystem (docker restart, Railway's ON_FAILURE retries) finds the port an
  7. # earlier start wrote and sets it again, where a bare "Listen 80" would turn 8080 into 808080.
  8. sed -i "s/^Listen [0-9][0-9]*$/Listen ${PORT}/" /etc/apache2/ports.conf
  9. sed -i "s/^<VirtualHost \*:[0-9][0-9]*>$/<VirtualHost *:${PORT}>/" /etc/apache2/sites-available/000-default.conf
  10. a2dismod mpm_event mpm_worker >/dev/null 2>&1 || true
  11. a2enmod mpm_prefork rewrite >/dev/null 2>&1 || true
  12. mkdir -p cache cache_public logs tmp storage
  13. chmod a+rwX inc cache cache_public logs tmp storage
  14. if [ -f plugins/ffmpeg/ffmpeg.exe ]; then
  15. chmod 0755 plugins/ffmpeg/ffmpeg.exe # installer check only; UNA_FFMPEG_PATH is what runs
  16. fi
  17. parse_mysql_url() {
  18. _url="$1"
  19. _url="${_url#mysql://}"
  20. _url="${_url#mariadb://}"
  21. DB_USER="${_url%%:*}"
  22. _url="${_url#*:}"
  23. DB_PASSWORD="${_url%%@*}"
  24. _url="${_url#*@}"
  25. _hostport="${_url%%/*}"
  26. DB_NAME="${_url#*/}"
  27. DB_NAME="${DB_NAME%%\?*}"
  28. case "$_hostport" in
  29. *:*)
  30. DB_HOST="${_hostport%:*}"
  31. DB_PORT="${_hostport##*:}"
  32. ;;
  33. *)
  34. DB_HOST="$_hostport"
  35. DB_PORT="3306"
  36. ;;
  37. esac
  38. }
  39. DB_URL="${DATABASE_URL:-${MYSQL_URL:-${MARIADB_PRIVATE_URL:-${MARIADB_URL:-}}}}"
  40. if [ -n "$DB_URL" ]; then
  41. parse_mysql_url "$DB_URL"
  42. else
  43. DB_HOST="${MYSQLHOST:-${MARIADB_HOST:-${MARIADBHOST:-mariadb.railway.internal}}}"
  44. DB_PORT="${MYSQLPORT:-${MARIADB_PORT:-${MARIADBPORT:-3306}}}"
  45. DB_NAME="${MYSQLDATABASE:-${MARIADB_DATABASE:-una}}"
  46. DB_USER="${MYSQLUSER:-${MARIADB_USER:-una}}"
  47. DB_PASSWORD="${MYSQLPASSWORD:-${MARIADB_PASSWORD:-una}}"
  48. fi
  49. export DB_HOST DB_PORT DB_NAME DB_USER DB_PASSWORD
  50. export UNA_DB_HOST="$DB_HOST"
  51. export UNA_DB_PORT="$DB_PORT"
  52. export UNA_DB_NAME="$DB_NAME"
  53. export UNA_DB_USER="$DB_USER"
  54. export UNA_DB_PWD="$DB_PASSWORD"
  55. export UNA_ROOT_DIR=/var/www/html/
  56. export UNA_AUTO_HOSTNAME=1
  57. export UNA_SKIP_INSTALL_FOLDER_CHECK=1
  58. export UNA_SKIP_REDIRECT=1
  59. db_status() {
  60. php -r '
  61. $m = @new mysqli(
  62. getenv("DB_HOST"),
  63. getenv("DB_USER"),
  64. getenv("DB_PASSWORD"),
  65. getenv("DB_NAME"),
  66. (int)(getenv("DB_PORT") ?: 3306)
  67. );
  68. if ($m->connect_error) {
  69. fwrite(STDERR, $m->connect_error . "\n");
  70. exit(1);
  71. }
  72. $r = $m->query("SHOW TABLES LIKE \"sys_options\"");
  73. exit($r && $r->num_rows ? 0 : 2);
  74. '
  75. }
  76. echo "Waiting for MariaDB at ${DB_HOST}:${DB_PORT}..."
  77. i=0
  78. rc=1
  79. set +e
  80. while [ "$i" -lt 60 ]; do
  81. db_status
  82. rc=$?
  83. if [ "$rc" -eq 0 ] || [ "$rc" -eq 2 ]; then
  84. break
  85. fi
  86. i=$((i + 1))
  87. sleep 2
  88. done
  89. set -e
  90. if [ "$rc" -eq 1 ]; then
  91. echo "MariaDB is not reachable" >&2
  92. exit 1
  93. fi
  94. if [ "$rc" -eq 2 ]; then
  95. echo "Installing UNA with cmd.php..."
  96. php install/cmd.php \
  97. -m bx_posts,bx_timeline,bx_contact,bx_developer,bx_antispam,bx_convos,bx_organizations,bx_notifications,bx_ether,bx_accounts,bx_albums,bx_facebook,bx_tricon,bx_oauth,bx_payment,bx_market,bx_groups,bx_intercom,bx_googlecon,bx_linkedin,bx_twitter,bx_mailchimp,bx_forum,bx_acl,bx_events,bx_files,bx_glossary,bx_googletagman,bx_photos,bx_polls,bx_profiler,bx_quoteofday,bx_smtp,bx_shopify,bx_snipcart,bx_se_migration,bx_spaces,bx_stripe_connect,bx_videos,bx_charts,bx_dolcon,bx_fontawesome,bx_channels,bx_attendant,bx_massmailer,bx_directory,bx_plyr,bx_ads,bx_lucid,bx_credits,bx_wiki,bx_tasks,bx_courses,bx_classes,bx_artificer,bx_donations,bx_feedback,bx_help_tours,bx_stream,bx_attendant,bx_analytics,bx_reputation,bx_ru \
  98. --db_host="$DB_HOST" \
  99. --db_port="$DB_PORT" \
  100. --db_name="$DB_NAME" \
  101. --db_user="$DB_USER" \
  102. --db_password="$DB_PASSWORD" \
  103. --server_http_host="${RAILWAY_PUBLIC_DOMAIN:-localhost}" \
  104. --server_php_self=/install/index.php \
  105. --server_doc_root=/var/www/html/ \
  106. --site_title="${UNA_SITE_TITLE:-UNA}" \
  107. --admin_username="${UNA_ADMIN_USERNAME:-admin}" \
  108. --admin_password="${UNA_ADMIN_PASSWORD:-unauna}"
  109. else
  110. echo "UNA already installed; writing header from environment."
  111. if [ ! -f inc/header.inc.php ]; then
  112. cp install/patterns/header.inc.php inc/header.inc.php
  113. chmod 0666 inc/header.inc.php
  114. fi
  115. fi
  116. # API key, origins and client URL for the NEO front end (idempotent, every start).
  117. php /usr/local/lib/una-railway/configure.php
  118. if [ "${UNA_DEMO_SEED:-0}" = "1" ]; then
  119. echo "Seeding demo content..."
  120. # A failed seed must not keep the site down; the log says what went wrong.
  121. php /usr/local/lib/una-railway/seed.php || echo "Demo seed failed (exit $?)" >&2
  122. fi
  123. chown -R www-data:www-data inc cache cache_public logs tmp storage
  124. # UNA's scheduler. periodic/cron.php expects to be started every minute, like the crontab line
  125. # in UNA's install instructions (and scripts/docker-compose/Cron.Dockerfile). The image has no
  126. # cron daemon, so a background loop starts a run at the top of each minute. As under cron, runs
  127. # may overlap, so a long video transcode does not hold up the email and push queues. It starts
  128. # after install and seeding, so it never sees a half-installed site.
  129. # - It runs as www-data, so cache files it writes stay writable by Apache. setpriv keeps the
  130. # environment (UNA_DB_*, UNA_ROOT_DIR...), which su would reset.
  131. # - A CLI run has no request to take a host from, so UNA_HTTP_HOST and HTTPS give it the public
  132. # URL. They are set for the scheduler only: Apache keeps taking the host from each request.
  133. run_cron() {
  134. env UNA_HTTP_HOST="${RAILWAY_PUBLIC_DOMAIN:-localhost}" HTTPS=on \
  135. setpriv --reuid=www-data --regid=www-data --init-groups \
  136. php /var/www/html/periodic/cron.php || echo "UNA cron failed (exit $?)" >&2
  137. }
  138. while true; do
  139. sleep $((60 - $(date +%s) % 60))
  140. jobs >/dev/null # dash keeps a record of every finished run until it is listed
  141. run_cron &
  142. done &
  143. exec "$@"