from __future__ import with_statement
from fabdefs import *
from fabric.api import *
from contextlib import contextmanager


@contextmanager
def virtualenv():
    with cd(env.project_dir):
        with prefix(env.activate):
            yield


def upload_db():
    # tar the database file
    local('tar -czf tmp.tar.gz instance/tmp.db', capture=False)
    put('tmp.tar.gz', '/tmp/tmp.tar.gz')

    # enter application directory
    with cd(env.project_dir):
        # and unzip new files
        sudo('tar xzf /tmp/tmp.tar.gz')

    # now that all is set up, delete the tarballs again
    sudo('rm /tmp/tmp.tar.gz')
    local('rm tmp.tar.gz')

    set_permissions()
    restart()
    return


def download_db():
    tmp = get('%s/instance/tmp.db' % env.project_dir, '/tmp/tmp.db')
    if tmp.succeeded:
        print "Success"
        local('mv /tmp/tmp.db instance/tmp.db')
    return

def rebuild_db():
    sudo("supervisorctl stop pmg_cms")
    with virtualenv():
        with shell_env(FLASK_ENV='production'):
            sudo('python %s/rebuild_db.py' % env.project_dir)
    sudo("supervisorctl start pmg_cms")

def copy_db():
    local("pg_dump -dpmg -Upmg --clean --no-owner --no-privileges > pmg.sql")
    local("tar cvzf pmg.sql.tar.gz pmg.sql")
    put('pmg.sql.tar.gz', '/tmp/pmg.sql.tar.gz')
    sudo('tar xvzf /tmp/pmg.sql.tar.gz')
    sudo('psql -dpmg -f pmg.sql', user="postgres")
    local('rm pmg.sql')
    # local('rm pmg.sql.tar.gz')
    sudo('rm /tmp/pmg.sql.tar.gz')
    sudo('rm pmg.sql')


def setup_db():
    sudo("createuser pmg -S -D -R -e", user="postgres")
    sudo("createdb --locale=en_US.utf-8 -E utf-8 -O pmg pmg -T template0", user="postgres")
    rebuild_db()
    # sudo('echo "grant all privileges on database pmg to pmg;" | psql')


def restart():
    sudo("supervisorctl restart pmg_cms")
    sudo("supervisorctl restart pmg_frontend")
    sudo('service nginx restart')
    return


def set_permissions():
    """
     Ensure that www-data has access to the application folder
    """

    sudo('chown -R www-data:www-data ' + env.project_dir)
    return



def setup():

    sudo('apt-get update')

    # install packages
    sudo('apt-get -y install git')
    sudo('apt-get -y install build-essential python-dev sqlite3 libsqlite3-dev libpq-dev')
    sudo('apt-get -y install python-pip supervisor')
    sudo('pip install virtualenv')

    # create application directory if it doesn't exist yet
    with settings(warn_only=True):
        if run("test -d %s" % env.project_dir).failed:
            sudo('git clone https://github@github.com/code4sa/pmg-cms-2.git %s' % env.project_dir)
            sudo('mkdir %s/instance' % env.project_dir)
        if run("test -d %s/env" % env.project_dir).failed:
            # create virtualenv
            sudo('virtualenv --no-site-packages %s/env' % env.project_dir)

    # install nginx
    sudo('apt-get -y install nginx')
    # restart nginx after reboot
    sudo('update-rc.d nginx defaults')
    with settings(warn_only=True):
        sudo('rm /etc/nginx/sites-enabled/default')
    with settings(warn_only=True):
        sudo('service nginx start')

    # Database setup
    sudo('apt-get -y install postgresql')
    return


def deploy():
    # push any local changes to github
    local('git push origin ' + env.git_branch)

    with settings(warn_only=True):
        sudo('service nginx stop')

    # enter application directory and pull latest code from github
    with cd(env.project_dir):
        # ensure we are on the target branch
        sudo('git checkout ' + env.git_branch)
        # move db out of the way
        # with settings(warn_only=True):
            # sudo('mv instance/tmp.db /tmp/tmp.db')
        # first, discard local changes, then pull
        with settings(warn_only=True):
            # sudo('git stash')
            sudo('git reset --hard')
        sudo('git pull origin ' + env.git_branch)
        # put db back
        # sudo('mv /tmp/tmp.db instance/tmp.db')
        # sudo('git stash pop')

    # install dependencies
    with virtualenv():
        sudo('pip install -r %s/requirements/production.txt' % env.project_dir)

    with cd(env.project_dir):
        # nginx
        sudo('ln -sf ' + env.config_dir + '/nginx.conf /etc/nginx/sites-enabled/pmg.org.za')
        sudo('service nginx reload')

        # move supervisor config
        sudo('ln -sf ' + env.config_dir + '/supervisor.conf /etc/supervisor/conf.d/supervisor_pmg.conf')
        sudo('supervisorctl reread')
        sudo('supervisorctl update')

    set_permissions()
    restart()
    return
@@ -241,652 +241,8 @@
 d%0A%0A%0A
-def upload_db():%0A    # tar the database file%0A    local('tar -czf tmp.tar.gz instance/tmp.db', capture=False)%0A    put('tmp.tar.gz', '/tmp/tmp.tar.gz')%0A%0A    # enter application directory%0A    with cd(env.project_dir):%0A        # and unzip new files%0A        sudo('tar xzf /tmp/tmp.tar.gz')%0A%0A    # now that all is set up, delete the tarballs again%0A    sudo('rm /tmp/tmp.tar.gz')%0A    local('rm tmp.tar.gz')%0A%0A    set_permissions()%0A    restart()%0A    return%0A%0A%0Adef download_db():%0A    tmp = get('%25s/instance/tmp.db' %25 env.project_dir, '/tmp/tmp.db')%0A    if tmp.succeeded:%0A        print %22Success%22%0A        local('mv /tmp/tmp.db instance/tmp.db')%0A    return%0A%0A
 def 

Remove fab functions for handling sqlite db.