From e63f41def1e75c8037857829acedc47d53d375ba Mon Sep 17 00:00:00 2001 From: Joao M Date: Sat, 20 Aug 2022 16:38:38 +0000 Subject: [PATCH] Setting better log --- assets/scripts/certbot.sh | 8 +++++--- assets/scripts/certbot_to_haproxy.sh | 2 ++ assets/scripts/functions.sh | 9 +++++++++ assets/scripts/haproxy-reload.sh | 14 ++++++++------ assets/scripts/haproxy.sh | 4 +++- 5 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 assets/scripts/functions.sh diff --git a/assets/scripts/certbot.sh b/assets/scripts/certbot.sh index eea18eb..8678231 100755 --- a/assets/scripts/certbot.sh +++ b/assets/scripts/certbot.sh @@ -1,12 +1,14 @@ #!/usr/bin/env bash +source /scripts/functions.sh + if [ ! -f /scripts/letsencrypt_hosts.txt ]; then exit 0 fi # Semaphore if [ -f /tmp/certbot-lock ]; then - echo "[CERTBOT_JOB] $(date +"$EASYHAPROXY_DATEFORMAT") Another process is running" + log "notice" "CERTBOT_JOB" "Another process is running" exit 0 fi @@ -30,7 +32,7 @@ for domain in $(cat /scripts/letsencrypt_hosts.txt); do done if [ -n "$REQUEST_CERTS" ]; then - echo "[CERTBOT_JOB] $(date +"$EASYHAPROXY_DATEFORMAT") Requesting certificates for $REQUEST_CERTS" + log "info" "CERTBOT_JOB" "Requesting certificates for $REQUEST_CERTS" certbot certonly \ --standalone \ --preferred-challenges http \ @@ -45,7 +47,7 @@ if [ -n "$REQUEST_CERTS" ]; then fi if [ -n "$RENEW_CERTS" ]; then - echo "[CERTBOT_JOB] $(date +"$EASYHAPROXY_DATEFORMAT") Resquesting renew certificated fort $RENEW_CERTS" + log "info" "CERTBOT_JOB" "Resquesting renew certificated fort $RENEW_CERTS" certbot renew --post-hook "/scripts/certbot_to_haproxy.sh" fi diff --git a/assets/scripts/certbot_to_haproxy.sh b/assets/scripts/certbot_to_haproxy.sh index 7d84250..532a265 100755 --- a/assets/scripts/certbot_to_haproxy.sh +++ b/assets/scripts/certbot_to_haproxy.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /scripts/functions.sh + # Loop through all Let's Encrypt certificates for CERTIFICATE in `find /etc/letsencrypt/live/* -type d`; do CERTIFICATE=`basename $CERTIFICATE` diff --git a/assets/scripts/functions.sh b/assets/scripts/functions.sh new file mode 100644 index 0000000..e90e730 --- /dev/null +++ b/assets/scripts/functions.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +function log() { + # ARGS: + # - loglevel + # - app + # - message + echo [$2] $(date +"$EASYHAPROXY_DATEFORMAT") [$1]: $3 +} diff --git a/assets/scripts/haproxy-reload.sh b/assets/scripts/haproxy-reload.sh index 8e2df83..2dcd9b1 100755 --- a/assets/scripts/haproxy-reload.sh +++ b/assets/scripts/haproxy-reload.sh @@ -1,11 +1,13 @@ #!/usr/bin/env bash +source /scripts/functions.sh + cd /scripts RELOAD="true" if [[ "static|docker|swarm" != *"$EASYHAPROXY_DISCOVER"* ]];then - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") ERROR: EASYHAPROXY_DISCOVER should be 'static', 'docker', or 'swarm'. I got '$EASYHAPROXY_DISCOVER' instead." + log "info" "CONF_CHECK" "ERROR: EASYHAPROXY_DISCOVER should be 'static', 'docker', or 'swarm'. I got '$EASYHAPROXY_DISCOVER' instead." exit 1 fi @@ -40,7 +42,7 @@ else RELOAD="false" else python3 swarm.py > /etc/haproxy/haproxy.cfg - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") New configuration found" + log "info" "CONF_CHECK" "New configuration found" fi fi @@ -49,23 +51,23 @@ if cmp -s ${CONTROL_FILE} ${CONTROL_FILE}.old ; then fi if [[ ! -z "$1" ]]; then - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") Initial configuration. Skip certbot." + log "info" "CONF_CHECK" "Initial configuration. Skip certbot." else /scripts/certbot.sh fi # If Certbot reloads successfully will create the file /tmp/force-reload if [ -f /tmp/force-reload ]; then - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") New certificates found..." + log "info" "CONF_CHECK" "New certificates found..." RELOAD="true" rm /tmp/force-reload fi if [[ ! -z "$1" ]]; then - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") Starting haproxy..." + log "info" "CONF_CHECK" "Starting haproxy..." /usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg $(ls /etc/haproxy/conf.d/*.cfg 2>/dev/null | xargs -I{} echo -f {}) -p /run/haproxy.pid -S /var/run/haproxy.sock & elif [[ "$RELOAD" == "true" ]]; then - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") Reloading..." + log "info" "CONF_CHECK" "Reloading..." /usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg $(ls /etc/haproxy/conf.d/*.cfg 2>/dev/null | xargs -I{} echo -f {}) -p /run/haproxy.pid -x /var/run/haproxy.sock -sf $(cat /run/haproxy.pid) & fi diff --git a/assets/scripts/haproxy.sh b/assets/scripts/haproxy.sh index 08b98c8..92c15e5 100755 --- a/assets/scripts/haproxy.sh +++ b/assets/scripts/haproxy.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +source /scripts/functions.sh + /usr/sbin/haproxy -v if [ -z "$EASYHAPROXY_DATEFORMAT" ]; then @@ -22,6 +24,6 @@ echo while true; do sleep $EASYHAPROXY_REFRESH_CONF - echo "[CONF_CHECK] $(date +"$EASYHAPROXY_DATEFORMAT") - Heartbeat." + log "info" "CONF_CHECK" "Heartbeat." /scripts/haproxy-reload.sh done