Explorar el Código

Environment variables as config - https://github.com/unacms/UNA/issues/5479

Aleksandr Trofimov hace 5 meses
padre
commit
da80589799
Se han modificado 3 ficheros con 155 adiciones y 57 borrados
  1. 63 18
      Dockerfile
  2. 27 39
      docker-entrypoint.sh
  3. 65 0
      header.inc.php

+ 63 - 18
Dockerfile

@@ -1,5 +1,6 @@
-# Container OS should have PHP and Apache already
-FROM php:8.2-apache
+# ---- Builder ----
+
+FROM php:8.3-apache AS builder
 
 MAINTAINER at@una.io
 
@@ -7,18 +8,14 @@ USER root
 
 # PHP extensions and necessary packages
 
-RUN apt-get update && apt-get install -y \
-        cron \
+RUN apt-get update && apt-get install -y --no-install-recommends \
         libfreetype6-dev \
         libjpeg62-turbo-dev \
-        libmcrypt-dev \
         libpng-dev \
         libwebp-dev \
         libonig-dev \
         libmagickwand-dev \
         libzip-dev \
-        sendmail sendmail-bin \
-        unzip \
  && docker-php-ext-install -j$(nproc) exif \
  && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
  && docker-php-ext-install -j$(nproc) gd \
@@ -28,23 +25,57 @@ RUN apt-get update && apt-get install -y \
  && docker-php-ext-install -j$(nproc) pdo \
  && docker-php-ext-install -j$(nproc) pdo_mysql \
  && docker-php-ext-install -j$(nproc) zip \
