from fabric.api import roles, env, run, cd
from fabric.contrib.files import upload_template

env.hosts = [
    'ubuntu@nutsurv-dev.eocng.org',
    'ubuntu@nutsurv-staging.eocng.org',
    'ubuntu@nutsurv.eocng.org',
    'ubuntu@bigbuilder.eocng.org',
]

env.roledefs = {
    'dev': ['ubuntu@nutsurv-dev.eocng.org', ],
    'staging': ['ubuntu@nutsurv-staging.eocng.org'],
    'production': ['ubuntu@nutsurv.eocng.org'],
    'build': ['ubuntu@bigbuilder.eocng.org'],
}


def echo():
    run('ls /')


@roles('build')
def build_dockerimage(branch_or_tag='develop'):

    with cd('~/build_setup/'):
        run('./update_env.sh')

    with cd('/home/ubuntu/build_setup/dockersetup/projects/nutsurv'):
        run('BRANCH={} ./run_build.sh'.format(branch_or_tag))
        run('docker push docker-registry.eocng.org/ehealthafrica/nutsurv:{}'.format(
            branch_or_tag))

@roles('dev', 'staging', 'production')
def deploy(branch_or_tag=None):

    if env.host_string == 'ubuntu@nutsurv-dev.eocng.org':
        if branch_or_tag is None:
            branch_or_tag = 'develop'

    else:
        assert branch_or_tag, "Please specify a tag"
        assert branch_or_tag.startswith('v'), "You can only deploy tags to {}".format(env.host_string)

    run('docker pull docker-registry.eocng.org/ehealthafrica/nutsurv:{}'.format(
        branch_or_tag))

    stop_result = run('docker stop nutsurvdeploy_web_1', warn_only=True)

    if (stop_result
            and (stop_result != 'nutsurvdeploy_web_1')
            and ('no such' not in stop_result.lower())):
        assert False, stop_result

    upload_template(
        'docker-compose-deploy.yml.template',
        '~/nutsurv_deploy/docker-compose-deploy.yml',
        context={'branch_or_tag': branch_or_tag, }
    )


@roles('dev', 'staging', 'production')
def up():
    with cd('~/nutsurv_deploy'):
        run('docker-compose -f ~/nutsurv_deploy/docker-compose-deploy.yml up -d web')

@roles('dev', 'staging', 'production')
def migrate():
    with cd('~/nutsurv_deploy'):
        run('docker-compose -f ~/nutsurv_deploy/docker-compose-deploy.yml run web python /opt/nutsurv/nutsurv/manage.py migrate')
        run('docker-compose -f ~/nutsurv_deploy/docker-compose-deploy.yml run web python /opt/nutsurv/nutsurv/manage.py createcachetable')


@@ -35,16 +35,22 @@
  run, cd
+, sudo
 %0Afrom fa
@@ -496,16 +496,108 @@
 ls /')%0A%0A
+def ensure_docker_compose():%0A    sudo('which docker-compose %7C%7C pip install docker-compose')%0A
 %0A@roles(
@@ -1031,16 +1031,30 @@
 tag=None
+, do_stop=True
 ):%0A%0A    
@@ -1453,16 +1453,44 @@
 _tag))%0A%0A
+    if do_stop is True:%0A    
     stop
@@ -1555,16 +1555,20 @@
 =True)%0A%0A
+    
     if (
@@ -1587,24 +1587,28 @@
             
+  
+  
 and (stop_re
@@ -1638,16 +1638,20 @@
 web_1')%0A
+    
         
@@ -1703,24 +1703,28 @@
 )):%0A        
+   
+ 
 assert False
@@ -1738,16 +1738,108 @@
 result%0A%0A
+    run('test -e ~/nutsurv_deploy/ %7C%7C mkdir ~/nutsurv_deploy/')%0A    ensure_docker_compose()%0A
     uplo
@@ -2058,16 +2058,200 @@
 ef up():
+%0A%0A    run('test -e ~/nutsurv_deploy/configuration.py')%0A    run('test -e ~/nutsurv_deploy/certs/cert.pem')%0A    run('test -e ~/nutsurv_deploy/certs/key.pem')%0A%0A    ensure_docker_compose()
 %0A    wit
@@ -2413,24 +2413,52 @@
  migrate():%0A
+    ensure_docker_compose()%0A
     with cd(

Improve deployment script