diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index f55ff7b..01a4ae5 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -8,19 +8,21 @@ FROM debian:bookworm-slim RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop # User and Group -RUN groupadd -r hugo && useradd -r -g hugo hugo +#RUN groupadd -r hugo && useradd -r -g hugo hugo # Hugo Directory Tree -RUN mkdir -pv /var/www/hugo && chown -Rv hugo:hugo /var/www/hugo +RUN mkdir -pv /var/www/hugo/ # NGINX Directory Tree -RUN mkdir -pv /var/www/html/ && chown -Rv hugo:hugo /var/www/html/ +RUN mkdir -pv /var/www/html/ # Copy Cron Job to Update Git Repo -COPY files/hugo.crontab /etc/cron.d/hugo -COPY files/hugo.cronjob.sh /var/www/hugo/cronjob.sh +COPY files/crontab /etc/crontab +COPY files/cronjob.sh /var/www/hugo/cronjob.sh + +# Hugo User Permissions +RUN chown -Rv www-data:www-data /var/www/ RUN chmod +x /var/www/hugo/cronjob.sh -RUN crontab /etc/cron.d/hugo # Copy Start Script COPY files/main.sh /root/main.sh diff --git a/Config/HugoExample/files/cronjob.sh b/Config/HugoExample/files/cronjob.sh new file mode 100644 index 0000000..88accb9 --- /dev/null +++ b/Config/HugoExample/files/cronjob.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +cd /var/www/hugo/site + +echo "*** Running cronjob @ `date` ***" + +# Pull any updates, and if the project is already up to date, exit successfully. +git pull | grep -v "up to date" +status="$?" +echo "* Pull status is '$status'." +if [[ $status != 0 && -e /var/www/html/index.html ]]; then + echo "* Site is already up to date and copied, exiting." + exit 0 +fi + +# If the project was not already up to date, build the new files for NGINX. +hugo --gc --minify +status="$?" +echo "* Hugo status is '$status'." +if [[ $status == 0 && -e public/index.html ]]; then + echo "* Copying files..." + rm -rfv /var/www/html/* + mv -v public/* /var/www/html/ +fi + +exit 0 diff --git a/Config/HugoExample/files/crontab b/Config/HugoExample/files/crontab new file mode 100644 index 0000000..cd21442 --- /dev/null +++ b/Config/HugoExample/files/crontab @@ -0,0 +1 @@ +* * * * * www-data /var/www/hugo/cronjob.sh diff --git a/Config/HugoExample/files/hugo.cronjob.sh b/Config/HugoExample/files/hugo.cronjob.sh deleted file mode 100644 index 8bb86b7..0000000 --- a/Config/HugoExample/files/hugo.cronjob.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash - -cd /var/www/hugo/site - -git pull - -hugo --gc --minify - -rm -rfv /var/www/html/* -cp -rfv public/* /var/www/html/ diff --git a/Config/HugoExample/files/hugo.crontab b/Config/HugoExample/files/hugo.crontab deleted file mode 100644 index 4bfa3ec..0000000 --- a/Config/HugoExample/files/hugo.crontab +++ /dev/null @@ -1 +0,0 @@ -* * * * * hugo /var/www/hugo/cronjob.sh diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh index 75bad27..73771a9 100644 --- a/Config/HugoExample/files/main.sh +++ b/Config/HugoExample/files/main.sh @@ -12,22 +12,36 @@ typeset -u DEV echo "DEV=$DEV" echo "*** Creating Git Repo ***" -sudo -u hugo git clone --recurse-submodules $REPO /var/www/hugo/site +sudo -u www-data git clone --recurse-submodules $REPO /var/www/hugo/site +status="$?" + +echo "*** Validating Git Repo ***" +if [[ $status != 0 || ! -d /var/www/hugo/site/.git ]]; then + echo "ERROR: Hugo project may not have cloned correctly. status='$status'" + echo "Aborting." + exit 1 +fi +echo "* Site exists!" echo "*** Copying Static Files to NGINX ***" -sudo -u hugo /var/www/hugo/cronjob.sh +rm -rfv /var/www/html/* +sudo -u www-data /var/www/hugo/cronjob.sh echo "*** Starting Cron ***" service cron start +service cron status if [[ "$PROD" == "Y"* || "$PROD" == "T"* ]]; then echo "*** Starting Production Server Loop ***" while true; do - curl -sS http://localhost:80 > /dev/null || { + http_code="`curl -sS http://localhost:80 -o /dev/null -w "%{http_code}"`" + if [[ $http_code != 200 ]]; then echo "* Prod server not detected, starting..." + service nginx status service nginx start - } + service nginx status + fi sleep 15 done & @@ -39,19 +53,28 @@ if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then echo "*** Starting Development Server Loop ***" while true; do - curl -sS http://localhost:1380 > /dev/null || { + http_code="`curl -sS http://localhost:1380 -o /dev/null -w "%{http_code}"`" + if [[ $http_code != 200 ]]; then echo "* Dev server not detected, starting..." cd /var/www/hugo/site killall hugo 2>/dev/null - sudo -u hugo hugo server -D --noBuildLock --bind 0.0.0.0 -p 1380 & - } + sudo -u www-data hugo server -D --noBuildLock --bind 0.0.0.0 -p 1380 & + fi sleep 30 done & fi +echo "*** Following Mail Files ***" +cd /var/mail +touch mail www-data +chown -v mail:mail mail +chown -v www-data:mail www-data +chmod -v 660 mail www-data +tail -f mail www-data & + cd -echo "*** Finished $0 ***" +echo "*** Finished $0 @ `date` ***" wait -n