Ejemplo n.º 1
0
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs):
    """
    Require an nginx site
    """
    server()

    config_filename = '/etc/nginx/sites-available/%s.conf' % server_name

    context = {
        'port': 80,
    }
    context.update(kwargs)
    context['server_name'] = server_name

    template_file(config_filename, template_contents, template_source, context, use_sudo=True)

    link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name
    if enabled:
        if not is_link(link_filename):
            sudo("ln -s %(config_filename)s %(link_filename)s" % locals())

        # Make sure we don't break the config
        if check_config:
            with settings(hide('running', 'warnings'), warn_only=True):
                if sudo("nginx -t").return_code > 0:
                    print red("Error in %(server_name)s nginx site config (disabling for safety)" % locals())
                    sudo("rm %(link_filename)s" % locals())
    else:
        if is_link(link_filename):
            sudo("rm %(link_filename)s" % locals())

    sudo("/etc/init.d/nginx reload")
Ejemplo n.º 2
0
def site(server_name, template_contents=None, template_source=None,
         enabled=True, check_config=True, **kwargs):
    """
    Require an nginx site.

    You must provide a template for the site configuration, either as a
    string (*template_contents*) or as the path to a local template
    file (*template_source*).

    ::

        from fabtools import require

        CONFIG_TPL = '''
        server {
            listen      %(port)d;
            server_name %(server_name)s %(server_alias)s;
            root        %(docroot)s;
            access_log  /var/log/nginx/%(server_name)s.log;
        }'''

        require.nginx.site('example.com', template_contents=CONFIG_TPL,
            port=80,
            server_alias='www.example.com',
            docroot='/var/www/mysite',
        )

    .. seealso:: :py:func:`fabtools.require.files.template_file`
    """
    if not is_installed('nginx-common'):
        # nginx-common is always installed if Nginx exists
        server()

    config_filename = '/etc/nginx/sites-available/%s.conf' % server_name

    context = {
        'port': 80,
    }
    context.update(kwargs)
    context['server_name'] = server_name

    template_file(config_filename, template_contents, template_source, context, use_sudo=True)

    link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name
    if enabled:
        if not is_link(link_filename):
            run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals())

        # Make sure we don't break the config
        if check_config:
            with settings(hide('running', 'warnings'), warn_only=True):
                if run_as_root('nginx -t').failed:
                    run_as_root("rm %(link_filename)s" % locals())
                    message = red("Error in %(server_name)s nginx site config (disabling for safety)" % locals())
                    abort(message)
    else:
        if is_link(link_filename):
            run_as_root("rm %(link_filename)s" % locals())

    reload_service('nginx')
Ejemplo n.º 3
0
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs):
    """
    Require an nginx site
    """
    server()

    config_filename = '/etc/nginx/sites-available/%s.conf' % server_name

    context = {
        'port': 80,
    }
    context.update(kwargs)
    context['server_name'] = server_name

    template_file(config_filename, template_contents, template_source, context, use_sudo=True)

    link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name
    if enabled:
        if not is_link(link_filename):
            sudo("ln -s %(config_filename)s %(link_filename)s" % locals())

        # Make sure we don't break the config
        if check_config:
            with settings(hide('running', 'warnings'), warn_only=True):
                if sudo("nginx -t").return_code > 0:
                    print red("Error in %(server_name)s nginx site config (disabling for safety)" % locals())
                    sudo("rm %(link_filename)s" % locals())
    else:
        if is_link(link_filename):
            sudo("rm %(link_filename)s" % locals())

    sudo("/etc/init.d/nginx reload")
