Removed the env.run variable, because it's not needed.
[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
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 ########## END GLOBALS
14
15
16 ########## HELPERS
17 @_contextmanager
18 def _virtualenv():
19     """
20     Changes to the proj_dir and activates the virtualenv
21     """
22     with cd(env.proj_dir):
23         with prefix(env.activate):
24             yield
25
26 ########## END HELPERS
27
28 ########## ENVIRONMENTS
29 @task
30 def beta():
31     """
32     Beta connection information
33     """
34     env.user = 'djkarma'
35     env.hosts = ['beta.karmanotes.org']
36     env.proj_root = '/var/www/karmaworld'
37     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
38
39
40 @task
41 def prod():
42     """
43     Production connection information
44     """
45     env.user = 'djkarma'
46     env.hosts = ['karmanotes.org']
47     env.proj_root = '/var/www/karmaworld'
48     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
49 ########## END ENVIRONMENTS
50
51
52 ########## DATABASE MANAGEMENT
53 @task
54 def syncdb():
55     """Runs syncdb (along with any pending South migrations)"""
56     run('python manage.py syncdb --noinput --migrate')
57 ########## END DATABASE MANAGEMENT
58
59
60 ########## FILE MANAGEMENT
61 @task
62 def collectstatic():
63     """Collect all static files, and copy them to S3 for production usage."""
64     run('python manage.py collectstatic --noinput')
65 ########## END FILE MANAGEMENT
66
67
68 ########## COMMANDS
69
70 @task
71 def make_virtualenv():
72     """
73     Creates a virtualenv on the remote host
74     """
75     run('mkvirtualenv %s' % env.virtualenv)
76
77
78 @task
79 def update_reqs():
80     """
81     Makes sure all packages listed in requirements are installed
82     """
83     with _virtualenv():
84         with cd(env.proj_dir):
85             run('pip install -r requirements/production.pip')
86
87
88 @task
89 def clone():
90     """
91     Clones the project from the central repository
92     """
93     run('git clone %s %s' % (env.proj_repo, env.proj_dir))
94
95
96 @task
97 def update_code():
98     """
99     Pulls the latest changes from the central repository
100     """
101     with cd(env.proj_dir):
102         run('git pull')
103
104
105 @task
106 def deploy():
107     """
108     Creates or updates the project, runs migrations, installs dependencies.
109     """
110     first_deploy = False
111     with settings(warn_only=True):
112         if run('test -d %s' % env.proj_dir).failed:
113             # first_deploy var is for initial deploy information
114             first_deploy = True
115             clone()
116         if run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
117             make_virtualenv()
118
119     update_code()
120     update_reqs()
121     syncdb()
122     #TODO: run gunicorn
123     #restart_uwsgi()
124 ########## END COMMANDS