Skip to content

Commit 8dee9e3

Browse files
committed
feat: Add support for custom compose files
Whereas the default.yml compose file will be used if an environment does not specify a custom configuration with the `COMPOSE_FILE` variable, this feature improves the `mdl calc-compose-path` script to actually do some work: It will check the existence of the compose file path (first relative to the `MDL_COMPOSE_DIR` directory, then as an absolute path), and it will output the path of the compose file, and throw an error if the compose file does not exist. This will enable users to customize their compose files. Closes #14.
1 parent 6224c93 commit 8dee9e3

7 files changed

Lines changed: 25 additions & 5 deletions

File tree

libexec/mdl-calc-compose-path.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ EOF
1717
[[ $* =~ -h || $* =~ --help ]] && display_help && exit
1818

1919
requires realpath
20+
mname=$("$scr_dir/mdl-select-env.sh" "$1" --no-all)
21+
export_env "$mname"
2022

2123
# The default config for all versions is `default.yml` file. But if the environment config
2224
# provides a specific compose file, use that one instead. Try first relative to the
2325
# compose directory, then the absolute path.
24-
realpath "$MDL_COMPOSE_DIR/default.yml"
26+
compose_file=${COMPOSE_FILE:-default.yml}
27+
compose_path=$(realpath "$MDL_COMPOSE_DIR/$compose_file" 2>/dev/null) || \
28+
compose_path=$(realpath "$compose_file" 2>/dev/null)
29+
if [[ -n $compose_path ]]; then
30+
echo "$compose_path"
31+
else
32+
echo "${red}Could not find compose file $ul$compose_file$rmul for $ul$mname$rmul.$norm" >&2
33+
exit 1
34+
fi

libexec/mdl-info.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ for mname in $mnames; do
166166
# ENVIRONMENT INFORMATION
167167
fields=(
168168
MOODLE_HOST WWWROOT MOODLE_PORT \
169-
MOODLE_IMAGE MARIADB_IMAGE \
169+
MOODLE_IMAGE MARIADB_IMAGE COMPOSE_FILE \
170170
DB_TYPE DB_HOST DATA_ROOT DB_NAME ROOT_PASSWORD DB_USERNAME DB_PASSWORD \
171171
SOURCE_HOST SOURCE_DATA_PATH SOURCE_SRC_PATH SOURCE_DB_NAME SOURCE_DB_USERNAME SOURCE_DB_PASSWORD \
172172
BOX_CLIENT_ID BOX_CLIENT_SECRET BOX_REDIRECT_URI BOX_FOLDER_ID \
173-
mname running env_path custom_path db_vol_name data_vol_name src_vol_name \
174-
env_status custom_status db_status data_status src_status
173+
mname running env_path compose_path custom_path db_vol_name data_vol_name src_vol_name \
174+
env_status compose_path_status custom_status db_status data_status src_status
175175
)
176176
# Standard configs
177177
export_env "$mname"
@@ -186,6 +186,7 @@ for mname in $mnames; do
186186
# Additional calculated fields
187187
. "$scr_dir/mdl-calc-images.sh" "$mname"
188188
env_path="$MDL_ENVS_DIR/$mname/.env"
189+
compose_path=$("$scr_dir/mdl-calc-compose-path.sh" "$mname" 2>/dev/null)
189190
custom_path="$MDL_ENVS_DIR/$mname/custom-config.sh"
190191
vols=$(container_tool volume ls -q --filter "label=com.docker.compose.project=$mname")
191192
db_vol_name=$(grep db <<< "$vols")
@@ -194,6 +195,7 @@ for mname in $mnames; do
194195
running=false && [[ -n $(container_tool ps -q -f name="$mname") ]] && running=true
195196
running_string="${red}not running$norm" && $running && running_string="${green}running$norm"
196197
env_status=false && [ -f "$env_path" ] && env_status=true
198+
compose_path_status=false && [ -n "$compose_path" ] && compose_path_status=true
197199
custom_status=false && [ -f "$custom_path" ] && custom_status=true
198200
db_status=false && [ -n "$db_vol_name" ] && db_status=true
199201
data_status=false && [ -n "$data_vol_name" ] && data_status=true
@@ -219,6 +221,7 @@ for mname in $mnames; do
219221
echo
220222
pretty_line 'Paths and Volumes'
221223
pretty_line 'Environment file' "$env_path" "$env_status"
224+
pretty_line 'Compose file' "${compose_path:-$COMPOSE_FILE}" "$compose_path_status"
222225
pretty_line 'Custom config file' "$custom_path" "$custom_status"
223226
pretty_line 'Database volume' "${db_vol_name:-${red}missing$norm}" "$db_status"
224227
pretty_line 'Data volume' "${data_vol_name:-${red}missing$norm}" "$data_status"

libexec/mdl-init.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ if [[ -n $mname ]]; then
182182
export_env "$mname"
183183
echo "Environment created at: $ul$MDL_ENVS_DIR/$mname$rmul"
184184
echo
185+
echo "${ul}Compose Configuration$rmul"
186+
COMPOSE_FILE=$(ask "Compose file" "${COMPOSE_FILE:-default.yml}")
187+
echo
185188
echo "${ul}Database Configuration$rmul"
186189
DB_NAME=$(ask "Database name" "$DB_NAME")
187190
ROOT_PASSWORD=$(ask "Root password" "$ROOT_PASSWORD")
@@ -255,7 +258,7 @@ if [[ -n $mname ]]; then
255258
env_file="$MDL_ENVS_DIR/$mname/.env"
256259
# Find any custom variables in the .env file that are not in the default list.
257260
variables=(
258-
ROOT_PASSWORD DB_NAME DB_USERNAME DB_PASSWORD MOODLE_HOST WWWROOT MOODLE_PORT
261+
COMPOSE_FILE ROOT_PASSWORD DB_NAME DB_USERNAME DB_PASSWORD MOODLE_HOST WWWROOT MOODLE_PORT
259262
SOURCE_HOST SOURCE_DATA_PATH SOURCE_SRC_PATH SOURCE_DB_NAME SOURCE_DB_USERNAME SOURCE_DB_PASSWORD
260263
BOX_CLIENT_ID BOX_CLIENT_SECRET BOX_REDIRECT_URI BOX_FOLDER_ID
261264
)

libexec/mdl-logs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ containers="$(container_tool ps -q -f name="$mname" 2> /dev/null)"
2424
[ -z "$containers" ] && echo "The $mname stack is not running." && exit 1
2525

2626
compose_path=$("$scr_dir/mdl-calc-compose-path.sh" "$mname")
27+
[[ -z $compose_path ]] && exit 1
2728
. "$scr_dir/mdl-calc-images.sh" "$mname"
2829
export_env_and_update_config "$mname"
2930
compose_tool -p "$mname" -f "$compose_path" logs "${@:2}"

libexec/mdl-start.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ done
4343
for mname in $mnames; do
4444

4545
compose_path=$("$scr_dir/mdl-calc-compose-path.sh" "$mname")
46+
[[ -z $compose_path ]] && continue
4647
$quiet || echo "Starting $mname..."
4748
. "$scr_dir/mdl-calc-images.sh" "$mname"
4849
export_env_and_update_config "$mname"

libexec/mdl-status.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ for mname in $mnames; do
3535
data_vol_name=$(grep data <<< "$vols")
3636
src_vol_name=$(grep src <<< "$vols")
3737
compose_path=$("$scr_dir/mdl-calc-compose-path.sh" "$mname")
38+
[[ -z $compose_path ]] && continue
3839

3940
$quiet || echo "${ul}Environment: $bold$mname$norm"
4041
# Status

libexec/mdl-stop.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ for mname in $mnames; do
3232
fi
3333

3434
compose_path=$("$scr_dir/mdl-calc-compose-path.sh" "$mname")
35+
[[ -z $compose_path ]] && continue
3536

3637
$quiet || echo "Stopping $mname..."
3738
. "$scr_dir/mdl-calc-images.sh" "$mname"

0 commit comments

Comments
 (0)