From 4368b40d0086bec80b1c476ac2f1caa71aaa440f Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Thu, 3 Aug 2023 19:16:27 +0200 Subject: [PATCH] wordpress complete --- .../contrib/webapps/backends/__init__.py | 2 +- .../contrib/webapps/backends/wordpress.py | 18 ++++++++++-- orchestra/contrib/webapps/filters.py | 4 +-- orchestra/contrib/webapps/settings.py | 16 ++++++++++- orchestra/contrib/webapps/types/cms.py | 19 ++++++++++--- orchestra/contrib/webapps/types/php.py | 28 +++++++++++-------- 6 files changed, 65 insertions(+), 22 deletions(-) diff --git a/orchestra/contrib/webapps/backends/__init__.py b/orchestra/contrib/webapps/backends/__init__.py index 0beb18a9..597db034 100644 --- a/orchestra/contrib/webapps/backends/__init__.py +++ b/orchestra/contrib/webapps/backends/__init__.py @@ -19,7 +19,7 @@ class WebAppServiceMixin(object): CREATED=0 if [[ ! -e %(app_path)s ]]; then mkdir -p %(app_path)s - chown %(sftpuser)s:%(sftpuser)s %(app_path)s + #chown %(sftpuser)s:%(sftpuser)s %(app_path)s CREATED=1 elif [[ -z $( ls -A %(app_path)s ) ]]; then CREATED=1 diff --git a/orchestra/contrib/webapps/backends/wordpress.py b/orchestra/contrib/webapps/backends/wordpress.py index 819be388..64907a30 100644 --- a/orchestra/contrib/webapps/backends/wordpress.py +++ b/orchestra/contrib/webapps/backends/wordpress.py @@ -4,6 +4,8 @@ import textwrap from django.utils.translation import gettext_lazy as _ from orchestra.contrib.orchestration import ServiceController, replace +from orchestra.settings import NEW_SERVERS +from django.template import Template, Context from .. import settings @@ -41,6 +43,14 @@ class WordPressController(WebAppServiceMixin, ServiceController): def save(self, webapp): context = self.get_context(webapp) + perms = Template(textwrap.dedent("""\ + {% if sftpuser %} + exc('chown -R {{ sftpuser }}:{{ sftpuser }} {{ app_path }}'); {% else %} + exc('chown -R {{ user }}:{{ group }} {{ app_path }}'); + {% endif %} + """ + )) + context.update({'perms' : perms.render(Context(context))}) self.append(textwrap.dedent("""\ if (count(glob("%(app_path)s/*")) > 1) { die("App directory not empty."); @@ -96,7 +106,8 @@ class WordPressController(WebAppServiceMixin, ServiceController): foreach ( $config_file as $line_num => $line ) { fwrite($fw, $line); } - exc('chown -R %(user)s:%(group)s %(app_path)s'); + //exc('chown -R %(user)s:%(group)s %(app_path)s'); + %(perms)s // Run wordpress installation process @@ -140,9 +151,10 @@ class WordPressController(WebAppServiceMixin, ServiceController): '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, 'title': "%s blog's" % webapp.account.get_full_name(), - 'cms_cache_dir': os.path.normpath(settings.WEBAPPS_CMS_CACHE_DIR) + 'cms_cache_dir': os.path.normpath(settings.WEBAPPS_CMS_CACHE_DIR), + 'sftpuser': webapp.sftpuser.username if webapp.target_server.name in NEW_SERVERS else None , }) return replace(context, '"', "'") diff --git a/orchestra/contrib/webapps/filters.py b/orchestra/contrib/webapps/filters.py index 794217bf..65434874 100644 --- a/orchestra/contrib/webapps/filters.py +++ b/orchestra/contrib/webapps/filters.py @@ -33,7 +33,7 @@ class DetailListFilter(SimpleListFilter): for field, values in apptype.get_detail_lookups().items(): for value in values: lookup_map[value[0]] = field - ret.add(value) + ret.add(tuple(value)) self.lookup_map = lookup_map return sorted(list(ret), key=lambda e: e[1]) @@ -47,5 +47,5 @@ class DetailListFilter(SimpleListFilter): except KeyError: return queryset else: - return queryset.filter(data__contains='"%s":"%s"' % (field, value)) + return queryset.filter(data__contains=value) return queryset diff --git a/orchestra/contrib/webapps/settings.py b/orchestra/contrib/webapps/settings.py index 1f5ae9da..45fb5356 100644 --- a/orchestra/contrib/webapps/settings.py +++ b/orchestra/contrib/webapps/settings.py @@ -99,12 +99,26 @@ WEBAPPS_PHP_VERSIONS = Setting('WEBAPPS_PHP_VERSIONS', ( ('5.3-cgi', 'PHP 5.3 FCGID'), ('5.2-cgi', 'PHP 5.2 FCGID'), ('4-cgi', 'PHP 4 FCGID'), - ('7-fpm', 'PHP 7 FPM') + ('7.0-fpm', 'PHP 7 FPM'), + ('7.3-fpm', 'PHP 7.3 FPM'), + ('7.4-fpm', 'PHP 7.4 FPM (web-11)'), + ('8.1-fpm', 'PHP 8.1 FPM (web-12)'), + ('8.2-fpm', 'PHP 8.2 FPM (web-12)'), ), help_text="Execution modle choose by ending -fpm or -cgi.", validators=[Setting.validate_choices] ) +WEBAPPS_PHP_VERSIONS_SERVERS = Setting('WEBAPPS_PHP_VERSIONS_SERVERS', { + 'web.pangea.lan' : ('php5.6-fpm', '7.0-fpm',), + 'web-ng' : ('5.6-fpm', '7.0-fpm', '7.3-fpm',), + 'web-11.pangea.lan': ('7.4-fpm',), + 'web-12.pangea.lan' : ('8.1-fpm', '8.2-fpm'), + 'bookworm' : ('8.1-fpm', '8.2-fpm'), + }, + help_text="PHP available for each server", +) + WEBAPPS_DEFAULT_PHP_VERSION = Setting('WEBAPPS_DEFAULT_PHP_VERSION', '5.6-fpm', diff --git a/orchestra/contrib/webapps/types/cms.py b/orchestra/contrib/webapps/types/cms.py index 03524d74..7707fdce 100644 --- a/orchestra/contrib/webapps/types/cms.py +++ b/orchestra/contrib/webapps/types/cms.py @@ -6,10 +6,13 @@ from django.utils.translation import gettext_lazy as _ from rest_framework import serializers from orchestra.contrib.databases.models import Database, DatabaseUser +from orchestra.contrib.orchestration.models import Server from orchestra.forms.widgets import SpanWidget from orchestra.utils.python import random_ascii +from orchestra.settings import NEW_SERVERS from .php import PHPApp, PHPAppForm, PHPAppSerializer +from .. import settings class CMSAppForm(PHPAppForm): @@ -68,14 +71,20 @@ class CMSApp(PHPApp): def get_password(self): return random_ascii(10) + + def get_server(self): + server = self.instance.target_server + return server def validate(self): super(CMSApp, self).validate() create = not self.instance.pk if create: - db = Database(name=self.get_db_name(), account=self.instance.account, type=self.db_type) + default_server_mysql = Server.objects.get(name=settings.WEBAPPS_DEFAULT_MYSQL_DATABASE_HOST) + server = self.get_server() if self.get_server().name in NEW_SERVERS else default_server_mysql + db = Database(name=self.get_db_name(), account=self.instance.account, type=self.db_type, target_server=server) user = DatabaseUser(username=self.get_db_user(), password=self.get_password(), - account=self.instance.account, type=self.db_type) + account=self.instance.account, type=self.db_type, target_server=server) for obj in (db, user): try: obj.full_clean() @@ -88,9 +97,11 @@ class CMSApp(PHPApp): db_name = self.get_db_name() db_user = self.get_db_user() password = self.get_password() - db, db_created = self.instance.account.databases.get_or_create(name=db_name, type=self.db_type) + default_server_mysql = Server.objects.get(name=settings.WEBAPPS_DEFAULT_MYSQL_DATABASE_HOST) + server = self.get_server() if self.get_server().name in NEW_SERVERS else default_server_mysql + db, db_created = self.instance.account.databases.get_or_create(name=db_name, type=self.db_type, target_server=server) if db_created: - user = DatabaseUser(username=db_user, account=self.instance.account, type=self.db_type) + user = DatabaseUser(username=db_user, account=self.instance.account, type=self.db_type, target_server=server) user.set_password(password) user.save() db.users.add(user) diff --git a/orchestra/contrib/webapps/types/php.py b/orchestra/contrib/webapps/types/php.py index 4aff667f..3b156a23 100644 --- a/orchestra/contrib/webapps/types/php.py +++ b/orchestra/contrib/webapps/types/php.py @@ -26,18 +26,24 @@ class PHPAppForm(ExtendedPluginDataForm): initial=settings.WEBAPPS_DEFAULT_PHP_VERSION, help_text=help_message) - # def clean_php_version(self): - # # TODO: restriccin PHP diferentes servers - # if not self.instance.id: - # webapp_server = self.cleaned_data.get("target_server") - # php_version = self.cleaned_data.get('php_version') - # if webapp_server is None: - # pass - # else: - # if webapp_server.name in NEW_SERVERS and not username: - # self.add_error("php_version", _(f"Server {webapp_server} not allow {php_version}")) - + def clean_php_version(self): + # valida que la version PHP este asignada al servidor + php_version = self.cleaned_data.get('php_version') + if not self.instance.id: + webapp_server = self.cleaned_data.get("target_server") + else: + webapp_server = self.instance.target_server + + if webapp_server is None: + pass + else: + if php_version not in settings.WEBAPPS_PHP_VERSIONS_SERVERS[webapp_server.name]: + self.add_error("php_version", _(f"Server {webapp_server.name} not allow {php_version}")) + else: + return php_version + + class PHPAppSerializer(serializers.Serializer): php_version = serializers.ChoiceField(label=_("PHP version"), choices=settings.WEBAPPS_PHP_VERSIONS,