Modularize proxy registration

This commit is contained in:
Disassembler 2018-09-12 13:14:57 +02:00
parent 9f02c98d1b
commit 5facbb1b35
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499

View File

@ -221,22 +221,21 @@ class VMMgr:
app = os.environ['LXC_NAME'] app = os.environ['LXC_NAME']
tools.update_hosts_lease(None, app) 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 # Rebuild nginx configuration using IP of referenced app container and reload nginx
if not validator.is_valid_app(app, self.conf): if not validator.is_valid_app(app, self.conf):
raise validator.InvalidValueException('app', app) raise validator.InvalidValueException('app', app)
self.update_proxy_conf(app, tools.get_container_ip(app)) ip = tools.get_container_ip(app)
tools.reload_nginx()
def update_proxy_conf(self, app, ip):
with open(os.path.join(NGINX_DIR, '{}.conf'.format(app)), 'w') as f: 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)) 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): def unregister_proxy(self, app):
# Remove nginx configuration to prevent proxy mismatch when the container IP is reassigned to another container # 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): if not validator.is_valid_app(app, self.conf):
raise validator.InvalidValueException('app', app) 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() tools.reload_nginx()
def update_host(self, domain, port, restart_nginx=True): def update_host(self, domain, port, restart_nginx=True):
@ -255,9 +254,10 @@ class VMMgr:
# Rebuild nginx config for the portal app # Rebuild nginx config for the portal app
with open(os.path.join(NGINX_DIR, 'default.conf'), 'w') as f: with open(os.path.join(NGINX_DIR, 'default.conf'), 'w') as f:
f.write(NGINX_DEFAULT_TEMPLATE.format(port=self.port)) 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']: 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 # Restart nginx to properly bind the new listen port
if restart_nginx: if restart_nginx:
tools.restart_nginx() tools.restart_nginx()