summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/nfs_ganesha_libs.py
blob: cda79a5e651186bab4a49f0aa631e4713acf73ce (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
#  Copyright (C) 2016-2017  Red Hat, Inc. <http://www.redhat.com>
#
#  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 2 of the License, or
#  any later version.
#
#  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.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
    Description: nfs ganesha base classes.
    Pre-requisite: Please install gdeploy package on the glusto-tests
    management node.
"""

import time
import socket
import re
from glusto.core import Glusto as g
from glustolibs.gluster.nfs_ganesha_ops import (
    is_nfs_ganesha_cluster_exists,
    is_nfs_ganesha_cluster_in_healthy_state,
    teardown_nfs_ganesha_cluster,
    create_nfs_ganesha_cluster,
    configure_ports_on_clients,
    ganesha_client_firewall_settings)
from glustolibs.gluster.volume_libs import is_volume_exported


def setup_nfs_ganesha(cls):
    """
    Create nfs-ganesha cluster if not exists
    Set client configurations for nfs-ganesha

    Returns:
        True(bool): If setup is successful
        False(bool): If setup is failure
    """
    # pylint: disable = too-many-statements, too-many-branches
    # pylint: disable = too-many-return-statements
    cluster_exists = is_nfs_ganesha_cluster_exists(
        cls.servers_in_nfs_ganesha_cluster[0])
    if cluster_exists:
        is_healthy = is_nfs_ganesha_cluster_in_healthy_state(
            cls.servers_in_nfs_ganesha_cluster[0])

        if is_healthy:
            g.log.info("Nfs-ganesha Cluster exists and is in healthy "
                       "state. Skipping cluster creation...")
        else:
            g.log.info("Nfs-ganesha Cluster exists and is not in "
                       "healthy state.")
            g.log.info("Tearing down existing cluster which is not in "
                       "healthy state")
            ganesha_ha_file = ("/var/run/gluster/shared_storage/"
                               "nfs-ganesha/ganesha-ha.conf")
            g_node = cls.servers_in_nfs_ganesha_cluster[0]

            g.log.info("Collecting server details of existing "
                       "nfs ganesha cluster")

            # Check whether ganesha ha file exists
            cmd = "[ -f {} ]".format(ganesha_ha_file)
            ret, _, _ = g.run(g_node, cmd)
            if ret:
                g.log.error("Unable to locate %s", ganesha_ha_file)
                return False

            # Read contents of ganesha_ha_file
            cmd = "cat {}".format(ganesha_ha_file)
            ret, ganesha_ha_contents, _ = g.run(g_node, cmd)
            if ret:
                g.log.error("Failed to read %s", ganesha_ha_file)
                return False

            servers_in_existing_cluster = re.findall(r'VIP_(.*)\=.*',
                                                     ganesha_ha_contents)

            ret = teardown_nfs_ganesha_cluster(
                servers_in_existing_cluster, force=True)
            if not ret:
                g.log.error("Failed to teardown unhealthy ganesha "
                            "cluster")
                return False

            g.log.info("Existing unhealthy cluster got teardown "
                       "successfully")

    if (not cluster_exists) or (not is_healthy):
        g.log.info("Creating nfs-ganesha cluster of %s nodes"
                   % str(cls.num_of_nfs_ganesha_nodes))
        g.log.info("Nfs-ganesha cluster node info: %s"
                   % cls.servers_in_nfs_ganesha_cluster)
        g.log.info("Nfs-ganesha cluster vip info: %s"
                   % cls.vips_in_nfs_ganesha_cluster)

        ret = create_nfs_ganesha_cluster(
            cls.ganesha_servers_hostname,
            cls.vips_in_nfs_ganesha_cluster)
        if not ret:
            g.log.error("Creation of nfs-ganesha cluster failed")
            return False

    if not is_nfs_ganesha_cluster_in_healthy_state(
            cls.servers_in_nfs_ganesha_cluster[0]):
        g.log.error("Nfs-ganesha cluster is not healthy")
        return False
    g.log.info("Nfs-ganesha Cluster exists is in healthy state")

    ret = configure_ports_on_clients(cls.clients)
    if not ret:
        g.log.error("Failed to configure ports on clients")
        return False

    ret = ganesha_client_firewall_settings(cls.clients)
    if not ret:
        g.log.error("Failed to do firewall setting in clients")
        return False

    for server in cls.servers:
        for client in cls.clients:
            cmd = ("if [ -z \"$(grep -R \"%s\" /etc/hosts)\" ]; then "
                   "echo \"%s %s\" >> /etc/hosts; fi"
                   % (client, socket.gethostbyname(client), client))
            ret, _, _ = g.run(server, cmd)
            if ret != 0:
                g.log.error("Failed to add entry of client %s in "
                            "/etc/hosts of server %s"
                            % (client, server))

    for client in cls.clients:
        for server in cls.servers:
            cmd = ("if [ -z \"$(grep -R \"%s\" /etc/hosts)\" ]; then "
                   "echo \"%s %s\" >> /etc/hosts; fi"
                   % (server, socket.gethostbyname(server), server))
            ret, _, _ = g.run(client, cmd)
            if ret != 0:
                g.log.error("Failed to add entry of server %s in "
                            "/etc/hosts of client %s"
                            % (server, client))
    return True


def wait_for_nfs_ganesha_volume_to_get_exported(mnode, volname, timeout=120):
    """Waits for the nfs ganesha volume to get exported

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name

    Kwargs:
        timeout (int): timeout value in seconds to wait for volume
            to get exported

    Returns:
        True on success, False otherwise

    Examples:
        >>> wait_for_volume_to_get_exported("abc.com", "testvol")
    """
    count = 0
    flag = 0
    while count < timeout:
        if is_volume_exported(mnode, volname, "nfs"):
            flag = 1
            break

        time.sleep(10)
        count = count + 10
    if not flag:
        g.log.error("Failed to export volume %s" % volname)
        return False

    return True


def wait_for_nfs_ganesha_volume_to_get_unexported(mnode, volname, timeout=120):
    """Waits for the nfs ganesha volume to get unexported

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name

    Kwargs:
        timeout (int): timeout value in seconds to wait for volume
            to get unexported

    Returns:
        True on success, False otherwise

    Examples:
        >>> wait_for_volume_to_get_unexported("abc.com", "testvol")
    """
    count = 0
    flag = 0
    while count < timeout:
        if not is_volume_exported(mnode, volname, "nfs"):
            flag = 1
            break

        time.sleep(10)
        count = count + 10
    if not flag:
        g.log.error("Failed to unexport volume %s" % volname)
        return False

    return True