Also specifying the branch in the environment-related tasks.
[oweals/karmaworld.git] / fabfile.py
1 """Management utilities."""
2
3 import os
4 from contextlib import contextmanager as _contextmanager
5
6 from fabric.api import cd, env, prefix, run, settings, task, local
7
8
9 ########## GLOBALS
10 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
11 env.virtualenv = 'venv-kw'
12 env.activate = 'workon %s' % env.virtualenv
13
14 # Using this env var to be able to specify the function
15 # used to run the commands. By default it's `run`, which
16 # runs commands remotely, but in the `here` task we set
17 # env.run to `local` to run commands locally.
18 env.run = run
19 ########## END GLOBALS
20
21
22 ########## HELPERS
23 @_contextmanager
24 def _virtualenv():
25     """
26     Changes to the proj_dir and activates the virtualenv
27     """
28     with cd(env.proj_dir):
29         with prefix(env.activate):
30             yield
31
32 ########## END HELPERS
33
34 ########## ENVIRONMENTS
35 @task
36 def here():
37     """
38     Connection information for the local machine
39     """
40     env.proj_dir = os.getcwd()
41     env.proj_root = os.path.dirname(env.proj_dir)
42     env.run = local
43     env.reqs = 'reqs/dev.txt'
44     env.confs = 'confs/dev/'
45     env.branch = 'master'
46
47
48 @task
49 def beta():
50     """
51     Beta connection information
52     """
53     env.user = 'djkarma'
54     env.hosts = ['beta.karmanotes.org']
55     env.proj_root = '/var/www/karmaworld'
56     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
57     env.reqs = 'reqs/prod.txt'
58     env.confs = 'confs/beta/'
59     env.branch = 'beta'
60
61
62 @task
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     env.reqs = 'reqs/prod.txt'
72     env.confs = 'confs/prod/'
73     env.branch = 'master'
74 ########## END ENVIRONMENTS
75
76
77 ########## DATABASE MANAGEMENT
78 @task
79 def syncdb():
80     """Runs syncdb (along with any pending South migrations)"""
81     env.run('python manage.py syncdb --noinput --migrate')
82 ########## END DATABASE MANAGEMENT
83
84
85 ########## FILE MANAGEMENT
86 @task
87 def manage_static():
88     collect_static()
89     compress_static()
90     upload_static()
91
92
93 @task
94 def collect_static():
95     """Collect all static files, and copy them to S3 for production usage."""
96     env.run('python manage.py collectstatic --noinput')
97
98
99 @task
100 def compress_static():
101     """
102     Compresses the static files.
103     """
104     pass
105
106
107 @task
108 def upload_static():
109     """
110     Uploads the static files to the specified host.
111     """
112     pass
113 ########## END FILE MANAGEMENT
114
115
116 ########## COMMANDS
117
118 @task
119 def make_virtualenv():
120     """
121     Creates a virtualenv on the remote host
122     """
123     env.run('mkvirtualenv %s' % env.virtualenv)
124
125
126 @task
127 def update_reqs():
128     """
129     Makes sure all packages listed in requirements are installed
130     """
131     with _virtualenv():
132         with cd(env.proj_dir):
133             env.run('pip install -r %s' % env.reqs)
134
135
136 @task
137 def clone():
138     """
139     Clones the project from the central repository
140     """
141     env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
142
143
144 @task
145 def update_code():
146     """
147     Pulls the latest changes from the central repository
148     """
149     with cd(env.proj_dir):
150         env.run('git pull')
151
152
153 @task
154 def deploy():
155     """
156     Creates or updates the project, runs migrations, installs dependencies.
157     """
158     first_deploy = False
159     with settings(warn_only=True):
160         if env.run('test -d %s' % env.proj_dir).failed:
161             # first_deploy var is for initial deploy information
162             first_deploy = True
163             clone()
164         if env.run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
165             make_virtualenv()
166
167     update_code()
168     update_reqs()
169     syncdb()
170     #TODO: run gunicorn
171     #restart_uwsgi()
172 ########## END COMMANDS