From 27f43650775848f7be4e63fbc1115aa8a383690f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michel=20Ka=CC=88ser?= Date: Sun, 9 Aug 2015 16:49:21 +0200 Subject: [PATCH] portmapping admin so one can see the open ports; should fix container cloning2 --- ipynbsrv/admin/admin.py | 32 +++++++++++++++++++++++++++++ ipynbsrv/core/models.py | 14 +++++++++++-- ipynbsrv/core/signals/containers.py | 1 - 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/ipynbsrv/admin/admin.py b/ipynbsrv/admin/admin.py index b53a461..5cfcdc5 100644 --- a/ipynbsrv/admin/admin.py +++ b/ipynbsrv/admin/admin.py @@ -451,6 +451,37 @@ def get_readonly_fields(self, request, obj=None): return [] +class PortMappingAdmin(admin.ModelAdmin): + + """ + Admin model for the `PortMapping` model. + """ + + list_display = ['container', 'external_port', 'internal_port'] + list_filter = [ + ('container', admin.RelatedOnlyFieldListFilter), + 'external_port', + 'internal_port', + ('server', admin.RelatedOnlyFieldListFilter), + ] + + fieldsets = [ + ('Server Properties', { + 'fields': ['server', 'external_port'] + }), + ('Container Properties', { + 'fields': ['container', 'internal_port'] + }) + ] + readonly_fields = ['container', 'external_port', 'internal_port', 'server'] + + def has_add_permission(self, request): + """ + :inherit. + """ + return False + + class ServerAdmin(admin.ModelAdmin): """ @@ -606,6 +637,7 @@ def has_add_permission(self, request): admin_site.register(ContainerSnapshot, ContainerSnapshotAdmin) admin_site.register(Group, GroupAdmin) admin_site.register(Notification, NotificationAdmin) +admin_site.register(PortMapping, PortMappingAdmin) admin_site.register(Server, ServerAdmin) admin_site.register(Share, ShareAdmin) admin_site.register(Tag, TagAdmin) diff --git a/ipynbsrv/core/models.py b/ipynbsrv/core/models.py index ff3883c..60d5d52 100644 --- a/ipynbsrv/core/models.py +++ b/ipynbsrv/core/models.py @@ -613,8 +613,13 @@ def get_backend_base_url(self): """ base_url = None if self.has_protected_port(): + image = None + if self.is_image_based(): + image = self.image + else: + image = self.clone_of.image for port_mapping in self.port_mappings.all(): - if port_mapping.internal_port == self.image.protected_port: + if port_mapping.internal_port == image.protected_port: ip_and_port = port_mapping.server.internal_ip + ':' + str(port_mapping.external_port) base_url = settings.CONTAINER_ACCESS_BASE_URI + ip_and_port.encode('hex') + '/' break @@ -649,7 +654,12 @@ def has_protected_port(self): """ Return `True` if the container is exposing a protected port. """ - return self.image.protected_port is not None + has_protected_port = False + if self.is_image_based(): + has_protected_port = self.image.protected_port is not None + elif self.is_clone() and self.clone_of.is_image_based(): + has_protected_port = self.clone_of.image.protected_port is not None + return has_protected_port has_protected_port.boolean = True def is_clone(self): diff --git a/ipynbsrv/core/signals/containers.py b/ipynbsrv/core/signals/containers.py index c12cdd7..75689d6 100644 --- a/ipynbsrv/core/signals/containers.py +++ b/ipynbsrv/core/signals/containers.py @@ -75,7 +75,6 @@ def create_on_server(sender, container, **kwargs): elif container.is_clone() and container.clone_of.is_image_based(): clone_of = container.clone_of.backend_pk cmd = container.clone_of.image.command - image = container.clone_of.image.backend_pk result = None try: