entrypoint.sh 5.7 KB

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