Esempio n. 1
0
def test_unsupported_system():

    from fabtools.system import UnsupportedFamily

    with pytest.raises(UnsupportedFamily) as excinfo:

        with patch('fabtools.system.distrib_id') as mock_distrib_id:
            mock_distrib_id.return_value = 'foo'

            raise UnsupportedFamily(supported=['debian', 'redhat'])

    exception_msg = str(excinfo.value)
    assert exception_msg == "Unsupported system foo (supported families: debian, redhat)"
    def test_unsupported_system(self):

        from fabtools.system import UnsupportedFamily

        with self.assertRaises(UnsupportedFamily) as cm:

            with patch('fabtools.system.distrib_id') as mock_distrib_id:
                mock_distrib_id.return_value = 'foo'

                raise UnsupportedFamily(supported=['debian', 'redhat'])


        exception_msg = str(cm.exception)
        self.assertEquals(exception_msg, "Unsupported system foo (supported families: debian, redhat)")
Esempio n. 3
0
def _choose(old_style, new_style):
    family = distrib_family()
    if family == 'debian':
        distrib = distrib_id()
        at_least_trusty = (
            distrib == 'Ubuntu' and V(distrib_release()) >= V('14.04'))
        at_least_jessie = (
            distrib == 'Debian' and V(distrib_release()) >= V('8.0'))
        if at_least_trusty or at_least_jessie:
            return new_style
        else:
            return old_style
    else:
        raise UnsupportedFamily(supported=['debian'])
Esempio n. 4
0
def server(version=None):
    """
    Require a PostgreSQL server to be installed and running.

    ::

        from fabtools import require

        require.postgres.server()

    """
    family = distrib_family()
    if family == 'debian':
        _server_debian(version)
    else:
        raise UnsupportedFamily(supported=['debian'])
Esempio n. 5
0
def server():
    """
    Require the Apache HTTP server to be installed and running.

    ::

        from fabtools import require

        require.apache.server()

    """
    family = distrib_family()
    if family == 'debian':
        _server_debian()
    else:
        raise UnsupportedFamily(supported=['debian'])
Esempio n. 6
0
def firewall(zones=None, interfaces=None, policy=None, rules=None,
             routestopped=None, masq=None):
    """
    Ensure that a firewall is configured.

    Example::

        from fabtools.shorewall import *
        from fabtools import require

        # We need a firewall with some custom rules
        require.shorewall.firewall(
            rules=[
                Ping(),
                SSH(),
                HTTP(),
                HTTPS(),
                SMTP(),
                rule(port=1234, source=hosts(['example.com'])),
            ]
        )

    """

    family = distrib_family()
    if family != 'debian':
        raise UnsupportedFamily(supported=['debian'])

    require_deb_package('shorewall')

    with watch(CONFIG_FILES) as config:
        _zone_config(zones)
        _interfaces_config(interfaces)
        _policy_config(policy)
        _rules_config(rules)
        _routestopped_config(routestopped)
        _masq_config(masq)

    if config.changed:
        puts("Shorewall configuration changed")
        if is_started():
            restart('shorewall')

    with settings(hide('running'), shell_env()):
        sed('/etc/default/shorewall', 'startup=0', 'startup=1', use_sudo=True)
Esempio n. 7
0
def server(version=None, password=None):
    """
    Require a MySQL server to be installed and running.

    Example::

        from fabtools import require

        require.mysql.server(password='******')

    """
    family = distrib_family()
    if family == 'debian':
        _server_debian(version, password)
    elif family == 'redhat':
        _server_redhat(version, password)
    else:
        raise UnsupportedFamily(supported=['debian', 'redhat'])
Esempio n. 8
0
def server(package_name='nginx'):
    """
    Require the nginx web server to be installed and running.

    You can override the system package name, if you need to install
    a specific variant such as `nginx-extras` or `nginx-light`.

    ::

        from fabtools import require

        require.nginx.server()

    """
    family = distrib_family()
    if family == 'debian':
        _server_debian(package_name)
    else:
        raise UnsupportedFamily(supported=['debian'])
Esempio n. 9
0
def query(query, use_sudo=True, **kwargs):
    """
    Run a MySQL query.
    """
    family = distrib_family()
    if family == 'debian':
        from fabtools.deb import install, is_installed
    elif family == 'redhat':
        from fabtools.rpm import install, is_installed
    else:
        raise UnsupportedFamily(supported=['debian', 'redhat'])

    func = use_sudo and run_as_root or run

    user = kwargs.get('mysql_user') or env.get('mysql_user')
    password = kwargs.get('mysql_password') or env.get('mysql_password')
    func_mysql = 'mysql'
    mysql_host = kwargs.get('mysql_host') or env.get('mysql_host')

    options = [
        '--batch',
        '--raw',
        '--skip-column-names',
    ]
    if user:
        options.append('--user=%s' % quote(user))
    if password:
        if not is_installed('sshpass'):
            install('sshpass')
        func_mysql = 'sshpass -p %(password)s mysql' % {
            'password': quote(password)
        }
        options.append('--password')
    if mysql_host:
        options.append('--host=%s' % quote(mysql_host))
    options = ' '.join(options)

    return func('%(cmd)s %(options)s --execute=%(query)s' % {
        'cmd': func_mysql,
        'options': options,
        'query': quote(query),
    })
