From c593385d5c7f318f9a038fb550d61094f4f679c1 Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Wed, 22 Nov 2023 11:11:02 +0100 Subject: [PATCH] prepare moodle to new servers --- orchestra/contrib/webapps/backends/moodle.py | 45 ++++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/orchestra/contrib/webapps/backends/moodle.py b/orchestra/contrib/webapps/backends/moodle.py index 5579e8a4..fa1f4b6b 100644 --- a/orchestra/contrib/webapps/backends/moodle.py +++ b/orchestra/contrib/webapps/backends/moodle.py @@ -4,7 +4,8 @@ import textwrap from django.utils.translation import gettext_lazy as _ from orchestra.contrib.orchestration import ServiceController, replace - +from django.template import Template, Context +from orchestra.settings import NEW_SERVERS from .. import settings from . import WebAppServiceMixin @@ -24,6 +25,15 @@ class MoodleController(WebAppServiceMixin, ServiceController): def save(self, webapp): context = self.get_context(webapp) + perms = Template(textwrap.dedent("""\ + {% if sftpuser %} + chown -R {{sftpuser}}:{{sftpuser}} {{home}}/webapps/{{app_name}} + {% else %} + chown -R {{user}}:{{group}} {{home}}/webapps/{{app_name}} + {% endif %} + """ + )) + context.update({'perms' : perms.render(Context(context))}) self.append(textwrap.dedent("""\ if [[ $(ls "%(app_path)s" | wc -l) -gt 1 ]]; then echo "App directory not empty." 2> /dev/null @@ -50,10 +60,15 @@ class MoodleController(WebAppServiceMixin, ServiceController): else tar -xzvf %(cms_cache_dir)s/moodle -C %(app_path)s --strip-components=1 fi - mkdir %(app_path)s/moodledata && { - chmod 750 %(app_path)s/moodledata - echo -n 'order deny,allow\\ndeny from all' > %(app_path)s/moodledata/.htaccess + # mkdir %(app_path)s/moodledata && { + # chmod 750 %(app_path)s/moodledata + # echo -n 'order deny,allow\\ndeny from all' > %(app_path)s/moodledata/.htaccess + # } + mkdir %(home)s/webapps/%(app_name)s/moodledata && { + chmod 750 %(home)s/webapps/%(app_name)s/moodledata + echo -n 'order deny,allow\\ndeny from all' > %(home)s/webapps/%(app_name)s/moodledata/.htaccess } + if [[ ! -e %(app_path)s/config.php ]]; then cp %(app_path)s/config-dist.php %(app_path)s/config.php sed -i "s#dbtype\s*= '.*#dbtype = '%(db_type)s';#" %(app_path)s/config.php @@ -61,24 +76,25 @@ class MoodleController(WebAppServiceMixin, ServiceController): sed -i "s#dbname\s*= '.*#dbname = '%(db_name)s';#" %(app_path)s/config.php sed -i "s#dbuser\s*= '.*#dbuser = '%(db_user)s';#" %(app_path)s/config.php sed -i "s#dbpass\s*= '.*#dbpass = '%(password)s';#" %(app_path)s/config.php - sed -i "s#dataroot\s*= '.*#dataroot = '%(app_path)s/moodledata';#" %(app_path)s/config.php + sed -i "s#dataroot\s*= '.*#dataroot = '%(home)s/webapps/%(app_name)s/moodledata';#" %(app_path)s/config.php sed -i "s#wwwroot\s*= '.*#wwwroot = '%(www_root)s';#" %(app_path)s/config.php fi rm %(app_path)s/.lock - chown -R %(user)s:%(group)s %(app_path)s + # chown -R %(user)s:%(group)s %(app_path)s + %(perms)s + # Run install moodle cli command on the background, because it takes so long... stdout=$(mktemp) stderr=$(mktemp) - nohup su - %(user)s --shell /bin/bash << 'EOF' > $stdout 2> $stderr & - php %(app_path)s/admin/cli/install_database.php \\ + # nohup su - %(user)s --shell /bin/bash << 'EOF' > $stdout 2> $stderr & + nohup su - %(sftpuser)s --shell /bin/bash << 'EOF' > $stdout 2> $stderr & + php -d max_input_vars=5000 %(app_path)s/admin/cli/install_database.php \\ --fullname="%(site_name)s" \\ --shortname="%(site_name)s" \\ --adminpass="%(password)s" \\ --adminemail="%(email)s" \\ - --non-interactive \\ - --agree-license \\ - --allow-unstable + --agree-license EOF pid=$! sleep 2 @@ -95,14 +111,15 @@ class MoodleController(WebAppServiceMixin, ServiceController): context = super(MoodleController, self).get_context(webapp) contents = webapp.content_set.all() context.update({ - 'db_type': 'mysqli', + 'db_type': 'mariadb', 'db_name': webapp.data['db_name'], 'db_user': webapp.data['db_user'], 'password': webapp.data['password'], - 'db_host': settings.WEBAPPS_DEFAULT_MYSQL_DATABASE_HOST, + 'db_host': 'localhost' if webapp.target_server.name in NEW_SERVERS else settings.WEBAPPS_DEFAULT_MYSQL_DATABASE_HOST, 'email': webapp.account.email, 'site_name': "%s Courses" % webapp.account.get_full_name(), 'cms_cache_dir': os.path.normpath(settings.WEBAPPS_CMS_CACHE_DIR), - 'www_root': contents[0].website.get_absolute_url() if contents else 'http://empty' + 'www_root': contents[0].website.get_absolute_url() if contents else 'http://empty', + 'sftpuser': webapp.sftpuser.username if webapp.target_server.name in NEW_SERVERS else None , }) return replace(context, '"', "'")