refactor: bash scripts [stip travis]

- shellchecke and shfmt checked and formatted
- indentation 2 spaces
- redirect errors to stderr
- short if where possible
- add function string to clarify visuaally identify its a funtion
- make [ ... ] to [[ ... ]]
- use == for comparision
- use cat based notes
- remove unecessary spaces
- double quotes where needed
This commit is contained in:
pratikbalar
2021-06-23 09:13:24 +05:30
parent e6a5209001
commit 12eb5c92e4
7 changed files with 566 additions and 596 deletions

View File

@@ -3,38 +3,36 @@
function configureEnv() {
if [[ ! -f /home/frappe/frappe-bench/sites/common_site_config.json ]]; then
if [[ -z "${MARIADB_HOST}" ]]; then
if [[ -z "${POSTGRES_HOST}" ]]; then
echo "MARIADB_HOST or POSTGRES_HOST is not set"
exit 1
fi
if [[ -z "${MARIADB_HOST}" && -z "${POSTGRES_HOST}" ]]; then
echo "MARIADB_HOST or POSTGRES_HOST is not set" >&2
exit 1
fi
if [[ -z "${REDIS_CACHE}" ]]; then
echo "REDIS_CACHE is not set"
echo "REDIS_CACHE is not set" >&2
exit 1
fi
if [[ -z "${REDIS_QUEUE}" ]]; then
echo "REDIS_QUEUE is not set"
echo "REDIS_QUEUE is not set" >&2
exit 1
fi
if [[ -z "${REDIS_SOCKETIO}" ]]; then
echo "REDIS_SOCKETIO is not set"
echo "REDIS_SOCKETIO is not set" >&2
exit 1
fi
if [[ -z "${SOCKETIO_PORT}" ]]; then
echo "SOCKETIO_PORT is not set"
echo "SOCKETIO_PORT is not set" >&2
exit 1
fi
if [[ -z "${DB_PORT}" ]]; then
export DB_PORT=3306
DB_PORT=3306
fi
export DB_HOST="${MARIADB_HOST:-$POSTGRES_HOST}"
DB_HOST="${MARIADB_HOST:-$POSTGRES_HOST}"
envsubst '${DB_HOST}
${DB_PORT}
@@ -46,141 +44,143 @@ function configureEnv() {
}
function checkConnection() {
. /home/frappe/frappe-bench/env/bin/activate
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/check_connection.py
}
function checkConfigExists() {
COUNTER=0
while [[ ! -e /home/frappe/frappe-bench/sites/common_site_config.json ]] && [[ ${COUNTER} -le 30 ]]; do
while [[ ! -e /home/frappe/frappe-bench/sites/common_site_config.json && ${COUNTER} -le 30 ]]; do
sleep 1
((COUNTER = COUNTER + 1))
echo "config file not created, retry ${COUNTER}"
echo "config file not created, retry ${COUNTER}" >&2
done
if [[ ! -e /home/frappe/frappe-bench/sites/common_site_config.json ]]; then
echo "timeout: config file not created"
echo "timeout: config file not created" >&2
exit 1
fi
}
if [[ ! -e /home/frappe/frappe-bench/sites/apps.txt ]]; then
find /home/frappe/frappe-bench/apps -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -r >/home/frappe/frappe-bench/sites/apps.txt
find /home/frappe/frappe-bench/apps -mindepth 1 -maxdepth 1 -type d -printf '%f\n' |
sort -r >/home/frappe/frappe-bench/sites/apps.txt
fi
# symlink node_modules
ln -sfn /home/frappe/frappe-bench/sites/assets/frappe/node_modules \
/home/frappe/frappe-bench/apps/frappe/node_modules
if [[ "$1" == 'start' ]]; then
configureEnv
checkConnection
case "$1" in
if [[ -z "${WORKERS}" ]]; then
export WORKERS=2
fi
start)
configureEnv
checkConnection
if [[ -z "${FRAPPE_PORT}" ]]; then
export FRAPPE_PORT=8000
fi
[[ -z "${WORKERS}" ]] && WORKERS='2'
if [[ -z "${WORKER_CLASS}" ]]; then
export WORKER_CLASS=gthread
fi
[[ -z "${FRAPPE_PORT}" ]] && FRAPPE_PORT='8000'
export LOAD_CONFIG_FILE=""
if [ "${WORKER_CLASS}" = "gevent" ]; then
export LOAD_CONFIG_FILE="-c /home/frappe/frappe-bench/commands/gevent_patch.py"
fi
[[ -z "${WORKER_CLASS}" ]] && WORKER_CLASS='gthread'
if [[ ! -z "${AUTO_MIGRATE}" ]]; then
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/auto_migrate.py
fi
LOAD_CONFIG_FILE=""
[[ "${WORKER_CLASS}" == "gevent" ]] &&
LOAD_CONFIG_FILE="-c /home/frappe/frappe-bench/commands/gevent_patch.py"
. /home/frappe/frappe-bench/env/bin/activate
gunicorn ${LOAD_CONFIG_FILE} -b 0.0.0.0:${FRAPPE_PORT} \
--worker-tmp-dir /dev/shm \
--threads=4 \
--workers ${WORKERS} \
--worker-class=${WORKER_CLASS} \
--log-file=- \
-t 120 frappe.app:application --preload
if [[ -n "${AUTO_MIGRATE}" ]]; then
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/auto_migrate.py
fi
elif [[ "$1" == 'worker' ]]; then
checkConfigExists
checkConnection
# default WORKER_TYPE=default
/home/frappe/frappe-bench/env/bin/activate
gunicorn ${LOAD_CONFIG_FILE} -b 0.0.0.0:${FRAPPE_PORT} \
--worker-tmp-dir /dev/shm \
--threads=4 \
--workers ${WORKERS} \
--worker-class=${WORKER_CLASS} \
--log-file=- \
-t 120 frappe.app:application --preload
;;
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/worker.py
worker)
checkConfigExists
checkConnection
# default WORKER_TYPE=default
elif [[ "$1" == 'schedule' ]]; then
checkConfigExists
checkConnection
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/worker.py
;;
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/background.py
schedule)
checkConfigExists
checkConnection
elif [[ "$1" == 'new' ]]; then
checkConfigExists
checkConnection
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/background.py
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/new.py
exit
;;
elif [[ "$1" == 'drop' ]]; then
checkConfigExists
checkConnection
new)
checkConfigExists
checkConnection
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/drop.py
exit
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/new.py
exit
;;
elif [[ "$1" = 'migrate' ]]; then
drop)
checkConfigExists
checkConnection
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/migrate.py
exit
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/drop.py
exit
;;
elif [[ "$1" == 'doctor' ]]; then
migrate)
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/migrate.py
exit
;;
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/doctor.py ${@:2}
exit
doctor)
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/doctor.py "${@:2}"
exit
;;
elif [[ "$1" == 'backup' ]]; then
backup)
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/backup.py
exit
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/backup.py
exit
;;
elif [[ "$1" == 'console' ]]; then
console)
if [[ -z "$2" ]]; then
echo "Need to specify a sitename with the command:" >&2
echo "console <sitename>" >&2
exit 1
fi
if [[ -z "$2" ]]; then
echo "Need to specify a sitename with the command:"
echo "console <sitename>"
exit 1
fi
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/console.py "$2"
exit
;;
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/console.py "$2"
exit
push-backup)
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/push_backup.py
exit
;;
elif [[ "$1" = 'push-backup' ]]; then
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/push_backup.py
exit
elif [[ "$1" = 'restore-backup' ]]; then
. /home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/restore_backup.py
exit
else
exec $@
fi
restore-backup)
/home/frappe/frappe-bench/env/bin/activate
python /home/frappe/frappe-bench/commands/restore_backup.py
exit
;;
*)
exec "$@"
;;
esac

