Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compose/compose.yml → compose/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: $mname
services:
mariadb:
image: $MARIADB_IMAGE
privileged: true
restart: unless-stopped
networks:
- backend
Expand All @@ -14,7 +13,7 @@ services:
- MARIADB_SKIP_TEST_DB=yes
volumes:
- /etc/localtime:/etc/localtime:ro
- db:/bitnami/mariadb
- db:/bitnami/mariadb/data
moodle:
image: $MOODLE_IMAGE
privileged: true
Expand Down
4 changes: 4 additions & 0 deletions custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ behaviour
bitnamilegacy
booktool
branchver
bsdtar
cachelock
Caddyfile
calendartype
Expand Down Expand Up @@ -52,7 +53,10 @@ moodledata
moodleuser
mygr
mymoodle
pbzip
pids
pigz
pixz
plib
profilefield
purgecaches
Expand Down
8 changes: 0 additions & 8 deletions docs/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ are listed at the bottom to make this reference easier to read.

<pre>{{ man['mdl-exec-sql'] }}</pre>

### `mdl fast-db-backup`

<pre>{{ man['mdl-fast-db-backup'] }}</pre>

### `mdl fast-db-restore`

<pre>{{ man['mdl-fast-db-restore'] }}</pre>

### `mdl info`

<pre>{{ man['mdl-info'] }}</pre>
Expand Down
6 changes: 3 additions & 3 deletions installers/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ branch=main
[[ $1 != -* && -n $1 ]] && branch=$1

# Requires curl
if [[ -z $(which curl 2>/dev/null) ]]; then
if ! command -v curl &>/dev/null; then
echo "$(tput bold)$(tput setaf 1)This command requires $(tput smul)curl$(tput rmul) to work.$(tput sgr0)" >&2
exit 1
fi

# If `mdl` is already installed, it must not be a symlink
mdl_path=$(which mdl 2>/dev/null)
mdl_path=$(command -v mdl)
if [[ -n $mdl_path && -L $mdl_path ]]; then
echo 'The mdl CLI is already installed in developer mode. You do not have to reinstall' >&2
echo 'it. If you want to reinstall it in normal mode, please uninstall it first.' >&2
Expand Down Expand Up @@ -85,4 +85,4 @@ echo 🎉 The mdl CLI is installed!
echo

# Initialize the system
mdl init --no-title -c "${MDL_BASE_URL/main/$branch}/compose/compose.yml"
mdl init --no-title -c "${MDL_BASE_URL/main/$branch}/compose/default.yml"
8 changes: 4 additions & 4 deletions installers/uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Check that `mdl` is installed
if [[ -z $(which mdl) ]]; then
if ! command -v mdl &>/dev/null; then
echo "Could not find mdl. Are you sure it is installed?" >&2
exit 1
fi
Expand All @@ -12,10 +12,10 @@ if [[ $EUID -ne 0 ]]; then
fi

