docker-entrypoint.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. set -eo pipefail
  3. # function
  4. file_env() {
  5. local var="$1"
  6. local fileVar="${var}_FILE"
  7. if [ -n "${!var:-}" ] && [ -n "${!fileVar:-}" ]; then
  8. echo "Both $var and $fileVar are set, but are exclusive"
  9. exit 1
  10. fi
  11. local val=""
  12. if [ -n "${!var:-}" ]; then
  13. val="${!var}"
  14. elif [ -n "${!fileVar:-}" ]; then
  15. val="$(< "${!fileVar}")"
  16. fi
  17. if [ -n "$val" ]; then
  18. export "$var"="$val"
  19. fi
  20. unset "$fileVar"
  21. }
  22. # expand secrets
  23. file_env UNA_DB_PWD
  24. file_env UNA_ADMIN_PWD
  25. file_env UNA_KEY
  26. file_env UNA_SECRET
  27. file_env UNA_HASH_SECRET
  28. file_env UNA_DEBUG_COOKIE
  29. # Mainenance page
  30. MAINTENANCE_DIR="/tmp/maintenance"
  31. mkdir -p $MAINTENANCE_DIR
  32. chmod 777 $MAINTENANCE_DIR
  33. echo '' > $MAINTENANCE_DIR/status.txt
  34. chmod 666 $MAINTENANCE_DIR/status.txt
  35. cat > $MAINTENANCE_DIR/index.php <<EOF
  36. <?php
  37. http_response_code(503);
  38. header('Retry-After: 10');
  39. header('Cache-Control: no-store, no-cache, must-revalidate');
  40. header('Pragma: no-cache');
  41. header('Content-Type: text/html; charset=utf-8');
  42. ?>
  43. <h1>🚀 Starting UNA...</h1>
  44. <p><?php readfile('$MAINTENANCE_DIR/status.txt'); ?></p>
  45. <script>
  46. setTimeout(()=>location.reload(), 2000);
  47. </script>
  48. EOF
  49. php -S 0.0.0.0:80 $MAINTENANCE_DIR/index.php &
  50. PID_PHP=$!
  51. # Unzip
  52. if [ -n "${UNA_ZIP_DOWNLOAD_URL:-}" ] && [ -n "${UNA_ZIP_FOLDER:-}" ] && \
  53. [ ! -e "index.php" ] && [ ! -e "inc/version.inc.php" ]; then
  54. su "$UNA_USER" -c "
  55. set -eo pipefail;
  56. echo 'Downloading...' > $MAINTENANCE_DIR/status.txt
  57. if [ \"${UNA_FOLDER_CLEANUP}\" = \"1\" ]; then
  58. find . -mindepth 1 -maxdepth 1 -exec rm -rf {} +;
  59. fi;
  60. echo 'Unzipping...' > $MAINTENANCE_DIR/status.txt
  61. curl -fSL \"${UNA_ZIP_DOWNLOAD_URL}\" -o una.zip;
  62. unzip -q una.zip;
  63. rm una.zip;
  64. mv \"${UNA_ZIP_FOLDER}\"/* .;
  65. mv \"${UNA_ZIP_FOLDER}/.htaccess\" .;
  66. rm -rf \"${UNA_ZIP_FOLDER}\";
  67. chmod 777 inc cache cache_public logs tmp storage;
  68. chmod +x plugins/ffmpeg/ffmpeg.exe;
  69. find storage -type f -exec chmod 666 {} \+;
  70. find storage -type d -exec chmod 777 {} \+;
  71. "
  72. fi
  73. # Clean folders
  74. rm -rf cache/* cache_public/* tmp/*
  75. # Change permissions
  76. chmod +x plugins/ffmpeg/ffmpeg.exe
  77. find . -exec chown $UNA_USER:$UNA_USER {} \+ || true
  78. # Install
  79. if [ -d "install" ] && [ ! -f "inc/header.inc.php" ]; then
  80. echo 'Installing...' > $MAINTENANCE_DIR/status.txt
  81. if [ -f /tmp/addon.sql ]; then
  82. echo "Found additional SQL file..."
  83. cat /tmp/addon.sql >> ./install/sql/addon.sql
  84. fi
  85. \cp -f /srv/header.inc.php install/patterns/ # make sure that old UNA version can work with ENV vars
  86. INSTALL_CMD=(
  87. php ./install/cmd.php
  88. --db_host="${UNA_DB_HOST:-$VAR_DEF_DB_HOST}"
  89. --db_port="${UNA_DB_PORT:-$VAR_DEF_DB_PORT}"
  90. --db_name="$UNA_DB_NAME"
  91. --db_user="${UNA_DB_USER:-$VAR_DEF_DB_USER}"
  92. --db_password="${UNA_DB_PWD:-$VAR_DEF_DB_PWD}"
  93. --server_http_host="${UNA_HTTP_HOST:-$VAR_DEF_HTTP_HOST}"
  94. --server_php_self="/install/index.php"
  95. --server_doc_root="/var/www/html/"
  96. --site_title="${UNA_SITE_TITLE:-$VAR_DEF_TITLE}"
  97. --site_email="${UNA_SITE_EMAIL:-$VAR_DEF_EMAIL}"
  98. --admin_username="${UNA_ADMIN_USERNAME:-$VAR_DEF_USERNAME}"
  99. --admin_email="${UNA_ADMIN_EMAIL:-$VAR_DEF_EMAIL}"
  100. --admin_password="${UNA_ADMIN_PWD:-$VAR_DEF_ADMIN_PWD}"
  101. --oauth_key="${UNA_KEY:-}"
  102. --oauth_secret="${UNA_SECRET:-}"
  103. )
  104. if command -v runuser >/dev/null; then
  105. runuser -u "$UNA_USER" -- "${INSTALL_CMD[@]}"
  106. else
  107. su "$UNA_USER" -c "$(printf '%q ' "${INSTALL_CMD[@]}")"
  108. fi
  109. rm -rf ./install
  110. fi
  111. # Crontab
  112. if [[ ! -v UNA_NO_CRONTAB ]]; then
  113. echo "* * * * * /usr/local/bin/php -c /var/www /var/www/html/periodic/cron.php 2>&1 | sed -e \"s/\(.*\)/[\`date\`] \1/\" >>/var/www/cron.log" > /var/www/crontab
  114. chown $UNA_USER:$UNA_USER /var/www/crontab
  115. crontab -u $UNA_USER /var/www/crontab
  116. /etc/init.d/cron start
  117. fi
  118. # subdir config
  119. if [ -n "$UNA_HTTP_PATH" ]; then
  120. echo "Creating Alias for $UNA_HTTP_PATH"
  121. cat > "/etc/apache2/conf-available/99-subfolder.conf" <<EOF
  122. Alias /${UNA_HTTP_PATH} /var/www/html/
  123. <Directory /var/www/html/>
  124. Require all granted
  125. AllowOverride All
  126. Options FollowSymLinks
  127. </Directory>
  128. EOF
  129. a2enconf 99-subfolder
  130. fi
  131. # Disable maintenance page
  132. kill $PID_PHP
  133. wait $PID_PHP || true
  134. #
  135. exec "$@"