install_moodle.sh 5.51 KB
Newer Older
1
2
3
4
5
6
7
#!/bin/bash

# Import environment variables from .env
set -a
. .env
set +a

8
9
10
11
12
13
14
15
16
17
18
19
# Ensure correct ownership and permissions before installation
docker-compose exec moodle chown -R www-data:www-data ${MOODLE_BASE_DIR}
docker-compose exec moodle chmod -R 755 ${MOODLE_BASE_DIR}

# Ensure necessary directories in moodledata exist
docker-compose exec moodle mkdir -p ${MOODLE_BASE_DIR_DATA}
docker-compose exec moodle mkdir -p ${MOODLE_BASE_DIR_DATA}/localcache
docker-compose exec moodle mkdir -p ${MOODLE_BASE_DIR_DATA}/sessions
docker-compose exec moodle mkdir -p ${MOODLE_BASE_DIR_DATA}/temp
docker-compose exec moodle mkdir -p ${MOODLE_BASE_DIR_DATA}/trashdir
docker-compose exec moodle chown -R www-data:www-data ${MOODLE_BASE_DIR_DATA}
docker-compose exec moodle chmod -R 775 ${MOODLE_BASE_DIR_DATA}
20
21
22
23
24
25
# Ensure correct ownership and permissions after installation
docker-compose exec moodle chown -R www-data:www-data ${MOODLE_BASE_DIR}
docker-compose exec moodle chmod -R 755 ${MOODLE_BASE_DIR}
docker-compose exec moodle mkdir -p ${MOODLE_PHPUNIT_DATAROOT}
docker-compose exec moodle chown -R www-data:www-data ${MOODLE_PHPUNIT_DATAROOT}
docker-compose exec moodle chmod -R 755 ${MOODLE_PHPUNIT_DATAROOT}
26

27
# Install Moodle
28
sleep 5
29
30
31
32
33
34
35
36
37
38
39
40
41
42
docker-compose exec moodle php ${MOODLE_BASE_DIR}/admin/cli/install.php \
                               --wwwroot="${MOODLE_WWWROOT}" \
                               --dataroot="${MOODLE_BASE_DIR_DATA}" \
                               --dbtype="mariadb" \
                               --dbname="${MOODLE_DATABASE_NAME}" \
                               --dbuser="${MOODLE_DATABASE_USER}" \
                               --dbpass="${MOODLE_DATABASE_PASSWORD}" \
                               --dbhost="${MOODLE_DATABASE_HOST}" \
                               --adminpass="${MOODLE_DATABASE_ROOT_PASSWORD}" \
                               --fullname="${MOODLE_FULLNAME}" \
                               --shortname="${MOODLE_SHORTNAME}" \
                               --agree-license \
                               --non-interactive

43
44
45
46
# Add phpunit_dataroot & phpunit_prefix to config.php
docker-compose exec moodle bash -c "echo '\$CFG->phpunit_dataroot = \"${MOODLE_PHPUNIT_DATAROOT}\";' >> ${MOODLE_BASE_DIR}/config.php"
docker-compose exec moodle bash -c "echo '\$CFG->phpunit_prefix = \"phpu_\";' >> ${MOODLE_BASE_DIR}/config.php"

47
# Check if database backup exists and restore it if it does
48
 BACKUP_FILE="moodle/moodle_backup.sql"
49
50
51
52
 if [ -f "$BACKUP_FILE" ]; then
 #    docker-compose exec mariadb apt-get update && apt-get install -y mysql-client && rm -rf /var/lib/apt/lists/*
     docker-compose exec mariadb bash -c "apt-get update && apt-get install -y mysql-client && rm -rf /var/lib/apt/lists/*"
     echo "Database backup found. Restoring..."
53
     docker-compose exec -T mariadb mysql -u ${MOODLE_DATABASE_USER} -p${MOODLE_DATABASE_PASSWORD} ${MOODLE_DATABASE_NAME} < moodle/moodle_backup.sql
54
55
56
57
58
     echo "Database restored from backup."
 else
     echo "No database backup found. Skipping restore."
 fi

59
60
61
# Locale setting inside the Docker container
docker-compose exec -u root moodle bash -c "apt-get update && apt-get install -y locales && \
    echo 'en_AU.UTF-8 UTF-8' >> /etc/locale.gen && \
Artem Baranovskyi's avatar
Artem Baranovskyi committed
62
    locale-gen"
63

64
# Composer installation to run phpunit tests
65
66
67
68
docker-compose exec moodle bash -c "
    php -r \"copy('https://getcomposer.org/installer', 'composer-setup.php');\" &&
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer &&
    php -r \"unlink('composer-setup.php');\" &&
69
    cd app &&
70
71
72
    /usr/local/bin/composer install --no-interaction --no-plugins --no-scripts --no-dev --prefer-dist &&
    /usr/local/bin/composer dump-autoload
"
Artem Baranovskyi's avatar
Artem Baranovskyi committed
73

74
# Next, configure PHPUnit for Moodle:
75
docker-compose exec moodle php admin/tool/phpunit/cli/init.php
76

77
78
79
80
# Define the path to the phpunit.xml file
PHPUNIT_XML_PATH="/var/www/html/moodle/phpunit.xml"

# Define the content to insert for your plugin's test suite
81
82
83
84
85
86
87
88
TEST_SUITE_ENTRY='<testsuite name="Unit Tests">\n    <directory>local/asystgrade/tests</directory>\n</testsuite>'

# Check if phpunit.xml exists
if [ -f "$PHPUNIT_XML_PATH" ]; then
    echo "$PHPUNIT_XML_PATH exists."
else
    echo "$PHPUNIT_XML_PATH does not exist."
fi
89
90
91
92
93
94
95
96
97
98

# Check if the plugin suite is already defined in phpunit.xml
if grep -q "local/asystgrade/tests" "$PHPUNIT_XML_PATH"; then
    echo "Test suite for asystgrade plugin already exists in phpunit.xml."
else
    # Use sed to insert the test suite entry right before @plugin_suites_end@
    sed -i "/@plugin_suites_end@/i $TEST_SUITE_ENTRY" "$PHPUNIT_XML_PATH"
    echo "Test suite for asystgrade plugin added to phpunit.xml."
fi

99
100
101
102
 # Set correct access rules for the plugin
 docker-compose exec moodle chown -R www-data:www-data ${MOODLE_BASE_DIR}/local/asystgrade
 docker-compose exec moodle chmod -R 775 ${MOODLE_BASE_DIR}/local/asystgrade
 sudo chown -R $(whoami):$(whoami) ./asystgrade
103
104

# Create the run_sag script file
105
docker-compose exec flask bash -c 'cat <<EOF > /usr/local/bin/run_sag
106
107
#!/bin/bash
. /opt/myenv/bin/activate
108
109
110
#cd ${MOODLE_BASE_DIR}/asyst/Source/Skript/german
cd /app/asyst/Source/Skript/german
/opt/myenv/bin/python3 /app/api.py
111
112
EOF'

113
# Make the script executable & run it
114
115
docker-compose exec flask chmod +x /usr/local/bin/run_sag
docker-compose exec flask /usr/local/bin/run_sag
116
117

# Adding cron-record at the Moodle container
118
119
120
121
122
123
docker-compose exec -u root moodle bash -c "
  mkdir -p /etc/crontabs &&
  touch /etc/crontabs/root &&
  echo '* * * * * /usr/bin/php ${MOODLE_BASE_DIR}/admin/cli/cron.php >/dev/null 2>&1' >> /etc/crontabs/root &&
  crontab /etc/crontabs/root
"