summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster-gd2/glustolibs/gluster/mount_ops.py
blob: da0a993d83517ad19c18105f4f23444940bcddb7 (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
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
#  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 Mount operations.
"""


from glusto.core import Glusto as g
from glustolibs.gluster.exceptions import ConfigError
import copy

class GlusterMount():
    """Gluster Mount class
    Args:
        mount (dict): Mount dict with mount_protocol, mountpoint,
            server, client, volname, options
        Example:
            mount =
                {'protocol': 'glusterfs',
                 'mountpoint': '/mnt/g1',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': 'def.lab.eng.xyz.com',
                 'volname': 'testvol',
                 'options': ''
                 }
    Returns:
        Instance of GlusterMount class
   """
    def __init__(self, mount):
        # Check for missing parameters
        for param in ['protocol', 'mountpoint', 'server',
                      'client', 'volname', 'options']:
            if param not in mount:
                raise ConfigError("Missing key %s" % param)

        # Get Protocol
        self.mounttype = mount.get('protocol', 'glusterfs')

        # Get mountpoint
        if bool(mount.get('mountpoint', False)):
            self.mountpoint = mount['mountpoint']
        else:
            self.mountpoint = "/mnt/%s" % self.mounttype

        # Get server
        self.server_system = mount.get('server', None)

        # Get client
        self.client_system = mount.get('client', None)

        # Get Volume name
        self.volname = mount['volname']

        # Get options
        self.options = mount.get('options', None)

    def mount(self):
        """Mounts the volume
        Args:
            uses instance args passed at init
        Returns:
            bool: True on success and False on failure.
        """
        ret, out, err = mount_volume(self.volname, mtype=self.mounttype,
                                     mpoint=self.mountpoint,
                                     mserver=self.server_system,
                                     mclient=self.client_system,
                                     options=self.options)
        if ret:
            g.log.error("Failed to mount the volume")
            return False
        return True

    def is_mounted(self):
        """Tests for mount on client
        Args:
            uses instance args passed at init
        Returns:
            bool: True on success and False on failure.
        """
        ret = is_volume_mounted(self.volname,
                                mpoint=self.mountpoint,
                                mserver=self.server_system,
                                mclient=self.client_system,
                                mtype=self.mounttype)

        if not ret:
            g.log.error("Volume is not mounted")
            return False
        return True

    def unmount(self):
        """Unmounts the volume
        Args:
            uses instance args passed at init
        Returns:
            bool: True on success and False on failure.
        """
        (ret, out, err) = umount_volume(mclient=self.client_system,
                                        mpoint=self.mountpoint,
                                        mtype=self.mounttype)
        if ret:
            g.log.error("Failed to unmount the volume")
            return False
        return True


def is_volume_mounted(volname, mpoint, mserver, mclient, mtype):
    """Check if mount exist.
    Args:
        volname (str): Name of the volume
        mpoint (str): Mountpoint dir
        mserver (str): Server to which it is mounted to
        mclient (str): Client from which it is mounted.
        mtype (str): Mount type (glusterfs)
    Returns:
        bool: True if mounted and False otherwise.
    """
    # python will error on missing arg, so just checking for empty args here
    for param in [volname, mpoint, mserver, mclient, mtype]:
        if not param:
            g.log.error("Missing arguments for mount.")
            return False

    ret, _, _ = g.run(mclient, "mount | grep %s | grep %s | grep \"%s\""
                      % (volname, mpoint, mserver))
    if ret:
        g.log.debug("Volume %s is mounted at %s:%s" % (volname, mclient,
                                                       mpoint))
        return True
    g.log.error("Volume %s is not mounted at %s:%s" % (volname,
                                                       mclient,
                                                       mpoint))
    return False


def mount_volume(volname, mtype, mpoint, mserver, mclient, options=''):
    """Mount the gluster volume with specified options.
    Args:
        volname (str): Name of the volume to mount.
        mtype (str): Protocol to be used to mount.
        mpoint (str): Mountpoint dir.
        mserver (str): Server to mount.
        mclient (str): Client from which it has to be mounted.
    Kwargs:
        option (str): Options for the mount command.
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            (0, '', '') if already mounted.
            (ret, out, err) of mount commnd execution otherwise.
    """
    if is_mounted(volname, mpoint, mserver, mclient, mtype):
        g.log.debug("Volume %s is already mounted at %s" %
                    (volname, mpoint))
        return (0, '', '')

    if not options:
        options = "-o %s" % options

    # Create mount dir
    g.run(mclient, "test -d %s || mkdir -p %s" % (mpoint, mpoint))

    # Mount
    mcmd = ("mount -t %s %s %s:/%s %s" %
            (mtype, options, mserver, volname, mpoint))

    # Create mount
    return g.run(mclient, mcmd)


def umount_volume(mclient, mpoint, mtype=''):
    """Unmounts the mountpoint.
    Args:
        mclient (str): Client from which it has to be mounted.
        mpoint (str): Mountpoint dir.
    Kwargs:
        mtype (str): Mounttype. Defaults to ''.
    Returns:
        tuple: Tuple containing three elements (ret, out, err) as returned by
            umount command execution.
    """
    cmd = ("umount %s || umount -f %s || umount -l %s" %
            (mpoint, mpoint, mpoint))

    return g.run(mclient, cmd)


def create_mount_objs(mounts):
    """Creates GlusterMount class objects for the given list of mounts
    Args:
        mounts (list): list of mounts with each element being dict having the
            specifics of each mount
        Example:
            mounts: [
                {'protocol': 'glusterfs',
                 'mountpoint': '/mnt/g1',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': {'host': 'def.lab.eng.xyz.com'},
                 'volname': 'testvol',
                 'options': '',
                 'num_of_mounts': 2}
                ]
    Returns:
        list : List of GlusterMount class objects.
    Example:
        mount_objs = create_mount_objs(mounts)
    """
    mount_obj_list = []
    for mount in mounts:
        temp_mount = copy.deepcopy(mount)
        if (mount['protocol'] == "glusterfs"):
            if 'mountpoint' in mount and mount['mountpoint']:
                temp_mount['mountpoint'] = mount['mountpoint']
            else:
                temp_mount['mountpoint'] = ("/mnt/%s_%s" %
                                            (mount['volname'],
                                             mount['protocol']))

        num_of_mounts = 1
        if 'num_of_mounts' in mount:
            if mount['num_of_mounts']:
                num_of_mounts = mount['num_of_mounts']
        if num_of_mounts > 1:
            mount_dir = temp_mount['mountpoint']
            for count in range(1, num_of_mounts + 1):
                if mount_dir != "*":
                    temp_mount['mountpoint'] = '_'.join(
                        [mount_dir, str(count)])

                mount_obj_list.append(GlusterMount(temp_mount))
        else:
            mount_obj_list.append(GlusterMount(temp_mount))

    return mount_obj_list


def operate_mounts(mount_objs, operation):
    """Mounts/Unmounts using the details as specified
    in the each mount obj
    Args:
        mount_objs (list): list of mounts objects with each element being
            the GlusterMount class object
        operation (str): Mount/unmount
    Returns:
        bool : True if creating the mount for all mount_objs is successful.
            False otherwise.
    Example:
        ret = operate_mounts(create_mount_objs(mounts), operation='mount')
    """
    _rc = True
    for mount_obj in mount_objs:
        if operation == 'mount':
            ret = mount_obj.mount()
        elif operation == 'unmount':
            ret = mount_obj.unmount()
        else:
            g.log.error("Operation not found")
            _rc = False
        return _rc


def create_mounts(mount_objs):
    """Creates Mounts using the details as specified in the each mount obj
    Args:
        mount_objs (list): list of mounts objects with each element being
            the GlusterMount class object
    Returns:
        bool : True if creating the mount for all mount_objs is successful.
            False otherwise.
    Example:
        ret = create_mounts(create_mount_objs(mounts))
    """
    return operate_mounts(mount_objs, operation='mount')


def unmount_mounts(mount_objs):
    """Unmounts using the details as specified in the each mount obj
    Args:
        mount_objs (list): list of mounts objects with each element being
            the GlusterMount class object
    Returns:
        bool : True if unmounting the mount for all mount_objs is successful.
            False otherwise.
    Example:
        ret = unmount_mounts(create_mount_objs(mounts))
    """
    return operate_mounts(mount_objs, operation='unmount')