#!/usr/bin/python3 # -*- coding: utf-8 -*- import os import re import utils def vhost_write_cmds(dir_path, name, server_name, **data): params = [('{{ server_name }}', server_name)] for k, v in data.items(): params.append(('{{ %s }}' % k, v)) return [ dict( line='write', template='%s/vhost_%s.conf' % (dir_path, name), target='/etc/nginx/sites-available/%s.conf' % name, params=params, ), 'ln -sfn ../sites-available/%s.conf /etc/nginx/sites-enabled/%s.conf' % (name, name), 'echo "Vhost %s updated (domain: %s)."' % (name, server_name), ] def setup(interactive=True): dir_path = utils.get_dir(__file__) cmds = [ 'apt-get install --yes nginx uwsgi uwsgi-plugin-python3', 'rm -f /etc/nginx/sites-enabled/default', ] hosts = list() # FTP: videos vhost if os.path.exists('/var/www/videos'): cmds.append('cp %s/crossdomain.xml /var/www/videos/crossdomain.xml' % dir_path) server_name = utils.get_conf('FTP_SERVER_NAME') or 'videos' cmds.extend(vhost_write_cmds(dir_path, 'videos', server_name)) hosts.append(server_name) # Wowza: streaming vhost if os.path.exists('/var/www/streaming'): cmds.append('cp %s/crossdomain.xml /var/www/streaming/crossdomain.xml' % dir_path) server_name = utils.get_conf('WOWZA_SERVER_NAME') or 'streaming' cmds.extend(vhost_write_cmds(dir_path, 'streaming', server_name)) hosts.append(server_name) # MediaServer: mediaserver-msuser vhost if os.path.exists('/home/msuser/msinstance'): server_name = utils.get_conf('MS_SERVER_NAME') or 'mediaserver' cmds.extend(vhost_write_cmds(dir_path, 'mediaserver-msuser', server_name, secret=utils.get_conf('MS_SECRET') or 'secret', worker_ip=utils.get_conf('CELERITY_WORKER_IP') or '127.0.1.1')) hosts.append(server_name) # Monitor: msmonitor vhost if os.path.exists('/home/msmonitor/msmonitor'): server_name = utils.get_conf('MONITOR_SERVER_NAME') or 'msmonitor' cmds.extend(vhost_write_cmds(dir_path, 'msmonitor', server_name)) hosts.append(server_name) # SkyReach: skyreach vhost if os.path.exists('/home/skyreach/htdocs'): server_name = utils.get_conf('CM_SERVER_NAME') or 'skyreach' cmds.extend(vhost_write_cmds(dir_path, 'skyreach', server_name)) hosts.append(server_name) utils.run_commands(cmds) # Update hosts file rc, hostname = utils.exec_cmd('hostname') if rc == 0 and hostname not in hosts: hosts.insert(0, hostname) with open('/etc/hosts', 'r') as fo: content = fo.read() new_content = list() found_127 = False for line in content.split('\n'): if not found_127 and line.startswith('127.0.0.1'): found_127 = True for host in hosts: if ' ' + host not in line: line += ' ' + host utils.log('Adding host %s to /etc/hosts 127.0.0.1 aliases.' % host) new_content.append(line) if not found_127: new_content.append('127.0.0.1 %s' % ' '.join(hosts)) new_content = '\n'.join(new_content) if new_content != content: with open('/etc/hosts', 'w') as fo: fo.write(new_content) utils.log('/etc/hosts updated.') else: utils.log('/etc/hosts is already up to date.') # Update certificate in ssl.conf ssl_conf = '/etc/nginx/conf.d/ssl.conf' if not os.path.exists(ssl_conf): utils.log('The SSL configuration file "%s" does not exist, SSL certificate not updated.' % ssl_conf) else: default_cert = '/etc/ssl/certs/ssl-cert-snakeoil.pem' ssl_cert = utils.get_conf('SSL_CERTIFICATE') or default_cert ssl_key = utils.get_conf('SSL_CERTIFICATE_KEY') or '/etc/ssl/private/ssl-cert-snakeoil.key' if ssl_cert == default_cert: utils.log('The configuration uses the default certificate, no modification will be made in "%s".' % ssl_conf) else: with open(ssl_conf, 'r') as fo: content = fo.read() new_content = content new_content = re.sub(r'ssl_certificate\s+([\w/\-\_\.]+);', 'ssl_certificate %s;' % ssl_cert, new_content) new_content = re.sub(r'ssl_certificate_key\s+([\w/\-\_\.]+);', 'ssl_certificate_key %s;' % ssl_key, new_content) if new_content != content: with open(ssl_conf, 'w') as fo: fo.write(new_content) utils.log('SSL configuration file %s updated.' % ssl_conf) else: utils.log('SSL configuration file %s already up to date.' % ssl_conf) utils.run_commands(['nginx -t', 'service nginx restart'])