View File

@@ -2,42 +2,42 @@
set -ea
function getUrl() {
cat ${1} | grep $2 | awk -v word=$2 '$word { gsub(/[",]/,"",$2); print $2}' | tr -d '\n'
grep "$2" "$1" | 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=$(getUrl "$COMMON_SITE_CONFIG_JSON" "db_host")
DB_PORT=$(getUrl "$COMMON_SITE_CONFIG_JSON" "db_port")
if [[ -z "$DB_PORT" ]]; then
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=$(getUrl "$COMMON_SITE_CONFIG_JSON" "redis_cache" | sed 's|redis://||g')
if [[ "$REDIS_CACHE" == *"/"* ]]; then
REDIS_CACHE=$(echo $REDIS_CACHE | cut -f1 -d"/")
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=$(getUrl "$COMMON_SITE_CONFIG_JSON" "redis_queue" | sed 's|redis://||g')
if [[ "$REDIS_QUEUE" == *"/"* ]]; then
REDIS_QUEUE=$(echo $REDIS_QUEUE | cut -f1 -d"/")
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=$(getUrl "$COMMON_SITE_CONFIG_JSON" "redis_socketio" | sed 's|redis://||g')
if [[ "$REDIS_SOCKETIO" == *"/"* ]]; then
REDIS_SOCKETIO=$(echo $REDIS_SOCKETIO | cut -f1 -d"/")
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
echo "Check $DB_HOST:$DB_PORT"
wait-for-it "$DB_HOST:$DB_PORT" -t 1
echo "Check $REDIS_CACHE"
wait-for-it "$REDIS_CACHE" -t 1
echo "Check $REDIS_QUEUE"
wait-for-it "$REDIS_QUEUE" -t 1
echo "Check $REDIS_SOCKETIO"
wait-for-it "$REDIS_SOCKETIO" -t 1
echo "Check ${DB_HOST}:${DB_PORT}"
wait-for-it "${DB_HOST}:${DB_PORT}" -t 1
echo "Check ${REDIS_CACHE}"
wait-for-it "${REDIS_CACHE}" -t 1
echo "Check ${REDIS_QUEUE}"
wait-for-it "${REDIS_QUEUE}" -t 1
echo "Check ${REDIS_SOCKETIO}"
wait-for-it "${REDIS_SOCKETIO}" -t 1
if [[ "$1" = "-p" || "$1" = "--ping-service" ]]; then
echo "Check $2"

View File

@@ -6,11 +6,11 @@ APP_BRANCH=${3}
cd /home/frappe/frappe-bench/
. env/bin/activate
env/bin/activate
cd ./apps
[ "${APP_BRANCH}" ] && BRANCH="-b ${APP_BRANCH}"
[[ -n "${APP_BRANCH}" ]] && BRANCH="-b ${APP_BRANCH}"
git clone --depth 1 -o upstream ${APP_REPO} ${BRANCH} ${APP_NAME}
pip3 install --no-cache-dir -e /home/frappe/frappe-bench/apps/${APP_NAME}
pip3 install --no-cache-dir -e /home/frappe/frappe-bench/apps/${APP_NAME}

View File

@@ -9,7 +9,7 @@ FRAPPE_BRANCH=${4}
mkdir -p /home/frappe/frappe-bench/sites/assets
cd /home/frappe/frappe-bench
echo -e "frappe\n${APP_NAME}" > /home/frappe/frappe-bench/sites/apps.txt
echo -ne "frappe\n${APP_NAME}" >/home/frappe/frappe-bench/sites/apps.txt
mkdir -p apps
cd apps
@@ -36,8 +36,8 @@ mkdir -p /home/frappe/frappe-bench/sites/assets/${APP_NAME}
cp -R /home/frappe/frappe-bench/apps/${APP_NAME}/${APP_NAME}/public/* /home/frappe/frappe-bench/sites/assets/${APP_NAME}
# Add frappe and all the apps available under in frappe-bench here
echo "rsync -a --delete /var/www/html/assets/frappe /assets" > /rsync
echo "rsync -a --delete /var/www/html/assets/${APP_NAME} /assets" >> /rsync
echo "rsync -a --delete /var/www/html/assets/frappe /assets" >/rsync
echo "rsync -a --delete /var/www/html/assets/${APP_NAME} /assets" >>/rsync
chmod +x /rsync
rm /home/frappe/frappe-bench/sites/apps.txt

View File

@@ -1,63 +1,38 @@
#!/bin/bash
#!/bin/bash -ae
## Thanks
# https://serverfault.com/a/919212
##
set -e
rsync -a --delete /var/www/html/assets/* /assets
. /rsync
/rsync
touch /var/www/html/sites/.build -r $(ls -td /assets/* | head -n 1)
touch /var/www/html/sites/.build -r "$(ls -td /assets/* | head -n 1)"
if [[ -z "$FRAPPE_PY" ]]; then
export FRAPPE_PY=0.0.0.0
fi
[[ -z "${FRAPPE_PY}" ]] && FRAPPE_PY='0.0.0.0'
if [[ -z "$FRAPPE_PY_PORT" ]]; then
export FRAPPE_PY_PORT=8000
fi
[[ -z "${FRAPPE_PY_PORT}" ]] && FRAPPE_PY_PORT='8000'
if [[ -z "$FRAPPE_SOCKETIO" ]]; then
export FRAPPE_SOCKETIO=0.0.0.0
fi
[[ -z "${FRAPPE_SOCKETIO}" ]] && FRAPPE_SOCKETIO='0.0.0.0'
if [[ -z "$SOCKETIO_PORT" ]]; then
export SOCKETIO_PORT=9000
fi
[[ -z "${SOCKETIO_PORT}" ]] && SOCKETIO_PORT='9000'
if [[ -z "$HTTP_TIMEOUT" ]]; then
export HTTP_TIMEOUT=120
fi
[[ -z "${HTTP_TIMEOUT}" ]] && HTTP_TIMEOUT='120'
if [[ -z "$UPSTREAM_REAL_IP_ADDRESS" ]]; then
export UPSTREAM_REAL_IP_ADDRESS=127.0.0.1
fi
[[ -z "${UPSTREAM_REAL_IP_ADDRESS}" ]] && UPSTREAM_REAL_IP_ADDRESS='127.0.0.1'
if [[ -z "$UPSTREAM_REAL_IP_RECURSIVE" ]]; then
export UPSTREAM_REAL_IP_RECURSIVE=off
fi
[[ -z "${UPSTREAM_REAL_IP_RECURSIVE}" ]] && UPSTREAM_REAL_IP_RECURSIVE='off'
if [[ -z "$UPSTREAM_REAL_IP_HEADER" ]]; then
export UPSTREAM_REAL_IP_HEADER="X-Forwarded-For"
fi
[[ -z "${UPSTREAM_REAL_IP_HEADER}" ]] && UPSTREAM_REAL_IP_HEADER='X-Forwarded-For'
if [[ -z "$FRAPPE_SITE_NAME_HEADER" ]]; then
export FRAPPE_SITE_NAME_HEADER="\$host"
fi
[[ -z "${FRAPPE_SITE_NAME_HEADER}" ]] && FRAPPE_SITE_NAME_HEADER="\$host"
if [[ -z "$HTTP_HOST" ]]; then
export HTTP_HOST="\$http_host"
fi
[[ -z "${HTTP_HOST}" ]] && HTTP_HOST="\$http_host"
if [[ -z "$SKIP_NGINX_TEMPLATE_GENERATION" ]]; then
export SKIP_NGINX_TEMPLATE_GENERATION=0
fi
[[ -z "${SKIP_NGINX_TEMPLATE_GENERATION}" ]] && SKIP_NGINX_TEMPLATE_GENERATION='0'
if [[ $SKIP_NGINX_TEMPLATE_GENERATION -eq 1 ]]
then
if [[ ${SKIP_NGINX_TEMPLATE_GENERATION} == 1 ]]; then
echo "Skipping default NGINX template generation. Please mount your own NGINX config file inside /etc/nginx/conf.d"
else
echo "Generating default template"
@@ -71,14 +46,14 @@ else
${FRAPPE_SITE_NAME_HEADER}
${HTTP_HOST}
${UPSTREAM_REAL_IP_HEADER}' \
< /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf
</etc/nginx/conf.d/default.conf.template >/etc/nginx/conf.d/default.conf
fi
echo "Waiting for frappe-python to be available on $FRAPPE_PY port $FRAPPE_PY_PORT"
timeout 10 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 1; done' $FRAPPE_PY $FRAPPE_PY_PORT
echo "Frappe-python available on $FRAPPE_PY port $FRAPPE_PY_PORT"
echo "Waiting for frappe-socketio to be available on $FRAPPE_SOCKETIO port $SOCKETIO_PORT"
timeout 10 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 1; done' $FRAPPE_SOCKETIO $SOCKETIO_PORT
echo "Frappe-socketio available on $FRAPPE_SOCKETIO port $SOCKETIO_PORT"
echo "Waiting for frappe-python to be available on ${FRAPPE_PY} port ${FRAPPE_PY_PORT}"
timeout 10 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 1; done' ${FRAPPE_PY} ${FRAPPE_PY_PORT}
echo "Frappe-python available on ${FRAPPE_PY} port ${FRAPPE_PY_PORT}"
echo "Waiting for frappe-socketio to be available on ${FRAPPE_SOCKETIO} port ${SOCKETIO_PORT}"
timeout 10 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 1; done' ${FRAPPE_SOCKETIO} ${SOCKETIO_PORT}
echo "Frappe-socketio available on ${FRAPPE_SOCKETIO} port ${SOCKETIO_PORT}"
exec "$@"