Merge branch 'master' of github.com:FinalsClub/karmaworld
[oweals/karmaworld.git] / fabfile.py
1 """Management utilities."""
2
3 import os
4 from contextlib import contextmanager as _contextmanager
5
6 from fabric.contrib.console import confirm
7 from fabric.api import abort, cd, env, local, prefix, run, settings, task
8
9
10 ########## GLOBALS
11 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
12 env.virtualenv = 'venv-kw'
13 env.activate = 'workon %s' % env.virtualenv
14 env.run = './manage.py'
15 ########## END GLOBALS
16
17
18 ########## HELPERS
19 def cont(cmd, message):
20     """Given a command, ``cmd``, and a message, ``message``, allow a user to
21     either continue or break execution if errors occur while executing ``cmd``.
22
23     :param str cmd: The command to execute on the local system.
24     :param str message: The message to display to the user on failure.
25
26     .. note::
27         ``message`` should be phrased in the form of a question, as if ``cmd``'s
28         execution fails, we'll ask the user to press 'y' or 'n' to continue or
29         cancel exeuction, respectively.
30
31     Usage::
32
33         cont('heroku run ...', "Couldn't complete %s. Continue anyway?" % cmd)
34     """
35     with settings(warn_only=True):
36         result = local(cmd, capture=True)
37
38     if message and result.failed and not confirm(message):
39         abort('Stopped execution per user request.')
40
41
42 @_contextmanager
43 def _virtualenv():
44     """
45     Changes to the proj_dir and activates the virtualenv
46     """
47     with cd(env.proj_dir):
48         with prefix(env.activate):
49             yield
50
51 ########## END HELPERS
52
53 ########## ENVIRONMENTS
54 def beta():
55     """
56     Beta connection information
57     """
58     env.user = 'djkarma'
59     env.hosts = ['beta.karmanotes.org']
60     env.proj_root = '/var/www/karmaworld'
61     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
62
63
64 def prod():
65     """
66     Production connection information
67     """
68     env.user = 'djkarma'
69     env.hosts = ['karmanotes.org']
70     env.proj_root = '/var/www/karmaworld'
71     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
72 ########## END ENVIRONMENTS
73
74
75 ########## DATABASE MANAGEMENT
76 @task
77 def syncdb():
78     """Run a syncdb."""
79     local('%(run)s syncdb --noinput' % env)
80
81
82 @task
83 def migrate(app=None):
84     """Apply one (or more) migrations. If no app is specified, fabric will
85     attempt to run a site-wide migration.
86
87     :param str app: Django app name to migrate.
88     """
89     if app:
90         local('%s migrate %s --noinput' % (env.run, app))
91     else:
92         local('%(run)s migrate --noinput' % env)
93 ########## END DATABASE MANAGEMENT
94
95
96 ########## FILE MANAGEMENT
97 @task
98 def collectstatic():
99     """Collect all static files, and copy them to S3 for production usage."""
100     local('%(run)s collectstatic --noinput' % env)
101 ########## END FILE MANAGEMENT
102
103
104 ########## COMMANDS
105
106 def make_virtualenv():
107     """
108     Creates a virtualenv on the remote host
109     """
110     run('mkvirtualenv %s' % env.virtualenv)
111
112
113 def update_reqs():
114     """
115     Makes sure all packages listed in requirements are installed
116     """
117     with _virtualenv():
118         with cd(env.proj_dir):
119             run('pip install -r requirements/production.pip')
120
121
122 def clone():
123     """
124     Clones the project from the central repository
125     """
126     run('git clone %s %s' % (env.proj_repo, env.proj_dir))
127
128
129 def update_code():
130     """
131     Pulls the latest changes from the central repository
132     """
133     with cd(env.proj_dir):
134         run('git pull')
135
136
137 def deploy():
138     """
139     Creates or updates the project, runs migrations, installs dependencies.
140     """
141     first_deploy = False
142     with settings(warn_only=True):
143         if run('test -d %s' % env.proj_dir).failed:
144             # first_deploy var is for initial deploy information
145             first_deploy = True
146             clone()
147         if run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
148             make_virtualenv()
149
150     update_code()
151     update_reqs()
152     syncdb()
153     #TODO: run gunicorn
154     #restart_uwsgi()
155 ########## END COMMANDS