Ejemplo n.º 4
0
def provision():        
    # install required packages (+extras) for LAMP server
    require.deb.packages([
        # 'build-essential',
        # 'devscripts',
        'locales',
        'apache2',
        'php5',
        'php5-mysql',        
    ], update=True)
       
    # change Apache envvars and set vagrant has main user
    require.files.template_file(
        template_source = './fabric/files/apache/envvars.template',
        path='/etc/apache2/envvars', 
        context = {
            'apache_run_user': '******',
            'apache_run_group': 'vagrant',
        },
        owner = 'root',
        group = 'root',
        use_sudo=True
    )

    # create a new virtual host and use ~/vagrant_www as document root
    require.files.template_file(
        template_source = './fabric/files/apache/vhost.conf.template',
        path='/etc/apache2/sites-available/vagrant', 
        context = {
            'server_name': 'localhost',
            'port': 80,
            'document_root': '/vagrant',
        },
        owner = 'root',
        group = 'root',
        use_sudo=True
    )

    # enable new vhost
    if not files.is_link('/etc/apache2/sites-enabled/vagrant'):
        sudo("a2dissite default")
        sudo("a2ensite vagrant")

    # activate mod_rewrite (usefull these days)
    if not files.is_link('/etc/apache2/mods-enabled/rewrite.load'):
        sudo("a2enmod rewrite")   
    
    # apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    require.files.file('/etc/apache2/httpd.conf', 
        source='./fabric/files/apache/httpd.conf',
        owner = 'root',
        group = 'root',
        use_sudo=True
    )
    # sudo("rm -R /var/lock/apache2")
    sudo("chown -R vagrant:vagrant /var/lock/apache2")
    sudo("/etc/init.d/apache2 restart")
    installMySql()
Ejemplo n.º 5
0
def site(server_name, template_contents=None, template_source=None,
         enabled=True, check_config=True, **kwargs):
    """
    Require an nginx site.

    You must provide a template for the site configuration, either as a
    string (*template_contents*) or as the path to a local template
    file (*template_source*).

    ::

        from fabtools import require

        CONFIG_TPL = '''
        server {
            listen      %(port)d;
            server_name %(server_name)s %(server_alias)s;
            root        %(docroot)s;
            access_log  /var/log/nginx/%(server_name)s.log;
        }'''

        require.nginx.site('example.com', template_contents=CONFIG_TPL,
            port=80,
            server_alias='www.example.com',
            docroot='/var/www/mysite',
        )

    .. seealso:: :py:func:`fabtools.require.files.template_file`
    """
    server()

    config_filename = '/etc/nginx/sites-available/%s.conf' % server_name

    context = {
        'port': 80,
    }
    context.update(kwargs)
    context['server_name'] = server_name

    template_file(config_filename, template_contents, template_source, context, use_sudo=True)

    link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name
    if enabled:
        if not is_link(link_filename):
            run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals())

        # Make sure we don't break the config
        if check_config:
            with settings(hide('running', 'warnings'), warn_only=True):
                if run_as_root('nginx -t').failed:
                    run_as_root("rm %(link_filename)s" % locals())
                    message = red("Error in %(server_name)s nginx site config (disabling for safety)" % locals())
                    abort(message)
    else:
        if is_link(link_filename):
            run_as_root("rm %(link_filename)s" % locals())

    reload_service('nginx')
Ejemplo n.º 6
0
def provision():
    # install required packages (+extras) for LAMP server
    require.deb.packages(
        [
            # 'build-essential',
            # 'devscripts',
            'locales',
            'apache2',
            'php5',
            'php5-mysql',
        ],
        update=True)

    # change Apache envvars and set vagrant has main user
    require.files.template_file(
        template_source='./fabric/files/apache/envvars.template',
        path='/etc/apache2/envvars',
        context={
            'apache_run_user': '******',
            'apache_run_group': 'vagrant',
        },
        owner='root',
        group='root',
        use_sudo=True)

    # create a new virtual host and use ~/vagrant_www as document root
    require.files.template_file(
        template_source='./fabric/files/apache/vhost.conf.template',
        path='/etc/apache2/sites-available/vagrant',
        context={
            'server_name': 'localhost',
            'port': 80,
            'document_root': '/vagrant',
        },
        owner='root',
        group='root',
        use_sudo=True)

    # enable new vhost
    if not files.is_link('/etc/apache2/sites-enabled/vagrant'):
        sudo("a2dissite default")
        sudo("a2ensite vagrant")

    # activate mod_rewrite (usefull these days)
    if not files.is_link('/etc/apache2/mods-enabled/rewrite.load'):
        sudo("a2enmod rewrite")

    # apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    require.files.file('/etc/apache2/httpd.conf',
                       source='./fabric/files/apache/httpd.conf',
                       owner='root',
                       group='root',
                       use_sudo=True)
    # sudo("rm -R /var/lock/apache2")
    sudo("chown -R vagrant:vagrant /var/lock/apache2")
    sudo("/etc/init.d/apache2 restart")
    installMySql()
