Пример #1
0
def server(version=None):
    """
    Require a PostgreSQL server to be installed and running.

    ::

        from fabtools import require

        require.postgres.server()

    """
    if version:
        pkg_name = 'postgresql-%s' % version
    else:
        pkg_name = 'postgresql'
    package(pkg_name)

    if is_file('/etc/init.d/postgresql'):
        service_name = 'postgresql'
    else:
        if version and is_file('/etc/init.d/postgresql-%s' % version):
            service_name = 'postgresql-%s' % version
        else:
            with cd('/etc/init.d'):
                with settings(hide('running', 'stdout')):
                    service_name = run('ls postgresql-*').splitlines()[0]
    started(service_name)
Пример #2
0
def server(version=None):
    """
    Require a PostgreSQL server to be installed and running.

    ::

        from fabtools import require

        require.postgres.server()

    """
    if version:
        pkg_name = 'postgresql-%s' % version
    else:
        pkg_name = 'postgresql'
    package(pkg_name)

    if is_file('/etc/init.d/postgresql'):
        service_name = 'postgresql'
    else:
        if version and is_file('/etc/init.d/postgresql-%s' % version):
            service_name = 'postgresql-%s' % version
        else:
            with cd('/etc/init.d'):
                with settings(hide('running', 'stdout')):
                    service_name = run('ls postgresql-*').splitlines()[0]
    started(service_name)
Пример #3
0
def distrib_id():
    """
    Get the OS distribution ID.

    Returns one of ``"Debian"``, ``"Ubuntu"``, ``"RHEL"``, ``"CentOS"``,
    ``"Fedora"``...

    Example::

        from fabtools.system import distrib_id

        if distrib_id() != 'Debian':
            abort(u"Distribution is not supported")

    """
    # lsb_release works on Ubuntu and Debian >= 6.0
    # but is not always included in other distros
    if is_file('/usr/bin/lsb_release'):
        with settings(hide('running', 'stdout')):
            return run('lsb_release --id --short')
    else:
        if is_file('/etc/debian_version'):
            return "Debian"
        elif is_file('/etc/fedora-release'):
            return "Fedora"
        elif is_file('/etc/redhat-release'):
            release = run('cat /etc/redhat-release')
            if release.startswith('Red Hat Enterprise Linux'):
                return "RHEL"
            elif release.startswith('CentOS'):
                return "CentOS"
            elif release.startswith('Scientific Linux'):
                return "SLES"
Пример #4
0
def distrib_id():
    """
    Get the OS distribution ID.

    Returns one of ``"Debian"``, ``"Ubuntu"``, ``"RHEL"``, ``"CentOS"``,
    ``"Fedora"``, ``"Archlinux"``...

    Example::

        from fabtools.system import distrib_id

        if distrib_id() != 'Debian':
            abort(u"Distribution is not supported")

    """
    # lsb_release works on Ubuntu and Debian >= 6.0
    # but is not always included in other distros
    if is_file('/usr/bin/lsb_release'):
        with settings(hide('running', 'stdout')):
            return run('lsb_release --id --short')
    else:
        if is_file('/etc/debian_version'):
            return "Debian"
        elif is_file('/etc/fedora-release'):
            return "Fedora"
        elif is_file('/etc/arch-release'):
            return "Archlinux"
        elif is_file('/etc/redhat-release'):
            release = run('cat /etc/redhat-release')
            if release.startswith('Red Hat Enterprise Linux'):
                return "RHEL"
            elif release.startswith('CentOS'):
                return "CentOS"
            elif release.startswith('Scientific Linux'):
                return "SLES"
Пример #5
0
def restart_services():
    command = 'redis-server /etc/redis.conf'
    require.files.directory('/var/log/redis', use_sudo=True, owner=env.user)
    require.files.file('/var/log/redis/error.log', owner=env.user, use_sudo=True)
    require.files.file('/var/log/redis/error_cache.log', owner=env.user, use_sudo=True)
    if not is_file('/etc/supervisor/conf.d/redis_cache.conf')\
       and is_file('/etc/supervisor/conf.d/redis.conf'):
        last_save = run('redis-cli lastsave')
        run('redis-cli bgsave')
        print "Saving redis, it may take time"
        for i in range(0, 600):
            if last_save != run('redis-cli lastsave'):
                break
            time.sleep(1)
        dbfile = run('redis-cli config get dbfilename|cut -f1|tail -n 1')
        dir_ = run('redis-cli config get dir|cut -f1|tail -n 1')
        run('sudo cp {} {}'.format(os.path.join(dir_, dbfile),
                              '/srv/dump_redis_save.rdb'))
    if not is_file('/etc/supervisor/conf.d/redis.conf'):
        require.supervisor.process('redis', command=command,
                stdout_logfile='/var/log/redis/error.log')
    require.supervisor.process('redis_cache',
            command='redis-server /etc/redis_cache.conf',
            stdout_logfile='/var/log/redis/error_cache.log')
    require.service.restarted('td-agent')
