Skip to content

Instantly share code, notes, and snippets.

@tobiase
Forked from saevarom/fabfile.py
Created January 23, 2018 18:37
Show Gist options
  • Save tobiase/b9f462f0e6e65ee7d12e90bce71557c1 to your computer and use it in GitHub Desktop.
Save tobiase/b9f462f0e6e65ee7d12e90bce71557c1 to your computer and use it in GitHub Desktop.

Revisions

  1. @saevarom saevarom created this gist Jan 23, 2018.
    97 changes: 97 additions & 0 deletions fabfile.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    from __future__ import with_statement
    from fabric.api import *
    from fabric.operations import get
    from fabric.contrib.files import exists
    from contextlib import contextmanager
    import os


    env.roledefs = {
    'web': ['server1.example.com', 'server2.example.com'],
    'db': ['server1.example.com'],
    }


    env.project_name = 'project_name'
    env.supervisor_app_name = 'project_name'
    env.git_url = '[email protected]:example/project_name.git'
    env.user = 'ubuntu'
    env.apps_path = '/home/ubuntu/'
    env.directory = '/home/ubuntu/project_name'
    env.activate = 'source /home/ubuntu/.virtualenvs/project_name/bin/activate'
    env.do_collectstatic = False
    env.django_settings_module = 'project_name.settings.prod'


    @contextmanager
    @roles('web')
    def virtualenv():
    with settings(sudo_user=env.user):
    with cd(env.directory):
    with prefix(env.activate):
    puts('Activated virtualenv')
    yield

    @roles('web')
    def with_static():
    env.do_collectstatic = True

    @roles('web')
    def uptime():
    with virtualenv():
    run('uptime')

    @roles('web')
    def git_pull():
    with virtualenv():
    run('git pull')

    @roles('web')
    def pip_install():
    with virtualenv():
    run('pip install -r requirements.txt')

    @roles('db')
    def syncdb():
    with virtualenv():
    run('python manage.py migrate')

    @roles('web')
    def collectstatic():
    with virtualenv():
    run('pwd')
    run('python manage.py collectstatic --noinput')

    @roles('web')
    def restart_app():
    sudo('supervisorctl restart %s' % env.supervisor_app_name)

    @roles('web')
    def nginx_reload():
    sudo('service nginx reload')


    @roles('db')
    def manage(arguments):
    with virtualenv():
    run('python manage.py %s' % arguments)


    @roles('web')
    def deploy():
    git_pull()
    pip_install()
    syncdb()
    if env.do_collectstatic:
    collectstatic()
    restart_app()

    @roles('db')
    def dump(arguments='latest.dump'):
    # this uses a management command called 'dbdump'
    # something similar to this package https://github.com/vitaliyf/django-dbdump
    # many more exist
    with virtualenv():
    run('python manage.py dbdump %s' % arguments)
    get('latest.dump', arguments)