From 5facbb1b3596b00e991f71de3e979c828edeb21d Mon Sep 17 00:00:00 2001 From: Disassembler Date: Wed, 12 Sep 2018 13:14:57 +0200 Subject: [PATCH] Modularize proxy registration --- basic/srv/vm/mgr/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/basic/srv/vm/mgr/__init__.py b/basic/srv/vm/mgr/__init__.py index 935d33b..a9875d9 100644 --- a/basic/srv/vm/mgr/__init__.py +++ b/basic/srv/vm/mgr/__init__.py @@ -221,22 +221,21 @@ class VMMgr: app = os.environ['LXC_NAME'] tools.update_hosts_lease(None, app) - def register_proxy(self, app): + def register_proxy(self, app, reload_nginx=True): # Rebuild nginx configuration using IP of referenced app container and reload nginx if not validator.is_valid_app(app, self.conf): raise validator.InvalidValueException('app', app) - self.update_proxy_conf(app, tools.get_container_ip(app)) - tools.reload_nginx() - - def update_proxy_conf(self, app, ip): + ip = tools.get_container_ip(app) with open(os.path.join(NGINX_DIR, '{}.conf'.format(app)), 'w') as f: f.write(NGINX_TEMPLATE.format(app=app, host=self.conf['apps'][app]['host'], ip=ip, domain=self.domain, port=self.port)) + if reload_nginx: + tools.reload_nginx() def unregister_proxy(self, app): # Remove nginx configuration to prevent proxy mismatch when the container IP is reassigned to another container if not validator.is_valid_app(app, self.conf): raise validator.InvalidValueException('app', app) - self.update_proxy_conf(app, tools.NULL_IP) + os.unlink(os.path.join(NGINX_DIR, '{}.conf'.format(app))) tools.reload_nginx() def update_host(self, domain, port, restart_nginx=True): @@ -255,9 +254,10 @@ class VMMgr: # Rebuild nginx config for the portal app with open(os.path.join(NGINX_DIR, 'default.conf'), 'w') as f: f.write(NGINX_DEFAULT_TEMPLATE.format(port=self.port)) - # Unregister nginx proxy for apps (will be repopulated on app restart) + # Re-register nginx proxy for running apps for app in self.conf['apps']: - self.update_proxy_conf(app, tools.NULL_IP) + if tools.is_service_started(app): + self.register_proxy(app, False) # Restart nginx to properly bind the new listen port if restart_nginx: tools.restart_nginx()