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