|
1 | 1 | #!/usr/bin/env bash |
2 | | -# source .env so that we know when to start in test mode |
| 2 | +set -e |
| 3 | + |
| 4 | +# This script is used to start a development environment using docker compose |
| 5 | +# It allows overriding app code with local directories and controlling execution mode. |
| 6 | + |
| 7 | +RED="\e[1;31m" |
3 | 8 | GREEN="\e[1;32m" |
4 | 9 | ENDCOLOR="\e[0m" |
5 | | -MODE="dev" |
6 | | - |
7 | | -source .env |
8 | | -extra_compose_args="" |
9 | | - |
10 | | -# Read the apps and their code paths from the arguments passed to the script |
11 | | -docker_compose_args=() |
12 | | - |
13 | | -# Keep a counter of the number of dev-envs to override |
14 | | -number_of_dev_envs=0 |
15 | | -for arg in "$@"; do |
16 | | - app=$(echo $arg | cut -d ':' -f 1) |
17 | | - path=$(echo $arg | cut -d ':' -f 2) |
18 | | - # Test if the specified path(s) exist. If they do not, halt the script and warn |
19 | | - # the user of this mistake |
20 | | - if [ ! -d ${path} ]; then |
21 | | - # Not going to start the env, so clear the env listing |
22 | | - rm .start-dev-env-listing |
23 | | - echo -e "The specified path for app '${app}' is not a directory. \n" |
24 | | - echo -e "Please review: '${path}'" |
25 | | - exit 1 |
26 | | - fi |
27 | | - echo "export ${app^^}_CODE_PATH=${path}" >>.start-dev-env-vars |
28 | | - # Keep a listing of all apps that are started in dev mode, for feedback purposes |
29 | | - echo "${app^^}: ${path}" >>.start-dev-env-listing |
30 | | - docker_compose_args+=("-f ./${app}/docker-compose.override.yml") |
31 | | - let number_of_dev_envs=number_of_dev_envs+1 |
32 | | -done |
33 | 10 |
|
34 | | -# Because numbering is off by one, reference the next arg |
35 | | -let number_of_dev_envs=number_of_dev_envs+1 |
| 11 | +# Clean up legacy files from previous versions of this script |
| 12 | +rm -f .start-dev-env-listing .start-dev-env-vars |
36 | 13 |
|
37 | | -# See if there are .start-dev-env-vars |
38 | | -if [ -f .start-dev-env-vars ]; then |
39 | | - # Read the generated env file with the apps and their code paths |
40 | | - source .start-dev-env-vars |
41 | | - rm .start-dev-env-vars |
| 14 | +# Check if the .env file exists |
| 15 | +if [ -f .env ]; then |
| 16 | + echo "Sourcing .env file" |
| 17 | + source .env |
| 18 | +else |
| 19 | + echo -e "${GREEN}No .env file was read.${ENDCOLOR}" |
42 | 20 | fi |
43 | 21 |
|
44 | | -if [ -f .start-dev-env-listing ]; then |
45 | | - echo -e "${MODE} overrides:\n" |
46 | | - cat .start-dev-env-listing |
47 | | - # Remove the listing |
48 | | - rm .start-dev-env-listing |
| 22 | +# Get script directory |
| 23 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 24 | + |
| 25 | +# Directory where the app dirs with the app's configuration files are located |
| 26 | +APP_DIR="${SCRIPT_DIR}" |
| 27 | +# Name of the docker-compose override file |
| 28 | +DOCKER_OVERRIDE_FILE="docker-compose.override.yml" |
| 29 | + |
| 30 | +docker_compose_up_options=() # Array to hold the options for docker compose up |
| 31 | +docker_compose_options=() # Array to hold the options for docker compose |
| 32 | + |
| 33 | +# Set the docker compose file to use and default profiles for Core |
| 34 | +docker_compose_options+=("-f") |
| 35 | +docker_compose_options+=("${SCRIPT_DIR}/docker-compose.yml") |
| 36 | +docker_compose_options+=("--profile") |
| 37 | +docker_compose_options+=("oidc") |
| 38 | +docker_compose_options+=("--profile") |
| 39 | +docker_compose_options+=("test") |
| 40 | + |
| 41 | +# Show help hint if no arguments are given |
| 42 | +if [ $# -eq 0 ]; then |
| 43 | + echo -e "${GREEN}No options were provided, use -h or --help to see the available options.${ENDCOLOR}" |
49 | 44 | fi |
50 | 45 |
|
51 | | -while true; do |
52 | | - read -p "Do you wish to run Docker compose in the foreground? (press ENTER for Yes)" yn |
53 | | - case $yn in |
54 | | - [Nn]*) |
55 | | - # Use docker compose to start the environment but with the modified override file(s) |
56 | | - echo -e "\nStarting the ${MODE} environment with the following command:\n" |
| 46 | +# Process command line arguments |
| 47 | +while [[ $# -gt 0 ]]; do |
| 48 | + option="$1" |
| 49 | + |
| 50 | + case $option in |
| 51 | + -h | --help) |
| 52 | + echo "Usage: $0 [-h] [-d] [<app>:<path> ...] [-- <docker-compose-args>]" |
| 53 | + echo "Start the Core development environment using docker compose up" |
| 54 | + echo "You can specify the apps for which you want to use the ${DOCKER_OVERRIDE_FILE} with your local code" |
| 55 | + echo "" |
| 56 | + echo "Options:" |
| 57 | + echo " -h, --help Show this help message" |
| 58 | + echo " -d, --detach Run docker compose up in detached mode (-d) with wait (--wait)" |
| 59 | + echo " <app>:<path> The <app> to override followed by the <path> to the local code for the app" |
| 60 | + echo "" |
| 61 | + echo " Any options after -- are passed to docker compose" |
| 62 | + # Create a list of the subdirs of the APP_DIR directory that have a DOCKER_OVERRIDE_FILE |
| 63 | + app_list=() |
| 64 | + for app in "${APP_DIR}"/*; do |
| 65 | + if [ -d "$app" ]; then |
| 66 | + app_name=$(basename "$app") |
| 67 | + if [ -f "${app}/${DOCKER_OVERRIDE_FILE}" ]; then |
| 68 | + app_list+=("${app_name}") |
| 69 | + fi |
| 70 | + fi |
| 71 | + done |
| 72 | + echo "" |
| 73 | + echo -e " Available apps: ${app_list[*]}" |
| 74 | + exit 0 |
| 75 | + ;; |
| 76 | + -d | --detach) |
| 77 | + shift |
| 78 | + # add "--wait" which implies -d but waits for healthchecks |
| 79 | + docker_compose_up_options+=("--wait") |
| 80 | + ;; |
| 81 | + --) |
| 82 | + shift |
| 83 | + # End of options for this script, everything after this is passed to docker compose |
| 84 | + # We need to collect the rest of the arguments |
| 85 | + while [[ $# -gt 0 ]]; do |
| 86 | + docker_compose_up_options+=("$1") |
| 87 | + shift |
| 88 | + done |
| 89 | + break |
| 90 | + ;; |
57 | 91 |
|
58 | | - echo -e "docker compose --profile oidc --profile test -f docker-compose.yml "${docker_compose_args[@]}" "${extra_compose_args}" up -d "${@:$number_of_dev_envs}" --pull always\n" |
59 | | - docker compose --profile oidc --profile test -f docker-compose.yml ${docker_compose_args[@]} ${extra_compose_args} up -d "${@:$number_of_dev_envs}" --pull always |
60 | | - break |
| 92 | + -*) |
| 93 | + # Unknown option |
| 94 | + echo -e "${RED}Error: Unknown option: '${option}'${ENDCOLOR}" |
| 95 | + exit 1 |
61 | 96 | ;; |
| 97 | + |
62 | 98 | *) |
63 | | - # Use docker compose to start the environment but with the modified override file(s) |
64 | | - echo -e "Starting the ${MODE} environment with the following command:\n" |
| 99 | + # This must be an app:path argument |
| 100 | + arg="$1" |
| 101 | + app=$(echo "$arg" | cut -d ':' -f 1) |
| 102 | + path=$(echo "$arg" | cut -d ':' -f 2) |
65 | 103 |
|
66 | | - echo -e "docker compose --profile oidc --profile test -f docker-compose.yml "${docker_compose_args[@]}" "${extra_compose_args}" up "${@:$number_of_dev_envs}" --pull always\n" |
67 | | - docker compose --profile oidc --profile test -f docker-compose.yml ${docker_compose_args[@]} ${extra_compose_args} up "${@:$number_of_dev_envs}" --pull always |
68 | | - break |
69 | | - ;; |
| 104 | + # Basic validation |
| 105 | + if [[ -z "$app" ]] || [[ -z "$path" ]]; then |
| 106 | + echo -e "${RED}Invalid argument format: '$arg'. Expected <app>:<path>${ENDCOLOR}" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + |
| 110 | + # Check if the app is a valid subdirectory in the APP_DIR directory |
| 111 | + if ! [[ $app =~ ^[a-zA-Z0-9_-]+$ ]]; then |
| 112 | + echo -e "${RED}The specified app '${app}' is not a valid name for an app.${ENDCOLOR}\n"; |
| 113 | + exit 1; |
| 114 | + fi |
| 115 | + if [ ! -f "${APP_DIR}/${app}/${DOCKER_OVERRIDE_FILE}" ]; then |
| 116 | + echo -e "${RED}The specified app '${app}' is not a valid directory/app."; |
| 117 | + echo -e "Verify that '${app}' exists in '${APP_DIR}' and contains a '${DOCKER_OVERRIDE_FILE}' file.${ENDCOLOR}\n"; |
| 118 | + exit 1; |
| 119 | + fi |
| 120 | + |
| 121 | + # Replace "~/" with the user's home directory |
| 122 | + path=${path/#\~/$HOME} |
| 123 | + |
| 124 | + # Test if the specified path(s) exist. |
| 125 | + if [ ! -d "${path}" ]; then |
| 126 | + echo -e "${RED}The specified path for app '${app}' is not a directory."; |
| 127 | + echo -e "Please verify that the '${path}' directory exists.${ENDCOLOR}\n"; |
| 128 | + exit 1; |
| 129 | + fi |
| 130 | + |
| 131 | + # Make sure the path is absolute |
| 132 | + path=$(cd "${path}" && pwd) |
| 133 | + echo -e "${GREEN}Configuring override:${ENDCOLOR} ${app} -> ${path}" |
| 134 | + export "${app^^}_CODE_PATH=${path}" |
| 135 | + |
| 136 | + # Add the DOCKER_OVERRIDE_FILE to the docker compose command |
| 137 | + docker_compose_options+=("-f") |
| 138 | + docker_compose_options+=("${APP_DIR}/${app}/${DOCKER_OVERRIDE_FILE}") |
| 139 | + |
| 140 | + shift |
| 141 | + ;; |
70 | 142 | esac |
71 | 143 | done |
| 144 | + |
| 145 | +# Run docker compose up command with the previously prepared options |
| 146 | +echo -e "docker compose ${docker_compose_options[*]} up ${docker_compose_up_options[*]}" |
| 147 | +docker compose "${docker_compose_options[@]}" up "${docker_compose_up_options[@]}" |
0 commit comments