From c19158cd76e273b672db6fbf87ee54c8295acbfe Mon Sep 17 00:00:00 2001 From: Pratik <68642400+pratikbalar@users.noreply.github.com> Date: Mon, 21 Jun 2021 18:19:46 +0530 Subject: [PATCH 1/4] refactor: remove exports, add bash flags healthchecks Add bash flags and removed - errexit - better for fast fail of healthchecks - allexport - export vars by default - nolog - not to add in history (optional) --- build/common/worker/healthcheck.sh | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/build/common/worker/healthcheck.sh b/build/common/worker/healthcheck.sh index 6fac34b7..5811d740 100755 --- a/build/common/worker/healthcheck.sh +++ b/build/common/worker/healthcheck.sh @@ -1,28 +1,29 @@ #!/bin/bash +set -o errexit -o allexport -o nolog -export COMMON_SITE_CONFIG_JSON='/home/frappe/frappe-bench/sites/common_site_config.json' +COMMON_SITE_CONFIG_JSON='/home/frappe/frappe-bench/sites/common_site_config.json' # Set DB Host and port -export DB_HOST=`cat $COMMON_SITE_CONFIG_JSON | awk '/db_host/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'` -export DB_PORT=`cat $COMMON_SITE_CONFIG_JSON | awk '/db_port/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'` +DB_HOST=`cat $COMMON_SITE_CONFIG_JSON | awk '/db_host/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'` +DB_PORT=`cat $COMMON_SITE_CONFIG_JSON | awk '/db_port/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'` if [[ -z "$DB_PORT" ]]; then - export DB_PORT=3306 + DB_PORT=3306 fi # Set REDIS host:port -export REDIS_CACHE=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_cache/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` +REDIS_CACHE=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_cache/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` if [[ "$REDIS_CACHE" == *"/"* ]]; then - export REDIS_CACHE=`echo $REDIS_CACHE | cut -f1 -d"/"` + REDIS_CACHE=`echo $REDIS_CACHE | cut -f1 -d"/"` fi -export REDIS_QUEUE=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_queue/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` +REDIS_QUEUE=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_queue/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` if [[ "$REDIS_QUEUE" == *"/"* ]]; then - export REDIS_QUEUE=`echo $REDIS_QUEUE | cut -f1 -d"/"` + REDIS_QUEUE=`echo $REDIS_QUEUE | cut -f1 -d"/"` fi -export REDIS_SOCKETIO=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_socketio/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` +REDIS_SOCKETIO=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_socketio/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` if [[ "$REDIS_SOCKETIO" == *"/"* ]]; then - export REDIS_SOCKETIO=`echo $REDIS_SOCKETIO | cut -f1 -d"/"` + REDIS_SOCKETIO=`echo $REDIS_SOCKETIO | cut -f1 -d"/"` fi echo "Check $DB_HOST:$DB_PORT" From 4ce63ced9aed940e6ea2fa39c4f643d477ec88b9 Mon Sep 17 00:00:00 2001 From: Pratik <68642400+pratikbalar@users.noreply.github.com> Date: Mon, 21 Jun 2021 22:32:37 +0530 Subject: [PATCH 2/4] refactor: short bash flags insted of verbose style Co-authored-by: Chinmay Pai --- build/common/worker/healthcheck.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/common/worker/healthcheck.sh b/build/common/worker/healthcheck.sh index 5811d740..d42c57be 100755 --- a/build/common/worker/healthcheck.sh +++ b/build/common/worker/healthcheck.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -o errexit -o allexport -o nolog +set -ea COMMON_SITE_CONFIG_JSON='/home/frappe/frappe-bench/sites/common_site_config.json' From 3d67150229abc3b4107ebb979fe4ca7f90007764 Mon Sep 17 00:00:00 2001 From: pratikbalar Date: Mon, 21 Jun 2021 22:56:05 +0530 Subject: [PATCH 3/4] refactor: add function for redis - Style it as per google shell developing guide --- build/common/worker/healthcheck.sh | 34 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/build/common/worker/healthcheck.sh b/build/common/worker/healthcheck.sh index d42c57be..de3309ec 100755 --- a/build/common/worker/healthcheck.sh +++ b/build/common/worker/healthcheck.sh @@ -1,41 +1,45 @@ #!/bin/bash set -ea +function getRedisUrl() { + cat ${1} | grep $2 | awk -v word=$2 '$word { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g' +} + COMMON_SITE_CONFIG_JSON='/home/frappe/frappe-bench/sites/common_site_config.json' # Set DB Host and port -DB_HOST=`cat $COMMON_SITE_CONFIG_JSON | awk '/db_host/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'` -DB_PORT=`cat $COMMON_SITE_CONFIG_JSON | awk '/db_port/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'` +DB_HOST=$(cat $COMMON_SITE_CONFIG_JSON | awk '/db_host/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n') +DB_PORT=$(cat $COMMON_SITE_CONFIG_JSON | awk '/db_port/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n') if [[ -z "$DB_PORT" ]]; then - DB_PORT=3306 + DB_PORT=3306 fi # Set REDIS host:port -REDIS_CACHE=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_cache/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` +REDIS_CACHE=$(getRedisUrl "$COMMON_SITE_CONFIG_JSON" "redis_cache") if [[ "$REDIS_CACHE" == *"/"* ]]; then - REDIS_CACHE=`echo $REDIS_CACHE | cut -f1 -d"/"` + REDIS_CACHE=$(echo $REDIS_CACHE | cut -f1 -d"/") fi -REDIS_QUEUE=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_queue/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` +REDIS_QUEUE=$(getRedisUrl "$COMMON_SITE_CONFIG_JSON" "redis_queue") if [[ "$REDIS_QUEUE" == *"/"* ]]; then - REDIS_QUEUE=`echo $REDIS_QUEUE | cut -f1 -d"/"` + REDIS_QUEUE=$(echo $REDIS_QUEUE | cut -f1 -d"/") fi -REDIS_SOCKETIO=`cat $COMMON_SITE_CONFIG_JSON | awk '/redis_socketio/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g'` +REDIS_SOCKETIO=$(getRedisUrl "$COMMON_SITE_CONFIG_JSON" "redis_socketio") if [[ "$REDIS_SOCKETIO" == *"/"* ]]; then - REDIS_SOCKETIO=`echo $REDIS_SOCKETIO | cut -f1 -d"/"` + REDIS_SOCKETIO=$(echo $REDIS_SOCKETIO | cut -f1 -d"/") fi echo "Check $DB_HOST:$DB_PORT" -wait-for-it $DB_HOST:$DB_PORT -t 1 +wait-for-it "$DB_HOST:$DB_PORT" -t 1 echo "Check $REDIS_CACHE" -wait-for-it $REDIS_CACHE -t 1 +wait-for-it "$REDIS_CACHE" -t 1 echo "Check $REDIS_QUEUE" -wait-for-it $REDIS_QUEUE -t 1 +wait-for-it "$REDIS_QUEUE" -t 1 echo "Check $REDIS_SOCKETIO" -wait-for-it $REDIS_SOCKETIO -t 1 +wait-for-it "$REDIS_SOCKETIO" -t 1 if [[ "$1" = "-p" ]] || [[ "$1" = "--ping-service" ]]; then - echo "Check $2" - wait-for-it $2 -t 1 + echo "Check $2" + wait-for-it "$2" -t 1 fi From 7f26399ebc321eca3c404cb52132f03958329531 Mon Sep 17 00:00:00 2001 From: pratikbalar Date: Mon, 21 Jun 2021 23:02:00 +0530 Subject: [PATCH 4/4] refactor: Change function name, wide support --- build/common/worker/healthcheck.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build/common/worker/healthcheck.sh b/build/common/worker/healthcheck.sh index de3309ec..78973bea 100755 --- a/build/common/worker/healthcheck.sh +++ b/build/common/worker/healthcheck.sh @@ -1,31 +1,31 @@ #!/bin/bash set -ea -function getRedisUrl() { - cat ${1} | grep $2 | awk -v word=$2 '$word { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' | sed 's|redis://||g' +function getUrl() { + cat ${1} | grep $2 | awk -v word=$2 '$word { gsub(/[",]/,"",$2); print $2}' | tr -d '\n' } COMMON_SITE_CONFIG_JSON='/home/frappe/frappe-bench/sites/common_site_config.json' # Set DB Host and port -DB_HOST=$(cat $COMMON_SITE_CONFIG_JSON | awk '/db_host/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n') -DB_PORT=$(cat $COMMON_SITE_CONFIG_JSON | awk '/db_port/ { gsub(/[",]/,"",$2); print $2}' | tr -d '\n') +DB_HOST=$(getUrl "$COMMON_SITE_CONFIG_JSON" "db_host") +DB_PORT=$(getUrl "$COMMON_SITE_CONFIG_JSON" "db_port") if [[ -z "$DB_PORT" ]]; then DB_PORT=3306 fi # Set REDIS host:port -REDIS_CACHE=$(getRedisUrl "$COMMON_SITE_CONFIG_JSON" "redis_cache") +REDIS_CACHE=$(getUrl "$COMMON_SITE_CONFIG_JSON" "redis_cache" | sed 's|redis://||g') if [[ "$REDIS_CACHE" == *"/"* ]]; then REDIS_CACHE=$(echo $REDIS_CACHE | cut -f1 -d"/") fi -REDIS_QUEUE=$(getRedisUrl "$COMMON_SITE_CONFIG_JSON" "redis_queue") +REDIS_QUEUE=$(getUrl "$COMMON_SITE_CONFIG_JSON" "redis_queue" | sed 's|redis://||g') if [[ "$REDIS_QUEUE" == *"/"* ]]; then REDIS_QUEUE=$(echo $REDIS_QUEUE | cut -f1 -d"/") fi -REDIS_SOCKETIO=$(getRedisUrl "$COMMON_SITE_CONFIG_JSON" "redis_socketio") +REDIS_SOCKETIO=$(getUrl "$COMMON_SITE_CONFIG_JSON" "redis_socketio" | sed 's|redis://||g') if [[ "$REDIS_SOCKETIO" == *"/"* ]]; then REDIS_SOCKETIO=$(echo $REDIS_SOCKETIO | cut -f1 -d"/") fi