From 4a4a486f9f8d51a116714e97691654ba2231a0aa Mon Sep 17 00:00:00 2001 From: Mario Di Lorenzo <mario.dilorenzo@garr.it> Date: Thu, 30 Nov 2023 09:33:10 +0100 Subject: [PATCH] Added gomplate to templating and docker compose deploy --- .otp.env | 7 +++++ Dockerfile | 6 +++- README.md | 28 +++++++++++++++---- config/config-template.php | 7 ----- config/config.php.template | 4 +++ docker-compose.yml | 11 ++++++++ ...g-template.php => otp_config.php.template} | 8 +++--- templating.sh | 8 ++++++ 8 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 .otp.env delete mode 100644 config/config-template.php create mode 100644 config/config.php.template create mode 100644 docker-compose.yml rename otp_server_config/{otp_config-template.php => otp_config.php.template} (67%) create mode 100644 templating.sh diff --git a/.otp.env b/.otp.env new file mode 100644 index 0000000..dd0a7a7 --- /dev/null +++ b/.otp.env @@ -0,0 +1,7 @@ +# otp_config.php +DB_HOST=edugain-db +DB_DATABASE=otp +USER=otp +PASSWORD=xxx + +DB_CONFIG_LOCATION=/var/otp_server_config/otp_config.php diff --git a/Dockerfile b/Dockerfile index 30f474f..4536422 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,13 +2,17 @@ FROM php:8.1-apache WORKDIR /var/www/html RUN docker-php-ext-install mysqli +COPY --from=hairyhenderson/gomplate:v3.11.3 /gomplate /bin/gomplate COPY --from=docker.io/library/composer:latest /usr/bin/composer /usr/bin/composer COPY composer.json . RUN apt-get update -RUN apt-get install -y git +RUN apt-get install -y git vim RUN composer update COPY otp_server.php otp_server.php COPY config/ config COPY otp_server_config/ /var/otp_server_config EXPOSE 80 + +COPY templating.sh /templating.sh +RUN chmod a+x /templating.sh diff --git a/README.md b/README.md index 2d08651..b78c2bf 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,13 @@ If you run stuff within secure environment (as you should) you do not need https When you unpack the code from Git: - Run composer to pull the TOTP PHP package. -- Create a directory where you will place server config file and copy **otp_config-template.php** there +- Create a directory where you will place server config file and copy **otp_config.php.template** there using a name of your choice; this file contains access details to the OTP database and it must be read by the server, but should not be placed within the web-server directory, in case a problem with your PHP could lead to exposure. -- In the config directory, copy `config-template.php` to `config.php` and put in the location of the otp_server config file. +- In the config directory, copy `config.php.template` to `config.php` and put in the location of the otp_server config file. - Configure your httpd server to be able to execute otp_server.php + ### Using Docker We suggest that you use **--network host** Docker run option which will allow address @@ -54,12 +55,12 @@ resolution based on the host machine and standard port 80 for connections. - You need docker instaled on your host machine - If you do not want to use ` --network host ` option then decide which port will be mapped to your Docker image (say 8080) - Download the code from git -- In the ` config ` directory, copy ` config-template.php ` to ` config.php `; do not modify the settings -- In the ` otp_server_config ` directory copy ` otp_config-template.php ` to ` otp_confi.php ` and modify the settings as needed +- In the ` config ` directory, copy ` config.php.template. ` to ` config.php `; do not modify the settings +- In the ` otp_server_config ` directory copy ` otp_config.php.template ` to ` otp_config.php ` and modify the settings as needed - As root run ` docker build -t otp_server:latest . ` - As root run ` docker run -d --name otp_server --network host --rm otp_server:latest ` -### Running the dosker image at boot +### Running the docker image at boot For systems using systemctl we suggest to install the new service called ` otp-docker ` to do that create the ` otp-docker.service ` file in ` /usr/lib/systemd/system ` with the contents as below: @@ -87,6 +88,23 @@ systemctl start otp-docker systemctl enable otp-docker ``` +## Create and upload the image on the Container Registry + +- You need docker installed on your host machine +- Download the code from git +- As root run ` docker login gitlab.software.geant.org:5050 ` +- As root run ` docker build -t gitlab.software.geant.org:5050/edugain/ot/totp-server:<VERSION> . ` +- As root run ` docker push gitlab.software.geant.org:5050/edugain/ot/totp-server:<VERSION> ` + + +## Using Docker Compose + +- You need docker and docker compose installed on your host machine +- Modify the settings file ` .otp.env ` as needed +- Update the ` docker-compose.yml ` file as needed +- As root run ` docker compose up -d ` +- As root run ` docker exec otp_server /templating.sh ` + ## Testing diff --git a/config/config-template.php b/config/config-template.php deleted file mode 100644 index d563f97..0000000 --- a/config/config-template.php +++ /dev/null @@ -1,7 +0,0 @@ -<?php -// location of otp_config.php -//current path corresponds to the settings in the Dockerfile -// therefore you you are using Docker intall do not change this - -define('DB_CONFIG_LOCATION', '/var/otp_server_config/otp_config.php'); - diff --git a/config/config.php.template b/config/config.php.template new file mode 100644 index 0000000..003c1ce --- /dev/null +++ b/config/config.php.template @@ -0,0 +1,4 @@ +<?php +// location of otp_config.php + +define('DB_CONFIG_LOCATION', '{{ .Env.DB_CONFIG_LOCATION }}'); diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f37566e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3.9' +services: + otp_server: + image: "gitlab.software.geant.org:5050/edugain/ot/totp-server:1.0.0" + container_name: "edugain_otp_server" + hostname: "edugain_otp_server" + env_file: + - ".otp.env" + ports: + - "8080:80" + diff --git a/otp_server_config/otp_config-template.php b/otp_server_config/otp_config.php.template similarity index 67% rename from otp_server_config/otp_config-template.php rename to otp_server_config/otp_config.php.template index 99fe5ae..1bb8967 100644 --- a/otp_server_config/otp_config-template.php +++ b/otp_server_config/otp_config.php.template @@ -7,8 +7,8 @@ * config of the technical site, however the database user must not be the same * access provileges need to be different */ -define('DB_HOST','edugain-db'); -define('DB_DATABASE','otp'); -define('USER', 'otp'); -define('PASSWORD', 'xxxx'); +define('DB_HOST','{{ .Env.DB_HOST }}'); +define('DB_DATABASE','{{ .Env.DB_DATABASE }}'); +define('USER', '{{ .Env.USER }}'); +define('PASSWORD', '{{ .Env.PASSWORD }}'); diff --git a/templating.sh b/templating.sh new file mode 100644 index 0000000..a9d6f3c --- /dev/null +++ b/templating.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +gomplate -f /var/www/html/config/config.php.template -o /var/www/html/config/config.php +rm /var/www/html/config/config.php.template + +gomplate -f /var/otp_server_config/otp_config.php.template -o /var/otp_server_config/otp_config.php +rm /var/otp_server_config/otp_config.php.template + -- GitLab