Skip to content
Snippets Groups Projects
0_setup.py 5.74 KiB
Newer Older
#!/usr/bin/env python3
Stéphane Diemer's avatar
Stéphane Diemer committed
# -*- coding: utf-8 -*-
import os
Stéphane Diemer's avatar
Stéphane Diemer committed
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.7-linux-x64-installer.deb'
Stéphane Diemer's avatar
Stéphane Diemer committed
    utils.log('It may take a while to download the Wowza installer from the UbiCast server.')
    cmds = [
        'apt-get install -y openjdk-8-jre-headless',
Stéphane Diemer's avatar
Stéphane Diemer committed
        # 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},
Stéphane Diemer's avatar
Stéphane Diemer committed
        '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'),
Stéphane Diemer's avatar
Stéphane Diemer committed
        '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')),
Stéphane Diemer's avatar
Stéphane Diemer committed
        )),
        'cp "%s/Tune.xml" /usr/local/WowzaStreamingEngine/conf/Tune.xml' % dir_path,
        'sed -i "s@#### BEGIN INIT INFO@### BEGIN INIT INFO@" /etc/init.d/WowzaStreamingEngine',
        'sed -i "s@#### BEGIN INIT INFO@### BEGIN INIT INFO@" /etc/init.d/WowzaStreamingEngineManager',
        'sed -i "s@<IPAddress>*</IPAddress>@<IPAddress>127.0.0.1</IPAddress>@" /usr/local/WowzaStreamingEngine/conf/Server.xml',
        'sed -i "s@<IpAddress>*</IpAddress>@<IpAddress>127.0.0.1</IpAddress>@" /usr/local/WowzaStreamingEngine/conf/Server.xml',
        '''gawk -i inplace '/<IpAddress>/{c++; if (c==3) {sub("*","127.0.0.1"); c=0}}1' /usr/local/WowzaStreamingEngine/conf/VHost.xml''',
        'sed -i "s@war --httpPort@war --httpListenAddress=127.0.0.1 --httpPort@" /usr/local/WowzaStreamingEngine/manager/bin/startmgr.sh',
        '''gawk -i inplace '/<Enable>/{c++; if (c==3) {sub("true","false"); c=0}}1' /usr/local/WowzaStreamingEngine/conf/Server.xml''',
        'systemctl enable WowzaStreamingEngine',
        'systemctl enable WowzaStreamingEngineManager',
        'systemctl restart WowzaStreamingEngine',
        'systemctl restart WowzaStreamingEngineManager',
Stéphane Diemer's avatar
Stéphane Diemer committed
    ]
    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'):
Stéphane Diemer's avatar
Stéphane Diemer committed
        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)
    # Write cron script to clean logs
    with open('/etc/cron.daily/wowza-logs-cleaning', 'w') as fo:
        fo.write('#!/bin/sh\nfind /usr/local/WowzaStreamingEngine/logs/ -type f -mtime +7 -delete')
    os.chmod('/etc/cron.daily/wowza-logs-cleaning', 0o755)
    # 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)
Stéphane Diemer's avatar
Stéphane Diemer committed
    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.')