django-fab-deploy / fab_deploy /

The default branch has multiple heads

#coding: utf-8
from __future__ import with_statement
import os.path
from fabric.api import run, settings, env, cd, sudo
from fabric.contrib import files
from fabric import utils as fabric_utils
from fab_deploy import utils

__all__ = ['create_linux_account', 'create_sudo_linux_account',
           'ssh_add_key', 'install_sudo']

def prepare_server():
    """ Prepares server: installs system packages. """
    os = utils.detect_os()
    if os in ['lenny', 'squeeze']:


def install_sudo():
    """ Installs sudo on server. """
    run('aptitude install -y sudo')

def install_common_software():
    """ Installs common system packages. """
    common_packages = [
        'python', 'build-essential', 'python-dev', 'python-setuptools',
        'python-profiler', 'libjpeg-dev', 'zlib1g-dev',
        'libssl-dev', 'libcurl3-dev',
        'libxml2-dev', 'libxslt1-dev', # for lxml

        'screen', 'locales-all', 'curl',
    extra_packages = {
        'lenny': ['libmysqlclient15-dev'],
        'squeeze': ['libmysqlclient-dev'],
        'maverick': ['libmysqlclient-dev'],
        'lucid': ['libmysqlclient-dev'],

    os = utils.detect_os()
    if os not in extra_packages:
        fabric_utils.abort('Your OS (%s) is unsupported now.' % os)

    aptitude_install(" ".join(common_packages + extra_packages[os]))

    # git and mercurial are outdated in stable Debian Lenny and
    # don't work with some source repositories on github and bitbucket
    vcs_options = {'lenny': '-t lenny-backports'}
    aptitude_install('mercurial git git-core', vcs_options.get(os, ""))
    aptitude_install('bzr', '--without-recommends')

    sudo('easy_install -U pip')
    sudo('pip install -U virtualenv')

def setup_backports():
    """ Adds backports repo to apt sources. """
    os = utils.detect_os()
    backports = {
        'lenny': ' lenny-backports main contrib non-free',
        'squeeze': ' squeeze-backports main contrib non-free',
        'maverick': ' maverick-backports main universe multiverse restricted',
        'lucid': ' lucid-backports main universe multiverse restricted',

    if os not in backports:
        fabric_utils.puts("Backports are not available for " + os)

    sudo("echo 'deb %s' > /etc/apt/sources.list.d/backports.sources.list" % backports[os])
    with settings(warn_only=True):
        sudo('aptitude update')

def create_linux_account(pub_key_file, username=None):
    Creates linux account, setups ssh access and pip.conf file.


        fab create_linux_account:"/home/kmike/.ssh/"

    with open(os.path.normpath(pub_key_file), 'rt') as f:
        ssh_key =

    username = username or env.conf.USER

    with (settings(warn_only=True)):
        sudo('adduser %s --disabled-password --gecos ""' % username)

    home_dir = utils.get_home_dir(username)
    with cd(home_dir):
        sudo('mkdir -p .ssh')
        files.append('.ssh/authorized_keys', ssh_key, use_sudo=True)
        sudo('chown -R %s:%s .ssh' % (username, username))

    from fab_deploy import virtualenv

def create_sudo_linux_account(pub_key_file, username=None):
    """ Creates linux account, setups ssh access and
    adds the created user to sudoers. This command requires root ssh access. """
    username = username or env.conf.SUDO_USER
    if username == 'root':

    home_dir = '/home/%s' % username

    with open(os.path.normpath(pub_key_file), 'rt') as f:
        ssh_key =

    with (settings(warn_only=True)):
        run('adduser %s --disabled-password --gecos ""' % username)
        with cd(home_dir):
            run('mkdir -p .ssh')
            files.append('.ssh/authorized_keys', ssh_key)
            run('chown -R %s:%s .ssh' % (username, username))

    line = '%s ALL=(ALL) NOPASSWD: ALL' % username
    files.append('/etc/sudoers', line)

def ssh_add_key(pub_key_file):
    """ Adds a ssh key from passed file to user's authorized_keys on server. """
    with open(os.path.normpath(pub_key_file), 'rt') as f:
        ssh_key =
    run('mkdir -p .ssh')
    files.append('.ssh/authorized_keys', ssh_key)

def aptitude_install(packages, options=''):
    """ Installs package via aptitude. """
    if not hasattr(env.conf, '_APTITUDE_UPDATED'):
        sudo('aptitude update')
        env.conf._APTITUDE_UPDATED = True
    sudo('aptitude install %s -y %s' % (options, packages,))

#def install_backup_system():
#    sudo('aptitude install -y s3cmd ruby rubygems libxml2-dev libxslt-dev libopenssl-ruby')
#    sudo('gem install rubygems-update')
#    sudo('/var/lib/gems/1.8/bin/update_rubygems')
#    sudo('gem install astrails-safe --source')