"""Deployment helpers. """ from os.path import join as join_path from fabric.api import * from fabric.contrib import files def ve_run(cmd): """Runs a command inside the activated virtualenv. """ require('root', provided_by=['setups.stage', 'setups.production']) with prefix('source %(root)s/bin/activate' % env): run(cmd) def manage(cmd): """Runs a command with manage.py. """ require('manage_py', provided_by=['setups.stage', 'setups.production']) ve_run('%s %s' % (env.manage_py, cmd)) def update_project(): """Updates the project source. """ require('src_root', provided_by=['setups.stage', 'setups.production']) require('git_branch', provided_by=['setups.stage', 'setups.production']) if files.exists(env.src_root): with cd(env.src_root): run('git pull origin %(git_branch)s' % env) else: run('git clone -b %(git_branch)s %(git_url)s %(src_root)s' % env) def link_settings(): """Links the appropriate settings. """ require('stage', provided_by=['setups.stage', 'setups.production']) require('config', provided_by=['setups.stage', 'setups.production']) require('proj_root', provided_by=['setups.stage', 'setups.production']) if env.stage: host_settings = join_path(env.config, '%(host)s_stage.py' % env) else: host_settings = join_path(env.config, '%(host)s.py' % env) settings = join_path(env.proj_root, 'local_settings.py') if files.exists(host_settings): run('ln -sf %s %s' % (host_settings, settings)) else: abort('No host specific settings file found. Create one at %s' % host_settings) def collect_static(): """Collects all static files. """ manage('collectstatic --noinput') @task def syncdb(): """Runs the syncdb management command. """ manage('syncdb --noinput') @task def migrate(app_name=None): """Runs the migrations for one or all apps. """ if app_name is None: manage('migrate') else: manage('migrate %s' % app_name) @task(alias='id') def identify(): """Identifes the deployed project. Displays the deployed project version, all Python packages installed inside the virtualenv and the state of the migrations. """ require('proj_root', provided_by=['setups.stage', 'setups.production']) with cd(env.proj_root): run('git log -1') ve_run('pip freeze') manage('migrate -list') @task(alias='install') def install_requirements(): """Installs the requirements. """ require('pip_file', provided_by=['setups.stage', 'setups.production']) ve_run('pip install -r %(pip_file)s' % env) @task(alias='load') def load_fixture(fixture_path): """Loads one or more fixtures.""" manage('loaddata %s' % fixture_path)