Ejemplo n.º 7
0
def apache():
    """
    Check apache server, enabling and disabling sites.
    """

    from fabric.api import run, sudo
    from fabtools import require
    from fabtools.files import is_link
    from fabtools.system import set_hostname

    set_hostname('www.example.com')

    require.apache.server()

    require.apache.disabled('default')
    assert not is_link('/etc/apache2/sites-enabled/000-default')

    require.apache.enabled('default')
    assert is_link('/etc/apache2/sites-enabled/000-default')

    require.apache.disabled('default')
    assert not is_link('/etc/apache2/sites-enabled/000-default')

    run('mkdir -p ~/example.com/')
    run('echo "example page" > ~/example.com/index.html')

    require.apache.site(
        'example.com',
        template_contents="""
<VirtualHost *:%(port)s>
    ServerName %(hostname)s

    DocumentRoot %(document_root)s

    <Directory %(document_root)s>
        Options Indexes FollowSymLinks MultiViews

        AllowOverride All

        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
        """,
        port=80,
        hostname='www.example.com',
        document_root='/home/vagrant/example.com/',
    )

    with shell_env(http_proxy=''):
        body = run(
            'wget -qO- --header="Host: www.example.com" http://localhost/')

    assert body == 'example page'
Ejemplo n.º 8
0
def apache():
    """
    Check apache server, enabling and disabling sites.
    """

    from fabric.api import run, sudo
    from fabtools import require
    from fabtools.files import is_link
    from fabtools.system import set_hostname

    set_hostname('www.example.com')

    require.apache.server()

    require.apache.disabled('default')
    assert not is_link('/etc/apache2/sites-enabled/000-default')

    require.apache.enabled('default')
    assert is_link('/etc/apache2/sites-enabled/000-default')

    require.apache.disabled('default')
    assert not is_link('/etc/apache2/sites-enabled/000-default')

    run('mkdir -p ~/example.com/')
    run('echo "example page" > ~/example.com/index.html')

    require.apache.site(
        'example.com',
        template_contents="""
<VirtualHost *:%(port)s>
    ServerName %(hostname)s

    DocumentRoot %(document_root)s

    <Directory %(document_root)s>
        Options Indexes FollowSymLinks MultiViews

        AllowOverride All

        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
        """,
        port=80,
        hostname='www.example.com',
        document_root='/home/vagrant/example.com/',
    )

    with shell_env(http_proxy=''):
        body = run('wget -qO- --header="Host: www.example.com" http://localhost/')

    assert body == 'example page'
Ejemplo n.º 9
0
def nginx():
    """
    Check nginx server, enabling and disabling sites.
    """

    from fabtools import require
    from fabtools.files import is_link

    require.nginx.server()

    require.nginx.disabled('default')
    assert not is_link('/etc/nginx/sites-enabled/default')

    require.nginx.enabled('default')
    assert is_link('/etc/nginx/sites-enabled/default')
Ejemplo n.º 10
0
def configure_tomcat(path, overwrite=False):
    from fabric.contrib.files import append
    startup_script = """
#!/bin/sh
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Tomcat
# Description:       Start Tomcat
### END INIT INFO

case $1 in
start)
sh %(path)s/bin/startup.sh
;;
stop)
sh %(path)s/bin/shutdown.sh
;;
restart)
sh %(path)s/bin/shutdown.sh
sh %(path)s/bin/startup.sh
;;
esac
exit 0""" % {
        'path': path
    }

    # Check for existing files and overwrite.
    if is_file('/etc/init.d/tomcat'):
        if overwrite is False:
            raise OSError(
                "/etc/init.d/tomcat already exists and not overwriting.")
        else:
            run_as_root("rm -f /etc/init.d/tomcat")

    # Now create the file and symlinks.
    append('/etc/init.d/tomcat', startup_script, use_sudo=True)
    run_as_root('chmod 755 /etc/init.d/tomcat')

    if not is_link('/etc/rc1.d/K99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat')

    if not is_link('/etc/rc2.d/S99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat')
