def conditional_symlink_current_release(deployed=False): """Swap the 'current' symlink to point to the new release if it doesn't point there already. Requires the env keys: pretty_release - set by make_pretty_release(), a commit identifier release_path - root target directory on the remote server """ current_version = None if exists(utils.absolute_release_path()): with settings(cd(utils.absolute_release_path()), warn_only=True): current_version = run('git describe') if (not exists(utils.absolute_release_path()) or deployed or current_version != env.pretty_release): _symlink_current_release(env.release_path)
def rollback(): """Swaps the deployed version of the app to the previous version. Requires the env keys: path - root deploy target for this app releases_root - subdirectory that stores the releases current_release_symlink - name of the symlink pointing to the currently deployed version Optional: crontab - relative path from the project root to a crontab to install deploy_user - user that should run the crontab """ require('path') require('releases_root') require('current_release_symlink') require('crontab') require('deploy_user') with cd(os.path.join(env.path, env.releases_root)): previous_link = deploy.release.alternative_release_path() conditional_rm(env.current_release_symlink) run('ln -fs %s %s' % (previous_link, env.current_release_symlink)) deploy.cron.conditional_install_crontab(utils.absolute_release_path(), env.crontab, env.deploy_user) restart_webserver()
def maintenancemode(): """If using the maintenancemode app (https://github.com/jezdez/django-maintenancemode), this command will toggle it on and off. It finds the `MAINTENANCE_MODE` variable in your `settings.py` on the remote server, toggles its value and restarts the web server. Requires the env keys: toggle - set by enable() or disable(), indicates whether we should turn maintenance mode on or off. settings - relative path from the project root to the settings.py file current_release_path - path to the current release on the remote server """ require('toggle', provided_by=[enable, disable]) require('settings') require('current_release_path') settings_file = os.path.join(utils.absolute_release_path(), env.settings) if exists(settings_file): sed(settings_file, '(MAINTENANCE_MODE = )(False|True)', '\\1%(toggle)s' % env) restart_webserver() else: warn('Settings file %s could not be found' % settings_file)
def alternative_release_path(): """Determine the release directory that is not currently in use. For example if the 'current' symlink points to the 'a' release directory, this method returns 'b'. Requires the env keys: release_paths - a tuple of length 2 with the release directory names (defaults to 'a' and 'b') """ if exists(utils.absolute_release_path()): current_release_path = run('readlink %s' % utils.absolute_release_path()) if os.path.basename(current_release_path) == env.release_paths[0]: alternative = env.release_paths[1] else: alternative = env.release_paths[0] return alternative else: return env.release_paths[0]
def virtualenv_run(command, path=None): path = path or absolute_release_path() with cd(path): run("%s/bin/python %s" % (env.virtualenv, command))
def shell(): env.release_path = absolute_release_path() django_manage_run('shell')