Esempio n. 10
0
def locales(names):
    """
    Require the list of locales to be available.
    """

    if distrib_id() == "Ubuntu":
        config_file = '/var/lib/locales/supported.d/local'
        if not is_file(config_file):
            run_as_root('touch %s' % config_file)
    else:
        config_file = '/etc/locale.gen'

    # Regenerate locales if config file changes
    with watch(config_file, use_sudo=True) as config:

        # Add valid locale names to the config file
        supported = dict(supported_locales())
        for name in names:
            if name in supported:
                charset = supported[name]
                locale = "%s %s" % (name, charset)
                uncomment(config_file,
                          escape(locale),
                          use_sudo=True,
                          shell=True)
                append(config_file,
                       locale,
                       use_sudo=True,
                       partial=True,
                       shell=True)
            else:
                warn('Unsupported locale name "%s"' % name)

    if config.changed:
        family = distrib_family()
        if family == 'debian':
            run_as_root('dpkg-reconfigure --frontend=noninteractive locales')
        elif family in ['arch', 'gentoo']:
            run_as_root('locale-gen')
        else:
            raise UnsupportedFamily(supported=['debian', 'arch', 'gentoo'])
Esempio n. 11
0
def locales(names):
    """
    Require the list of locales to be available.

    Raises UnsupportedLocales if some of the required locales
    are not supported.
    """

    family = distrib_family()
    if family == 'debian':
        command = 'dpkg-reconfigure --frontend=noninteractive locales'
        config_file = '/etc/locale.gen'
        _locales_generic(names, config_file=config_file, command=command)
    elif family in ['arch', 'gentoo']:
        _locales_generic(names,
                         config_file='/etc/locale.gen',
                         command='locale-gen')
    elif distrib_family() == 'redhat':
        _locales_redhat(names)
    else:
        raise UnsupportedFamily(
            supported=['debian', 'arch', 'gentoo', 'redhat'])
Esempio n. 12
0
def setuptools(version=MIN_SETUPTOOLS_VERSION, python_cmd='python'):
    """
    Require `setuptools`_ to be installed.

    If setuptools is not installed, or if a version older than *version*
    is installed, the latest version will be installed.

    .. _setuptools: http://pythonhosted.org/setuptools/
    """

    from fabtools.require.deb import package as require_deb_package
    from fabtools.require.rpm import package as require_rpm_package

    if not is_setuptools_installed(python_cmd=python_cmd):
        family = distrib_family()

        if family == 'debian':
            require_deb_package('python-dev')
        elif family == 'redhat':
            require_rpm_package('python-devel')
        else:
            raise UnsupportedFamily(supported=['debian', 'redhat'])

        install_setuptools(python_cmd=python_cmd)
Esempio n. 13
0
def process(name, **kwargs):
    """
    Require a supervisor process to be running.

    Keyword arguments will be used to build the program configuration
    file. Some useful arguments are:

    - ``command``: complete command including arguments (**required**)
    - ``directory``: absolute path to the working directory
    - ``user``: run the process as this user
    - ``stdout_logfile``: absolute path to the log file

    You should refer to the `supervisor documentation`_ for the
    complete list of allowed arguments.

    .. note:: the default values for the following arguments differs from
              the ``supervisor`` defaults:

              - ``autorestart``: defaults to ``true``
              - ``redirect_stderr``: defaults to ``true``

    Example::

        from fabtools import require

        require.supervisor.process('myapp',
            command='/path/to/venv/bin/myapp --config production.ini --someflag',
            directory='/path/to/working/dir',
            user='******',
            stdout_logfile='/path/to/logs/myapp.log',
            )

    .. _supervisor documentation: http://supervisord.org/configuration.html#program-x-section-values
    """

    from fabtools.require import file as require_file
    from fabtools.require.deb import package as require_deb_package
    from fabtools.require.rpm import package as require_rpm_package
    from fabtools.require.arch import package as require_arch_package
    from fabtools.require.service import started as require_started

    family = distrib_family()

    if family == 'debian':
        require_deb_package('supervisor')
        require_started('supervisor')
    elif family == 'redhat':
        require_rpm_package('supervisor')
        require_started('supervisord')
    elif family == 'arch':
        require_arch_package('supervisor')
        require_started('supervisord')
    else:
        raise UnsupportedFamily(supported=['debian', 'redhat', 'arch'])

    # Set default parameters
    params = {}
    params.update(kwargs)
    params.setdefault('autorestart', 'true')
    params.setdefault('redirect_stderr', 'true')

    # Build config file from parameters
    lines = []
    lines.append('[program:%(name)s]' % locals())
    for key, value in sorted(params.items()):
        lines.append("%s=%s" % (key, value))

    # Upload config file
    if family == 'debian':
        filename = '/etc/supervisor/conf.d/%(name)s.conf' % locals()
    elif family == 'redhat':
        filename = '/etc/supervisord.d/%(name)s.ini' % locals()
    elif family == 'arch':
        filename = '/etc/supervisor.d/%(name)s.ini' % locals()

    with watch(filename, callback=update_config, use_sudo=True):
        require_file(filename, contents='\n'.join(lines), use_sudo=True)

    # Start the process if needed
    if process_status(name) == 'STOPPED':
        start_process(name)