fabfile.py 3.77 KB
Newer Older
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
1 2
# Too many lines in module pylint: disable-msg=C0302
# Too many arguments (7/5) pylint: disable-msg=R0913
3

Vangelis Koukis's avatar
Vangelis Koukis committed
4
# Copyright (C) 2010-2014 GRNET S.A.
5
#
Vangelis Koukis's avatar
Vangelis Koukis committed
6 7 8 9
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
10
#
Vangelis Koukis's avatar
Vangelis Koukis committed
11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
Vangelis Koukis's avatar
Vangelis Koukis committed
16 17
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
19 20 21 22 23
"""
Fabric file for snf-deploy

"""

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
24
from __future__ import with_statement
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
25 26 27 28
from fabric.api import env, execute, parallel
from snfdeploy import context
from snfdeploy import constants
from snfdeploy import roles
29
import copy
30 31 32 33 34 35 36
import logging


FORMAT = "%(name)s %(funcName)s:%(lineno)d %(message)s"
# Needed to avoid:
# No handlers could be found for logger "paramiko.transport"
logging.basicConfig(format=FORMAT, level=logging.INFO)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59


def with_ctx(fn):
    def wrapper(*args):
        ctx = context.Context()
        return fn(*args, ctx=ctx)
    return wrapper


def with_cluster(fn):
    def wrapper(old_ctx, *args):
        ctx = copy.deepcopy(old_ctx)
        ctx.update(cluster=env.host)
        return fn(ctx, *args)
    return wrapper


def with_node(fn):
    def wrapper(old_ctx, *args):
        ctx = copy.deepcopy(old_ctx)
        ctx.update(node=env.host)
        return fn(ctx, *args)
    return wrapper
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
60 61


62
# Helper methods that are invoked via fabric's execute
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
63

64 65 66
@with_node
def _setup_vmc(ctx):
    VMC = roles.get(constants.VMC, ctx)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
67
    VMC.setup()
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
68

69

70 71 72 73 74 75 76 77 78 79
@with_node
def _setup_master(ctx):
    MASTER = roles.get(constants.MASTER, ctx)
    MASTER.setup()


@with_node
def _setup_role(ctx, role):
    ROLE = roles.get(role, ctx)
    ROLE.setup()
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
80 81


82 83 84 85
@with_cluster
def _setup_cluster(ctx):
    execute(_setup_master, ctx, hosts=ctx.masters)
    execute(_setup_vmc, ctx, hosts=ctx.vmcs)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
86 87


88 89
# Helper method that get a context snapshot and
# invoke fabric's execute with proper host argument
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
90

91 92 93
@with_ctx
def setup_role(role, ctx=None):
    execute(_setup_role, ctx, role, hosts=ctx.get(role))
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
94

95 96 97 98

@with_ctx
def setup_cluster(ctx=None):
    execute(_setup_cluster, ctx, hosts=ctx.clusters)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
99

100

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
101 102
def setup_synnefo():
    setup_role(constants.NS)
103
    setup_role(constants.CA)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
104 105 106
    setup_role(constants.NFS)
    setup_role(constants.DB)
    setup_role(constants.MQ)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
107

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
108 109 110
    setup_role(constants.ASTAKOS)
    setup_role(constants.PITHOS)
    setup_role(constants.CYCLADES)
111
    setup_role(constants.ADMIN)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
112
    setup_role(constants.CMS)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
113

114
    setup_cluster()
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
115

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
116 117
    setup_role(constants.STATS)
    setup_role(constants.CLIENT)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
118 119


Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
120 121 122
def setup_ganeti():
    setup_role(constants.NS)
    setup_role(constants.NFS)
123
    setup_cluster()
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
124

125

126 127
@with_cluster
def _setup_qa(ctx):
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
128 129 130 131
    setup_role(constants.NS)
    setup_role(constants.NFS)
    setup_cluster()
    setup_role(constants.DEV)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
132

Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
133

134 135 136
@with_ctx
def setup_qa(ctx=None):
    execute(_setup_qa, ctx, hosts=ctx.clusters)
137 138


139 140
@with_ctx
def setup(ctx=None):
141

142 143 144 145 146 147 148
    if context.node:
        if context.component:
            C = roles.get(context.component, ctx)
        elif context.role:
            C = roles.get(context.role, ctx)
        if context.method:
            fn = getattr(C, context.method)
149 150 151 152
            fn()
        else:
            C.setup()

153
    elif context.cluster:
154
        _setup_cluster(ctx)
155 156 157 158 159 160 161 162 163 164 165 166 167


@with_ctx
def run(ctx=None):
    if context.target_nodes:
        nodes = context.target_nodes.split(",")
    else:
        nodes = ctx.all_nodes

    for node in nodes:
        ctx.update(node=node)
        c = roles.get("HW", ctx)
        c.run(context.cmd)