Пример #6
0
def file(path=None,
         contents=None,
         source=None,
         url=None,
         md5=None,
         use_sudo=False,
         owner=None,
         group='',
         mode=None):
    """
    Require a file

    You can provide either:
    - contents: the required contents of the file
    - source: the filename of a local file to upload
    - url: the address of a file to download (path is optional)
    """
    func = use_sudo and sudo or run

    # 1) Only a path is given
    if path and not (contents or source or url):
        assert path
        if not is_file(path):
            func('touch "%(path)s"' % locals())

    # 2) A URL is specified (path is optional)
    elif url:
        if not path:
            path = os.path.basename(urlparse(url).path)

        if not is_file(path) or md5 and md5sum(path) != md5:
            func('wget --progress=dot %(url)s' % locals())

    # 3) A local filename, or a content string, is specified
    else:
        if source:
            assert not contents
            contents = open(source).read()
        else:
            tmp_file = NamedTemporaryFile(delete=False)
            tmp_file.write(contents)
            tmp_file.close()

        if not is_file(
                path) or md5sum(path) != hashlib.md5(contents).hexdigest():
            with settings(hide('running')):
                if source:
                    put(source, path, use_sudo=use_sudo)
                else:
                    put(tmp_file.name, path, use_sudo=use_sudo)
                    os.remove(tmp_file.name)

    # Ensure correct owner
    if owner:
        func('chown %(owner)s:%(group)s "%(path)s"' % locals())

    # Ensure correct mode
    if mode:
        func('chmod %(mode)s "%(path)s"' % locals())
Пример #7
0
def test_cd_in_guest_context_manager(container):
    from fabtools.openvz import guest

    with guest(container):
        with cd('/tmp'):
            run('touch bar')
            assert is_file('bar')
        assert is_file('/tmp/bar')
Пример #8
0
def test_cd_in_guest_context_manager(container):
    from fabtools.openvz import guest

    with guest(container):
        with cd("/tmp"):
            run("touch bar")
            assert is_file("bar")
        assert is_file("/tmp/bar")
Пример #9
0
def test_cd_in_guest_context_manager(container):
    from fabtools.openvz import guest

    with guest(container):
        with cd('/tmp'):
            run('touch bar')
            assert is_file('bar')
        assert is_file('/tmp/bar')
Пример #10
0
def configure_srid_900913():

    if is_file(EPSG_SHARED, use_sudo=True):

        append(EPSG_SHARED, SRID_900913_DEFINITION, use_sudo=True)

    if is_file(EPSG_LOCAL, use_sudo=True):

        append(EPSG_LOCAL, SRID_900913_DEFINITION, use_sudo=True)
Пример #11
0
def configure_srid_900913():

    if is_file(EPSG_SHARED, use_sudo=True):

        append(EPSG_SHARED, SRID_900913_DEFINITION, use_sudo=True)

    if is_file(EPSG_LOCAL, use_sudo=True):

        append(EPSG_LOCAL, SRID_900913_DEFINITION, use_sudo=True)
Пример #12
0
def _service_name(version=None):

    if is_file("/etc/init.d/postgresql"):
        return "postgresql"

    if version and is_file("/etc/init.d/postgresql-%s" % version):
        return "postgresql-%s" % version

    with cd("/etc/init.d"):
        with settings(hide("running", "stdout")):
            return run("ls postgresql-*").splitlines()[0]
Пример #13
0
def _service_name(version=None):

    if is_file('/etc/init.d/postgresql'):
        return 'postgresql'

    if version and is_file('/etc/init.d/postgresql-%s' % version):
        return 'postgresql-%s' % version

    with cd('/etc/init.d'):
        with settings(hide('running', 'stdout')):
            return run('ls postgresql-*').splitlines()[0]
