diff --git a/set_app_domain.py b/set_app_domain.py index 0d87cfb116f5dfedc39938f7ffd1099f42311c47..88570e2ceeea9591b6e89ef6d3dc9650ea6611d2 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: