import os
import sys

from fabric.api import env, run, cd


DEFAULTS = {
    'home': '/home/wsgi/srv/',
    'repo_name': 'formhub',
}

DEPLOYMENTS = {
    'dev': {
        'home': '/home/ubuntu/src/',
        'host_string': 'ubuntu@dev.ona.io',
        'project': 'ona',
        'key_filename': os.path.expanduser('~/.ssh/ona.pem'),
        'virtualenv': '/home/ubuntu/.virtualenvs/ona',
        'celeryd': '/etc/init.d/celeryd-ona'
    },
    'prod': {
        'home': '/home/ubuntu/src/',
        'host_string': 'ubuntu@ona.io',
        'project': 'ona',
        'key_filename': os.path.expanduser('~/.ssh/ona.pem'),
        'virtualenv': '/home/ubuntu/.virtualenvs/ona',
        'celeryd': '/etc/init.d/celeryd-ona'
    },
    'kobocat': {
        'home': '/home/ubuntu/src/',
        'host_string':
        'ubuntu@ec2-54-200-151-185.us-west-2.compute.amazonaws.com',
        'project': 'kobocat',
        'key_filename': os.path.expanduser('~/.ssh/kobo01.pem'),
        'virtualenv': '/home/ubuntu/.virtualenvs/kobocat',
        'celeryd': '/etc/init.d/celeryd'
    },
}


def run_in_virtualenv(command):
    d = {
        'activate': os.path.join(
            env.virtualenv, 'bin', 'activate'),
        'command': command,
    }
    run('source %(activate)s && %(command)s' % d)


def check_key_filename(deployment_name):
    if 'key_filename' in DEPLOYMENTS[deployment_name] and \
       not os.path.exists(DEPLOYMENTS[deployment_name]['key_filename']):
        print "Cannot find required permissions file: %s" % \
            DEPLOYMENTS[deployment_name]['key_filename']
        return False
    return True


def setup_env(deployment_name):
    env.update(DEFAULTS)
    env.update(DEPLOYMENTS[deployment_name])
    if not check_key_filename(deployment_name):
        sys.exit(1)
    env.code_src = os.path.join(env.home, env.project)
    env.pip_requirements_file = os.path.join(env.code_src, 'requirements.pip')


def deploy(deployment_name, branch='master'):
    setup_env(deployment_name)
    with cd(env.code_src):
        run("git fetch origin")
        run("git checkout origin/%s" % branch)
        run("git submodule init")
        run("git submodule update")
        run('find . -name "*.pyc" -exec rm -rf {} \;')
    # numpy pip install from requirements file fails
    run_in_virtualenv("pip install numpy")
    run_in_virtualenv("pip install -r %s" % env.pip_requirements_file)
    with cd(env.code_src):
        run_in_virtualenv(
            "python manage.py syncdb --settings=formhub.local_settings")
        run_in_virtualenv(
            "python manage.py migrate --settings=formhub.local_settings")
        run_in_virtualenv(
            "python manage.py collectstatic --settings=formhub.local_settings "
            "--noinput")
    run("sudo %s restart" % env.celeryd)
    #run("sudo /etc/init.d/celerybeat-ona restart")
    run("sudo /usr/local/bin/uwsgi --reload /var/run/ona.pid")

@@ -42,96 +42,28 @@
 ort 
-env, run, cd%0A%0A%0ADEFAULTS = %7B%0A    'home': '/home/wsgi/srv/',%0A    'repo_name': 'formhub',%0A%7D
+cd, env, prefix, run
 %0A%0ADE
@@ -262,63 +262,8 @@
 '),%0A
-        'virtualenv': '/home/ubuntu/.virtualenvs/ona',%0A
     
@@ -956,210 +956,58 @@
 def 
-run_in_virtualenv(command):%0A    d = %7B%0A        'activate': os.path.join(%0A            env.virtualenv, 'bin', 'activate'),%0A        'command': command,%0A    %7D%0A    run('source %25(activate)s && %25(command)
+source(path):%0A    return prefix('source %25
 s' %25 
-d
+path
 )%0A%0A%0A
@@ -1374,33 +1374,8 @@
 e):%0A
-    env.update(DEFAULTS)%0A
     
@@ -1415,16 +1415,17 @@
 _name%5D)%0A
+%0A
     if n
@@ -1484,16 +1484,153 @@
 exit(1)%0A
+%0A    env.virtualenv = os.path.join('/home', 'ubuntu', '.virtualenvs',%0A                                  env.project, 'bin', 'activate')%0A%0A
     env.
@@ -2065,16 +2065,17 @@
 %7B%7D %5C;')%0A
+%0A
     # nu
@@ -2123,33 +2123,56 @@
 ils%0A    
-run_in_virtualenv
+with source(env.virtualenv):%0A        run
 (%22pip in
@@ -2189,33 +2189,23 @@
 y%22)%0A    
-run_in_virtualenv
+    run
 (%22pip in
@@ -2246,16 +2246,17 @@
 s_file)%0A
+%0A
     with
@@ -2274,39 +2274,48 @@
 e_src):%0A        
-run_in_
+with source(env.
 virtualenv(%0A    
@@ -2300,33 +2300,34 @@
 e(env.virtualenv
-(
+):
 %0A            %22py
@@ -2315,32 +2315,36 @@
 v):%0A            
+run(
 %22python manage.p
@@ -2400,39 +2400,16 @@
     
-run_in_virtualenv(%0A        
     
+run(
 %22pyt
@@ -2478,39 +2478,16 @@
     
-run_in_virtualenv(%0A        
     
+run(
 %22pyt
@@ -2513,16 +2513,35 @@
 ctstatic
+%22%0A                %22
  --setti
@@ -2571,23 +2571,8 @@
 ngs 
-%22%0A            %22
 --no
@@ -2575,24 +2575,25 @@
 --noinput%22)%0A
+%0A
     run(%22sud

use context to simplify run in virtual env