summaryrefslogtreecommitdiffstats
path: root/qa/tasks/rabbitmq.py
blob: c78ac1e568fa9dc3922ec6ce037d1cddfdc04c8d (plain)
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
"""
Deploy and configure RabbitMQ for Teuthology
"""
import contextlib
import logging

from teuthology import misc as teuthology
from teuthology import contextutil
from teuthology.orchestra import run

log = logging.getLogger(__name__)


@contextlib.contextmanager
def install_rabbitmq(ctx, config):
    """
    Downloading the RabbitMQ package.
    """
    assert isinstance(config, dict)
    log.info('Installing RabbitMQ...')

    for (client, _) in config.items():
        (remote,) = ctx.cluster.only(client).remotes.keys()

        ctx.cluster.only(client).run(args=[
             'sudo', 'yum', '-y', 'install', 'epel-release'
        ])

        link1 = 'https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh'

        ctx.cluster.only(client).run(args=[
             'curl', '-s', link1, run.Raw('|'), 'sudo', 'bash'
        ])

        ctx.cluster.only(client).run(args=[
             'sudo', 'yum', '-y', 'install', 'erlang'
        ])

        link2 = 'https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh'

        ctx.cluster.only(client).run(args=[
             'curl', '-s', link2, run.Raw('|'), 'sudo', 'bash'
        ])

        ctx.cluster.only(client).run(args=[
             'sudo', 'yum', '-y', 'install', 'rabbitmq-server'
        ])

    try:
        yield
    finally:
        log.info('Removing packaged dependencies of RabbitMQ...')

        for (client, _) in config.items():
            ctx.cluster.only(client).run(args=[
                 'sudo', 'yum', '-y', 'remove', 'rabbitmq-server.noarch'
            ])


@contextlib.contextmanager
def run_rabbitmq(ctx, config):
    """
    This includes two parts:
    1. Starting Daemon
    2. Starting RabbitMQ service
    """
    assert isinstance(config, dict)
    log.info('Bringing up Daemon and RabbitMQ service...')
    for (client,_) in config.items():
        (remote,) = ctx.cluster.only(client).remotes.keys()

        ctx.cluster.only(client).run(args=[
             'sudo', 'chkconfig', 'rabbitmq-server', 'on'
            ],
        )

        ctx.cluster.only(client).run(args=[
             'sudo', '/sbin/service', 'rabbitmq-server', 'start'
            ],
        )

        '''
        # To check whether rabbitmq-server is running or not
        ctx.cluster.only(client).run(args=[
             'sudo', '/sbin/service', 'rabbitmq-server', 'status'
            ],
        )
        '''

    try:
        yield
    finally:
        log.info('Stopping RabbitMQ Service...')

        for (client, _) in config.items():
            (remote,) = ctx.cluster.only(client).remotes.keys()

            ctx.cluster.only(client).run(args=[
                 'sudo', '/sbin/service', 'rabbitmq-server', 'stop'
                ],
            )


@contextlib.contextmanager
def task(ctx,config):
    """
    To run rabbitmq the prerequisite is to run the tox task. Following is the way how to run
    tox and then rabbitmq::
    tasks:
    - rabbitmq:
        client.0:
    """
    assert config is None or isinstance(config, list) \
        or isinstance(config, dict), \
        "task rabbitmq only supports a list or dictionary for configuration"

    all_clients = ['client.{id}'.format(id=id_)
                   for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client')]
    if config is None:
        config = all_clients
    if isinstance(config, list):
        config = dict.fromkeys(config)

    log.debug('RabbitMQ config is %s', config)

    with contextutil.nested(
        lambda: install_rabbitmq(ctx=ctx, config=config),
        lambda: run_rabbitmq(ctx=ctx, config=config),
        ):
        yield