Пример #14
0
def _service_name(version=None):

    if is_file('/etc/init.d/postgresql'):
        return 'postgresql'

    if version and is_file('/etc/init.d/postgresql-%s' % version):
        return 'postgresql-%s' % version

    with cd('/etc/init.d'):
        with settings(hide('running', 'stdout')):
            return run('ls postgresql-*').splitlines()[0]
Пример #15
0
def distrib_desc():
    """
    Get the description of the Linux distribution.

    For example: ``Debian GNU/Linux 6.0.7 (squeeze)``.
    """
    with settings(hide('running', 'stdout')):
        if is_file('/etc/redhat-release'):
            return run('cat /etc/redhat-release')
        elif is_file('/etc/openwrt_release'):
            return run('cat /etc/openwrt_release | grep DISTRIB_DESCRIPTION | cut -d= -f2').replace('"', '')
        return run('lsb_release --desc --short')
Пример #16
0
def test_install_and_uninstall_local_package(nodejs):

    from fabtools.nodejs import install_package, package_version, uninstall_package

    if not package_version('underscore', local=True):
        install_package('underscore', version='1.4.2', local=True)

    assert is_file('node_modules/underscore/underscore.js')
    assert package_version('underscore', local=True) == '1.4.2'

    uninstall_package('underscore', local=True)

    assert package_version('underscore', local=True) is None
    assert not is_file('node_modules/underscore/underscore.js')
Пример #17
0
def test_install_and_uninstall_global_package(nodejs):

    from fabtools.nodejs import install_package, package_version, uninstall_package

    if not package_version('underscore'):
        install_package('underscore', version='1.4.2')

    assert package_version('underscore') == '1.4.2'
    assert is_file('/usr/local/lib/node_modules/underscore/underscore.js')

    uninstall_package('underscore')

    assert package_version('underscore') is None
    assert not is_file('/usr/local/lib/node_modules/underscore/underscore.js')
Пример #18
0
def file(path=None, contents=None, source=None, url=None, md5=None, use_sudo=False, owner=None, group='', mode=None):
    """
    Require a file

    You can provide either:
    - contents: the required contents of the file
    - source: the filename of a local file to upload
    - url: the address of a file to download (path is optional)
    """
    func = use_sudo and sudo or run

    # 1) Only a path is given
    if path and not (contents or source or url):
        assert path
        if not is_file(path):
            func('touch "%(path)s"' % locals())

    # 2) A URL is specified (path is optional)
    elif url:
        if not path:
            path = os.path.basename(urlparse(url).path)

        if not is_file(path) or md5 and md5sum(path) != md5:
            func('wget --progress=dot %(url)s' % locals())

    # 3) A local filename, or a content string, is specified
    else:
        if source:
            assert not contents
            contents = open(source).read()
        else:
            tmp_file = NamedTemporaryFile(delete=False)
            tmp_file.write(contents)
            tmp_file.close()

        if not is_file(path) or md5sum(path) != hashlib.md5(contents).hexdigest():
            with settings(hide('running')):
                if source:
                    put(source, path, use_sudo=use_sudo)
                else:
                    put(tmp_file.name, path, use_sudo=use_sudo)
                    os.remove(tmp_file.name)

    # Ensure correct owner
    if owner:
        func('chown %(owner)s:%(group)s "%(path)s"' % locals())

    # Ensure correct mode
    if mode:
        func('chmod %(mode)s "%(path)s"' % locals())
Пример #19
0
def test_install_and_uninstall_local_package(nodejs):

    from fabtools.nodejs import install_package, package_version, uninstall_package

    if not package_version('underscore', local=True):
        install_package('underscore', version='1.4.2', local=True)

    assert is_file('node_modules/underscore/underscore.js')
    assert package_version('underscore', local=True) == '1.4.2'

    uninstall_package('underscore', local=True)

    assert package_version('underscore', local=True) is None
    assert not is_file('node_modules/underscore/underscore.js')
Пример #20
0
def test_install_and_uninstall_local_package(nodejs):

    from fabtools.nodejs import install_package, package_version, uninstall_package

    if not package_version("underscore", local=True):
        install_package("underscore", version="1.4.2", local=True)

    assert is_file("node_modules/underscore/underscore.js")
    assert package_version("underscore", local=True) == "1.4.2"

    uninstall_package("underscore", local=True)

    assert package_version("underscore", local=True) is None
    assert not is_file("node_modules/underscore/underscore.js")
