ptut-virtenv/metactl/post-start.sh

121 lines
3.0 KiB
Bash

#!/bin/bash
source $(dirname `realpath $0`)/environment.sh;
#############################################
## ##
## Executed after launching containers ##
## ##
#############################################
## [1] Wait for database to be READY
# Restore mariadb database
echo -ne " * mariadb socket..";
docker exec $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null;
DB_RESTORED="$?";
sleep 5s;
# Try until connection OK
while [ "$DB_RESTORED" == "1" ]; do
sleep .5;
echo -ne ".";
timeout 1 docker exec $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null;
DB_RESTORED="$?";
done;
echo "ready";
## [2] Restore database
# 1. Check if dump file exists
echo -ne " * Check dump file...........";
test -f $MARIADB_DUMP;
DUMP_FILE_EXISTS="$?";
# 2. Restore database
if [ "$DUMP_FILE_EXISTS" = "0" ]; then
echo "found";
# Restore mariadb database
echo -ne " * Restore database..........";
cat $MARIADB_DUMP | docker exec -i $MARIADB_SERVICE mysql -uroot -p$MARIADB_ROOT_PASSWORD 2>/dev/null && echo "restored" || echo "failed";
else
echo "missing";
echo -ne " * Create trigger............";
test ! -d ./virtual/ci && mkdir ./virtual/ci;
# Create 'persistent' directory if missing
test ! -d $ROOT/persistent && mkdir -p $ROOT/persistent && echo "created" || ( echo "error"; exit 1 );
echo " * Create dump file..........created";
touch $MARIADB_DUMP;
fi;
## [3] Grant www-data for CI triggers
echo -n " * Grant php on triggers.....";
docker exec php-fpm chown www-data -R /ci && echo "granted" || (echo "failed"; exit 1);
## [4] Update vhost libs/builds
# 1. Update composer
echo -n " * Build composer............";
docker exec php-fpm bash -c "cd /vhost; composer install" >/dev/null 2>&1 && echo "built" || (echo "failed"; exit 1);
# 2. Re-build js/scss
echo -n " * Webpack build.............";
docker exec php-fpm bash -c "cd /vhost; npm install; npm update; npm run build" >/dev/null 2>&1 && echo "built" || (echo "failed"; exit 1);
# 3. touch the file the backup loop will use to run
echo -n " * Mariadb backup loop.......";
backup(){
if [ ! -d $ROOT/backups ]; then
mkdir $ROOT/backups;
fi
touch $ROOT/backuploop
while [ -f $ROOT/backuploop ]; do
# sleep 24 hours before next backup kick in
sleep 86400;
docker exec $MARIADB_SERVICE mysqldump --all-databases --flush-privileges -uroot -p$MARIADB_ROOT_PASSWORD | gzip > $ROOT/backups/$(date -d "today" +"%Y%m%d%H%M").sql.gz;
done;
}
backup &
echo "done"
## [5] Setup/Fix permissions (for API download)
# 1. Create download directories
echo -n " * Create download folders...";
docker exec php-fpm bash -c "mkdir -p /vhost/tmp";
docker exec php-fpm bash -c "mkdir -p /vhost/public_html/tmp";
echo "created"
# 2. Fix permissions
echo -n " * Setup permissions.........";
docker exec php-fpm bash -c "chown -R www-data:www-data /vhost";
docker exec php-fpm bash -c "chmod -R 775 /vhost";
echo "set"