summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/gluster_init.py
blob: 6e6b5aaf72db18dc5d628ab8565ece03fc10342e (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
#  Copyright (C) 2015-2020 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: This file contains the methods for starting/stopping glusterd
        and other initial gluster environment setup helpers.
"""
from time import sleep
from glusto.core import Glusto as g


def start_glusterd(servers):
    """Starts glusterd on specified servers if they are not running.

    Args:
        servers (str|list): A server|List of server hosts on which glusterd
            has to be started.

    Returns:
        bool : True if starting glusterd is successful on all servers.
            False otherwise.
    """
    if not isinstance(servers, list):
        servers = [servers]

    cmd = "pgrep glusterd || service glusterd start"
    results = g.run_parallel(servers, cmd)

    _rc = True
    for server, ret_values in results.items():
        retcode, _, _ = ret_values
        if retcode != 0:
            g.log.error("Unable to start glusterd on server %s", server)
            _rc = False
    if not _rc:
        return False

    return True


def stop_glusterd(servers):
    """Stops the glusterd on specified servers.

    Args:
        servers (str|list): A server|List of server hosts on which glusterd
            has to be stopped.

    Returns:
        bool : True if stopping glusterd is successful on all servers.
            False otherwise.
    """
    if not isinstance(servers, list):
        servers = [servers]

    cmd = "service glusterd stop"
    results = g.run_parallel(servers, cmd)

    _rc = True
    for server, ret_values in results.items():
        retcode, _, _ = ret_values
        if retcode != 0:
            g.log.error("Unable to stop glusterd on server %s", server)
            _rc = False
    if not _rc:
        return False

    return True


def restart_glusterd(servers):
    """Restart the glusterd on specified servers.

    Args:
        servers (str|list): A server|List of server hosts on which glusterd
            has to be restarted.

    Returns:
        bool : True if restarting glusterd is successful on all servers.
            False otherwise.
    """
    if not isinstance(servers, list):
        servers = [servers]

    cmd = "service glusterd restart"
    results = g.run_parallel(servers, cmd)

    _rc = True
    for server, ret_values in results.items():
        retcode, _, _ = ret_values
        if retcode != 0:
            g.log.error("Unable to restart glusterd on server %s", server)
            _rc = False
    if not _rc:
        return False

    return True


def reset_failed_glusterd(servers):
    """Reset-failed glusterd on specified servers.

    Args:
        servers (str|list): A server|List of server hosts on which glusterd
            has to be reset-failed.

    Returns:
        bool : True if reset-failed glusterd is successful on all servers.
            False otherwise.
    """
    if not isinstance(servers, list):
        servers = [servers]

    cmd = "systemctl reset-failed glusterd"
    results = g.run_parallel(servers, cmd)

    for server, (retcode, _, _) in results.items():
        if retcode:
            g.log.error("Unable to reset glusterd on server %s", server)
            return False
    return True


def is_glusterd_running(servers):
    """Checks the glusterd status on specified servers.

    Args:
        servers (str|list): A server|List of server hosts on which glusterd
            status has to be checked.

    Returns:
            0  : if glusterd running
            1  : if glusterd not running
           -1  : if glusterd not running and PID is alive

    """
    if not isinstance(servers, list):
        servers = [servers]

    cmd1 = "service glusterd status"
    cmd2 = "pidof glusterd"
    cmd1_results = g.run_parallel(servers, cmd1)
    cmd2_results = g.run_parallel(servers, cmd2)

    _rc = 0
    for server, ret_values in cmd1_results.items():
        retcode, _, _ = ret_values
        if retcode != 0:
            g.log.error("glusterd is not running on the server %s", server)
            _rc = 1
            if cmd2_results[server][0] == 0:
                g.log.error("PID of glusterd is alive and status is not "
                            "running")
                _rc = -1
    return _rc


# TODO: THIS IS NOT IMPLEMENTED YET. PLEASE DO THIS MANUALLY
# TILL WE IMPLEMENT THIS
def env_setup_servers(servers):
    """Set up environment on all the specified servers.

    Args:
        servers (str|list): A server|List of server hosts on which environment
            has to be setup.

    Returns:
        bool : True if setting up environment is successful on all servers.
            False otherwise.

    """
    if not isinstance(servers, list):
        servers = [servers]

    g.log.info("The function isn't implemented fully")
    g.log.info("Please setup the bricks manually.")

    if not start_glusterd(servers):
        return False

    return True


def get_glusterd_pids(nodes):
    """
    Checks if glusterd process is running and
    return the process id's in dictionary format

    Args:
        nodes (str|list) : Node(s) of the cluster

    Returns:
        tuple : Tuple containing two elements (ret, gluster_pids).
        The first element 'ret' is of type 'bool', True if only if
        glusterd is running on all the nodes in the list and each
        node contains only one instance of glusterd running.
        False otherwise.

        The second element 'glusterd_pids' is of type dictonary and
        it contains the process ID's for glusterd.

    """
    glusterd_pids = {}
    _rc = True
    if not isinstance(nodes, list):
        nodes = [nodes]

    cmd = "pidof glusterd"
    g.log.info("Executing cmd: %s on node %s", cmd, nodes)
    results = g.run_parallel(nodes, cmd)
    for node in results:
        ret, out, _ = results[node]
        if ret == 0:
            if len(out.strip().split("\n")) == 1:
                if not out.strip():
                    g.log.error("NO glusterd process found "
                                "on node %s", node)
                    _rc = False
                    glusterd_pids[node] = ['-1']
                else:
                    g.log.info("glusterd process with "
                               "pid %s found on %s",
                               out.strip().split("\n"), node)
                    glusterd_pids[node] = (out.strip().split("\n"))
            else:
                g.log.error("More than one glusterd process "
                            "found on node %s", node)
                _rc = False
                glusterd_pids[node] = out
        else:
            g.log.error("Not able to get glusterd process "
                        "from node %s", node)
            _rc = False
            glusterd_pids[node] = ['-1']

    return _rc, glusterd_pids


def wait_for_glusterd_to_start(servers, glusterd_start_wait_timeout=80):
    """Checks glusterd is running on nodes with timeout.

    Args:
        servers (str|list): A server|List of server hosts on which glusterd
            status has to be checked.
        glusterd_start_wait_timeout: timeout to retry glusterd running
            check in node.

    Returns:
    bool : True if glusterd is running on servers.
        False otherwise.

    """
    if not isinstance(servers, list):
        servers = [servers]
    count = 0
    while count <= glusterd_start_wait_timeout:
        ret = is_glusterd_running(servers)
        if not ret:
            g.log.info("glusterd is running on %s", servers)
            return True
        sleep(1)
        count += 1
    g.log.error("glusterd is not running on %s", servers)
    return False


def get_gluster_version(host):
    """Checks the gluster version on the nodes

    Args:
        host(str): IP of the host whose gluster version has to be checked.

    Returns:
        (float): The gluster version value.
    """
    command = 'gluster --version'
    _, out, _ = g.run(host, command)
    g.log.info("The Gluster verion of the cluster under test is %s",
               out)
    return float(out.split(' ')[1])