summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/block_ops.py
blob: f51c3e4b235c7ffb69b0c4c1885e2c67bfabd684 (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
#  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


def block_create(mnode, volname, blockname, servers, size=None,
                 **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|None): Size of the block, none if storage link passed

    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)
                - storage : (str)|None
                - ring-buffer: (int)

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

           (ret, out, err): As returned by block create command execution.

    Example:
        block_create(mnode, volname, blockname, servers, size,
        **block_args_info)
    """
    if not isinstance(servers, list):
        servers = [servers]

    ha = auth = prealloc = storage = ring_buffer = ''

    if block_args_info.get('ha'):
        ha = "ha %d" % int(block_args_info['ha'])

    if block_args_info.get('auth'):
        auth = "auth %s" % block_args_info['auth']

    if block_args_info.get('prealloc'):
        prealloc = "prealloc %s" % block_args_info['prealloc']

    if block_args_info.get('ring-buffer'):
        ring_buffer = "ring-buffer %d" % int(block_args_info['ring-buffer'])

    if block_args_info.get('storage'):
        storage = "storage %s" % block_args_info['storage']
        size = ''

    cmd = ("gluster-block create %s/%s %s %s %s %s %s %s %s " %
           (volname, blockname, ha, auth, ring_buffer, storage,
            prealloc, ','.join(servers), size))

    return g.run(mnode, cmd)


def block_modify(mnode, volname, blockname, auth=None, size=None, force=False):
    """modify the block device by either enabling the auth or changing the
    block size.

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of volume
        blockname (str): Name of the block
        auth (str): enable/disable
        size (str): New block size

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

           (ret, out, err): As returned by block create command execution.
    """
    force_block_modify = ''
    if force is True:
        force_block_modify = "force"

    if size is None:
        cmd = ("gluster-block modify %s/%s auth %s %s --json-pretty" %
               (volname, blockname, auth, force_block_modify))
    else:
        cmd = ("gluster-block modify %s/%s size %s %s --json-pretty " %
               (volname, blockname, size, force_block_modify))

    return g.run(mnode, cmd)


def block_list(mnode, volname):
    """list available block devices for the volume

    Args:
        mnode(str): server on which command has to be executed
        volname(str): volume name for which blocks has to be listed

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

           (ret, out, err): As returned by block create command execution.
    """
    cmd = "gluster-block list %s --json-pretty" % volname
    return g.run(mnode, cmd)


def block_info(mnode, volname, blockname):
    """Get info of block device

    Args:
        mnode(str): server on which command has to be executed
        volname(str): volume name for which block info has got
        blockname(str): block name for which info has to be got

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

           (ret, out, err): As returned by block create command execution.

    """
    cmd = "gluster-block info %s/%s --json-pretty" % (volname, blockname)
    return g.run(mnode, cmd)


def block_delete(mnode, volname, blockname, unlink_storage="yes", force=False):
    """Delete the block from volname

    Args:
        mnode(str): server on which command has to be executed
        volname(str): volume name from which block has to be delete
        blockname(str): block name to be deleted

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

           (ret, out, err): As returned by block create command execution.

    """
    force_delete_block = ''
    if force is True:
        force_delete_block = "force"

    if unlink_storage == "yes":
        cmd = "gluster-block delete %s/%s %s" % (volname, blockname,
                                                 force_delete_block)
    else:
        cmd = ("gluster-block delete %s/%s unlink-storage no %s"
               % (volname, blockname, force_delete_block))
    return g.run(mnode, cmd)