Commits

Sebastien Beal committed 0fb4127

merge

Comments (0)

Files changed (2)

fab_deploy/deploy.py

-#coding: utf-8
-from __future__ import with_statement
-from fabric.api import task, abort, settings, cd, sudo, run, env, puts
-from fabric.contrib import console
-from fabric.contrib import files
-
-from fab_deploy import utils
-from fab_deploy import pip
-from fab_deploy import django_commands as dj_cmd
-from fab_deploy import system
-from fab_deploy import vcs
-from fab_deploy import webserver
-from fab_deploy.webserver import apache, nginx
-
-
-__all__ = ['full_deploy', 'deploy_project',
-           'update_django_config', 'up', 'setup_web_server', 'push',
-           'remove']
-
-@task
-def full_deploy():
-    """ Prepares server and deploys the project. """
-    os = utils.detect_os()
-    if not console.confirm("Is the OS detected correctly (%s)?" % os, default=False):
-        abort("Detection fails. Please set env.conf.OS to correct value.")
-    system.prepare()
-    deploy_project()
-
-@task
-def deploy_project():
-    """ Deploys project on prepared server. """
-    pip.virtualenv_create()
-    _make_clone()
-
-    pip.install_r(env.conf.PIP_REQUIREMENTS, restart=False)
-
-    setup_web_server()
-    update_django_config()
-
-    dj_cmd.syncdb()
-    dj_cmd.migrate()
-
-@task
-def _make_clone():
-    """ Creates repository clone on remote server. """
-    run('mkdir -p ' + env.conf.SRC_DIR)
-    with cd(env.conf.SRC_DIR):
-        with settings(warn_only=True):
-            vcs.init()
-    vcs.push()
-    with cd(env.conf.SRC_DIR):
-        vcs.up()
-    update_django_config(restart=False)
-    vcs.configure()
-
-@task
-def update_django_config(restart=True):
-    """ Updates :file:`config.py` on server (using :file:`config.server.py`) """
-    files.upload_template(
-        utils._project_path(env.conf.REMOTE_CONFIG_TEMPLATE),
-        utils._remote_project_path(env.conf.LOCAL_CONFIG),
-        env.conf, True
-    )
-    if restart:
-        if 'apache' in env.conf.WEB_SERVERS:
-            apache.touch()
-
-@task
-def up(branch=None, before_restart=lambda: None):
-    """ Runs vcs ``up`` or ``checkout`` command on server and reloads
-    mod_wsgi process. """
-    utils.delete_pyc()
-    with cd('src/' + env.conf['INSTANCE_NAME']):
-        vcs.up(branch)
-    before_restart()
-    apache.touch()
-
-@task
-def setup_web_server():
-    """ Sets up a web server based on the WEB_SERVERS list. """
-    for server in env.conf.WEB_SERVERS:
-        try:
-            s = getattr(webserver, server)
-        except:
-            abort("Server %s is not supported." % server)    
-        s.install()
-
-    for server in env.conf.WEB_SERVERS:
-        s = getattr(webserver, server)
-        s.setup()
-
-@task
-def push(*args, **kwargs):
-    ''' Run it instead of your VCS push command.
-
-    The following strings are allowed as positional arguments:
-
-    * 'notest' - don't run tests
-    * 'syncdb' - run syncdb before code reloading
-    * 'migrate' - run migrate before code reloading
-    * 'pip_update' - run virtualenv.update_r before code reloading
-    * 'norestart' - do not reload source code
-
-    Keyword arguments:
-
-    * before_restart - callable to be executed after code uploading
-      but before the web server reloads the code.
-
-    Customization example can be found  :ref:`here <fab-push-customization>`.
-
-    '''
-    allowed_args = set(['notest', 'syncdb', 'migrate', 'pip_update', 'norestart'])
-    for arg in args:
-        if arg not in allowed_args:
-            puts('Invalid argument: %s' % arg)
-            puts('Valid arguments are: %s' % allowed_args)
-            return
-
-    vcs.push()
-    utils.delete_pyc()
-    with cd('src/' + env.conf['INSTANCE_NAME']):
-        vcs.up()
-
-    if 'pip_update' in args:
-        pip.update_r(restart=False)
-    if 'syncdb' in args:
-        dj_cmd.syncdb()
-    if 'migrate' in args:
-        dj_cmd.migrate()
-
-    # execute 'before_restart' callback
-    kwargs.get('before_restart', lambda: None)()
-
-    if 'norestart' not in args:
-        apache.touch()
-    if 'notest' not in args:
-        dj_cmd.test()
-
-@task
-def remove(confirm=True):
-    """ Shuts site down. This command doesn't clean everything, e.g.
-    user data (database, backups) is preserved. """
-
-    if confirm:
-        message = "Do you wish to undeploy host %s?" % env.hosts[0]
-        if not console.confirm(message, default=False):
-            abort("Aborting.")
-
-    @utils.run_as_sudo
-    def wipe_web():
-        sudo('rm -f /etc/nginx/sites-enabled/' + env.conf['INSTANCE_NAME'])
-        sudo('a2dissite ' + env.conf['INSTANCE_NAME'])
-        sudo('invoke-rc.d nginx reload')
-        sudo('invoke-rc.d apache2 reload')
-
-    wipe_web()
-    run('rm -rf %s' % env.conf.SRC_DIR)
-    for folder in ['bin', 'include', 'lib', 'src']:
-        run('rm -rf %s' % env.conf.ENV_DIR + '/' + folder)

