Skip to content

Instantly share code, notes, and snippets.

@katoquro
Last active March 15, 2023 06:46
Show Gist options
  • Save katoquro/7d45f39f710423c1648f367bbe03ddb3 to your computer and use it in GitHub Desktop.
Save katoquro/7d45f39f710423c1648f367bbe03ddb3 to your computer and use it in GitHub Desktop.
ops.sh - Bash template to create simple run script for composite projects
#!/usr/bin/env bash
set -e
# use docker BuildKit in compose
export COMPOSE_DOCKER_CLI_BUILD=1
#
# Global Variables
#
# should be in .gitignore
CACHE_DIR=".ops-cache"
REPO_ROOT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
OPS_PATH="${REPO_ROOT_PATH}/ops"
CACHE_PATH="${REPO_ROOT_PATH}/${CACHE_DIR}"
PGID_PATH="${CACHE_PATH}/bg_gids"
cd "${REPO_ROOT_PATH}"
mkdir -p "${CACHE_PATH}" "${PGID_PATH}"
if [ "${CI}" = 'true' ]; then
export CI_GRADLE_OPTS=' --console=plain'
export BUILDKIT_PROGRESS=plain
fi
#
# Color Logging
#
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
NO_COLOR='\033[0m'
log_error() {
echo -e "${RED}$*${NO_COLOR}"
}
log_warn() {
echo -e "${YELLOW}$*${NO_COLOR}"
}
log_info() {
echo -e "${GREEN}$*${NO_COLOR}"
}
log_debug() {
if [ "${DEB_LOG}" = 'true' ]; then
echo -e "${BLUE}$*${NO_COLOR}"
fi
}
log_magenta() {
echo -e "${MAGENTA}$*${NO_COLOR}"
}
#
# Util Functions
#
case_error() {
echo "$(log_error "Error:") Unsupported param '$(log_warn "$1")' in args line: $2" >&2
exit 1
}
# shellcheck disable=SC2120
compose_up() {
local service="${1:-infra}"
log_info "RUNNING DOCKER SERVICES FOR: ${service}"
docker compose up --build -d "${service}"
}
compose_stop() {
log_info "STOPPING DOCKER SERVICES"
docker compose stop
}
compose_down() {
log_info "DESTROYING DOCKER SERVICES (CLEANUP)"
docker compose rm -vsf
docker compose down
}
run_bg_job() {
log_info "RUN: ${*:2} (WORK_DIR: '$1')"
local run_dir="${1}"
(
# shellcheck disable=SC2068
cd "${run_dir}" && ${@:2} &
local pid=$!
touch "${PGID_PATH}/$(ps -o pgid= "${pid}" | xargs)"
)
}
stop_bg_jobs() {
for pgid_file in "${PGID_PATH}"/*; do
[[ -e "${pgid_file}" ]] || break
pgid=$(basename "$pgid_file")
log_debug "STOP gid: ${pgid}"
kill -15 -"${pgid}" 2>/dev/null || log_warn "Warning: not jobs with PGID ${pgid}. Cleanup PGID"
rm "${pgid_file}"
done
}
wait_port() {
local port="${1}"
local host="${2:-localhost}"
while
cat </dev/null >"/dev/tcp/${host}/${port}"
[ $? -ne 0 ]
do
echo "wait for ${host}:${port} up"
sleep 3
done
}
#
# Subcommand Functions
#
#
# MAIN
#
log_debug "DEBUG ENABLED"
case "$1" in
help) # this help
log_info "Available subcommands:"
echo -e "$(grep -E '^help).*$' -m1 -A9999 "$0" | grep -E '^(([[:space:]]*[[:alnum:][:space:]|-]+\))|###).*$' | tr -d '#')"
log_magenta "\nRun with 'DEB_LOG=true' to enable debug"
;;
###
run | start) # run all modules
case "$2" in
none) # start without docker
log_info "RUNNING NOTHING AND EXIT"
;;
*)
if [ "$2" = '' ]; then
compose_up
else
case_error "$2" "$@"
fi
;;
esac
;;
###
*)
if [ "$1" = '' ]; then
log_error "No subcommand specified"
echo
exec "$0" help
else
case_error "$1" "$@"
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment