Skip to content

Commit b1bf41b

Browse files
committed
fix: Remove hard-coded /bitnami references (WIP!)
WIP. Mostly done but mdl-common isn't done, and all needs testing. Closes #13.
1 parent 28abd2f commit b1bf41b

4 files changed

Lines changed: 28 additions & 16 deletions

File tree

lib/mdl-common.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ function update_config() {
162162
local -r env_dir="$MDL_ENVS_DIR/$1"
163163
local -r env_config_file="$env_dir/src/config.php"
164164

165+
# Get the Moodle container and its mount paths for this environment
166+
local -r container="$(container_tool ps -f "label=com.docker.compose.project=$1" --format '{{.Names}}' | grep moodle | head -1)"
167+
local -r data_path=${container:+$(container_tool inspect "$container" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("data"))) | .Destination')}
168+
local -r src_path=${container:+$(container_tool inspect "$container" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("src"))) | .Destination')}
169+
170+
# TODO: Maybe this isn't necessary anymore if WWWROOT is automatically set in .env files.
171+
# TODO: Plus, technically the $env_config_file referenced in this if block is broken.
165172
# Get desired wwwroot value
166173
if [ -z "$WWWROOT" ]; then
167174
local -r defaultwwwroot="$(grep -o -E "CFG->wwwroot\s*=\s*'(.*)';" "$env_config_file" | cut -d"'" -f2)"
@@ -183,6 +190,7 @@ function update_config() {
183190
source /env/.env
184191
WWWROOT=$WWWROOT
185192
"
193+
# TODO: Properly handle config `dataroot` updates, excluding if data_path is not known.
186194
cmd="$cmd $(cat <<'EOF'
187195
replace_config_value() {
188196
local content="$1"

libexec/mdl-cli.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,21 @@ mnames=$("$scr_dir"/mdl-select-env.sh "$1")
5353

5454
for mname in $mnames; do
5555

56-
# Get the Moodle container for this environment
56+
# Get the Moodle container and base directory for this environment
5757
container="$(container_tool ps -f "label=com.docker.compose.project=$mname" --format '{{.Names}}' | grep moodle | head -1)"
5858
[[ -z $container ]] && echo "${red}Could not find a container running Moodle for $ul$mname$rmul!$norm" >&2 && exit 1
59+
base_dir=$(container_tool inspect "$container" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("src"))) | .Destination')
60+
[[ -z $base_dir ]] && echo "${red}Could not determine Moodle base directory for $ul$mname$rmul!$norm" >&2 && exit 1
5961

6062
cmd="$2"
6163
# If they did not provide a cmd, list the available commands
6264
if [[ -z $cmd ]]; then
6365
echo "${bold}${ul}Available Commands$norm"
64-
container_tool exec -t "$container" find /bitnami/moodle/admin/cli -maxdepth 1 -type f -exec basename {} .php \; | sort | sed 's/^/ - /'
66+
container_tool exec -t "$container" find "$base_dir/admin/cli" -maxdepth 1 -type f -exec basename {} .php \; | sort | sed 's/^/ - /'
6567
exit
6668
fi
6769

6870
# Run the command, passing any additional arguments they passed on to this script
69-
container_tool exec $paramI -t "$container" php "/bitnami/moodle/admin/cli/$cmd.php" "${@:3}"
71+
container_tool exec $paramI -t "$container" php "$base_dir/admin/cli/$cmd.php" "${@:3}"
7072

7173
done

libexec/mdl-init.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,12 @@ if [[ -n $mname ]]; then
318318
fi
319319
done
320320
echo
321-
# Start the environment. Bitnami image will automatically bootstrap install. Wait to finish.
321+
# Start the environment, automatically bootstrapping install. Wait to finish.
322322
branchver="$branchver" "$scr_dir/mdl-start.sh" "$mname" -q
323323
moodle_svc=$(container_tool ps --filter "label=com.docker.compose.project=$mname" --format '{{.Names}}' | grep moodle)
324324
src_vol_name=$(container_tool volume ls -q --filter "label=com.docker.compose.project=$mname" | grep src)
325+
src_path=$(container_tool inspect "$moodle_svc" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("src"))) | .Destination')
326+
data_path=$(container_tool inspect "$moodle_svc" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("data"))) | .Destination')
325327
# Do git install once standard install completes.
326328
function git_cmd() {
327329
container_tool run --rm -t --name "$mname-git-$(uuidgen)" -v "$src_vol_name":/git "$MDL_GIT_IMAGE" -c safe.directory=/git "$@"
@@ -359,7 +361,7 @@ if [[ -n $mname ]]; then
359361
}
360362
!skip { print }
361363
'
362-
config_file=/bitnami/moodle/config.php
364+
config_file=$src_path/config.php
363365
revised_config_file=$(mktemp)
364366
container_tool exec -it "$moodle_svc" awk "$awk_cmd" "$config_file" > "$revised_config_file"
365367
container_tool cp "$revised_config_file" "$moodle_svc":"$config_file"
@@ -382,13 +384,13 @@ if [[ -n $mname ]]; then
382384
"$scr_dir/mdl-cli.sh" "$mname" upgrade --non-interactive
383385
# After upgrades, we need to fix permissions.
384386
# Ref: https://docs.moodle.org/4x/sv/Security_recommendations#Running_Moodle_on_a_dedicated_server
385-
container_tool exec -it "${moodle_svc}" bash -c '
386-
chown -R daemon:daemon /bitnami/moodle /bitnami/moodledata
387-
find /bitnami/moodle -type d -print0 | xargs -0 chmod 755
388-
find /bitnami/moodle -type f -print0 | xargs -0 chmod 644
389-
find /bitnami/moodledata -type d -print0 | xargs -0 chmod 700
390-
find /bitnami/moodledata -type f -print0 | xargs -0 chmod 600
391-
'
387+
container_tool exec -it "${moodle_svc}" bash -c "
388+
chown -R daemon:daemon '$src_path' '$data_path'
389+
find '$src_path' -type d -print0 | xargs -0 chmod 755
390+
find '$src_path' -type f -print0 | xargs -0 chmod 644
391+
find '$data_path' -type d -print0 | xargs -0 chmod 700
392+
find '$data_path' -type f -print0 | xargs -0 chmod 600
393+
"
392394
fi
393395
echo 🎉 Done!
394396
else

