From ffa11a8b71bccda6a0dc9691b204a9d0b881fdf9 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Feb 2018 15:28:52 +0100 Subject: [PATCH] +add `make start`/`make stop` with pre/post scripts +implemented database restore & backup --- Makefile | 15 +++++++++++++++ README.md | 10 +++++++--- metactl/environment.sh | 13 +++++++++++++ metactl/post-start.sh | 20 ++++++++++++++++++++ metactl/post-stop.sh | 9 +++++++++ metactl/pre-start.sh | 9 +++++++++ metactl/pre-stop.sh | 12 ++++++++++++ 7 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 Makefile create mode 100644 metactl/environment.sh create mode 100644 metactl/post-start.sh create mode 100644 metactl/post-stop.sh create mode 100644 metactl/pre-start.sh create mode 100644 metactl/pre-stop.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4020b18 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +start: + @echo -n "> 1. Execute pre-start script.........."; + @bash ./metactl/pre-start.sh && echo "done" || (echo "failed"; exit 1); + @echo -n "> 2. Build & launch environment........"; + @docker-compose up -d > /dev/null 2>&1 && echo "done" || (echo "failed"; exit 1); + @echo -n "> 3. Execute post-start script........."; + @bash ./metactl/post-start.sh && echo "done" || (echo "failed"; exit 1); + +stop: + @echo -n "> 1. Execute pre-stop script..........."; + @bash ./metactl/pre-stop.sh && echo "done" || (echo "failed"; exit 1); + @echo -n "> 2. Stop and remove environment....."; + @docker-compose down > /dev/null 2>&1 && echo "done" || (echo "failed"; exit 1); + @echo -n "> 3. Execute post-stop script.........."; + @bash ./metactl/post-stop.sh && echo "done" || (echo "failed"; exit 1); \ No newline at end of file diff --git a/README.md b/README.md index 25db57b..f5a96a6 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ git clone --recursive https://git.xdrm.io/ptut/virtenv.git You need a consistent Linux System with the following packages : - `docker` - `docker-compose` +- `make` You need to add a local dns record in `/etc/hosts`: @@ -26,12 +27,15 @@ You need to add a local dns record in `/etc/hosts`: **Launch the environment** ```bash -docker-compose up -d +make start ``` +- *pre-start* and *post-start* script will be executed (*i.e.* restore database, etc) - the mounted files will match in the `./virtual` folder. - the website will be accessible at 'ptut.com:8080'. **Stop the environment** ```bash -docker-compose down -``` \ No newline at end of file +make stop +``` +- *pre-stop* and *post-stop* script will be executed (*i.e.* backup database, etc) +- the mounted files will remain in `virtual/` (*e.g.* `/var/log/`). \ No newline at end of file diff --git a/metactl/environment.sh b/metactl/environment.sh new file mode 100644 index 0000000..616005f --- /dev/null +++ b/metactl/environment.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# 1. Absolute directory +ROOT="$(dirname `realpath $0`)"; + +# 2. Service container names +MARIADB_SERVICE="mariadb"; +PHPFPM_SERVICE="php-fpm"; +NGINX_SERVICE="nginx"; + +# 3. Mariadb credentials +MARIADB_ROOT_PASSWORD="root"; +MARIADB_DATABASE_NAME="vhost"; \ No newline at end of file diff --git a/metactl/post-start.sh b/metactl/post-start.sh new file mode 100644 index 0000000..3716862 --- /dev/null +++ b/metactl/post-start.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +source $(dirname `realpath $0`)/environment.sh; + +############################################# +## ## +## Executed after launching containers ## +## ## +############################################# + +# 1.1. Restore mariadb database +cat $ROOT/persistent/mariadb.sql | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME 2>/dev/null; +DB_RESTORED="$?"; + +# 1.2. Try until connection OK +while [ "$DB_RESTORED" != "0" ]; do + sleep 1; + cat $ROOT/persistent/mariadb.sql | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME 2>/dev/null; + DB_RESTORED="$?"; +done; \ No newline at end of file diff --git a/metactl/post-stop.sh b/metactl/post-stop.sh new file mode 100644 index 0000000..3243e0b --- /dev/null +++ b/metactl/post-stop.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +source $(dirname `realpath $0`)/environment.sh; + +############################################ +## ## +## Executed after stopping containers ## +## ## +############################################ \ No newline at end of file diff --git a/metactl/pre-start.sh b/metactl/pre-start.sh new file mode 100644 index 0000000..7ae3bf3 --- /dev/null +++ b/metactl/pre-start.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +source $(dirname `realpath $0`)/environment.sh; + +############################################## +## ## +## Executed before launching containers ## +## ## +############################################## \ No newline at end of file diff --git a/metactl/pre-stop.sh b/metactl/pre-stop.sh new file mode 100644 index 0000000..772376d --- /dev/null +++ b/metactl/pre-stop.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +source $(dirname `realpath $0`)/environment.sh; + +############################################# +## ## +## Executed before stopping containers ## +## ## +############################################# + +# 1. Backup mariadb database +docker exec $MARIADB_SERVICE mysqldump -uroot -p$MARIADB_ROOT_PASSWORD $MARIADB_DATABASE_NAME > $ROOT/persistent/mariadb.sql; \ No newline at end of file