Ejemplo n.º 11
0
def test_site_enabled(nginx_server):

    from fabtools.require.nginx import enabled as require_nginx_site_enabled
    from fabtools.files import is_link

    require_nginx_site_enabled('default')
    assert is_link('/etc/nginx/sites-enabled/default')
Ejemplo n.º 12
0
def test_site_enabled(nginx_server):

    from fabtools.require.nginx import enabled as require_nginx_site_enabled
    from fabtools.files import is_link

    require_nginx_site_enabled('default')
    assert is_link('/etc/nginx/sites-enabled/default')
Ejemplo n.º 13
0
def install_from_oracle_site(version=DEFAULT_VERSION):
    """
    Download tarball from Oracle site and install JDK.

    ::

        import fabtools

        # Install Oracle JDK
        fabtools.oracle_jdk.install_from_oracle_site()

    """

    prefix = '/opt'

    release, build = version.split('-')
    major, update = release.split('u')
    if len(update) == 1:
        update = '0' + update

    arch = _required_jdk_arch()

    self_extracting_archive = (major == '6')

    extension = 'bin' if self_extracting_archive else 'tar.gz'
    filename = 'jdk-%(release)s-linux-%(arch)s.%(extension)s' % locals()
    download_path = posixpath.join('/tmp', filename)
    url = 'http://download.oracle.com/otn-pub/java/jdk/%(version)s/%(filename)s' % locals(
    )

    _download(url, download_path)

    # Prepare install dir
    install_dir = 'jdk1.%(major)s.0_%(update)s' % locals()
    with cd(prefix):
        if is_dir(install_dir):
            run_as_root('rm -rf %s' % quote(install_dir))

    # Extract
    if self_extracting_archive:
        run('chmod u+x %s' % quote(download_path))
        with cd('/tmp'):
            run_as_root('rm -rf %s' % quote(install_dir))
            run_as_root('./%s' % filename)
            run_as_root('mv %s %s' % (quote(install_dir), quote(prefix)))
    else:
        with cd(prefix):
            run_as_root('tar xzvf %s' % quote(download_path))

    # Set up link
    link_path = posixpath.join(prefix, 'jdk')
    if is_link(link_path):
        run_as_root('rm -f %s' % quote(link_path))
    run_as_root('ln -s %s %s' % (quote(install_dir), quote(link_path)))

    # Remove archive
    run('rm -f %s' % quote(download_path))

    _create_profile_d_file(prefix)
Ejemplo n.º 14
0
def install_from_oracle_site(version=DEFAULT_VERSION):
    """
    Download tarball from Oracle site and install JDK.

    ::

        import fabtools

        # Install Oracle JDK
        fabtools.oracle_jdk.install_from_oracle_site()

    """

    prefix = '/opt'

    release, build = version.split('-')
    major, update = release.split('u')
    if len(update) == 1:
        update = '0' + update

    arch = _required_jdk_arch()

    self_extracting_archive = (major == '6')

    extension = 'bin' if self_extracting_archive else 'tar.gz'
    filename = 'jdk-%(release)s-linux-%(arch)s.%(extension)s' % locals()
    download_path = posixpath.join('/tmp', filename)
    url = 'http://download.oracle.com/otn-pub/java/jdk/'\
          '%(version)s/%(filename)s' % locals()

    _download(url, download_path)

    # Prepare install dir
    install_dir = 'jdk1.%(major)s.0_%(update)s' % locals()
    with cd(prefix):
        if is_dir(install_dir):
            run_as_root('rm -rf %s' % quote(install_dir))

    # Extract
    if self_extracting_archive:
        run('chmod u+x %s' % quote(download_path))
        with cd('/tmp'):
            run_as_root('rm -rf %s' % quote(install_dir))
            run_as_root('./%s' % filename)
            run_as_root('mv %s %s' % (quote(install_dir), quote(prefix)))
    else:
        with cd(prefix):
            run_as_root('tar xzvf %s' % quote(download_path))

    # Set up link
    link_path = posixpath.join(prefix, 'jdk')
    if is_link(link_path):
        run_as_root('rm -f %s' % quote(link_path))
    run_as_root('ln -s %s %s' % (quote(install_dir), quote(link_path)))

    # Remove archive
    run('rm -f %s' % quote(download_path))

    _create_profile_d_file(prefix)
