#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import re import utils def setup(interactive=True): dir_path = utils.get_dir(__file__) license = utils.get_conf('WOWZA_LICENSE') if not license: utils.log('No Wowza license set, skipping Wowza installation.') return wowza_setup_name = 'WowzaStreamingEngine-4.7.1-linux-x64-installer.deb' utils.log('It may take a while to download the Wowza installer from the UbiCast server.') if utils.check_cmd('lsb_release -a | grep 14') == 0: jre_package = 'openjdk-7-jre-headless' # 14.04 else: jre_package = 'openjdk-8-jre-headless' # 16.04 cmds = [ 'apt-get install -y %s' % jre_package, # Get and install Wowza '[ -f "/tmp/%(name)s" ] && (dpkg -I "/tmp/%(name)s" || rm "/tmp/%(name)s") || true' % {'name': wowza_setup_name}, '[ -f "/tmp/%(name)s" ] || wget -q "https://panel.ubicast.eu/media/storage/%(name)s" -O "/tmp/%(name)s"' % {'name': wowza_setup_name}, 'dpkg -i "/tmp/%s"' % wowza_setup_name, # Configure Wowza 'echo "%s" > /usr/local/WowzaStreamingEngine/conf/Server.license' % license, 'echo "ubicast %s admin" > /usr/local/WowzaStreamingEngine/conf/admin.password' % utils.get_conf('WOWZA_MANAGER_PWD'), 'update-rc.d WowzaStreamingEngine defaults', 'update-rc.d WowzaStreamingEngineManager defaults', 'chmod +x /usr/local/WowzaStreamingEngine/logs', 'cp -R /usr/local/WowzaStreamingEngine/examples/LiveVideoStreaming/conf/live /usr/local/WowzaStreamingEngine/conf/', 'mkdir -p /usr/local/WowzaStreamingEngine/applications/live', dict(line='write', template='%s/live-application.xml' % dir_path, target='/usr/local/WowzaStreamingEngine/conf/live/Application.xml', backup=True, params=( ('{{ live_pwd }}', utils.get_conf('WOWZA_LIVE_PWD')), )), 'cp "%s/Tune.xml" /usr/local/WowzaStreamingEngine/conf/Tune.xml' % dir_path, '/etc/init.d/WowzaStreamingEngine restart', '/etc/init.d/WowzaStreamingEngineManager restart', ] ms_conf = '/etc/mediaserver/lives_conf.py' if os.path.exists(ms_conf): utils.log('The file "%s" already exists, it will not be changed.' % ms_conf) else: cmds.extend([ 'mkdir -p /etc/mediaserver', 'echo "RTMP_PWD = \'%s\'" > %s' % (utils.get_conf('WOWZA_LIVE_PWD'), ms_conf), ]) if utils.get_conf('WOWZA_SERVER_NAME'): cmds.append('mkdir -p /var/www/streaming') if os.path.exists('/home/ftp/storage/www'): cmds.extend([ '[ -d "/usr/local/WowzaStreamingEngine/content-back" ] || mv /usr/local/WowzaStreamingEngine/content /usr/local/WowzaStreamingEngine/content-back', 'ln -sfn /home/ftp/storage/www /usr/local/WowzaStreamingEngine/content', ]) utils.run_commands(cmds) # Proxy for license key path = '/usr/local/WowzaStreamingEngine/conf/Server.xml' with open(path, 'r') as fo: content = fo.read() start_index = content.rfind('<Properties>') if start_index < 0: raise ValueError('Unexpected content in "%s". Properties section not found.' % path) start_index += len('<Properties>') properties = content[start_index:] end_index = properties.find('</Properties>') if end_index < 0: raise ValueError('Unexpected content in "%s". Properties section not found.' % path) properties = properties[:end_index] end_index += start_index http_proxy = utils.get_conf('PROXY_HTTP') if http_proxy: regexp = r'http(s){0,1}://(([\w_\-]*)(:[\w_\-]*){0,1}@){0,1}([\w_\-\.]*)(:[\d]*){0,1}[/]*' m = re.match(regexp, http_proxy) if not m: raise ValueError('Invalid value for PROXY_HTTP (value do not match reg exp: %s).' % regexp) https, creds, user, pwd, host, port = m.groups() if port: port = port.strip(':') else: port = '443' if https else '80' pwd = pwd.strip(':') if pwd else '' user = user if user else '' if not host: raise ValueError('Invalid value for PROXY_HTTP (no host found using regexp: %s).' % regexp) with open('%s/Proxy.xml' % dir_path, 'r') as fo: proxy_tplt = fo.read() new_properties = proxy_tplt % dict(user=user, pwd=pwd, host=host, port=port) else: new_properties = '' new_properties += '\n\t\t' if properties != new_properties: new_content = content[:start_index] + new_properties + content[end_index:] with open(path, 'w') as fo: fo.write(new_content) utils.log('The file "%s" has been updated.' % path) utils.log('Edit /usr/local/WowzaStreamingEngine/conf/admin.password to change web manager access password.') utils.log('Edit /usr/local/WowzaStreamingEngine/conf/Server.license to change license key.')