Files
devops/orchestrate/run.sh
2025-09-23 10:54:10 -05:00

192 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# @author Craig McDaniel
#
# Fontend script to docker compose
#
# This is how we run various services in development environments for development.
#
INCLUDE_DIR="/opt/neutopia/git/devops/orchestrate/lib"
# Read in libs
source ${INCLUDE_DIR}/lib.sh
source ${INCLUDE_DIR}/build-lib.sh
# Set some default values. We'll fill these out a few lines down.
COMMAND=""
COMPOSE_FILE=""
AUTO_PREPARE=1
# If the user specifies this flag, then don't run the prepare Ansible playbooks.
if [[ $@ =~ "--no-prepare" ]]; then
AUTO_PREPARE=0
fi
# Use getopts to get the value of the -f argument so we know what compose file to use.
OPTERR=0
while getopts "c:y:" OPTION $@;
do
if [ "${OPTION}" == "c" ]; then
COMMAND=$OPTARG
elif [ "${OPTION}" == "y" ]; then
COMPOSE_FILE=$OPTARG
fi
done
# Build the $DOCKERARGS variable. Exclude any options that we don't want to pass directly to the
# docker-compose script. When we're done, this string will be passed literally to docker-compose.
ARGSORIG=($@)
DOCKER_COMMAND_ARGS=""
i=0
while [ $i -lt 25 ]; do
# Skip the next 2 args: the arg and the value.
if [ "${ARGSORIG[$i]}" == "-c" ]; then
((i=i+2))
# Skip the next 2 args: the arg and the value.
elif [ "${ARGSORIG[$i]}" == "-y" ]; then
((i=i+2))
# Skip the next arg.
elif [ "${ARGSORIG[$i]}" == "--no-prepare" ]; then
((i=i+1))
else
DOCKER_COMMAND_ARGS="${DOCKER_COMMAND_ARGS} ${ARGSORIG[$i]}"
((i++))
fi;
done
# docker-compose expects arguments in this order
DOCKERARGS="-f ${COMPOSE_FILE} ${COMMAND} ${DOCKER_COMMAND_ARGS}"
# Set these ENV variables so we can use them inside the compose YAML files
# Get the image names
# whmcsphp
export BILLING_APACHE2_TAG=`cd /opt/neutopia/git/whmcsphp; git log -n 1 --pretty=format:"%H" .`
export BILLING_APACHE2_IMAGE_NAME="billing-apache2:${BILLING_APACHE2_TAG}"
export BILLING_PHPFPM_TAG=`cd /opt/neutopia/git/whmcsphp; git log -n 1 --pretty=format:"%H" .`
export BILLING_PHPFPM_IMAGE_NAME="billing-phpfpm:${BILLING_PHPFPM_TAG}"
# You have to do this in order for docker-compose to be able to use $HOSTNAME
export HOSTNAME=$HOSTNAME
# Show help text
show_help()
{
echo
echo "Run services in a development environment using docker compose."
echo
echo "Usage: "
echo "$0 -c <command> -y <compose-yaml-file> [command-options]"
echo
echo "Options that are used by this script only:"
echo " -c The command to pass to docker compose. 'run', 'start', 'stop', 'logs', so on..."
echo " -y Specify compose YAML file to use."
echo " --no-prepare Optionally. Do not run the Ansible prepare playbook. You will do the preparing yourself!"
echo
echo "Commands are as follows:"
echo " prepare Run the Ansible prepare playbook to prepare the dev environment for the matching compose YML file."
echo " up Bring all services up and connect stdin/stdout to it."
echo " logs View your container logs. Combine with -f to continually display logs as they are generated."
echo " down Bring services down."
echo " * Run docker compose --help for an exhaustive list of commands."
echo
echo "Command options are passed directly to docker-compose and depend on the command."
echo " * For an example of options to the 'up' command, run docker compose up --help"
echo " * For an example of options to the 'down' command, run docker compose down --help"
echo " * And so on ..."
echo
}
# Show help?
if [[ $@ =~ "--help" ]]; then
show_help
exit 0
fi
# Main entry point.
main()
{
if [ "${COMMAND}" = "build" ]; then
echo
echo "The build command is not supported here. You must use the build.sh script to build images.";
echo
exit 1
elif [ "${COMMAND}" = "prepare" ]; then
check_for_valid_args
prepare
elif [ "${COMMAND}" != "" ]; then
check_for_valid_args
echo "Using compose file: ${COMPOSE_FILE}"
#prepare
docker compose $DOCKERARGS
else
show_help
fi
}
# Make sure the docker compose file and the command are both specified. Show error messages if not.
check_for_valid_args()
{
if [ -z $COMMAND ]; then
echo "No command was specified. Please specify a command to pass to docker compose with -c"
echo " --help for details."
exit 1
fi
if [ -z $COMPOSE_FILE ]; then
echo "Please specify a docker compose file with the -y argument. None was specified."
echo " --help for details."
exit 1
fi
}
# ** CURRENTLY NOT USED **
# Run the correct ansible playbook to prepare the environment, depending on what compose file we're running.
# If --no-prepare was specified, we don't execute this.
prepare()
{
# Only prepare if AUTO_PREPARE=1
if [ "${AUTO_PREPARE}" != 1 ]; then
return
fi
# Only run prepare for 'prepare', 'up' and 'start' commands.
if [[ "${COMMAND}" != "prepare" && "${COMMAND}" != "up" && "${COMMAND}" != "start" ]]; then
return
fi
if [ "${COMPOSE_FILE}" = "docker-compose-one.yml" ]; then
prepare_billing
elif [ "${COMPOSE_FILE}" = "docker-compose-two.yml" ]; then
prepare_hehe
if [ $? != 0 ]; then
echo
echo "Just tried to create a docker container with docker image '${IMAGE_NAME}', but that operation failed."
echo "You may need to build the apiserver image again if you have increased the borgdrone SVN revision since the last time you built the image.";
echo "Run 'docker image ls' to see what tags you have for the appserver image, if any."
exit $?;
fi
else
echo "Can't prepare anything because I don't know that docker compose YML file ${COMPOSE_FILE}. Make sure you edit run.sh and add the code for this compose file."
exit 1
fi
}
# ** CURRENTLY NOT USED **
# Prepare the the billing system to run in a dev environment.
prepare_billing()
{
ansible_playbook playbooks/build/prepare_billing.yml
if [ $? != 0 ]; then exit $?; fi
docker_stop apiserver-php
}
# Start the script
main