From 241fa00e0265bdd720d0299ff3dcf4f0979494c7 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Wed, 29 Nov 2023 14:49:12 +0100
Subject: [PATCH] fix composer deployment

---
 .env                                          |  5 ++
 app/.ash_history                              |  2 -
 app/bootstrap/cache/.gitignore                |  2 -
 .../assets}/js/turf.min.js                    |  0
 build/fpm/Dockerfile                          | 66 +++++++++++++++----
 build/fpm/entrypoint.sh                       | 23 +++++++
 build/fpm/installComposer.sh                  | 17 +++++
 docker-compose.yml                            | 31 +++++++--
 8 files changed, 125 insertions(+), 21 deletions(-)
 create mode 100644 .env
 delete mode 100644 app/.ash_history
 delete mode 100644 app/bootstrap/cache/.gitignore
 rename app/{public => resources/assets}/js/turf.min.js (100%)
 create mode 100644 build/fpm/entrypoint.sh
 create mode 100644 build/fpm/installComposer.sh

diff --git a/.env b/.env
new file mode 100644
index 0000000..70c58da
--- /dev/null
+++ b/.env
@@ -0,0 +1,5 @@
+ENVIRONMENT=development # Environment to deploy to. Can be: development|production
+USER=1000   # User ID used in the Docker containers
+GROUP=1000  # Group ID used in the Docker containers
+
+IMAGE_TAG=latest
\ No newline at end of file
diff --git a/app/.ash_history b/app/.ash_history
deleted file mode 100644
index b5c9de2..0000000
--- a/app/.ash_history
+++ /dev/null
@@ -1,2 +0,0 @@
-ls
-php artisan version
diff --git a/app/bootstrap/cache/.gitignore b/app/bootstrap/cache/.gitignore
deleted file mode 100644
index d6b7ef3..0000000
--- a/app/bootstrap/cache/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-*
-!.gitignore
diff --git a/app/public/js/turf.min.js b/app/resources/assets/js/turf.min.js
similarity index 100%
rename from app/public/js/turf.min.js
rename to app/resources/assets/js/turf.min.js
diff --git a/build/fpm/Dockerfile b/build/fpm/Dockerfile
index 6fcafa1..14cbd90 100644
--- a/build/fpm/Dockerfile
+++ b/build/fpm/Dockerfile
@@ -1,45 +1,87 @@
-FROM alpine:3.18.4 as base
+FROM alpine:3.13 as base
+
+ARG USER=1000
+ARG GROUP=1000
 
 RUN apk add --update \
     tzdata \
     ca-certificates \
     zip \
     redis \
-    php81 \
-    php81-fpm \
-    php81-xdebug \
+    php7 \
+    php7-common \
+    php7-phar \
+    php7-fpm \
+    php7-curl \
+    php7-mbstring \
+    php7-sqlite3 \
+    php7-pdo_mysql \
+    php7-pdo_sqlite \
+    php7-dom \
+    php7-xml \
+    php7-xmlwriter \
+    php7-tokenizer \
+    php7-zip \
+    php7-redis \
+    php7-gd \
+    php7-json \
+    php7-pcntl \
+    php7-fileinfo \
+    php7-xdebug \
     && rm -rf /var/cache/apk/*
 
 RUN cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime
 
-RUN adduser mgmaps -h /html -D -s /bin/sh -k /dev/null
+RUN addgroup -g $GROUP mgmaps && adduser mgmaps -h /mgmaps -D -s /bin/sh -k /dev/null -u $USER -G mgmaps
 USER mgmaps
 
 WORKDIR /html
+RUN mkdir -p /html/vendor /html/bootstrap/cache /mgmaps/.composer
 EXPOSE 80
 
+FROM base as composer
+
+USER root
+
+WORKDIR /mgmaps
+
+ADD ./build/fpm/installComposer.sh /usr/bin/installComposer
+RUN chmod go+x /usr/bin/installComposer
+RUN installComposer && rm /usr/bin/installComposer
+
+USER mgmaps
+WORKDIR /html
+VOLUME ["/mgmaps/.composer", "/html/vendor", "/html/bootstrap/cache"]
+CMD ["composer", "install"]
+
 FROM base as development
 
 USER root
 
 # Add common configurations development & production
-ADD ./build/fpm/configuration/common/*.ini /etc/php81/conf.d/
-ADD ./build/fpm/configuration/common/*.conf /etc/php81/php-fpm.d/
+ADD ./build/fpm/configuration/common/*.ini /etc/php7/conf.d/
+ADD ./build/fpm/configuration/common/*.conf /etc/php7/php-fpm.d/
 
 # Add development only configuration
-ADD ./build/fpm/configuration/development/*.ini /etc/php81/conf.d/
-ADD ./build/fpm/configuration/development/*.conf /etc/php81/php-fpm.d/
+ADD ./build/fpm/configuration/development/*.ini /etc/php7/conf.d/
+ADD ./build/fpm/configuration/development/*.conf /etc/php7/php-fpm.d/
+
+ADD ./build/fpm/entrypoint.sh /entrypoint.sh
+RUN chmod ug+x /entrypoint.sh
+
+ENTRYPOINT [ "/entrypoint.sh" ]
 
-CMD php-fpm81 --nodaemonize
+CMD ["php-fpm7", "--nodaemonize"]
 
 USER mgmaps
+VOLUME ["/html/vendor", "/html/bootstrap/cache"]
 
 FROM development as production
 
 USER root
 
-RUN apk del php81-xdebug
-RUN rm /etc/php81/conf.d/xdebug.ini
+RUN apk del php7-xdebug
+RUN rm /etc/php7/conf.d/xdebug.ini
 
 USER mgmaps
 
diff --git a/build/fpm/entrypoint.sh b/build/fpm/entrypoint.sh
new file mode 100644
index 0000000..18e05c5
--- /dev/null
+++ b/build/fpm/entrypoint.sh
@@ -0,0 +1,23 @@
+#! /bin/sh
+
+_term() {
+    echo "Stopping Process!"
+    kill -TERM "$fpm" 2>/dev/null
+}
+
+trap _term SIGTERM
+
+cd ~
+
+if [ ! -f .env ]
+then
+  pwd
+  ls -alh
+  cp .env.example .env
+  php artisan key:generate
+fi
+
+$@ &
+
+fpm=$!
+wait "$fpm"
\ No newline at end of file
diff --git a/build/fpm/installComposer.sh b/build/fpm/installComposer.sh
new file mode 100644
index 0000000..6c531f8
--- /dev/null
+++ b/build/fpm/installComposer.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
+php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
+ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
+
+if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
+then
+    >&2 echo 'ERROR: Invalid installer checksum'
+    rm composer-setup.php
+    exit 1
+fi
+
+php composer-setup.php --install-dir=/usr/local/bin --filename=composer --version=2.6.5
+RESULT=$?
+rm composer-setup.php
+exit $RESULT
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index e2bf7ec..f8c2eb8 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -2,10 +2,15 @@ version: "3"
 services:
   nginx:
     restart: unless-stopped
+    depends_on:
+      - fpm
     build:
       dockerfile: ./build/nginx/Dockerfile
-      target: development
-    image: metagermaps:nginx-latest
+      target: ${ENVIRONMENT}
+      args:
+        USER: ${USER}
+        GROUP: ${GROUP}
+    image: metagermaps:nginx-${IMAGE_TAG}
     ports:
       - 8080:80
     working_dir: /html
@@ -15,8 +20,24 @@ services:
     restart: unless-stopped
     build:
       dockerfile: ./build/fpm/Dockerfile
-      target: development
-    image: metagermaps:fpmlatest
-    working_dir: /html
+      target: ${ENVIRONMENT}
+    image: metagermaps:fpm-${IMAGE_TAG}
+    volumes:
+      - ./app:/html
+      - vendor:/html/vendor
+      - bootstrap-cache:/html/bootstrap/cache
+  composer:
+    restart: on-failure
+    build:
+      dockerfile: ./build/fpm/Dockerfile
+      target: composer
+    image: metagermaps:composer-${IMAGE_TAG}
     volumes:
       - ./app:/html
+      - composer-cache:/mgmaps/.composer
+      - vendor:/html/vendor
+      - bootstrap-cache:/html/bootstrap/cache
+volumes:
+  composer-cache: {}
+  vendor: {}
+  bootstrap-cache: {}
-- 
GitLab