1 """Management utilities."""
4 from contextlib import contextmanager as _contextmanager
6 from fabric.contrib.console import confirm
7 from fabric.api import abort, cd, env, local, prefix, run, settings, task
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
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``.
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.
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.
32 cont('heroku run ...', "Couldn't complete %s. Continue anyway?" % cmd)
34 with settings(warn_only=True):
35 result = local(cmd, capture=True)
37 if message and result.failed and not confirm(message):
38 abort('Stopped execution per user request.')
44 Changes to the proj_dir and activates the virtualenv
46 with cd(env.proj_dir):
47 with prefix(env.activate):
50 ########## END HELPERS
52 ########## ENVIRONMENTS
55 Beta connection information
58 env.hosts = ['beta.karmanotes.org']
59 env.proj_root = '/var/www/karmaworld'
60 env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
65 Production connection information
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
74 ########## DATABASE MANAGEMENT
78 local('%(run)s syncdb --noinput' % env)
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.
86 :param str app: Django app name to migrate.
89 local('%s migrate %s --noinput' % (env.run, app))
91 local('%(run)s migrate --noinput' % env)
92 ########## END DATABASE MANAGEMENT
95 ########## FILE MANAGEMENT
98 """Collect all static files, and copy them to S3 for production usage."""
99 local('%(run)s collectstatic --noinput' % env)
100 ########## END FILE MANAGEMENT
105 def make_virtualenv():
107 Creates a virtualenv on the remote host
109 run('mkvirtualenv %s' % env.virtualenv)
114 Makes sure all packages listed in requirements are installed
117 with cd(env.proj_dir):
118 run('pip install -r requirements/production.pip')
123 Clones the project from the central repository
125 run('git clone %s %s' % (env.proj_repo, env.proj_dir))
130 Pulls the latest changes from the central repository
132 with cd(env.proj_dir):
138 Creates or updates the project, runs migrations, installs dependencies.
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
146 if run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
154 ########## END COMMANDS