Ejemplo n.º 15
0
def configure_tomcat(path, overwrite=False):
    from fabric.contrib.files import append
    startup_script = """
#!/bin/sh
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Tomcat
# Description:       Start Tomcat
### END INIT INFO

case $1 in
start)
sh %(path)s/bin/startup.sh
;;
stop)
sh %(path)s/bin/shutdown.sh
;;
restart)
sh %(path)s/bin/shutdown.sh
sh %(path)s/bin/startup.sh
;;
esac
exit 0""" % {'path': path}

    # Check for existing files and overwrite.
    if is_file('/etc/init.d/tomcat'):
        if overwrite is False:
            raise OSError(
                "/etc/init.d/tomcat already exists and not overwriting.")
        else:
            run_as_root("rm -f /etc/init.d/tomcat")

    # Now create the file and symlinks.
    append('/etc/init.d/tomcat', startup_script, use_sudo=True)
    run_as_root('chmod 755 /etc/init.d/tomcat')

    if not is_link('/etc/rc1.d/K99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat')

    if not is_link('/etc/rc2.d/S99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat')
Ejemplo n.º 16
0
def active_uwsgi(webserver, appname):
    config_filename = '/etc/uwsgi/apps-available/%(webserver)s_%(appname)s.ini' % locals(
    )
    link_filename = '/etc/uwsgi/apps-enabled/%(webserver)s_%(appname)s.ini' % locals(
    )

    if not is_link(link_filename):
        run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals())
Ejemplo n.º 17
0
def site_disabled(server_name):
    if server_name != 'default':
        server_name += '.conf'

    link_filename = '/etc/nginx/sites-enabled/%s' % server_name
    if is_link(link_filename):
        root_run("rm %(link_filename)s" % locals())

    root_run("/etc/init.d/nginx reload")
Ejemplo n.º 18
0
def is_site_enabled(config):
    """
    Check if an Apache site is enabled.
    """
    config = _get_config_name(config)
    if config == 'default':
        config = '000-default'

    return is_link(_get_link_filename(config))
Ejemplo n.º 19
0
def is_site_enabled(config):
    """
    Check if an Apache site is enabled.
    """
    config = _get_config_name(config)
    if config == 'default':
        config = '000-default'

    return is_link(_get_link_filename(config))
Ejemplo n.º 20
0
def site_enabled(server_name):
    if server_name != 'default':
        server_name += '.conf'

    config_filename = '/etc/nginx/sites-available/%s' % server_name  # NOQA
    link_filename = '/etc/nginx/sites-enabled/%s' % server_name
    if not is_link(link_filename):
        root_run("ln -s %(config_filename)s %(link_filename)s" % locals())

    root_run("/etc/init.d/nginx reload")
Ejemplo n.º 21
0
def require_timezone(zone, city):
    """
    Check a timezone
    """
    config_file = '/etc/localtime'
    link = '/usr/share/zoneinfo/%(zone)s/%(city)s' % locals()
    if is_link(config_file):
        run_as_root('rm %(config_file)s' % locals())
    run_as_root('ln -s %(link)s %(config_file)s' % locals())
    run_as_root('hwclock --systohc --utc')
Ejemplo n.º 22
0
def require_timezone(zone, city):
    """
    Check a timezone
    """
    config_file = '/etc/localtime'
    link = '/usr/share/zoneinfo/%(zone)s/%(city)s' % locals()
    if is_link(config_file):
        run_as_root('rm %(config_file)s' % locals())
    run_as_root('ln -s %(link)s %(config_file)s' % locals())
    run_as_root('hwclock --systohc --utc')
Ejemplo n.º 23
0
def configure_tomcat(path, overwrite=False):
    from fabric.contrib.files import append
    startup_script = """
# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

case $1 in
start)
sh %(path)s/bin/startup.sh
;;
stop)
sh %(path)s/bin/shutdown.sh
;;
restart)
sh %(path)s/bin/shutdown.sh
sh %(path)s/bin/startup.sh
;;
esac
exit 0""" % {
        'path': path
    }

    # Check for existing files and overwrite.
    if is_file('/etc/init.d/tomcat'):
        if overwrite is False:
            raise OSError(
                "/etc/init.d/tomcat already exists and not overwriting.")
        else:
            run_as_root("rm -f /etc/init.d/tomcat")

    # Now create the file and symlinks.
    append('/etc/init.d/tomcat', startup_script, use_sudo=True)
    run_as_root('chmod 755 /etc/init.d/tomcat')

    if not is_link('/etc/rc1.d/K99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat')

    if not is_link('/etc/rc2.d/S99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat')
