docker-entrypoint.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. cat > $MAINTENANCE_DIR/index.php <<'EOF'
  34. <?php
  35. http_response_code(503);
  36. header('Retry-After: 10');
  37. header('Cache-Control: no-store, no-cache, must-revalidate');
  38. header('Pragma: no-cache');
  39. header('Content-Type: text/html; charset=utf-8');
  40. ?>
  41. <h1>🚀 Starting UNA...</h1>
  42. <p id="status"></p>
  43. <script>
  44. setTimeout(()=>location.reload(), 3000);
  45. var f = async () => {
  46. let r = await fetch('status.txt', { cache: 'no-store' });
  47. document.getElementById('status').innerHTML = await r.text();
  48. };
  49. setTimeout(f, 0);
  50. setInterval(f, 1000);
  51. </script>
  52. EOF
  53. php -S 0.0.0.0:80 -t $MAINTENANCE_DIR &
  54. PID_PHP=$!
  55. # Unzip
  56. if [ -n "${UNA_ZIP_DOWNLOAD_URL:-}" ] && [ -n "${UNA_ZIP_FOLDER:-}" ] && \
  57. [ ! -e "index.php" ] && [ ! -e "inc/version.inc.php" ]; then
  58. su "$UNA_USER" -c "
  59. set -eo pipefail;
  60. echo 'Downloading...' > $MAINTENANCE_DIR/status.txt
  61. if [ \"${UNA_FOLDER_CLEANUP}\" = \"1\" ]; then
  62. find . -mindepth 1 -maxdepth 1 -exec rm -rf {} +;
  63. fi;
  64. echo 'Unzipping...' > $MAINTENANCE_DIR/status.txt
  65. curl -fSL \"${UNA_ZIP_DOWNLOAD_URL}\" -o una.zip;
  66. unzip -q una.zip;
  67. rm una.zip;
  68. mv \"${UNA_ZIP_FOLDER}\"/* .;
  69. mv \"${UNA_ZIP_FOLDER}/.htaccess\" .;
  70. rm -rf \"${UNA_ZIP_FOLDER}\";
  71. chmod 777 inc cache cache_public logs tmp storage;
  72. chmod +x plugins/ffmpeg/ffmpeg.exe;
  73. find storage -type f -exec chmod 666 {} \+;
  74. find storage -type d -exec chmod 777 {} \+;
  75. "
  76. fi
  77. # Clean folders
  78. rm -rf cache/* cache_public/* tmp/*
  79. # Change permissions
  80. chmod +x plugins/ffmpeg/ffmpeg.exe
  81. find . -exec chown $UNA_USER:$UNA_USER {} \+ || true
  82. # Install
  83. if [ -d "install" ] && [ ! -f "inc/header.inc.php" ]; then
  84. echo 'Installing...' > $MAINTENANCE_DIR/status.txt
  85. if [ -f /tmp/addon.sql ]; then
  86. echo "Found additional SQL file..."
  87. cat /tmp/addon.sql >> ./install/sql/addon.sql
  88. fi
  89. \cp -f /srv/header.inc.php install/patterns/ # make sure that old UNA version can work with ENV vars
  90. INSTALL_CMD=(
  91. php ./install/cmd.php
  92. --db_host="${UNA_DB_HOST:-$VAR_DEF_DB_HOST}"
  93. --db_port="${UNA_DB_PORT:-$VAR_DEF_DB_PORT}"
  94. --db_name="$UNA_DB_NAME"
  95. --db_user="${UNA_DB_USER:-$VAR_DEF_DB_USER}"
  96. --db_password="${UNA_DB_PWD:-$VAR_DEF_DB_PWD}"
  97. --server_http_host="${UNA_HTTP_HOST:-$VAR_DEF_HTTP_HOST}"
  98. --server_php_self="/install/index.php"
  99. --server_doc_root="/var/www/html/"
  100. --site_title="${UNA_SITE_TITLE:-$VAR_DEF_TITLE}"
  101. --site_email="${UNA_SITE_EMAIL:-$VAR_DEF_EMAIL}"
  102. --admin_username="${UNA_ADMIN_USERNAME:-$VAR_DEF_USERNAME}"
  103. --admin_email="${UNA_ADMIN_EMAIL:-$VAR_DEF_EMAIL}"
  104. --admin_password="${UNA_ADMIN_PWD:-$VAR_DEF_ADMIN_PWD}"
  105. --oauth_key="${UNA_KEY:-}"
  106. --oauth_secret="${UNA_SECRET:-}"
  107. )
  108. if command -v runuser >/dev/null; then
  109. runuser -u "$UNA_USER" -- "${INSTALL_CMD[@]}"
  110. else
  111. su "$UNA_USER" -c "$(printf '%q ' "${INSTALL_CMD[@]}")"
  112. fi
  113. rm -rf ./install
  114. fi
  115. # Crontab
  116. if [[ ! -v UNA_NO_CRONTAB ]]; then
  117. 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
  118. chown $UNA_USER:$UNA_USER /var/www/crontab
  119. crontab -u $UNA_USER /var/www/crontab
  120. /etc/init.d/cron start
  121. fi
  122. # subdir config
  123. if [ -n "$UNA_HTTP_PATH" ]; then
  124. echo "Creating Alias for $UNA_HTTP_PATH"
  125. cat > "/etc/apache2/conf-available/99-subfolder.conf" <<EOF
  126. Alias /${UNA_HTTP_PATH} /var/www/html/
  127. <Directory /var/www/html/>
  128. Require all granted
  129. AllowOverride All
  130. Options FollowSymLinks
  131. </Directory>
  132. EOF
  133. a2enconf 99-subfolder
  134. fi
  135. # Disable maintenance page
  136. kill $PID_PHP
  137. wait $PID_PHP || true
  138. #
  139. exec "$@"