~deryck/credenza/do-more-with-users

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
fabfile for lpkanban.
"""

import os
import time

import bzrlib.branch

from fabric.api import *

# Connection variables
env.hosts = [''] # Add host name here.  Setup in /etc/hosts
env.user = '' # The user you wish to connect to env.host with.

# Project state
env.project = 'lpkanban' # The project name, i.e. "devurandom" or "launchpad"

# Functions for building filenames and paths.
def _get_revno():
    """Return the number of the latest revision."""
    branch, unused = bzrlib.branch.Branch.open_containing('.')
    return branch.last_revision_info()[0]

def _get_build_name():
    """Return a string useful for creating file names.

    For example, "devurandom-trunk-25".

    """
    return '%s-%s-r%s' % (
        env.project, os.getcwd().split('/')[-1], _get_revno())

def _get_tar_name():
    """Return the name of a tarball using _get_build_name."""
    return '%s.tar.gz' % _get_build_name()

# Remote paths.
env.build_dir = '' # The directory to unpack tarballs on env.host.
env.build_path = '%s/%s' % (env.build_dir, _get_build_name())
env.deploy_dir = '' # The directory on env.host to copy built code to.

def clean():
    """Clean up after a local build."""
    local('rm -rf bin develop-eggs downloads eggs include lib parts '
          '.installed.cfg .Python openid src/lpkanban.egg-info')
    local('find . -name \*.pyc|xargs rm')

def clean_local_builds():
    """Clean up the temp build directory."""
    local('rm -rf ../build/', capture=False)

def clean_remote_builds():
    """Clean up the remote build directory."""
    revno = _get_revno()
    prev_revno = revno - 1
    with cd(env.build_dir):
        run("""
            for i in `ls`;do
                if [[ $i != *%d* ]] && [[ $i != *%d* ]]; then
                    rm -rf $i
                fi;
            done
        """ % (revno, prev_revno))

def init():
    """Initialize virtualenv and bootstap."""
    if not os.path.isdir('bin'):
        local('virtualenv --no-site-packages .', capture=False)
        local('./bin/easy_install -U setuptools')
        local('./bin/python bootstrap.py', capture=False)

def devel():
    """Create a self-contained development build."""
    init()
    local('./bin/buildout', capture=False)

def pack():
    """Create a build for deployment."""
    clean()
    if not os.path.isdir('../build'):
        local('mkdir ../build')
    tar_command = 'tar czvf ../build/%s .' % _get_tar_name()
    local(tar_command, capture=False)

def push():
    """Push the build to the remote build directory."""
    put('../build/%s' % _get_tar_name(), env.build_dir)

def unpack():
    """Unpack the build in the remote build directory."""
    with cd(env.build_dir):
        run('mkdir %s' % _get_build_name())
        run('tar xzvf %s -C %s' % (
            _get_tar_name(), _get_build_name()))

def buildout():
    """Run buildout to make the remote site."""
    with cd(env.build_path):
        run('virtualenv --no-site-packages .')
        run('./bin/python bootstrap.py')
        run('./bin/buildout install django-deploy')

def link_code():
    """Link the built code for Apache to find it."""
    with cd(env.deploy_dir):
        run('rm -f %s' % env.project)
        run('ln -s %s %s' % (env.build_path, env.project))

def graceful():
    """Do a graceful Apache restart."""
    run('apache2ctl graceful')

def restart():
    """Force a hard Apache restart."""
    run('apache2ctl stop')
    time.sleep(1)
    run('apache2ctl start')

def stage():
    """Build but don't run the current branch on the remote server."""
    pack()
    push()
    unpack()
    buildout()

def deploy():
    """Deploy the current branch to the remote server."""
    pack()
    push()
    unpack()
    buildout()
    link_code()
    graceful()