summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/block_libs.py
blob: 268542620616311ce5121d6e71d72c850bc2f5ec (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
#  Copyright (C) 2018  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 block related helper functions. """

from glusto.core import Glusto as g
from glustolibs.gluster.block_ops import block_create, block_info, block_list


def if_block_exists(mnode, volname, blockname):
    """Check if block already exists

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of the volume.
        blockname(str): block name

    Returns:
        bool : True if block exists. False Otherwise
    """
    # Get block list for the volname
    ret, out, err = block_list(mnode, volname)
    if ret != 0:
        g.log.error("Failed to get block list for the volume %s: %s",
                    volname, err)
        return False

    # Check if block exists for the volname
    block_list_dict = g.load_json_string(out)
    if blockname in block_list_dict['blocks']:
        return True
    else:
        g.log.error("Block '%s' doesn't exists on volume %s",
                    blockname, volname)
        return False


def setup_block(mnode, volname, blockname, servers, size, **block_args_info):
    """Create the gluster block with specified configuration

    Args:
        mnode(str): server on which command has to be executed
        volname(str): volume name that has to be created
        blockname(str): block name that has to be created
        servers(list): List of servers IP's to be used to create block
        size(str): Size of the block

    block_args_info:
        **block_args_info
            The keys, values in block_args_info are:
                - ha : (int)|None
                - auth : (str)|None (enable|disable)
                - prealloc : (str)|None (full|no)
                - ...

    Returns:
        bool : True on successful setup. False Otherwise

    Example:
        setup_block(mnode, volname, blockname, servers, size,
        **block_args_info)
    """
    # Create the Block Device
    ret, _, err = block_create(mnode, volname, blockname, servers, size,
                               **block_args_info)
    if ret != 0:
        g.log.error("Failed to create block:%s\n%s", err, block_args_info)
        return False

    return True


def get_block_info(mnode, volname, blockname):
    """Get Block Info

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of the volume.
        blockname(str): block name

    Returns:
        dict if successful in getting block info, None if block doesn't exists
    """
    ret, out, err = block_info(mnode, volname, blockname)
    if ret != 0:
        g.log.error("Failed to get block info of block: %s/%s : %s",
                    volname, blockname, err)
        return None

    return g.load_json_string(out)


def get_block_list(mnode, volname):
    """ Get list of all blocks for the volume

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

    Returns:
        list of block names if successful in getting block list, None if
        block list command errors
    """
    # get block list for the volname
    ret, out, err = block_list(mnode, volname)
    if ret != 0:
        g.log.error("Failed to get block list for the volume %s: %s",
                    volname, err)
        return None

    block_list_dict = g.load_json_string(out)
    blocknames = block_list_dict.get('blocks', None)
    if blocknames is None:
        g.log.error("No blocks present on volume %s", volname)

    return blocknames


def get_block_gbid(mnode, volname, blockname):
    """Get Block IQN

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of the volume.
        blockname(str): block name

    Returns:
        gbid if successful, None if block doesn't exists
    """
    block_info_dict = get_block_info(mnode, volname, blockname)
    if not block_info_dict:
        return None

    block_gbid = block_info_dict.get('GBID')
    return block_gbid


def get_block_password(mnode, volname, blockname):
    """Get Block Password

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of the volume.
        blockname(str): block name

    Returns:
        Password if auth enable, None otherwise
    """
    block_info_dict = get_block_info(mnode, volname, blockname)
    if not block_info_dict:
        return None

    block_password = block_info_dict.get('PASSWORD')
    if block_password == '':
        g.log.error("Block authentication is disabled")
        return None
    else:
        return block_password


def get_volume_blocks_gbid(mnode, volname):
    """Get Block GBID for all the blocks present in the volume

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

    Returns:
        dict of block:gbid if successful,
        None if volume doesn't have any blocks or volume doesn't exists
    """
    blocknames = get_block_list(mnode, volname)
    if not blocknames:
        return None

    blocks_gbid = {}

    # get each block info
    for blockname in blocknames:
        ret = get_block_gbid(mnode, volname, blockname)
        blocks_gbid[blockname] = ret

    return blocks_gbid


def validate_block_info(mnode, volname, blockname, servers, size,
                        **block_args_info):
    """Validate the output of gluster-block info command with the params
       passed

    Args:
        mnode(str): server on which command has to be executed
        volname(str): volume name that has to be validated
        blockname(str): block name that has to be validated
        servers(list): List of servers IP's to be used to create block
        size(str): Size of the block

    block_args_info:
        **block_args_info
            The keys, values in block_args_info are:
                - ha : (int)|None

    Returns:
        bool : True if validation is successful. False Otherwise

    Example:
        validate_block_info(mnode, volname, blockname, servers, size,
        **block_args_info)
    """
    block_info_dict = get_block_info(mnode, volname, blockname)
    if not block_info_dict:
        g.log.error("Failed to get block info of block: %s/%s",
                    volname, blockname)
        return False

    # Check volname
    if volname != block_info_dict.get('VOLUME'):
        g.log.error("Volume name validation is unsuccessful")
        return False

    if blockname != block_info_dict.get("NAME"):
        g.log.error("Block name validation is unsuccessful")
        return False

    if (block_args_info.get('ha')) != block_info_dict.get("HA"):
        g.log.error("HA parameter validation is unsuccessful")
        return False

    if set(servers) != set(block_info_dict.get("EXPORTED ON")):
        g.log.error("Server information validation is unsuccessful")
        return False

    if size != block_info_dict.get("SIZE"):
        g.log.error("Size validation is unsuccessful")
        return False

    g.log.info("Information in config file matches with block"
               "information on server")
    return True


def check_device_logged_in(client, block_iqn):
    """Check if the block is logged in on the client

    Args:
        client: Client to check on
        block_iqn: IQN of block for which a check is done

    Returns:
        bool : True if validation is successful. False Otherwise

    """

    cmd = 'iscsiadm -m session | grep -F -m 1 %s' % block_iqn
    ret, out, err = g.run(client, cmd)
    if ret != 0:
        g.log.error("Failed to get device login details: %s", err)
        return False

    return True