docker-entrypoint.sh 4.8 KB

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