fabfile.py 1.88 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
'''Download/sync to git repository
Create/update virtualenv
django syncdb collectstatic'''

6 7
# Usage: fab prod:account=lacompagnieki deploy

8 9 10 11 12 13 14 15
import os
from fabric.api import env, local, run, settings, cd, lcd, sudo

name = 'flyers'
repository = 'http://git.domainepublic.net/git/%s.git' % name
directory = os.path.abspath(os.path.join(os.path.dirname(__file__), name))


16
def prod(account='chris'):
17 18 19 20
    env.user = 'chris'
    env.hosts = ['domainepublic.net']
    env.port = 3265
    global directory
21
    directory = '/var/alternc/html/%s/%s/flyers' % (account[0], account)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36


def deploy_remote():
    with settings(warn_only=True):
        if sudo("test -d %s" % directory).failed:
            sudo("git clone %s %s" % (repository, directory))
    with settings(warn_only=True), cd(directory):
        if sudo("test -d %s/env" % directory).failed:
            sudo('virtualenv env')
    run('echo "currently web folders are not owned by their owners"')
    with cd(directory):
        sudo('git pull')
        sudo('touch %s/wsgi.py' % name)
        sudo('mkdir -p media/uploads')
        sudo('env/bin/pip install -r requirements.txt')
Christophe Siraut's avatar
Christophe Siraut committed
37 38
        if sudo('test -f %s' % os.path.join(directory, 'db.sqlite3')).succeeded:
            sudo('env/bin/python manage.py syncdb --no-initial-data --noinput')
39
        else:
Christophe Siraut's avatar
Christophe Siraut committed
40 41
            sudo('env/bin/python manage.py syncdb --noinput')
        sudo('env/bin/python manage.py collectstatic --noinput')
42
        sudo('export gid=$(stat -c %g .); chown -R www-data:$gid .')
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


def deploy_local():
    with settings(warn_only=True):
        if local("test -d %s" % directory).failed:
            local("git clone %s %s" % (repository, directory))
    with lcd(directory):
        local('git pull')
        local('touch %s/wsgi.py' % name)


def deploy():
    if env.hosts:
        deploy_remote()
    else:
        deploy_local()


if __name__ == "__main__":
    deploy()