From 8a7f9300fb4cea968ef489c71a989320ff48ba72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu> Date: Fri, 16 Jun 2017 17:49:44 +0200 Subject: [PATCH] Added a check of server name in Nginx conf (refs #21514). --- set_app_domain.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/set_app_domain.py b/set_app_domain.py index 0d87cfb1..88570e2c 100755 --- a/set_app_domain.py +++ b/set_app_domain.py @@ -13,8 +13,9 @@ from utils import log class SetAppDomain(): - USAGE = '''%s [-d] [-h] [app] <domain> + USAGE = '''%s [-d] [-f] [-h] [app] <domain> -d: Debug mode (can be started with non root users). + -f: Force mode (to force replacement of configuration even if there are warnings). -h: Show this message. app: The application for which the new domain should be set. Possible values: @@ -22,8 +23,8 @@ class SetAppDomain(): It is possible to specify which MS instance should be targetted by using this format: ms-<instance name> (for example ms-msuser). domain: The new domain.''' % __file__ - MS_INSTANCE_USER_PATTERN = r'^[a-z0-9\-]+$' - NGINX_SERVER_NAME_PATTERN = r'^\s*server_name\s+([\w\-\_\.\ ]+);$' + MS_INSTANCE_USER_PATTERN = r'[a-z0-9\-]+' + NGINX_SERVER_NAME_PATTERN = r'\s*server_name\s+([\w\-\_\.\ ]+);' def __init__(self, *args): self.display_header() @@ -40,6 +41,10 @@ class SetAppDomain(): # Add to python path if root_dir not in sys.path: sys.path.append(root_dir) + # Check if force mode is enabled + self.force = '-f' in args + if self.force: + args.remove('-f') # Check that this script is run by root self.debug = '-d' in args if self.debug: @@ -88,11 +93,21 @@ class SetAppDomain(): with open(path, 'r') as fo: vhost = fo.read() new_vhost = '' + changed_lines = 0 for line in vhost.split('\n'): if re.match(self.NGINX_SERVER_NAME_PATTERN, line): new_vhost += re.sub(self.NGINX_SERVER_NAME_PATTERN, line, new_domain) + '\n' + changed_lines += 1 else: new_vhost += line + '\n' + if changed_lines != 2: + log('Warning the number of server_name occurence changed in Nginx configuration is not the expected number (2) but is %s.' % changed_lines) + if not self.force: + log('New configuration will be:') + log(new_vhost) + log('Use -f to force the replacement of the configuration.') + sys.exit(1) + new_vhost = new_vhost.strip() + '\n' if new_vhost != vhost: if self.debug: -- GitLab