summaryrefslogtreecommitdiff
path: root/fabfile/helpers.py
blob: ddeb7b845cc3042ab5a64a3ad06d5e3a51cfb839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Deployment helpers.
"""
from os.path import join as join_path

from fabric.api import *
from fabric.contrib import files


def ve_run(cmd):
    """Runs a command inside the activated virtualenv.
    """
    require('root', provided_by=['setups.stage', 'setups.production'])
    with prefix('source %(root)s/bin/activate' % env):
        run(cmd)


def manage(cmd):
    """Runs a command with manage.py.
    """
    require('manage_py', provided_by=['setups.stage', 'setups.production'])
    ve_run('%s %s' % (env.manage_py, cmd))


def update_project():
    """Updates the project source.
    """
    require('src_root', provided_by=['setups.stage', 'setups.production'])
    require('git_branch', provided_by=['setups.stage', 'setups.production'])

    if files.exists(env.src_root):
        with cd(env.src_root):
            run('git pull origin %(git_branch)s' % env)
    else:
        run('git clone -b %(git_branch)s %(git_url)s %(src_root)s' % env)


def link_settings():
    """Links the appropriate settings.
    """
    require('stage', provided_by=['setups.stage', 'setups.production'])
    require('config', provided_by=['setups.stage', 'setups.production'])
    require('proj_root', provided_by=['setups.stage', 'setups.production'])
    if env.stage:
        host_settings = join_path(env.config, '%(host)s_stage.py' % env)
    else:
        host_settings = join_path(env.config, '%(host)s.py' % env)
    settings = join_path(env.proj_root, 'local_settings.py')
    if files.exists(host_settings):
        run('ln -sf %s %s' % (host_settings, settings))
    else:
        abort('No host specific settings file found. Create one at %s' % host_settings)


def collect_static():
    """Collects all static files.
    """
    manage('collectstatic --noinput')


@task
def syncdb():
    """Runs the syncdb management command.
    """
    manage('syncdb --noinput')


@task
def migrate(app_name=None):
    """Runs the migrations for one or all apps.
    """
    if app_name is None:
        manage('migrate')
    else:
        manage('migrate %s' % app_name)


@task(alias='id')
def identify():
    """Identifes the deployed project.

    Displays the deployed project version, all Python packages installed inside
    the virtualenv and the state of the migrations.
    """
    require('proj_root', provided_by=['setups.stage', 'setups.production'])
    with cd(env.proj_root):
        run('git log -1')
    ve_run('pip freeze')
    manage('migrate -list')


@task(alias='install')
def install_requirements():
    """Installs the requirements.
    """
    require('pip_file', provided_by=['setups.stage', 'setups.production'])
    ve_run('pip install -r %(pip_file)s' % env)


@task(alias='load')
def load_fixture(fixture_path):
    """Loads one or more fixtures."""
    manage('loaddata %s' % fixture_path)