Пример #21
0
def _service_name(version=None):

    if distrib_family() == "arch":
        if is_file('/usr/bin/postgres'):
            return 'postgresql'
    else:
        if is_file('/etc/init.d/postgresql'):
            return 'postgresql'

        if version and is_file('/etc/init.d/postgresql-%s' % version):
            return 'postgresql-%s' % version

        with cd('/etc/init.d'):
            with settings(hide('running', 'stdout')):
                return run('ls postgresql-*').splitlines()[0]
Пример #22
0
def address(interface):
    """
    Get the IPv4 address assigned to an interface.

    Example::

        import fabtools

        # Print all configured IP addresses
        for interface in fabtools.network.interfaces():
            print(fabtools.network.address(interface))

    """
    with settings(hide('running', 'stdout')):
        if is_file('/sbin/ifconfig'):
            res = sudo("/sbin/ifconfig %(interface)s | grep 'inet '" % locals())
        else:
            res = run("/sbin/ip a show %(interface)s | grep 'inet '" % locals())
    if interface != "lo" and interface != "":
        if 'addr' in res:
            if 'sudo' in res:
                res = map(lambda line: line.split('addr:')[1], res.splitlines()[1:])
                res = map(lambda line: line.split(' '), res)
                return res[0]
            else:
                return res.split()[1].split(':')[1]
        else:
            return res.split()[1]
Пример #23
0
def template(name=None, url=None):
    """
    Require an OpenVZ OS template.

    If the OS template is not installed yet, it will be downloaded from
    *url* using :py:func:`~fabtools.openvz.download_template()`::

        from fabtools import require

        # Use custom OS template
        require.openvz.template(url='http://example.com/templates/mybox.tar.gz')

    If no *url* is provided, :py:func:`~fabtools.openvz.download_template()`
    will attempt to download the OS template from the
    `download.openvz.org <http://download.openvz.org/template/precreated/>`_
    repository::

        from fabtools import require

        # Use OS template from http://download.openvz.org/template/precreated/
        require.openvz.template('debian-6.0-x86_64')

    """
    if name is not None:
        filename = '%s.tar.gz' % name
    else:
        filename = os.path.basename(url)

    if not is_file(os.path.join('/var/lib/vz/template/cache', filename)):
        openvz.download_template(name, url)
Пример #24
0
def require_nodejs():
    """
    Test high level API
    """

    # Require Node.js

    require.nodejs.installed_from_source()

    assert is_file('/usr/local/bin/node')
    assert nodejs.version() == nodejs.DEFAULT_VERSION

    # Require a global package

    nodejs.uninstall_package('underscore')

    require.nodejs.package('underscore', version='1.4.1')
    assert nodejs.package_version('underscore') == '1.4.1'

    require.nodejs.package('underscore', version='1.4.0')
    assert nodejs.package_version('underscore') == '1.4.0'

    require.nodejs.package('underscore', version='1.4.2')
    assert nodejs.package_version('underscore') == '1.4.2'

    # Require a local package

    nodejs.uninstall_package('underscore', local=True)

    require.nodejs.package('underscore', version='1.4.2', local=True)

    assert nodejs.package_version('underscore', local=True) == '1.4.2'
Пример #25
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'])
Пример #26
0
def app_deploy():
    # Pull repository
    run('eval `ssh-agent -s` && ssh-add')

    with cd("/home/developer/scheduler"):
        run("git fetch origin")
        run("git reset origin/master --hard")

    # Create / update config
    if files.is_file('/etc/nginx/sites-enabled/default'):
        files.remove('/etc/nginx/sites-enabled/default', use_sudo=True)

    update_file('/etc/systemd/system/gunicorn.service',
                source='conf/gunicorn.service')
    update_file('/etc/nginx/sites-available/www.azacili.com',
                source='conf/www.azacili.com')

    # Create / update Python environment
    with python.virtualenv("/home/developer/.pyenv/versions/azacili/"):
        with cd('/home/developer/scheduler'):
            run("pip install -r requirements.txt --upgrade --upgrade-strategy eager"
                )
            run(r"find . -name '*.pyc' -exec rm -rf {} \;")

            run("python manage.py collectstatic -c --noinput --settings=core.settings.prod"
                )

    sudo("systemctl daemon-reload")
    sudo("service gunicorn restart")
    sudo("service nginx restart")

    return True
Пример #27
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'
        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'
        _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'])
