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 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 stage(branch=None, role='dev'):
    """
    Updates the remote site files to your local branch head.
    """
    update_function = get_update_function()
    branch = branch or get_git_branch()

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

    with cd(project_path):
        previous_head = update_function(branch)
        puts('Previous remote HEAD: {0}'.format(previous_head))
def sync_media(role):
    """
    Synchronizes the latest remote (live or dev) media directory with your
    local media directory.
    """
    remote = env.roledefs[role][0]
    remote_media_path = fb_env.role(role, 'media_path') + '/'

    # Rsync has weird syntax for the target directory
    local_media_dir = './' + fb_env.local_media_dir

    local('rsync -avz --progress {remote}:{remote_media_path} {local_media_dir}'.format(
        remote=remote,
        remote_media_path=remote_media_path,
        local_media_dir=local_media_dir,
    ))