Ejemplo n.º 24
0
def install_from_oracle_site(version=DEFAULT_VERSION):
    """
    Download tarball from Oracle site and install JDK.

    ::

        import fabtools

        # Install Oracle JDK
        fabtools.oracle_jdk.install_from_oracle_site()

    """

    from fabtools.require.files import directory as require_directory

    release, build = version.split("-")
    major, update = release.split("u")
    if len(update) == 1:
        update = "0" + update

    jdk_arch = _required_jdk_arch()

    if major == "6":
        jdk_filename = "jdk-%(release)s-linux-%(jdk_arch)s.bin" % locals()
    else:
        jdk_filename = "jdk-%(release)s-linux-%(jdk_arch)s.tar.gz" % locals()
    jdk_dir = "jdk1.%(major)s.0_%(update)s" % locals()

    jdk_url = "http://download.oracle.com/otn-pub/java/jdk/" + "%(version)s/%(jdk_filename)s" % locals()

    with cd("/tmp"):
        run("rm -rf %s" % jdk_filename)
        run(
            'wget --no-cookies --header="Cookie: gpw_e24=a" '
            + "--progress=dot:mega "
            + "%(jdk_url)s -O /tmp/%(jdk_filename)s" % locals()
        )

    require_directory("/opt", mode="777", use_sudo=True)
    with cd("/opt"):
        if major == "6":
            run("chmod u+x /tmp/%s" % jdk_filename)
            with cd("/tmp"):
                run("./%s" % jdk_filename)
                run("mv %s /opt/" % jdk_dir)
        else:
            run("tar -xzvf /tmp/%s" % jdk_filename)

        if is_link("jdk"):
            run("rm -rf jdk")
        run("ln -s %s jdk" % jdk_dir)

    _create_profile_d_file()
Ejemplo n.º 25
0
def install_from_oracle_site(version=DEFAULT_VERSION):
    """
    Download tarball from Oracle site and install JDK.

    ::

        import fabtools

        # Install Oracle JDK
        fabtools.oracle_jdk.install_from_oracle_site()

    """

    from fabtools.require.files import directory as require_directory

    release, build = version.split('-')
    major, update = release.split('u')
    if len(update) == 1:
        update = '0' + update

    jdk_arch = _required_jdk_arch()

    if major == '6':
        jdk_filename = 'jdk-%(release)s-linux-%(jdk_arch)s.bin' % locals()
    else:
        jdk_filename = 'jdk-%(release)s-linux-%(jdk_arch)s.tar.gz' % locals()
    jdk_dir = 'jdk1.%(major)s.0_%(update)s' % locals()

    jdk_url = 'http://download.oracle.com/otn-pub/java/jdk/' +\
              '%(version)s/%(jdk_filename)s' % locals()

    with cd('/tmp'):
        run('rm -rf %s' % jdk_filename)
        run('wget --no-cookies --no-check-certificate --header="Cookie: gpw_e24=a" ' +
            '--progress=dot:mega ' +
            '%(jdk_url)s -O /tmp/%(jdk_filename)s' % locals())

    require_directory('/opt', mode='777', use_sudo=True)
    with cd('/opt'):
        if major == '6':
            run('chmod u+x /tmp/%s' % jdk_filename)
            with cd('/tmp'):
                run('./%s' % jdk_filename)
                run('mv %s /opt/' % jdk_dir)
        else:
            run('tar -xzvf /tmp/%s' % jdk_filename)

        if is_link('jdk'):
            run('rm -rf jdk')
        run('ln -s %s jdk' % jdk_dir)

    _create_profile_d_file()