Пример #28
0
def virtualenv(directory,
               system_site_packages=False,
               python=None,
               use_sudo=False,
               user=None,
               clear=False):
    """
    Require a Python `virtual environment`_.

    ::

        from fabtools import require

        require.python.virtualenv('/path/to/venv')

    .. _virtual environment: http://www.virtualenv.org/
    """
    package('virtualenv', use_sudo=True)
    if not is_file(posixpath.join(directory, 'bin', 'python')):
        options = ['--quiet']
        if system_site_packages:
            options.append('--system-site-packages')
        if python:
            options.append('--python=%s' % python)
        if clear:
            options.append('--clear')
        options = ' '.join(options)
        command = 'virtualenv %(options)s "%(directory)s"' % locals()
        if use_sudo:
            sudo(command, user=user)
        else:
            run(command)
Пример #29
0
def installed_from_source(version=VERSION):
    """
    Require Redis to be installed from source
    """
    from fabtools import require

    require.user('redis')

    dest_dir = '/opt/redis-%(version)s' % locals()
    require.directory(dest_dir, use_sudo=True, owner='redis')

    if not is_file('%(dest_dir)s/redis-server' % locals()):

        with cd('/tmp'):

            # Download and unpack the tarball
            tarball = 'redis-%(version)s.tar.gz' % locals()
            require.file(tarball,
                         url='http://redis.googlecode.com/files/' + tarball)
            run('tar xzf %(tarball)s' % locals())

            # Compile and install binaries
            require.deb.package('build-essential')
            with cd('redis-%(version)s' % locals()):
                run('make')

                for filename in BINARIES:
                    sudo('cp -pf src/%(filename)s %(dest_dir)s/' % locals())
                    sudo('chown redis: %(dest_dir)s/%(filename)s' % locals())
Пример #30
0
def address(interface):
    """
    Get the IPv4 address assigned to an interface.

    Example::

        import fabtools

        # Print all configured IP addresses
        for interface in fabtools.network.interfaces():
            print(fabtools.network.address(interface))

    """
    with settings(hide('running', 'stdout')):
        if is_file('/sbin/ifconfig'):
            res = sudo("/sbin/ifconfig %(interface)s | grep 'inet '" %
                       locals())
        else:
            res = run("/sbin/ip a show %(interface)s | grep 'inet '" %
                      locals())
    if interface != "lo" and interface != "":
        if 'addr' in res:
            if 'sudo' in res:
                res = map(lambda line: line.split('addr:')[1],
                          res.splitlines()[1:])
                res = map(lambda line: line.split(' '), res)
                return res[0]
            else:
                return res.split()[1].split(':')[1]
        else:
            return res.split()[1]
Пример #31
0
def virtualenv_exists(directory):
    """
    Check if a Python `virtual environment`_ exists.

    .. _virtual environment: http://www.virtualenv.org/
    """
    return is_file(posixpath.join(directory, 'bin', 'python'))
Пример #32
0
def virtualenv(directory, system_site_packages=False, python=None, use_sudo=False, user=None):
    """
    Require a Python `virtual environment`_.

    ::

        from fabtools import require

        require.python.virtualenv('/path/to/venv')

    .. _virtual environment: http://www.virtualenv.org/
    """
    package('virtualenv', use_sudo=True)
    if not is_file(posixpath.join(directory, 'bin', 'python')):
        options = ['--quiet']
        if system_site_packages:
            options.append('--system-site-packages')
        if python:
            options.append('--python=%s' % python)
        options = ' '.join(options)
        command = 'virtualenv %(options)s "%(directory)s"' % locals()
        if use_sudo:
            sudo(command, user=user)
        else:
            run(command)
Пример #33
0
def locales(names):
    """
    Require the list of locales to be available.
    """

    config_file = '/var/lib/locales/supported.d/local'

    if not is_file(config_file):
        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:
        if distrib_id() == "Archlinux":
            run_as_root('locale-gen')
        else:
            run_as_root('dpkg-reconfigure --frontend=noninteractive locales')
Пример #34
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:
        if distrib_id() == "Archlinux":
            run_as_root('locale-gen')
        else:
            run_as_root('dpkg-reconfigure --frontend=noninteractive locales')
Пример #35
0
def locales(names):
    """
    Require the list of locales to be available.
    """

    config_file = '/var/lib/locales/supported.d/local'

    if not is_file(config_file):
        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)
                append(config_file, locale, use_sudo=True)
            else:
                warn('Unsupported locale name "%s"' % name)

    if config.changed:
        sudo('dpkg-reconfigure --frontend=noninteractive locales')
