From cefbe379b7efd891fbb7fca6b99c5bfbe29169ed Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Tue, 25 Jul 2023 22:16:38 +0200 Subject: [PATCH] webapps static complete --- orchestra/contrib/databases/admin.py | 2 +- orchestra/contrib/systemusers/backends.py | 3 +- orchestra/contrib/webapps/admin.py | 31 +++++++++++++++++-- .../contrib/webapps/backends/__init__.py | 18 +++++++++-- orchestra/contrib/webapps/backends/static.py | 13 +++----- orchestra/contrib/webapps/types/misc.py | 20 ++++++------ 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/orchestra/contrib/databases/admin.py b/orchestra/contrib/databases/admin.py index 42e4952f..4a18d6df 100644 --- a/orchestra/contrib/databases/admin.py +++ b/orchestra/contrib/databases/admin.py @@ -20,7 +20,7 @@ def save_selected(modeladmin, request, queryset): save_selected.short_description = "Re-save selected objects" class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): - list_display = ('name', 'type', 'display_users', 'account_link') + list_display = ('name', 'type', 'target_server', 'display_users', 'account_link') list_filter = ('type', HasUserListFilter) search_fields = ('name', 'account__username') change_readonly_fields = ('name', 'type', 'target_server') diff --git a/orchestra/contrib/systemusers/backends.py b/orchestra/contrib/systemusers/backends.py index 67e8fe81..789b81ce 100644 --- a/orchestra/contrib/systemusers/backends.py +++ b/orchestra/contrib/systemusers/backends.py @@ -766,6 +766,7 @@ class WebappUserController(ServiceController): elif [[ $useradd_code -ne 0 ]]; then exit $useradd_code fi + usermod -aG %(user)s www-data fi usermod -aG %(user)s %(parent)s @@ -828,5 +829,5 @@ class WebappUserController(ServiceController): 'webapp_path': os.path.normpath(user.get_base_home() + "/webapps/" + user.home), 'parent': user.get_parent(), } - context['deleted_home'] = context['webapp_path'] + ".delete" + context['deleted_home'] = context['webapp_path'] + ".deleted" return replace(context, "'", '"') diff --git a/orchestra/contrib/webapps/admin.py b/orchestra/contrib/webapps/admin.py index b75ce688..09422085 100644 --- a/orchestra/contrib/webapps/admin.py +++ b/orchestra/contrib/webapps/admin.py @@ -4,11 +4,14 @@ from django.urls import reverse from django.utils.encoding import force_str from django.utils.safestring import mark_safe from django.utils.translation import gettext, gettext_lazy as _ +from django.shortcuts import resolve_url +from django.contrib.admin.templatetags.admin_urls import admin_urlname from orchestra.admin import ExtendedModelAdmin from orchestra.admin.utils import admin_link, get_modeladmin from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.admin import AccountAdminMixin +from orchestra.contrib.systemusers.models import WebappUsers from orchestra.forms.widgets import DynamicHelpTextSelect from orchestra.plugins.admin import SelectPluginAdminMixin, display_plugin_field from orchestra.utils.html import get_on_site_link @@ -52,12 +55,12 @@ class WebAppOptionInline(admin.TabularInline): class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin): list_display = ( - 'name', 'display_type', 'display_detail', 'display_websites', 'account_link' + 'name', 'display_type', 'display_detail', 'display_websites', 'account_link', 'target_server', ) list_filter = ('type', HasWebsiteListFilter, DetailListFilter) inlines = [WebAppOptionInline] readonly_fields = ('account_link',) - change_readonly_fields = ('name', 'type', 'display_websites', 'sftpuser', 'target_server') + change_readonly_fields = ('name', 'type', 'display_websites', 'display_sftpuser', 'target_server',) search_fields = ('name', 'account__username', 'data', 'website__domains__name') list_prefetch_related = ('content_set__website', 'content_set__website__domains') plugin = AppType @@ -67,6 +70,15 @@ class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin) display_type = display_plugin_field('type') + def display_sftpuser(self, obj): + salida = "" + if obj.sftpuser is None: + salida = None + else: + url = resolve_url(admin_urlname(WebappUsers._meta, 'change'), obj.sftpuser.id) + salida += f'{obj.sftpuser}
' + return mark_safe(salida) + display_sftpuser.short_description = _("user sftp") @mark_safe def display_websites(self, webapp): @@ -95,4 +107,19 @@ class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin) display_detail.short_description = _("detail") + def save_model(self, request, obj, form, change): + if not change: + user = form.cleaned_data['username'] + if user: + user = WebappUsers( + username=form.cleaned_data['username'], + account_id=obj.account.pk, + target_server=form.cleaned_data['target_server'], + home=form.cleaned_data['name'] + ) + user.set_password(form.cleaned_data["password1"]) + user.save() + obj.sftpuser = user + super(WebAppAdmin, self).save_model(request, obj, form, change) + admin.site.register(WebApp, WebAppAdmin) diff --git a/orchestra/contrib/webapps/backends/__init__.py b/orchestra/contrib/webapps/backends/__init__.py index 57c2c77e..2e120fc0 100644 --- a/orchestra/contrib/webapps/backends/__init__.py +++ b/orchestra/contrib/webapps/backends/__init__.py @@ -13,7 +13,18 @@ class WebAppServiceMixin(object): doc_settings = (settings, ('WEBAPPS_UNDER_CONSTRUCTION_PATH', 'WEBAPPS_MOVE_ON_DELETE_PATH',) ) - + def check_webapp_dir(self, context): + self.append(textwrap.dedent(""" + # Create webapp dir + CREATED=0 + if [[ ! -e %(app_path)s ]]; then + mkdir -p %(app_path)s + CREATED=1 + elif [[ -z $( ls -A %(app_path)s ) ]]; then + CREATED=1 + fi""") % context + ) + def create_webapp_dir(self, context): self.append(textwrap.dedent(""" # Create webapp dir @@ -57,14 +68,15 @@ class WebAppServiceMixin(object): def get_context(self, webapp): context = webapp.type_instance.get_directive_context() context.update({ - 'user': webapp.get_username(), - 'group': webapp.get_groupname(), + 'user': webapp.sftpuser.username if webapp.target_server.name in settings.WEBAPP_NEW_SERVERS else webapp.get_username(), + 'group': webapp.sftpuser.username if webapp.target_server.name in settings.WEBAPP_NEW_SERVERS else webapp.get_groupname(), 'app_name': webapp.name, 'app_type': webapp.type, 'app_path': webapp.get_path(), 'banner': self.get_banner(), 'under_construction_path': settings.WEBAPPS_UNDER_CONSTRUCTION_PATH, 'is_mounted': webapp.content_set.exists(), + 'target_server': webapp.target_server, }) context['deleted_app_path'] = settings.WEBAPPS_MOVE_ON_DELETE_PATH % context return context diff --git a/orchestra/contrib/webapps/backends/static.py b/orchestra/contrib/webapps/backends/static.py index 0756759c..c2f2cb5e 100644 --- a/orchestra/contrib/webapps/backends/static.py +++ b/orchestra/contrib/webapps/backends/static.py @@ -18,18 +18,13 @@ class StaticController(WebAppServiceMixin, ServiceController): if context.get('target_server').name in WEBAPP_NEW_SERVERS: self.check_webapp_dir(context) self.set_under_construction(context) - # TODO: crea el usuario sftp - # webapp.name = webapp.sftpuser.directory.replace("webapps/", "") - # webapp.save() - else: self.create_webapp_dir(context) self.set_under_construction(context) - + def delete(self, webapp): context = self.get_context(webapp) - if context.get('target_server').name not in WEBAPP_NEW_SERVERS: - self.delete_webapp_dir(context) + if context.get('target_server').name in WEBAPP_NEW_SERVERS: + webapp.sftpuser.delete() else: - # TODO: elimina el usuario sftp - pass + self.delete_webapp_dir(context) diff --git a/orchestra/contrib/webapps/types/misc.py b/orchestra/contrib/webapps/types/misc.py index cfcde517..977bac6e 100644 --- a/orchestra/contrib/webapps/types/misc.py +++ b/orchestra/contrib/webapps/types/misc.py @@ -35,21 +35,21 @@ class StaticForm(PluginDataForm): def __init__(self, *args, **kwargs): super(StaticForm, self).__init__(*args, **kwargs) - if self.instance.id is None: - self.fields['sftpuser'].widget = forms.HiddenInput() - else: + self.fields['sftpuser'].widget = forms.HiddenInput() + if self.instance.id is not None: self.fields['username'].widget = forms.HiddenInput() self.fields['password1'].widget = forms.HiddenInput() self.fields['password2'].widget = forms.HiddenInput() def clean(self): - webapp_server = self.cleaned_data.get("target_server") - sftpuser = self.cleaned_data.get('sftpuser') - if webapp_server is None: - self.add_error("target_server", _("choice some target_server")) - else: - if webapp_server.name in WEBAPP_NEW_SERVERS and sftpuser == None: - self.add_error("sftpuser", _("SFTP user is required by new webservers")) + if not self.instance.id: + webapp_server = self.cleaned_data.get("target_server") + username = self.cleaned_data.get('username') + if webapp_server is None: + self.add_error("target_server", _("choice some target_server")) + else: + if webapp_server.name in WEBAPP_NEW_SERVERS and username == '': + self.add_error("username", _("SFTP user is required by new webservers")) def clean_password2(self): password1 = self.cleaned_data.get("password1")