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}