Пример #36
0
def template(name=None, url=None):
    """
    Require an OpenVZ OS template.

    If the OS template is not installed yet, it will be downloaded from
    *url* using :py:func:`~fabtools.openvz.download_template()`::

        from fabtools import require

        # Use custom OS template
        require.openvz.template(url='http://example.com/templates/mybox.tar.gz')

    If no *url* is provided, :py:func:`~fabtools.openvz.download_template()`
    will attempt to download the OS template from the
    `download.openvz.org <http://download.openvz.org/template/precreated/>`_
    repository::

        from fabtools import require

        # Use OS template from http://download.openvz.org/template/precreated/
        require.openvz.template('debian-6.0-x86_64')

    """
    if name is not None:
        filename = "%s.tar.gz" % name
    else:
        filename = os.path.basename(url)

    if not is_file(os.path.join("/var/lib/vz/template/cache", filename)):
        openvz.download_template(name, url)
Пример #37
0
def ppa(name, auto_yes=False):
    """
    Require a `PPA`_ package source.

    Example::

        from fabtools import require

        # Node.js packages by Chris Lea
        require.deb.ppa('ppa:chris-lea/node.js', auto_yes=True)

    .. _PPA: https://help.launchpad.net/Packaging/PPA
    """
    assert name.startswith('ppa:')
    user, repo = name[4:].split('/', 2)
    distrib = distrib_codename()
    source = '%(user)s-%(repo)s-%(distrib)s.list' % locals()

    if not is_file(source):
        package('python-software-properties')
        if auto_yes:
            prompt = '-y '
        else:
            prompt = ''
        run_as_root('add-apt-repository %s %s' % (prompt, name), pty=False)
        update_index()
Пример #38
0
def virtualenv_exists(directory):
    """
    Check if a Python `virtual environment`_ exists.

    .. _virtual environment: http://www.virtualenv.org/
    """
    return is_file(posixpath.join(directory, 'bin', 'python'))
Пример #39
0
def installed_from_source(version=VERSION):
    """
    Require Redis to be installed from source.

    The compiled binaries will be installed in ``/opt/redis-{version}/``.
    """
    from fabtools import require

    require.user('redis', home='/var/lib/redis')

    dest_dir = '/opt/redis-%(version)s' % locals()
    require.directory(dest_dir, use_sudo=True, owner='redis')

    if not is_file('%(dest_dir)s/redis-server' % locals()):

        with cd('/tmp'):

            # Download and unpack the tarball
            tarball = 'redis-%(version)s.tar.gz' % locals()
            require.file(tarball, url='http://redis.googlecode.com/files/' + tarball)
            run('tar xzf %(tarball)s' % locals())

            # Compile and install binaries
            require.deb.package('build-essential')
            with cd('redis-%(version)s' % locals()):
                run('make')

                for filename in BINARIES:
                    sudo('cp -pf src/%(filename)s %(dest_dir)s/' % locals())
                    sudo('chown redis: %(dest_dir)s/%(filename)s' % locals())
Пример #40
0
def setup_s3cmd(aws_access_key_id, aws_secret_access_key, s3cfg=None):
    """
    Install s3cmd for multipart uploads to Amazon S3
    """

    s3cfg = s3cfg if s3cfg is not None else DEFAULT_S3_CFG

    # Install s3cmd
    if not is_file('/usr/local/bin/s3cmd'):
        require.file(url='http://sourceforge.net/projects/s3tools/files/s3cmd/1.5.0-alpha1/s3cmd-1.5.0-alpha1.tar.gz')
        if not is_dir('s3cmd-1.5.0-alpha1'):
            run('tar xzf s3cmd-1.5.0-alpha1.tar.gz')
        with cd('s3cmd-1.5.0-alpha1'):
            require.deb.package('python-setuptools')
            sudo('python setup.py install')

    # Optional dependencies
    require.python.package('python-magic', use_sudo=True)

    # S3 config file (including credentials)
    require.file(
        '/var/lib/postgresql/.s3cfg',
        contents=s3cfg.format(
            aws_access_key_id=aws_access_key_id,
            aws_secret_access_key=aws_secret_access_key),
        use_sudo=True
    )
Пример #41
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'
        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'
        _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'])
