fabfile.py 4.93 KB
Newer Older
1
#!/bin/echo Usage: fab --list -f
Őry Máté committed
2 3 4
import contextlib
import datetime

Őry Máté committed
5
from fabric.api import env, run, settings, sudo, prefix, cd, execute
Őry Máté committed
6
from fabric.context_managers import shell_env
Őry Máté committed
7
from fabric.decorators import roles, parallel
Őry Máté committed
8 9


Őry Máté committed
10
env.roledefs['portal'] = ['localhost']
11 12

try:
13 14
    from vm.models import Node as _Node
    from storage.models import DataStore as _DataStore
15 16 17 18
except Exception as e:
    print e
else:
    env.roledefs['node'] = [unicode(n.host.ipv4)
19 20
                            for n in _Node.objects.filter(enabled=True)]
    env.roledefs['storage'] = [_DataStore.objects.get().hostname]
Őry Máté committed
21 22 23


def update_all():
Őry Máté committed
24
    "Update and restart portal+manager, nodes and storage"
Őry Máté committed
25
    execute(stop_portal)
Őry Máté committed
26
    execute(parallel(update_node))
Őry Máté committed
27
    execute(update_storage)
Őry Máté committed
28
    execute(update_portal)
Őry Máté committed
29 30


Őry Máté committed
31 32 33 34 35 36
def pip(env, req):
    "Install pip requirements"
    with _workon(env):
        run("pip install -r %s" % req)


Őry Máté committed
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
@roles('portal')
def migrate():
    "Run db migrations"
    with _workon("circle"), cd("~/circle/circle"):
        run("./manage.py migrate")


@roles('portal')
def compile_js():
    "Generate JS translation objects"
    with _workon("circle"), cd("~/circle/circle"):
        run("./manage.py compilejsi18n -o dashboard/static/jsi18n")


@roles('portal')
def collectstatic():
    "Collect static files"
    with _workon("circle"), cd("~/circle/circle"):
        run("./manage.py collectstatic --noinput")


@roles('portal')
def compile_messages():
    "Generate MO translation objects"
    with _workon("circle"), cd("~/circle/circle"):
        run("./manage.py compilemessages")


@roles('portal')
def compile_things():
    "Compile translation and collect static files"
    compile_js()
    collectstatic()
    compile_messages()


@roles('portal')
def make_messages():
    "Update PO translation templates and commit"
    with _workon("circle"), cd("~/circle/circle"):
        run("git status")
        run("./manage.py makemessages -d djangojs -a --ignore=jsi18n/*")
        run("./manage.py makemessages -d django -a")
        run("git commit -avm 'update PO templates'")


@roles('portal')
def test(test=""):
    "Run portal tests"
    with _workon("circle"), cd("~/circle/circle"):
87
        run("./manage.py test --settings=circle.settings.test " +
88 89
            "--exclude-dir=dashboard/tests/selenium %s" % test)

90

91 92 93 94
@roles('portal')
def selenium(test="", driver="firefox"):
    "Run portal selenium tests"
    with _workon("circle"), cd("~/circle/circle"):
95 96
        run("./manage.py test --settings=circle.settings.test " +
            " --with-selenium-driver --selenium-driver=" +
97
            "%s -w dashboard/tests/selenium %s" % (driver, test))
Őry Máté committed
98 99 100 101 102


def pull(dir="~/circle/circle"):
    "Pull from upstream branch (stash any changes)"
    now = unicode(datetime.datetime.now())
Őry Máté committed
103 104 105 106 107
    with cd(dir), shell_env(GIT_AUTHOR_NAME="fabric",
                            GIT_AUTHOR_EMAIL="fabric@local",
                            GIT_COMMITTER_NAME="fabric",
                            GIT_COMMITTER_EMAIL="fabric@local"):
        run("git stash save update %s" % now)
Őry Máté committed
108 109 110 111 112 113 114 115
        run("git pull --ff-only")


@roles('portal')
def update_portal(test=False):
    "Update and restart portal+manager"
    with _stopped("portal", "mancelery"):
        pull()
Őry Máté committed
116
        pip("circle", "~/circle/requirements.txt")
Őry Máté committed
117 118 119 120 121 122
        migrate()
        compile_things()
        if test:
            test()


Őry Máté committed
123 124
@roles('portal')
def stop_portal(test=False):
Őry Máté committed
125
    "Stop portal and manager"
Őry Máté committed
126 127 128
    _stop_services("portal", "mancelery")


Őry Máté committed
129 130 131
@roles('node')
def update_node():
    "Update and restart nodes"
Őry Máté committed
132
    with _stopped("node", "agentdriver"):
Őry Máté committed
133
        pull("~/vmdriver")
Őry Máté committed
134
        pip("vmdriver", "~/vmdriver/requirements/production.txt")
Őry Máté committed
135
        pull("~/agentdriver")
Őry Máté committed
136
        pip("agentdriver", "~/agentdriver/requirements.txt")
Őry Máté committed
137 138 139 140 141 142 143 144


@parallel
@roles('storage')
def update_storage():
    "Update and restart storagedriver"
    with _stopped("storage"):
        pull("~/storagedriver")
145
        pip("storagedriver", "~/storagedriver/requirements/production.txt")
Őry Máté committed
146 147 148 149 150 151 152 153 154 155


@parallel
@roles('node')
def checkout(vmdriver="master", agent="master"):
    """Checkout specific branch on nodes"""
    with settings(warn_only=True), cd("~/vmdriver"):
        run("git checkout %s" % vmdriver)
    with settings(warn_only=True), cd("~/agentdriver"):
        run("git checkout %s" % agent)
Őry Máté committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177


def _stop_services(*services):
    "Stop given services (warn only if not running)"
    with settings(warn_only=True):
        for service in reversed(services):
            sudo("stop %s" % service)


def _start_services(*services):
    for service in services:
        sudo("start %s" % service)


def _restart_service(*services):
    "Stop and start services"
    _stop_services(*services)
    _start_services(*services)


@contextlib.contextmanager
def _stopped(*services):
Őry Máté committed
178
    _stop_services(*services)
Őry Máté committed
179 180 181 182 183 184 185
    yield
    _start_services(*services)


def _workon(name):
    return prefix("source ~/.virtualenvs/%s/bin/activate && "
                  "source ~/.virtualenvs/%s/bin/postactivate" % (name, name))