Ejemplo n.º 26
0
def install_from_oracle_site(version=DEFAULT_VERSION):
    """
    Download tarball from Oracle site and install JDK.

    ::

        import fabtools

        # Install Oracle JDK
        fabtools.oracle_jdk.install_from_oracle_site()

    """

    from fabtools.require.files import directory as require_directory

    release, build = version.split('-')
    major, update = release.split('u')
    if len(update) == 1:
        update = '0' + update

    jdk_arch = _required_jdk_arch()

    if major == '6':
        jdk_filename = 'jdk-%(release)s-linux-%(jdk_arch)s.bin' % locals()
    else:
        jdk_filename = 'jdk-%(release)s-linux-%(jdk_arch)s.tar.gz' % locals()
    jdk_dir = 'jdk1.%(major)s.0_%(update)s' % locals()

    jdk_url = 'http://download.oracle.com/otn-pub/java/jdk/' +\
              '%(version)s/%(jdk_filename)s' % locals()

    with cd('/tmp'):
        run('rm -rf %s' % jdk_filename)
        run('wget --header "Cookie: oraclelicense=accept-securebackup-cookie" ' +
            '--progress=dot:mega ' +
            '%(jdk_url)s -O /tmp/%(jdk_filename)s' % locals())

    require_directory('/opt', mode='777', use_sudo=True)
    with cd('/opt'):
        if major == '6':
            run('chmod u+x /tmp/%s' % jdk_filename)
            with cd('/tmp'):
                run('./%s' % jdk_filename)
                run('mv %s /opt/' % jdk_dir)
        else:
            run('tar -xzvf /tmp/%s' % jdk_filename)

        if is_link('jdk'):
            run('rm -rf jdk')
        run('ln -s %s jdk' % jdk_dir)

    _create_profile_d_file()
Ejemplo n.º 27
0
def configure_tomcat(path, overwrite=False):
    from fabric.contrib.files import append
    startup_script = """
# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

case $1 in
start)
sh %(path)s/bin/startup.sh
;;
stop)
sh %(path)s/bin/shutdown.sh
;;
restart)
sh %(path)s/bin/shutdown.sh
sh %(path)s/bin/startup.sh
;;
esac
exit 0""" % {'path': path}

    # Check for existing files and overwrite.
    if is_file('/etc/init.d/tomcat'):
        if overwrite is False:
            raise OSError("/etc/init.d/tomcat already exists and not overwriting.")
        else:
            run_as_root("rm -f /etc/init.d/tomcat")

    # Now create the file and symlinks.
    append('/etc/init.d/tomcat', startup_script, use_sudo=True)
    run_as_root('chmod 755 /etc/init.d/tomcat')

    if not is_link('/etc/rc1.d/K99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat')

    if not is_link('/etc/rc2.d/S99tomcat'):
        run_as_root('ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat')
Ejemplo n.º 28
0
def disable(config):
    """
    Delete link in /etc/nginx/sites-enabled/

    (does not reload nginx config)

    ::
        from fabtools import require

        require.nginx.disable('default')

    .. seealso:: :py:func:`fabtools.require.nginx.disabled`
    """
    link_filename = '/etc/nginx/sites-enabled/%s' % config

    if is_link(link_filename):
        run_as_root("rm %(link_filename)s" % locals())
Ejemplo n.º 29
0
def enable(config):
    """
    Create link from /etc/nginx/sites-available/ in /etc/nginx/sites-enabled/

    (does not reload nginx config)

    ::
        from fabtools import require

        require.nginx.enable('default')

    .. seealso:: :py:func:`fabtools.require.nginx.enabled`
    """
    config_filename = '/etc/nginx/sites-available/%s' % config
    link_filename = '/etc/nginx/sites-enabled/%s' % config

    if not is_link(link_filename):
        run_as_root("ln -s %(config_filename)s %(link_filename)s" % {
            'config_filename': quote(config_filename),
            'link_filename': quote(link_filename),
        })
Ejemplo n.º 30
0
def is_site_enabled(site_name):
    """
    Check if an Apache site is enabled.
    """
    return is_link(_site_link_path(site_name))
Ejemplo n.º 31
0
def is_module_enabled(module):
    """
    Check if an Apache module is enabled.
    """
    return is_link('/etc/apache2/mods-enabled/%s.load' % module)
Ejemplo n.º 32
0
def is_site_enabled(site_name):
    return is_link(_get_link_filename(_get_site_name(site_name)))