Пример #42
0
def require_nodejs():
    """
    Test high level API
    """

    # Require Node.js

    require.nodejs.installed_from_source()

    assert is_file('/usr/local/bin/node')
    assert nodejs.version() == nodejs.DEFAULT_VERSION

    # Require a global package

    nodejs.uninstall_package('underscore')

    require.nodejs.package('underscore', version='1.4.1')
    assert nodejs.package_version('underscore') == '1.4.1'

    require.nodejs.package('underscore', version='1.4.0')
    assert nodejs.package_version('underscore') == '1.4.0'

    require.nodejs.package('underscore', version='1.4.2')
    assert nodejs.package_version('underscore') == '1.4.2'

    # Require a local package

    nodejs.uninstall_package('underscore', local=True)

    require.nodejs.package('underscore', version='1.4.2', local=True)

    assert nodejs.package_version('underscore', local=True) == '1.4.2'
Пример #43
0
def test_install_redis_in_guest_context_manager(container):
    from fabtools.openvz import guest
    from fabtools.require.redis import VERSION, instance

    with guest(container):
        instance('test')
        assert is_file('/etc/redis/test.conf')
        assert run('echo PING | /opt/redis-%s/redis-cli' % VERSION) == 'PONG'
Пример #44
0
def test_require_directory_in_guest_context_manager(container):
    from fabtools.openvz import guest

    with guest(container):
        require_directory('/tmp/newdir')
        with cd('/tmp/newdir'):
            run('touch baz')
        assert is_file('/tmp/newdir/baz')
Пример #45
0
def install_influxdb():
    if files.is_file('/etc/init.d/influxdb'):
        return
    with cd('/tmp/'):
        package_name = 'influxdb_0.9.2_amd64.deb'
        run('wget http://influxdb.s3.amazonaws.com/{}'.format(package_name))
        sudo('dpkg -i {}'.format(package_name))
        run('rm {}'.format(package_name))
Пример #46
0
def test_uwsgi_is_started(now):
    testing_file = '/tmp/test_uwsgi.py'
    if files.is_file(testing_file):
        files.remove(testing_file)
    put('files/test_uwsgi.py', testing_file)

    output = run('python {} {} {}'.format(testing_file, env.uwsgi_socket(now), env.server_name))
    assert '"message"' in output
Пример #47
0
def test_install_and_uninstall_global_package(nodejs):

    from fabtools.nodejs import install_package, package_version, uninstall_package

    # This is not in root's PATH on RedHat systems
    with path('/usr/local/bin'):

        if not package_version('underscore'):
            install_package('underscore', version='1.4.2')

        assert package_version('underscore') == '1.4.2'
        assert is_file('/usr/local/lib/node_modules/underscore/underscore.js')

        uninstall_package('underscore')

        assert package_version('underscore') is None
        assert not is_file('/usr/local/lib/node_modules/underscore/underscore.js')
Пример #48
0
def test_install_and_uninstall_global_package(nodejs):

    from fabtools.nodejs import install_package, package_version, uninstall_package

    # This is not in root's PATH on RedHat systems
    with path("/usr/local/bin"):

        if not package_version("underscore"):
            install_package("underscore", version="1.4.2")

        assert package_version("underscore") == "1.4.2"
        assert is_file("/usr/local/lib/node_modules/underscore/underscore.js")

        uninstall_package("underscore")

        assert package_version("underscore") is None
        assert not is_file("/usr/local/lib/node_modules/underscore/underscore.js")
Пример #49
0
def test_require_directory_in_guest_context_manager(container):
    from fabtools.openvz import guest

    with guest(container):
        require_directory('/tmp/newdir')
        with cd('/tmp/newdir'):
            run('touch baz')
        assert is_file('/tmp/newdir/baz')
Пример #50
0
def test_install_redis_in_guest_context_manager(container):
    from fabtools.openvz import guest
    from fabtools.require.redis import VERSION, instance

    with guest(container):
        instance('test')
        assert is_file('/etc/redis/test.conf')
        assert run('echo PING | /opt/redis-%s/redis-cli' % VERSION) == 'PONG'
Пример #51
0
def test_require_default_jdk_version():

    from fabtools.oracle_jdk import version, DEFAULT_VERSION
    from fabtools.require.oracle_jdk import installed

    installed()

    assert is_file('/opt/jdk/bin/java')
    assert version() == DEFAULT_VERSION
Пример #52
0
def test_install_debian_package_in_guest_context_manager(container):
    from fabtools.deb import update_index
    from fabtools.openvz import guest
    from fabtools.require.deb import package

    with guest(container):
        update_index()
        package('htop')
        assert is_file('/usr/bin/htop')
