diff --git a/.env.prod.example b/.env.prod.example new file mode 100644 index 0000000..31dc666 --- /dev/null +++ b/.env.prod.example @@ -0,0 +1,21 @@ +APP_ENV=production +APP_KEY= +APP_DEBUG=false +APP_TIMEZONE=Europe/Rome +APP_URL=http://localhost + +BCRYPT_ROUNDS=12 + +LOG_LEVEL=debug +LOG_CHANNEL=errorlog +LOG_DEPRECATIONS_CHANNEL=errorlog + +DB_CONNECTION=sqlite +DB_DATABASE=/var/www/html/storage/database.sqlite + +FILESYSTEM_DISK=local +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_DEFAULT_REGION= +AWS_BUCKET= +AWS_ENDPOINT= diff --git a/containers/php/Dockerfile b/containers/php/Dockerfile index e2b8747..a8e2bff 100644 --- a/containers/php/Dockerfile +++ b/containers/php/Dockerfile @@ -11,7 +11,6 @@ ADD --chmod=777 \ --checksum=sha256:206a8f9b2177703fc5aa924d85ad6c72e82413e2d09635b4c9c82a1b65b5b3d5 \ https://github.com/eficode/wait-for/releases/download/v2.2.4/wait-for /usr/local/bin/wait-for # configure entrypoints and image -COPY --chmod=777 containers/php/entrypoint.sh /usr/local/sbin/entrypoint RUN echo "* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1" > /etc/crontabs/www-data \ && echo "access.log = /dev/null" >> /usr/local/etc/php-fpm.d/zz-docker.conf ENTRYPOINT ["entrypoint"] @@ -26,6 +25,7 @@ RUN apk add --no-cache \ && install-php-extensions xdebug \ && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \ && echo "ALL ALL=(ALL:ALL) NOPASSWD: ALL" > "/etc/sudoers.d/password_less_sudo" +COPY --chmod=777 containers/php/entrypoint-dev.sh /usr/local/sbin/entrypoint ARG UID ARG GID RUN usermod -u "$UID" www-data \ @@ -37,6 +37,7 @@ USER www-data FROM base as production RUN install-php-extensions opcache \ && mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" +COPY --chmod=777 containers/php/entrypoint.sh /usr/local/sbin/entrypoint COPY containers/php/config/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini ARG COMPOSER_ALLOW_SUPERUSER=1 COPY composer.json . diff --git a/containers/php/entrypoint-dev.sh b/containers/php/entrypoint-dev.sh new file mode 100644 index 0000000..f569822 --- /dev/null +++ b/containers/php/entrypoint-dev.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env sh + +set -e + +if [ -f '.env' ]; then + . .env +fi + +if [ "$1" = "php-fpm" ]; then + composer install + php artisan migrate --force +elif [ "$1" = "scheduler" ]; then + set -- crond -f +elif [ "$1" = "worker" ]; then + exec php artisan queue:work --tries=3 --timeout=1800 +fi + +exec "$@" diff --git a/containers/php/entrypoint.sh b/containers/php/entrypoint.sh index 35a8c1d..031f935 100644 --- a/containers/php/entrypoint.sh +++ b/containers/php/entrypoint.sh @@ -6,16 +6,17 @@ if [ -f '.env' ]; then . .env fi -if [ "$1" = 'php-fpm' ]; then - if [ "$APP_ENV" = "local" ]; then - composer install - else - php artisan optimize - fi +php artisan optimize + +if [ "$1" = "php-fpm" ]; then + # migrate database php artisan migrate --force - chown -R www-data:www-data storage -elif [ "$1" = 'crond' ] || [ "$3" = 'queue:work' ] || [ "$3" = 'queue:listen' ]; then - wait-for "${PHP_HOST:?Missing PHP_HOST}:${PHP_PORT:?Missing PHP_PORT}" -t 60 +elif [ "$1" = "scheduler" ]; then + # remove scheduler command, replace the $@ + set -- crond -f +elif [ "$1" = "worker" ]; then + # remove worker command, replace the $@ + set -- su -s /bin/sh -c "php artisan queue:work --tries=3 --timeout=1800" www-data fi exec "$@" diff --git a/docker-compose.production.yml b/deploy/compose/docker-compose.yml similarity index 59% rename from docker-compose.production.yml rename to deploy/compose/docker-compose.yml index d2797c1..4268211 100644 --- a/docker-compose.production.yml +++ b/deploy/compose/docker-compose.yml @@ -1,3 +1,4 @@ +name: parceler-prod services: nginx: image: ghcr.io/nethesis/parceler-nginx:latest @@ -14,25 +15,20 @@ services: - storage:/var/www/html - /etc/localtime:/etc/localtime:ro env_file: - - .env.production + - .env worker: - image: ghcr.io/nethesis/parceler-php:latest + extends: + service: php stop_signal: SIGKILL - user: www-data - command: php artisan queue:work --tries=3 --timeout=1800 - volumes: - - storage:/var/www/html - - /etc/localtime:/etc/localtime:ro + command: worker environment: PHP_HOST: php PHP_PORT: 9000 scheduler: - image: ghcr.io/nethesis/parceler-php:latest + extends: + service: php stop_signal: SIGKILL - command: crond -f - volumes: - - storage:/var/www/html - - /etc/localtime:/etc/localtime:ro + command: scheduler environment: PHP_HOST: php PHP_PORT: 9000 diff --git a/deploy/quadlet/install.sh b/deploy/quadlet/install.sh new file mode 100755 index 0000000..28e1794 --- /dev/null +++ b/deploy/quadlet/install.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env sh + +set -e + +INSTALL_PATH=${INSTALL_PATH:-"$HOME/.config/containers/systemd"} +# Ensure the directory exists +mkdir -p "${INSTALL_PATH}" +# Install the service files +install -Dm644 parceler.pod "${INSTALL_PATH}/parceler.pod" +install -Dm644 storage.volume "${INSTALL_PATH}/storage.volume" +install -Dm644 php.container "${INSTALL_PATH}/php.container" +install -Dm644 nginx.container "${INSTALL_PATH}/nginx.container" +install -Dm644 scheduler.container "${INSTALL_PATH}/scheduler.container" +install -Dm644 worker.container "${INSTALL_PATH}/worker.container" +# Reload the systemd user service +systemctl --user daemon-reload +systemctl --user reload-or-restart parceler-pod diff --git a/deploy/quadlet/nginx.container b/deploy/quadlet/nginx.container new file mode 100644 index 0000000..378ed81 --- /dev/null +++ b/deploy/quadlet/nginx.container @@ -0,0 +1,7 @@ +[Container] +Pod=parceler.pod +Image=ghcr.io/nethesis/parceler-nginx:latest +ContainerName=parceler-nginx +Volume=/etc/localtime:/etc/localtime:ro +Environment=FPM_HOST=parceler-php +Environment=FPM_PORT=9000 diff --git a/deploy/quadlet/parceler.pod b/deploy/quadlet/parceler.pod new file mode 100644 index 0000000..9a28c85 --- /dev/null +++ b/deploy/quadlet/parceler.pod @@ -0,0 +1,9 @@ +[Pod] +PodName=parceler +PublishPort=8080:80 + +[Service] +Restart=always + +[Install] +WantedBy=default.target diff --git a/deploy/quadlet/php.container b/deploy/quadlet/php.container new file mode 100644 index 0000000..7886e9f --- /dev/null +++ b/deploy/quadlet/php.container @@ -0,0 +1,7 @@ +[Container] +Pod=parceler.pod +Image=ghcr.io/nethesis/parceler-php:latest +ContainerName=parceler-php +Volume=/etc/localtime:/etc/localtime:ro +Volume=storage.volume:/var/www/html +EnvironmentFile=%S/parceler.env diff --git a/deploy/quadlet/scheduler.container b/deploy/quadlet/scheduler.container new file mode 100644 index 0000000..c3c64cf --- /dev/null +++ b/deploy/quadlet/scheduler.container @@ -0,0 +1,8 @@ +[Container] +Pod=parceler.pod +Image=ghcr.io/nethesis/parceler-php:latest +ContainerName=parceler-scheduler +Volume=/etc/localtime:/etc/localtime:ro +Volume=storage.volume:/var/www/html +Exec=scheduler +EnvironmentFile=%S/parceler.env diff --git a/deploy/quadlet/storage.volume b/deploy/quadlet/storage.volume new file mode 100644 index 0000000..4fd6534 --- /dev/null +++ b/deploy/quadlet/storage.volume @@ -0,0 +1 @@ +[Volume] diff --git a/deploy/quadlet/worker.container b/deploy/quadlet/worker.container new file mode 100644 index 0000000..69b34b6 --- /dev/null +++ b/deploy/quadlet/worker.container @@ -0,0 +1,8 @@ +[Container] +Pod=parceler.pod +Image=ghcr.io/nethesis/parceler-php:latest +ContainerName=parceler-worker +Volume=/etc/localtime:/etc/localtime:ro +Volume=storage.volume:/var/www/html +Exec=worker +EnvironmentFile=%S/parceler.env diff --git a/docker-compose.yml b/docker-compose.yml index 29aae59..6e811e3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,7 +23,7 @@ services: extends: service: php stop_signal: SIGKILL - command: php artisan queue:listen --tries=3 --timeout=1800 + command: worker environment: PHP_HOST: php PHP_PORT: 9000 @@ -31,7 +31,7 @@ services: extends: service: php stop_signal: SIGKILL - command: crond -f + command: scheduler environment: PHP_HOST: php PHP_PORT: 9000