# Determine paths and load common functions
base=$(realpath "$(dirname "$(realpath "$(which mdl)")")/..")
base=$(realpath "$(dirname "$(realpath "$(command -v mdl)")")/..")
# Explicitly set scr_dir in case we're running from a curl pipe
scr_dir="$base/libexec"
[[ -L $(which mdl) ]] && linked=true || linked=false
[[ -L $(command -v mdl) ]] && linked=true || linked=false
# shellcheck source=../lib/mdl-common.sh
[[ -f $base/lib/mdl-common.sh ]] && . "$base/lib/mdl-common.sh"
# shellcheck source=../lib/mdl-ui.sh
Expand All @@ -31,7 +31,7 @@ if $linked; then
echo 'It appears you installed mdl in developer mode, which just installs a symlink to'
echo 'the project in your path.'
echo
yorn "Do you want to remove the symlink?" 'y' && sudo rm "$(which mdl)"
yorn "Do you want to remove the symlink?" 'y' && sudo rm "$(command -v mdl)"
else
yorn "Remove the mdl executable and its associated files?" 'y' && \
sudo rm -fv "$base"/bin/mdl "$base"/lib/mdl-*.sh "$base"/libexec/mdl-*.sh \
Expand Down
59 changes: 47 additions & 12 deletions lib/mdl-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function container_tool() { "${MDL_CONTAINER_TOOL[@]}" "$@"; }
function requires() {
local ok=true
for cmd in "$@"; do
if [[ -z $(which "$cmd" 2>/dev/null) ]]; then
if ! command -v "$cmd" &>/dev/null; then
echo "${red}${bold}This command requires $ul$cmd$rmul to work.$norm" >&2
ok=false
elif [[ $cmd =~ docker || $cmd =~ podman ]]; then
Expand Down Expand Up @@ -87,8 +87,18 @@ function support_long_options() {
fi
}

function calc_compression_tool() {
local ext=${1##*.}
local cmd
[[ $ext == bz2 ]] && cmd=bzip2 && command -v pbzip2 &>/dev/null && cmd=pbzip2
[[ $ext == gz ]] && cmd=gzip && command -v pigz &>/dev/null && cmd=pigz
[[ $ext == xz ]] && cmd=xz && command -v pixz &>/dev/null && cmd=pixz
echo "$cmd"
}

# Receives file path and decompresses it. Can detect bzip2, gzip and xz files. If none of
# those extensions match the filename, it throws an error. After successful decompression,
# those extensions match the filename, it throws an error. It will always try to use the
# parallel processing version of the command if available. After successful decompression,
# the original file is deleted unless you specify `--keep`.
#
# Parameters:
Expand All @@ -99,14 +109,11 @@ function decompress() {
local file_path=$1
local out=$2
local ext=${file_path##*.}
local cmd
local cmd=$(calc_compression_tool "$ext")
# Check options
[[ $* =~ -k || $* =~ --keep ]] && keep=true || keep=false
# File path is required
[[ -z $file_path ]] && return 1
[[ $ext == bz2 ]] && cmd=bzip2
[[ $ext == gz ]] && cmd=gzip
[[ $ext == xz ]] && cmd=xz
if [[ -n $cmd ]]; then
# If they didn't provide an explicit output path, use file path sans extension
[[ -z $out ]] && out=${file_path%".$ext"}
Expand All @@ -122,6 +129,21 @@ function decompress() {
return 2
}

# Function that uses awk to safely extract label from filename. Expects to receive
# filename from pipe, such as: `echo $mname | extract_label <MNAME> <TYPE>`
function extract_label() {
cat | awk -v type="$2" -v mname="$1" '
# Filter essentially looks for: /_<TYPE>\./ for example type "src" matches "_src."
/_'"$2"'\./ {
# Remove leading "<MNAME>_", so mname "moodle" matches "moodle_" at start of name.
sub("^" mname "_", "");
# Remove ending "_<TYPE>.*", so type "src" matches "_src.tar", "_src.tar.bz2", etc at end of name.
sub("_" type "\\..*$", "");
print $0
}
'
}

# Used to clear all known vars to proactively avoid data leaks.
# Usage: unset_env <ENV>
function unset_env() {
Expand Down Expand Up @@ -149,22 +171,32 @@ function export_env() {
export mname=$1
}

# Updates config.php with the environment variables.
# Updates config.php with the environment variables. Assumes that export_env has already been run.
# Usage: update_config <ENV>
function update_config() {
local -r env_dir="$MDL_ENVS_DIR/$1"
local -r env_config_file="$env_dir/src/config.php"

# Get the Moodle container and its mount paths for this environment
local -r container="$(container_tool ps -a -f "label=com.docker.compose.project=$1" --format '{{.Names}}' | grep moodle | head -1)"
local -r data_path=${container:+$(container_tool inspect "$container" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("data"))) | .Destination')}

# Get desired wwwroot value
if [ -z "$WWWROOT" ]; then
local -r defaultwwwroot="$(grep -o -E "CFG->wwwroot\s*=\s*'(.*)';" "$env_config_file" | cut -d"'" -f2)"
local defaultwwwroot=''
# Determine default wwwroot from existing config if possible (container must be running)
if [[ -n $container ]]; then
local -r src_path=${container:+$(container_tool inspect "$container" | jq -r '.[] .Mounts[] | select(.Name != null and (.Name | contains("src"))) | .Destination')}
if [[ -n $src_path ]]; then
defaultwwwroot=$(container_tool exec -t "$container" php -r "define('CLI_SCRIPT', true); require '$src_path/config.php'; echo \$CFG->wwwroot;")
fi
fi
local altwwwroot1="http://$HOSTNAME"
[ "$defaultwwwroot" = "$altwwwroot1" ] && altwwwroot1=''
local altwwwroot2="http://$MOODLE_HOST"
[ "$defaultwwwroot" = "$altwwwroot2" ] && altwwwroot2=''
echo 'You can avoid this prompt by setting WWWROOT in your .env file.'
PS3="Select a desired wwwroot value or type your own: "
select WWWROOT in "$defaultwwwroot" $altwwwroot1 $altwwwroot2; do
select WWWROOT in $defaultwwwroot $altwwwroot1 $altwwwroot2; do
WWWROOT="${WWWROOT:-$REPLY}"
break
done
Expand Down Expand Up @@ -195,9 +227,11 @@ function update_config() {
replace_config_value dbname "$DB_NAME" |
replace_config_value dbuser "$DB_USERNAME" |
replace_config_value dbpass "$DB_PASSWORD" |
replace_config_value wwwroot "$WWWROOT" |
replace_config_value dataroot "${DATA_ROOT:-/bitnami/moodledata}"
replace_config_value wwwroot "$WWWROOT"
)
if [[ -n "$DATA_ROOT" || -n "$data_path" ]]; then
config_content=$(replace_config_value "$config_content" dataroot "${DATA_ROOT:-$data_path}")
fi
# Run custom updates if provided
custom_script="$env_dir/custom-config.sh"
[[ -f "$custom_script" ]] && config_content=$(. "$custom_script" "$config_content")
Expand All @@ -211,6 +245,7 @@ EOF
container_tool run --rm -t --name "${1}_worker_update_config" \
-v "$env_dir":/env:Z,ro \
-v "${1}_src":/src \
-e data_path="$data_path" \
"$MDL_SHELL_IMAGE" sh -c "$cmd"
}

Expand Down
Loading