Пример #53
0
def test_tomcat_7_version(jdk):

    from fabtools.require.tomcat import installed
    from fabtools.tomcat import version, DEFAULT_VERSION

    installed()

    assert is_file(os.path.join(PATH, 'bin/catalina.sh'))
    assert version(PATH) == DEFAULT_VERSION
Пример #54
0
def server(version='8.4'):
    """
    Require a PostgreSQL server
    """
    package('postgresql-%s' % version)
    service = 'postgresql-%s' % version
    if not is_file(os.path.join('/etc/init.d', service)):
        service = 'postgresql'
    started(service)
Пример #55
0
def test_require_jdk_version_6():

    from fabtools.oracle_jdk import version
    from fabtools.require.oracle_jdk import installed

    installed('6u45-b06')

    assert is_file('/opt/jdk/bin/java')
    assert version() == '6u45-b06'
Пример #56
0
def setup():
    sudo(
        'apt-get install -y python-dev autoconf g++ libatlas-dev liblapack-dev libblas-dev gfortran'
    )

    # install java
    # sudo('apt-get install -y openjdk-7-jre-headless')
    _setup_java()

    # install nginx
    sudo('apt-get install -y nginx')

    # TODO: put nginx config

    require.files.directory('/usr/local/%s' % app_name,
                            owner='root',
                            group='root',
                            use_sudo=True)

    recorder_logfile = '/var/log/sr2-recorder.log'
    if not is_file(recorder_logfile):
        require.files.file(recorder_logfile,
                           contents='',
                           owner='root',
                           group='root',
                           use_sudo=True)

    controller_logfile = '/var/log/sr2-controller.log'
    if not is_file(controller_logfile):
        require.files.file(controller_logfile,
                           contents='',
                           owner='root',
                           group='root',
                           use_sudo=True)

    pg_flag_file = '/usr/local/%s/pgsetup.flag' % app_name
    if not is_file(pg_flag_file):
        _setup_postgresql()

    setup_stat()

    setup_cron()

    deploy()
Пример #57
0
def distrib_desc():
    """
    Get the description of the Linux distribution.

    For example: ``Debian GNU/Linux 6.0.7 (squeeze)``.
    """
    with settings(hide('running', 'stdout')):
        if not is_file('/etc/redhat-release'):
            return run('lsb_release --desc --short')
        return run('cat /etc/redhat-release')
Пример #58
0
def configure_nginx(distro):
    """
    Add Nginx configuration
    """
    # Local webvirtmgr site template
    conf = os.path.join(LOCAL_BASE_DIR, "deploy", "fabric", "templates",
                        "nginx.conf")
    # Remote location
    conf_path = os.path.join("/etc/nginx/conf.d", "webvirtmgr.conf")
    context = {
        "server_name": fsettings.SERVER_NAME
    }

    # Upload template to server
    files.upload_template(conf, conf_path, context=context, use_sudo=True)

    # Nginx, make sure `default` website is not running.
    if distro in ["Debian", "Ubuntu"]:
        disable_site("default")
    elif distro in ["Fedora"]:
        # Fedora places the default server:80 in nginx.conf!
        # we will replace nginx.conf
        default = "/etc/nginx/nginx.conf"
        default_bak = default + ".bak"

        # Local default nginx.conf template
        conf = os.path.join(LOCAL_BASE_DIR, "deploy", "fabric",
                            "templates", "original.nginx.conf")

        if not files.is_file(default_bak):
            # only replace backup if required
            sudo("mv %s %s" % (default, default + ".bak"))

        # Upload new nginx.conf to server
        files.upload_template(conf, default, use_sudo=True)
    else:
        default = "/etc/nginx/conf.d/default.conf"
        if files.is_file(default):
            sudo("mv %s %s" % (default, default + ".bak"))

    # Ensure running ...
    # require.nginx.server()
    require.service.restart("nginx")
Пример #59
0
def interfaces():
    """
    Get the list of network interfaces. Will return all datalinks on SmartOS.
    """
    with settings(hide('running', 'stdout')):
        if is_file('/usr/sbin/dladm'):
            res = run('/usr/sbin/dladm show-link')
        else:
            res = sudo('/sbin/ifconfig -s')
    return list(map(lambda line: line.split(' ')[0], res.splitlines()[1:]))