- && pecl install mcrypt-1.0.6 \
- && docker-php-ext-enable mcrypt \
  && pecl install imagick \
  && docker-php-ext-enable imagick \
  && rm -rf /var/lib/apt/lists/*
 
+# ---- Runtime ----
+
+FROM php:8.3-apache
+
+RUN apt-get update && apt-get install -y --no-install-recommends \
+    cron \
+    libfreetype6 \
+    libjpeg62-turbo \
+    libpng16-16 \
+    libwebp7 \
+    libonig5 \
+    imagemagick \
+    libzip5 \
+    unzip \
+ && rm -rf /var/lib/apt/lists/* /tmp/*
+
+ # Copy compiled extensions
+
+COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
+COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
+
+# ENV vars
+
+ENV UNA_USER="www-una" \
+        VAR_DEF_DB_HOST="localhost" \
+        VAR_DEF_DB_PORT="3306" \
+        VAR_DEF_DB_USER="root" \
+        VAR_DEF_DB_PWD="root" \
+        VAR_DEF_HTTP_HOST="localhost" \
+        VAR_DEF_TITLE="UNA" \
+        VAR_DEF_USERNAME="admin" \
+        VAR_DEF_ADMIN_PWD="admin" \
+        VAR_DEF_EMAIL="admin@example.com" \
+        VAR_DEF_DB_ENGINE="MYISAM" \
+        VAR_DEF_AUTO_HOSTNAME=0 \
+        VAR_DEF_VERSION="14.0.0" \
+        APACHE_RUN_USER=$UNA_USER \
+        APACHE_RUN_GROUP=$UNA_USER
+
 # User & folder 
 
-RUN groupadd -r --gid 2483 www-una \
- && useradd -r --uid 2483 -g www-una www-una \
- && chown www-una:www-una /var/www/html /var/www
+RUN groupadd -r --gid 2483 $UNA_USER \
+ && useradd -r --uid 2483 -g $UNA_USER $UNA_USER \
+ && chown $UNA_USER:$UNA_USER /var/www/html /var/www
 
 # Apache configuration
 
-ENV APACHE_RUN_USER www-una
-ENV APACHE_RUN_GROUP www-una
-
 RUN echo "memory_limit=192M \n\
 post_max_size=100M \n\
 upload_max_filesize=100M \n\
@@ -52,10 +83,9 @@ error_log=/var/log/php/error.log \n\
 error_reporting=E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT \n\
 display_errors=Off \n\
 log_errors=On \n\
-sendmail_path=/usr/sbin/sendmail -t -i \n\
 date.timezone=UTC" > /usr/local/etc/php/conf.d/una.ini
 
-RUN mkdir /var/log/php && mkdir /var/www/ssl && chown www-una:www-una /var/log/php && chmod 777 /var/log/php && su www-una -c "ln -s /dev/stderr /var/log/php/error.log"
+RUN mkdir /var/log/php && mkdir /var/www/ssl && chown $UNA_USER:$UNA_USER /var/log/php && chmod 777 /var/log/php && su $UNA_USER -c "ln -s /dev/stderr /var/log/php/error.log; ln -s /dev/stderr /var/www/cron.log"
 
 RUN a2enmod rewrite expires ssl
 
@@ -63,6 +93,21 @@ RUN a2enmod rewrite expires ssl
 
 WORKDIR /var/www/html
 
+COPY header.inc.php /srv/
+
+RUN set -eux; \
+    su "$UNA_USER" -c "\
+        curl -fSL \"http://ci.una.io/builds/UNA-v.${VAR_DEF_VERSION}.zip\" -o una.zip && \
+        unzip -o una.zip && \
+        rm una.zip && \
+        mv \"UNA-v.${VAR_DEF_VERSION}\"/* . && \
+        mv \"UNA-v.${VAR_DEF_VERSION}\"/.htaccess . && \
+        rm -rf \"UNA-v.${VAR_DEF_VERSION}\" && \
+        chmod 777 inc cache cache_public logs tmp storage && \        
+        chmod +x plugins/ffmpeg/ffmpeg.exe && \
+        \\cp -f --no-preserve=mode,ownership /srv/header.inc.php install/patterns/ \
+    "
+
 VOLUME /var/www
 
 EXPOSE 80
@@ -73,4 +118,4 @@ COPY docker-entrypoint.sh /usr/local/bin/
 
 ENTRYPOINT ["docker-entrypoint.sh"]
 
-CMD ["apache2-foreground"]
+CMD ["apache2-foreground"]

+ 27 - 39
docker-entrypoint.sh

@@ -1,24 +1,5 @@
 #!/bin/bash
-set -euox pipefail
-
-UNA_USER="www-una"
-
-VAR_DEF_DB_HOST="localhost"
-VAR_DEF_DB_PORT="3306"
-VAR_DEF_DB_USER="root"
-VAR_DEF_DB_PWD="root"
-VAR_DEF_HTTP_HOST="localhost"
-VAR_DEF_TITLE="UNA"
-VAR_DEF_USERNAME="admin"
-VAR_DEF_ADMIN_PWD="admin"
-VAR_DEF_EMAIL="admin@example.com"
-
-VAR_DEF_DB_ENGINE="MYISAM"
-VAR_DEF_AUTO_HOSTNAME=0
-
-VAR_DEF_VERSION="14.0.0"
-VAR_DEF_ZIP_DOWNLOAD_URL="http://ci.una.io/builds/UNA-v.${UNA_VERSION:-$VAR_DEF_VERSION}.zip"
-VAR_DEF_ZIP_FOLDER="UNA-v.${UNA_VERSION:-$VAR_DEF_VERSION}"
+set -eo pipefail
 
 # function
 
@@ -34,18 +15,34 @@ qs() {
 
 # Unzip
 
-if [ ! -e "index.php" ] && [ ! -e "inc/version.inc.php" ]; then
-    su $UNA_USER -c "curl -fSL $(qs ${UNA_ZIP_DOWNLOAD_URL:-$VAR_DEF_ZIP_DOWNLOAD_URL}) -o una.zip"
-    su $UNA_USER -c "unzip -o una.zip"
-    su $UNA_USER -c "rm una.zip"
-    su $UNA_USER -c "mv $(qs ${UNA_ZIP_FOLDER:-$VAR_DEF_ZIP_FOLDER})/* ."
-    su $UNA_USER -c "mv $(qs ${UNA_ZIP_FOLDER:-$VAR_DEF_ZIP_FOLDER}/.htaccess) ."
-    su $UNA_USER -c "rm -rf $(qs ${UNA_ZIP_FOLDER:-$VAR_DEF_ZIP_FOLDER})"
+if [ -n "${UNA_ZIP_DOWNLOAD_URL:-}" ] && [ -n "${UNA_ZIP_FOLDER:-}" ] && \
+   [ ! -e "index.php" ] && [ ! -e "inc/version.inc.php" ]; then
+
+    su "$UNA_USER" -c "
+        set -eo pipefail;
+
+        if [ \"${UNA_FOLDER_CLEANUP}\" = \"1\" ]; then
+            find . -mindepth 1 -maxdepth 1 -exec rm -rf {} +;
+        fi;
+
+        curl -fSL \"${UNA_ZIP_DOWNLOAD_URL}\" -o una.zip;
+        unzip -q una.zip;
+        rm una.zip;
+
+        mv \"${UNA_ZIP_FOLDER}\"/* .;
+        mv \"${UNA_ZIP_FOLDER}/.htaccess\" .;
+        rm -rf \"${UNA_ZIP_FOLDER}\";
+
+        chmod 777 inc cache cache_public logs tmp storage;
+        chmod +x plugins/ffmpeg/ffmpeg.exe;
+
+        // find storage -exec chmod $UNA_USER:$UNA_USER {} \+ 
+    "
 fi
 
 # Clean folders
 
-rm -rf cache/* cache_public/* tmp/*
+rm -rf cache/* cache_public/* tmp/* 
 
 # Change permissions
 
@@ -61,6 +58,8 @@ if [ -d "install" ] && [ ! -f "inc/header.inc.php" ]; then
         cat /tmp/addon.sql >> ./install/sql/addon.sql
     fi
 
+    \cp -f /srv/header.inc.php install/patterns/ # make sure that old UNA version can work with ENV vars
+
     su $UNA_USER -c "php ./install/cmd.php \
         --db_host=$(qs ${UNA_DB_HOST:-$VAR_DEF_DB_HOST}) \
         --db_port=$(qs ${UNA_DB_PORT:-$VAR_DEF_DB_PORT}) \
@@ -81,16 +80,6 @@ if [ -d "install" ] && [ ! -f "inc/header.inc.php" ]; then
     rm -rf ./install
 fi
 
-# Config alteration
-
-if [ ${UNA_DB_ENGINE:-$VAR_DEF_DB_ENGINE} != "MYISAM" ]; then
-    sed -r -i "s/^define\('BX_DATABASE_ENGINE', 'MYISAM'\);/define\('BX_DATABASE_ENGINE', '${UNA_DB_ENGINE:-$VAR_DEF_DB_ENGINE}'\);/g" /var/www/html/inc/header.inc.php
-fi
-
-if [ ${UNA_AUTO_HOSTNAME:-$VAR_DEF_AUTO_HOSTNAME} != 0 ]; then
-    sed -r -i "s/^define\('BX_DOL_URL_ROOT', .*?;/define\('BX_DOL_URL_ROOT', \(\(isset\(\$_SERVER['HTTPS']\) \&\& \$_SERVER['HTTPS'] == 'on'\) || \(!empty\(\$_SERVER['HTTP_X_FORWARDED_PROTO']\) \&\& \$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty\(\$_SERVER['HTTP_X_FORWARDED_SSL']\) \&\& \$_SERVER['HTTP_X_FORWARDED_SSL'] == 'on'\) ? 'https' : 'http'\) . ':\/\/' . \$_SERVER['HTTP_HOST'] . '\/'\);/g" /var/www/html/inc/header.inc.php
-fi
-
 # Crontab
 
 if [[ ! -v UNA_NO_CRONTAB ]]; then
@@ -98,7 +87,6 @@ if [[ ! -v UNA_NO_CRONTAB ]]; then
     chown $UNA_USER:$UNA_USER /var/www/crontab
     crontab -u $UNA_USER /var/www/crontab
 
-    rm -f /etc/cron.d/sendmail
     /etc/init.d/cron start
 fi
 

+ 65 - 0
header.inc.php

@@ -0,0 +1,65 @@
+<?php
+
+define('BX_DOL', 1);
+
+if (isset($_ENV['UNA_HTTP_HOST']) || isset($_ENV['UNA_AUTO_HOSTNAME'])) {
+    define('BX_DOL_URL_ROOT',
+        ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
+         ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https')
+        ? 'https://' : 'http://'
+        . ($_ENV['UNA_HTTP_HOST'] ?? $_SERVER['HTTP_HOST'] ?? 'localhost') . '/'
+    );
+}
+else {
+    define('BX_DOL_URL_ROOT', '%SITE_URL%'); ///< site url
+}
+
+define('BX_DIRECTORY_PATH_ROOT', $_ENV['UNA_ROOT_DIR'] ?? '%ROOT_DIR%'); ///< site path
+
+define('BX_DATABASE_HOST', $_ENV['UNA_DB_HOST'] ?? '%DB_HOST%'); ///< db host
+define('BX_DATABASE_SOCK', $_ENV['UNA_DB_SOCK'] ?? '%DB_SOCK%'); ///< db socket
+define('BX_DATABASE_PORT', $_ENV['UNA_DB_PORT'] ?? '%DB_PORT%'); ///< db port
+define('BX_DATABASE_USER', $_ENV['UNA_DB_USER'] ?? '%DB_USER%'); ///< db user
+define('BX_DATABASE_PASS', $_ENV['UNA_DB_PWD'] ?? '%DB_PASSWORD%'); ///< db password
+define('BX_DATABASE_NAME', $_ENV['UNA_DB_NAME'] ?? '%DB_NAME%'); ///< db name
+define('BX_DATABASE_ENGINE', $_ENV['UNA_DB_ENGINE'] ?? '%DB_ENGINE%'); ///< db engine
+if (isset($_ENV['UNA_DATABASE_COLLATE'])) {
+    define('BX_DATABASE_COLLATE', $_ENV['UNA_DATABASE_COLLATE']); ///< db collate
+}
+if (isset($_ENV['UNA_DATABASE_PERSISTENT'])) {
+    define('BX_DATABASE_PERSISTENT', $_ENV['UNA_DATABASE_PERSISTENT']); ///< db use porsistent connection (true by default)
+}
+
+define('BX_SYSTEM_JAVA', $_ENV['UNA_JAVA_PATH'] ?? '%JAVA_PATH%'); ///< path to java binary
+define('BX_SYSTEM_FFMPEG', $_ENV['UNA_FFMPEG_PATH'] ?? '%FFMPEG_PATH%'); ///< path to ffmpeg binary
+define('BX_DOL_SECRET', $_ENV['UNA_SECRET'] ?? '%SECRET%'); ///< secret word
+
+define('BX_DB_FULL_VISUAL_PROCESSING', $_ENV['UNA_DEBUG_VISUAL_PROCESSING'] ?? true); ///< upon db error - show error message
+define('BX_DB_FULL_DEBUG_MODE', $_ENV['UNA_DEBUG_MODE'] ?? false); ///< upon db error - show detailed report (turn off in production mode)
+define('BX_DB_DO_EMAIL_ERROR_REPORT', $_ENV['UNA_DEBUG_EMAIL_REPORT'] ?? true); ///< upon db error - send email with detailed report
+if (isset($_ENV['UNA_DEBUG_COOKIE'])) {
+    define('BX_DBG_COOKIE', $_ENV['UNA_DEBUG_COOKIE']); ///< debug cookie name, if set then display errors
+}
+error_reporting($_ENV['UNA_DEBUG_ERROR_REPORTING'] ?? E_ALL); ///< error reporting level
+
+if (isset($_ENV['UNA_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT'])) {
+    define('BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT', $_ENV['UNA_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT']); ///< max % of changed files to force autoupdate, 0.05 (5%) by default
+}
+if (isset($_ENV['UNA_INT_MAX'])) {
+    define('BX_DOL_INT_MAX', $_ENV['UNA_INT_MAX']); ///< max interger value, by default 2147483647 to support 32bit systems
+}
+if (isset($_ENV['UNA_STUDIO_FOLDER'])) {
+    define('BX_DOL_STUDIO_FOLDER', $_ENV['UNA_STUDIO_FOLDER']); ///< studio folder name
+}
+
+mb_internal_encoding('UTF-8');
+mb_regex_encoding('UTF-8');
+date_default_timezone_set('UTC');
+
+require_once('params.inc.php');
+
+bx_check_debug_mode();
+bx_check_maintenance_mode(true);
+if (empty($_ENV['UNA_SKIP_MINIMAL_REQUIREMENTS'])) bx_check_minimal_requirements(true);
+if (empty($_ENV['UNA_SKIP_REDIRECT'])) bx_check_redirect_to_correct_hostname(true);
+if (empty($_ENV['UNA_SKIP_INSTALL_FOLDER_CHECK'])) bx_check_redirect_to_remove_install_folder(true);