Ejemplo n.º 33
0
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs):
    """
    Require an apache2 site.

    You must provide a template for the site configuration, either as a
    string (*template_contents*) or as the path to a local template
    file (*template_source*).

    ::

        from fabtools import require

        VHOST_SITE_TEMPLATE = '''
        NameVirtualHost %(server_name)s:%(port)s
        <VirtualHost %(server_name)s:%(port)s>
          ServerName %(server_name)s
          ServerAdmin webmaster@%(server_name)s
          DocumentRoot %(docroot)s
          <Directory %(docroot)s>
            AllowOverride all
            Order allow,deny
            allow from all
          </Directory>
          ErrorLog /var/log/apache2/%(server_name)s.error.log
          CustomLog /var/log/apache2/%(server_name)s.access.log combined
          LogLevel warn
          ServerSignature Off
        </VirtualHost>
        '''

        require.apache2.site('example.com', template_contents=VHOST_SITE_TEMPLATE,
                port=80,
                server_alias='www.example.com',
                docroot='/var/www/mysite',
            )
        )

    .. seealso:: :py:func:`fabtools.require.files.template_file`
    """

    server()

    config_filename = "/etc/apache2/sites-available/%s.conf" % server_name

    context = {"port": 80}
    context.update(kwargs)
    context["server_name"] = server_name

    template_file(config_filename, template_contents, template_source, context, use_sudo=True)

    link_filename = "/etc/apache2/sites-enabled/%s.conf" % server_name
    if enabled:
        if not is_link(link_filename):
            sudo("ln -s %(config_filename)s %(link_filename)s" % locals())
        # Make sure we don't break the config
        if check_config:
            with settings(hide("running", "warnings"), warn_only=True):
                if not sudo("apache2ctl configtest" % locals()) == "Syntax OK":
                    print red("Error in %(server_name)s apache2 site config (disabling for safety)" % locals())
                    sudo("rm %(link_filename)s" % locals())
    else:
        if is_link(link_filename):
            sudo("rm %(link_filename)s" % locals())

    restarted("apache2")
Ejemplo n.º 34
0
def test_require_site_enabled(apache, example_site):
    from fabtools.require.apache import site_enabled
    site_enabled(example_site)
    assert is_link('/etc/apache2/sites-enabled/{0}.conf'.format(example_site))
Ejemplo n.º 35
0
def test_require_module_enabled(apache):
    from fabtools.require.apache import module_enabled
    module_enabled('rewrite')
    assert is_link('/etc/apache2/mods-enabled/rewrite.load')
Ejemplo n.º 36
0
def is_site_enabled(config):
    config = _get_config_name(config)
    if config == 'default':
        config = '000-default'

    return is_link(_get_link_filename(config))
Ejemplo n.º 37
0
def is_module_enabled(module):
    return is_link('/etc/apache2/mods-enabled/%s.load' % module)
Ejemplo n.º 38
0
def is_site_enabled(config):
    config = _get_config_name(config)
    if config == 'default':
        config = '000-default'

    return is_link(_get_link_filename(config))
Ejemplo n.º 39
0
def is_module_enabled(module):
    return is_link('/etc/apache2/mods-enabled/%s.load' % module)
Ejemplo n.º 40
0
def test_require_module_enabled(apache):
    from fabtools.require.apache import module_enabled
    module_enabled('rewrite')
    assert is_link('/etc/apache2/mods-enabled/rewrite.load')
Ejemplo n.º 41
0
def test_require_site_enabled(apache, example_site):
    from fabtools.require.apache import site_enabled
    site_enabled(example_site)
    assert is_link('/etc/apache2/sites-enabled/{0}.conf'.format(example_site))
Ejemplo n.º 42
0
def test_require_site_enabled(apache):
    from fabtools.require.apache import site_enabled
    site_enabled('default')
    assert is_link('/etc/apache2/sites-enabled/000-default')
Ejemplo n.º 43
0
def active_uwsgi(webserver, appname):
    config_filename = '/etc/uwsgi/apps-available/%(webserver)s_%(appname)s.ini' % locals()
    link_filename = '/etc/uwsgi/apps-enabled/%(webserver)s_%(appname)s.ini' % locals()

    if not is_link(link_filename):
        run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals())