summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/brickmux_ops.py
blob: b56434741451744335b638c3654c0a39774c58ff (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
#!/usr/bin/env python
#  Copyright (C) 2017-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: Module for gluster brick multiplex operations
"""

from glusto.core import Glusto as g
from glustolibs.gluster.volume_ops import get_volume_status


def get_brick_mux_status(mnode):
    """Gets brick multiplex status

        Args:
            mnode (str): Node on which cmd has to be executed.

        Returns:
            str : Brick multiplex status. None otherwise
        """
    cmd = ("gluster v get all all | grep cluster.brick-multiplex |"
           "awk '{print $2}'")
    _, out, _ = g.run(mnode, cmd)
    return out.strip()


def is_brick_mux_enabled(mnode):
    """Gets brick multiplex status and checks for positive and negative states

        Args:
            mnode (str): Node on which cmd has to be executed.

        Returns:
            True : Brick multiplex status is one of 'true, on, 1, enable'.
            False : Brick multiplex status is one of 'false, off, 0, disable'.
            Exception : otherwise
        """
    positive_states = ['true', 'on', '1', 'enable']
    negative_states = ['off', 'disable', '0', 'false']
    if get_brick_mux_status(mnode) in positive_states:
        return True
    elif get_brick_mux_status(mnode) in negative_states:
        return False
    else:
        raise ValueError('Brick mux status %s is incorrect',
                         get_brick_mux_status(mnode))


def enable_brick_mux(mnode):
    """Enables brick multiplex operation on all servers

    Args:
        mnode (str): Node on which cmd has to be executed.

    Returns:
        bool : True if successfully enabled brickmux. False otherwise.
    """
    cmd = "gluster v set all cluster.brick-multiplex enable --mode=script"
    ret, _, err = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to enable brick multiplex: %s", err)
        return False
    return True


def disable_brick_mux(mnode):
    """Disables brick multiplex operation on all servers

    Args:
        mnode (str): Node on which cmd has to be executed.

    Returns:
        bool : True if successfully disabled brickmux. False otherwise.
    """
    cmd = "gluster v set all cluster.brick-multiplex disable --mode=script"
    ret, _, err = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to disable brick multiplex: %s", err)
        return False
    return True


def check_brick_pid_matches_glusterfsd_pid(mnode, volname):
    # pylint: disable=invalid-name
    """Checks for brick process(es) both volume status
       and 'ps -eaf | grep glusterfsd' matches for
       the given volume

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): Name of the volume.

    Returns:
        bool : True if pid's matches. False otherwise.
    """
    from glustolibs.gluster.brick_libs import get_all_bricks
    _rc = True
    bricks_list = get_all_bricks(mnode, volname)
    for brick in bricks_list:
        brick_node, brick_path = brick.split(":")
        ret = get_volume_status(mnode, volname)
        brick_pid = ret[volname][brick_node][brick_path]["pid"]
        if brick_pid == "None":
            g.log.error("Failed to get brick pid on node %s "
                        "of brick path %s", brick_node, brick_path)
            _rc = False

        cmd = "pgrep -x glusterfsd"
        ret, pid, _ = g.run(brick_node, cmd)
        if ret != 0:
            g.log.error("Failed to run the command %s on "
                        "node %s", cmd, brick_node)
            _rc = False

        else:
            glusterfsd_pid = pid.split('\n')[:-1]

        if brick_pid not in glusterfsd_pid:
            g.log.error("Brick pid %s doesn't match glusterfsd "
                        "pid %s of the node %s", brick_pid,
                        glusterfsd_pid, brick_node)
            _rc = False

    return _rc


def get_brick_processes_count(mnode):
    """
    Get the brick process count for a given node.

    Args:
        mnode (str): Node on which brick process has to be counted.

    Returns:
        int: Number of brick processes running on the node.
        None: If the command fails to execute.
    """
    ret, out, _ = g.run(mnode, "pgrep -x glusterfsd")
    if not ret:
        list_of_pids = out.split("\n")
        list_of_pids.pop()
        return len(list_of_pids)
    else:
        return None