libexec/mdl-restore.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ for mname in $mnames; do
186186
-e MARIADB_DATABASE="${DB_NAME:-moodle}" \
187187
-e MARIADB_COLLATE=utf8mb4_unicode_ci \
188188
-e MARIADB_SKIP_TEST_DB=yes \
189-
-v "$db_vol_name":/bitnami/mariadb/data \
189+
-v "$db_vol_name":"$db_path" \
190190
-v "$sql_path":/docker-entrypoint-initdb.d/restore.sql:Z,ro \
191191
"$MARIADB_IMAGE" > /dev/null
192192
# MariaDB doesn't have a "run task and exit" mode, so we just wait until
@@ -201,6 +201,9 @@ for mname in $mnames; do
201201
pid_db=$!
202202
fi
203203

204+
# Update Moodle config
205+
export_env_and_update_config "$mname"
206+
204207
# When done, clean up. Down the stack and remove the temp volume.
205208
wait $pid_src
206209
container_tool volume rm -f "$temp_vol_name" > /dev/null
@@ -213,9 +216,6 @@ for mname in $mnames; do
213216
rm -fv "$MDL_BACKUP_DIR/$data_target" "$MDL_BACKUP_DIR/$src_target" "$MDL_BACKUP_DIR/$db_target"
214217
fi
215218

216-
# Update Moodle config
217-
export_env_and_update_config "$mname"
218-
219219
echo "Done restoring $ul$mname$rmul from $backup_source_desc backup set with label $ul$label$norm."
220220

221221
# Unset environment variables

0 commit comments

Comments
 (0)