fab_deploy/django_commands.py

-#coding: utf-8
-from __future__ import with_statement
-from fabric.api import *
-
-from fab_deploy import utils
-from fab_deploy import db
-
-__all__ = ['migrate', 'manage', 'syncdb', 'compress', 'collectstatic', 'test',
-           'command_is_available']
-
-@task
-@utils.inside_project
-def command_is_available(command):
-    with settings(hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True):
-        output = run('python manage.py help ' + command)
-
-    if output.succeeded:
-        return True
-
-    # that's ugly
-    unknown_command_msg = "Unknown command: '%s'" % command
-    if unknown_command_msg in output:
-        return False
-
-    # re-raise the original exception
-    run('python manage.py help ' + command)
-
-@task
-@utils.inside_project
-def manage(command):
-    """
-    Runs django management command. Example::
-
-        fab manage:createsuperuser
-
-    """
-    command_name = command.split()[0]
-    if not command_is_available(command_name):
-        warn('Management command "%s" is not available' % command_name)
-    else:
-        run('python manage.py ' + command)
-
-@task
-def migrate(params='', do_backup=True):
-    """ Runs migrate management command. Database backup is performed
-    before migrations until ``do_backup=False`` is passed. """
-    if do_backup:
-        database = db.get_backend()
-        backup_dir = env.conf['ENV_DIR'] + '/var/backups/before-migrate'
-        run('mkdir -p ' + backup_dir)
-        database.dump(backup_dir)
-    manage('migrate --noinput %s' % params)
-
-@task
-def syncdb(params=''):
-    """ Runs syncdb management command. """
-    manage('syncdb --noinput %s' % params)
-
-@task
-def compress(params=''):
-    with settings(warn_only=True):
-        manage('synccompress %s' % params)
-
-@task
-def collectstatic(params=''):
-    manage('collectstatic --noinput %s' % params)
-
-@task
-@utils.inside_project
-def test(what=''):
-    """
-    Runs 'runtests.sh' script from project root.
-    Example runtests.sh content::
-
-        #!/bin/sh
-
-        default_tests='accounts forum firms blog'
-        if [ $# -eq 0 ]
-        then
-            ./manage.py test $default_tests --settings=test_settings
-        else
-            ./manage.py test $* --settings=test_settings
-        fi
-    """
-    with settings(warn_only=True):
-        run('./runtests.sh ' + what)
-
-#def coverage():
-#    pass
-#    with cd(env.conf['SRC_DIR']):
-#        run('source %s/bin/activate; ./bin/runcoverage.sh' % env.conf['ENV_DIR'])
-