def sync_db(role):
    """
    Downloads the latest remote (live or dev) database backup and loads it on your local
    machine.
    """
    remote_project_path = fb_env.role(role, 'project_path')
    remote_virtualenv_path = fb_env.role(role, 'virtualenv_path')
    remote_backups_dir = fb_env.role(role, 'backups_dir')

    local('python manage.py backupdb')

    with cd(remote_project_path):
        with virtualenv(remote_virtualenv_path):
            run('python manage.py backupdb --backup-name=sync --pg-dump-options="--no-owner --no-privileges"')

            # Download
            get(
                '{remote_backups_dir}/*-sync.*.gz'.format(
                    remote_backups_dir=remote_backups_dir,
                ),
                './{local_backups_dir}/'.format(
                    local_backups_dir=fb_env.local_backups_dir,
                ),
            )

    local('python manage.py restoredb --backup-name=sync')
def shell():
    """
    Fires up a shell on the live server.
    """
    with cd(fb_env.live_project_path):
        with virtualenv(fb_env.live_virtualenv_path):
            run('bash -')
def stage(pip=False, migrate=False, syncdb=False, branch=None, role='dev'):
    """
    Updates the remote site files to your local branch head, collects static
    files, migrates, and installs pip requirements if necessary.
    """
    update_function = get_update_function()
    branch = branch or get_git_branch()

    project_name = fb_env.role(role, 'project_name')
    project_path = fb_env.role(role, 'project_path')
    virtualenv_path = fb_env.role(role, 'virtualenv_path')
    restart_cmd = fb_env.role(role, 'restart_cmd')

    with cd(project_path):
        previous_head = update_function(branch)
        puts('Previous remote HEAD: {0}'.format(previous_head))

        update_pip = pip or files_changed(previous_head, 'requirements.txt')
        migrate = migrate or files_changed(previous_head, '*/migrations/* {project_name}/settings.py requirements.txt'.format(project_name=project_name))
        syncdb = syncdb or files_changed(previous_head, '*/settings.py')

        with virtualenv(virtualenv_path):
            if update_pip:
                run('pip install -r ./requirements.txt')

            if syncdb:
                run('python manage.py syncdb')

            if migrate:
                run('python manage.py backupdb')
                run('python manage.py migrate')

            run('python manage.py collectstatic --noinput')

        run(restart_cmd)
        def new_fn(*args, **kwargs):
            retval = old_fn(*args, **kwargs)

            project_path = fb_env.role(role, 'project_path')
            virtualenv_path = fb_env.role(role, 'virtualenv_path')

            with cd(project_path):
                # Use the venv so we have the right python version
                with virtualenv(virtualenv_path):
                    obfuscate()

            return retval
    def test_virtualenv_runs_commands_with_the_prefix_contextmanager(self):
        with patch('fusionbox.fabric.utils.prefix') as mock_prefix:
            with virtualenv('/var/virtualenvs/test'):
                pass

        mock_prefix.assert_called_with('source /var/virtualenvs/test/bin/activate')