diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..027ce35 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_config/_localconfig.sh diff --git a/_config/_localconfig.dist.sh b/_config/_localconfig.dist.sh new file mode 100644 index 0000000..b9b04e7 --- /dev/null +++ b/_config/_localconfig.dist.sh @@ -0,0 +1,101 @@ +#!/bin/bash + +################################### +### local config: moodle-docker +################################### + +# Path to moodle-docker-compose +# The path where the moodle-docker-compose script is located +pathtobin="$HOME/Workspaces/moodle-docker/bin/moodle-docker-compose" + + +################################### +### local config: clusters +################################### + +# Clusters: +# The moodle instance clusters which should be supported for on-demand usage +clusters=(core plugins) + + +################################### +### local config: versions +################################### + +# Versions: +# The moodle versions which should be supported for on-demand usage +versions=(master 401 402 403 404 405) + + +################################### +### local config: php +################################### + +# PHP versions: +# The PHP versions which should be supported for on-demand usage +phps=(7.4 8.0 8.1 8.2 8.3) + +# Default PHP version: +# The PHP version which is used if the parameter is not given when the script is run +phps_default='8.2' + + +################################### +### local config: databases +################################### + +# Databases: +# The databases engines which should be supported for on-demand usage +databases=(pgsql mariadb mysql mssql oracle) + +# Default database: +# The database engine which is used if the parameter is not given when the script is run +databases_default='pgsql' + + +################################### +### local config: browsers +################################### + +# Browsers: +# The browsers for selenium testing which should be supported for on-demand usage +browsers=(firefox chrome) + +# Default browser: +# The browser for selenium testing which is used if the parameter is not given when the script is run +browsers_default='firefox' + + +################################### +### local config: wwwroot +################################### + +# WWWroot base: +# The base path which will be used for building the particular instance's wwwroot +wwwrootbase="$HOME/Workspaces/moodle-sites/moodle-docker-" + + +################################### +### local config: instances +################################### + +# Instance name base: +# The prefix which will be used for building the instance's name +instancenamebase='moodle-docker-' + + +################################### +### local config: hosts and ports +################################### + +# Host: +# The host which the container should bind its ports to +bindto='127.0.0.1' + +# Webserver port base: +# The first digit of the port which the webserver container should bind its port to +webportbase='6' + +# VNC port base: +# The first digit of the port which the selenium VNC container should bind its port to +vncportbase='5' diff --git a/_include/_functions.sh b/_include/_functions.sh new file mode 100755 index 0000000..380e47d --- /dev/null +++ b/_include/_functions.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +################################### +### functions +################################### + +success () +{ + echo -e "\033[32mSuccess\033[0m" + return 0 +} + +failure () +{ + echo -e "\033[31mFailed (return code: $?)\033[0m" >&2 + exit 1 +} + +now () +{ + echo + echo -e "\033[1;35mNow: $1\033[0m" +} + +confirm () +{ + read -p "Continue (y/n)? " CONT + if [ "$CONT" != "y" ]; then + echo "Ok, exiting..."; + exit 1; + fi +} + +ask () +{ + read -p "Do you want me to do this (y/n)? " CONT + if [ "$CONT" == "y" ]; then + echo "Ok, I will do this..."; + $1 \ + && success || failure + elif [ "$CONT" == "n" ]; then + echo "Ok, I will not do this..."; + else + echo "Invalid answer, exiting..."; + exit 1; + fi +} + +acknowledge () +{ + read -p "Press any key to continue." +} + +array_join_by () +{ + local IFS="$1" + shift + echo "$*" +} + +array_contains_element () +{ + local e match="$1" + shift + for e + do + if [[ "$e" == "$match" ]]; then + return 0 + fi + done + return 1 +} + +array_get_index_of() +{ + needle=$1 && shift + haystack=("$@") + for i in "${!haystack[@]}"; do + if [[ "${haystack[$i]}" = "${needle}" ]]; then + echo "${i}" + fi + done +} diff --git a/bin/moodle-docker-bash.sh b/bin/moodle-docker-bash.sh new file mode 100755 index 0000000..43981dd --- /dev/null +++ b/bin/moodle-docker-bash.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +################################### +### includes +################################### + +CURRENTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source "$CURRENTDIR/../_include/_functions.sh" +source "$CURRENTDIR/../_config/_localconfig.sh" + + +################################### +### check startup +################################### + +usage () +{ + cat < -v <$versionsstring> [options] + +Mandatory parameters: + -c <$clustersstring> + The moodle instance cluster which is to be used + Defaults to: $clusters_default + -v <$versionsstring> + The moodle version which is to be used + Defaults to: $versions_default + +Optional parameters: + -p <$phpsstring> + The PHP version which is to be used + Defaults to: $phps_default + -d <$databasesstring> + The database engine which is to be used + Defaults to: $databases_default + -s <$browsersstring> + The browser which is to be used for selenium testing + Defaults to: $browsers_default +EOU +} + + +# Verify that the script is being sourced (to be able to give the environment variables back to the calling shell) +if [[ $0 == "$BASH_SOURCE" ]]; then + usage + exit 1 +fi + +# Process parameters +OPTIND=1 # This is needed as the script is sourced +while getopts 'hc:v:p:d:s:' OPTION; do + case "$OPTION" in + h) + usage + return 0 + ;; + c) + if ! array_contains_element "$OPTARG" "${clusters[@]}"; then + usage + return 1 + else + cluster=$OPTARG + fi + ;; + v) + if ! array_contains_element "$OPTARG" "${versions[@]}"; then + usage + return 1 + else + version=$OPTARG + fi + ;; + p) + if ! array_contains_element "$OPTARG" "${phps[@]}"; then + usage + return 1 + else + php=$OPTARG + fi + ;; + d) + if ! array_contains_element "$OPTARG" "${databases[@]}"; then + usage + return 1 + else + database=$OPTARG + fi + ;; + s) + if ! array_contains_element "$OPTARG" "${browsers[@]}"; then + usage + return 1 + else + browser=$OPTARG + fi + ;; + :) + usage + return 1 + ;; + *) + usage + return 1 + ;; + ?) + usage + return 1 + ;; + esac +done +shift $((OPTIND-1)) + + +# Verify that a cluster was given +if [[ -z "$cluster" ]]; then + usage + return 1 +fi + +# Verify that a version was given +if [[ -z "$version" ]]; then + usage + return 1 +fi + +# Handle specialties of the master branch +if [[ "$version" == 'master' ]]; then + versionname=$version +else + versionname=stable$version +fi + +# If no PHP version was given, use the default +if [[ -z "$php" ]]; then + php=$phps_default +fi + +# If no database was given, use the default +if [[ -z "$database" ]]; then + database=$databases_default +fi + +# If no browser was given, use the default +if [[ -z "$browser" ]]; then + browser=$browsers_default +fi + + +################################### +### compose data +################################### + +# Compose the instance path +instancepath=${wwwrootbase}${versionname}_${cluster} + +# Compose the instance name +instancename=${instancenamebase}${versionname}_${cluster}_${database}_php${php//.} + +# Compose the instance ports +port2=$(array_get_index_of $cluster "${clusters[@]}") +port3=$(array_get_index_of $version "${versions[@]}") +port4=$(array_get_index_of $database "${databases[@]}") +port5=$(array_get_index_of $php "${phps[@]}") +webport=${webportbase}${port2}${port3}${port4}${port5} +vncport=${vncportbase}${port2}${port3}${port4}${port5} + + +################################### +### set environment +################################### + +export COMPOSE_PROJECT_NAME=$instancename +export MOODLE_DOCKER_WWWROOT=$instancepath +export MOODLE_DOCKER_DB=$database +export MOODLE_DOCKER_PHP_VERSION=$php +export MOODLE_DOCKER_WEB_PORT=$bindto:$webport +export MOODLE_DOCKER_BROWSER=$browser +export MOODLE_DOCKER_SELENIUM_VNC_PORT=$bindto:$vncport + + +################################### +### change directory +################################### + +cd $MOODLE_DOCKER_WWWROOT + + +################################### +### information +################################### + +echo +echo -e "Instance name: \033[1m${instancename}\033[0m" +echo -e "Instance path: \033[1m${instancepath}\033[0m" +echo -e "Cluster: \033[1m${cluster}\033[0m" +echo -e "Moodle Version: \033[1m${version}\033[0m" +echo -e "PHP Version: \033[1m${php}\033[0m" +echo -e "Database: \033[1m${database}\033[0m" +echo -e "Selenium browser: \033[1m${browser}\033[0m" +echo +echo -e "Webserver URL: \033[1m\033[93mhttp://${bindto}:${webport}/\033[39m\033[0m" +echo -e "VNC Port: \033[1m${bindto}:${vncport}\033[0m" diff --git a/bin/moodle-docker-init.sh b/bin/moodle-docker-init.sh new file mode 100755 index 0000000..837786a --- /dev/null +++ b/bin/moodle-docker-init.sh @@ -0,0 +1,148 @@ +#!/bin/bash + +################################### +### includes +################################### + +CURRENTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +source "$CURRENTDIR/../_include/_functions.sh" +source "$CURRENTDIR/../_config/_localconfig.sh" + + +################################### +### check startup +################################### + +usage () +{ + cat < -v <$versionsstring> [options] + +Mandatory parameters: + -c <$clustersstring> + The moodle instance cluster which is to be used + Defaults to: $clusters_default + -v <$versionsstring> + The moodle version which is to be used + Defaults to: $versions_default + +Optional parameters: + -p <$phpsstring> + The PHP version which is to be used + Defaults to: $phps_default + -d <$databasesstring> + The database engine which is to be used + Defaults to: $databases_default + -s <$browsersstring> + The browser which is to be used for selenium testing + Defaults to: $browsers_default + -m + Initialize the database for manual usage + -e + Initialize the data set with example courses and test users + -b + Initialize the behat testing environment + -u + Initialize the PHPUnit testing environment +EOU +} + + +# Verify that the script is being sourced (to be able to give the environment variables back to the calling shell) +if [[ $0 == "$BASH_SOURCE" ]]; then + usage + exit 1 +fi + +# Process parameters +while getopts 'hc:v:p:d:s:mebu' OPTION; do + case "$OPTION" in + h) + usage + return 0 + ;; + c) + if ! array_contains_element "$OPTARG" "${clusters[@]}"; then + usage + return 1 + else + cluster="-c $OPTARG" + fi + ;; + v) + if ! array_contains_element "$OPTARG" "${versions[@]}"; then + usage + return 1 + else + version="-v $OPTARG" + fi + ;; + p) + if ! array_contains_element "$OPTARG" "${phps[@]}"; then + usage + return 1 + else + php="-p $OPTARG" + fi + ;; + d) + if ! array_contains_element "$OPTARG" "${databases[@]}"; then + usage + return 1 + else + database="-d $OPTARG" + fi + ;; + s) + if ! array_contains_element "$OPTARG" "${browsers[@]}"; then + usage + return 1 + else + browser="-s $OPTARG" + fi + ;; + m) + initmanual="-m" + ;; + e) + initdata="-e" + ;; + b) + initbehat="-b" + ;; + u) + initunit="-u" + ;; + :) + usage + return 1 + ;; + *) + usage + return 1 + ;; + ?) + usage + return 1 + ;; + esac +done +shift $((OPTIND-1)) + + +# Verify that a cluster was given +if [[ -z "$cluster" ]]; then + usage + return 1 +fi + +# Verify that a version was given +if [[ -z "$version" ]]; then + usage + return 1 +fi + + +################################### +### run dedicated scripts +################################### + +source moodle-docker-env.sh $cluster $version $php $database $browser + +echo +$pathtobin up -d + +if [[ -n "$initmanual" ]] || [[ -n "$initdata" ]] || [[ -n "$initbehat" ]] || [[ -n "$initunit" ]]; then + echo + moodle-docker-init.sh $initmanual $initdata $initbehat $initunit +fi