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 env.run = './manage.py'
15 ########## END GLOBALS
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``.
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.
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.
33 cont('heroku run ...', "Couldn't complete %s. Continue anyway?" % cmd)
35 with settings(warn_only=True):
36 result = local(cmd, capture=True)
38 if message and result.failed and not confirm(message):
39 abort('Stopped execution per user request.')
45 Changes to the proj_dir and activates the virtualenv
47 with cd(env.proj_dir):
48 with prefix(env.activate):
51 ########## END HELPERS
53 ########## ENVIRONMENTS
56 Beta connection information
59 env.hosts = ['beta.karmanotes.org']
60 env.proj_root = '/var/www/karmaworld'
61 env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
66 Production connection information
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
75 ########## DATABASE MANAGEMENT
79 local('%(run)s syncdb --noinput' % env)
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.
87 :param str app: Django app name to migrate.
90 local('%s migrate %s --noinput' % (env.run, app))
92 local('%(run)s migrate --noinput' % env)
93 ########## END DATABASE MANAGEMENT
96 ########## FILE MANAGEMENT
99 """Collect all static files, and copy them to S3 for production usage."""
100 local('%(run)s collectstatic --noinput' % env)
101 ########## END FILE MANAGEMENT
106 def make_virtualenv():
108 Creates a virtualenv on the remote host
110 run('mkvirtualenv %s' % env.virtualenv)
115 Makes sure all packages listed in requirements are installed
118 with cd(env.proj_dir):
119 run('pip install -r requirements/production.pip')
124 Clones the project from the central repository
126 run('git clone %s %s' % (env.proj_repo, env.proj_dir))
131 Pulls the latest changes from the central repository
133 with cd(env.proj_dir):
139 Creates or updates the project, runs migrations, installs dependencies.
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
147 if run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
155 ########## END COMMANDS