summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/mount_ops.py
blob: cc63d05880f214ff10a624c5a4feb5eed9200c49 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/env python
#  Copyright (C) 2015-2016  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.windows_libs import powershell
import copy


class GlusterMount():
    """Gluster Mount class

    Args:
        mount (dict): Mount dict with mount_protocol, mountpoint,
            server, client, volname, options, smbuser, smbpasswd,
            platform, super_user as keys

            Note: smbuser, smbpasswd are applicable for windows mounts.

            client is a dict with host, super_user, platform as keys.

                platform should be specified in case of windows. By default
                it is assumed to be linux.

                super_user is 'root' by default.
                In case of windows the super_user can be the user who has
                all the admin previliges.

        Example:
            mount =
                {'mount_protocol': 'glusterfs',
                 'mountpoint': '/mnt/g1',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': {'host': 'def.lab.eng.xyz.com'},
                 'volname': 'testvoi',
                 'options': ''
                 }

            mount =
                {'mount_protocol': 'nfs',
                 'mountpoint': '/mnt/n1',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': {'host': 'def.lab.eng.xyz.com'},
                 'volname': 'testvoi',
                 'options': ''}

            mount =
                {'mount_protocol': 'smb',
                 'mountpoint': '',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': {
                     'host': 'def.lab.eng.xyz.com',
                     'super_user': 'Admin'
                     },
                 'volname': 'testvoi',
                 'options': '',
                 'smbuser': 'abc',
                 'smbpasswd': 'def'}
    Returns:
        Instance of GlusterMount class
   """
    def __init__(self, mount):
        self.mounttype = None
        self.mountpoint = None
        self.server_system = None
        self.client_system = None
        self.volname = None
        self.options = ''
        self.smbuser = None
        self.smbpasswd = None
        self.user = None
        self.platform = None

        # Get Protocol
        if 'protocol' in mount:
            self.mounttype = mount['protocol']
        else:
            self.mounttype = "glusterfs"

        # Get mountpoint
        mount_point_defined = False
        if 'mountpoint' in mount:
            if mount['mountpoint']:
                mount_point_defined = True

        if mount_point_defined:
            self.mountpoint = mount['mountpoint']
        else:
            if self.mounttype == "smb":
                self.mountpoint = "*"
            else:
                self.mountpoint = "/mnt/%s" % self.mounttype

        # Get server
        self.server_system = None
        if 'server' in mount and mount['server']:
            self.server_system = mount['server']

        # Get client
        self.client_system = mount['client']['host']

        # Get super_user
        user_defined = False
        if 'super_user' in mount['client']:
            if mount['client']['super_user']:
                self.user = mount['client']['super_user']
                user_defined = True

        if not user_defined:
            self.user = "root"

        # Get platform
        platform_defined = False
        if 'platform' in mount['client']:
            if mount['client']['platform']:
                self.platform = mount['client']['platform']
                platform_defined = True

        if not platform_defined:
            self.platform = 'linux'

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

        # Get options
        if 'options' in mount:
            self.options = mount['options']

        # If mounttype is 'smb' or 'cifs' get 'smbuser' and 'smbpassword'
        if self.mounttype == 'smb' or self.mounttype == 'cifs':
            if 'smbuser' in mount:
                if mount['smbuser']:
                    self.smbuser = mount['smbuser']

            if 'smbpasswd' in mount:
                if mount['smbpasswd']:
                    self.smbpasswd = mount['smbpasswd']

    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,
                                     smbuser=self.smbuser,
                                     smbpasswd=self.smbpasswd,
                                     user=self.user)
        if ret != 0:
            return False
        else:
            if self.mounttype == "smb":
                self.mountpoint = out.strip()
        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_mounted(self.volname,
                         mpoint=self.mountpoint,
                         mserver=self.server_system,
                         mclient=self.client_system,
                         mtype=self.mounttype,
                         user=self.user)

        if ret:
            return True
        else:
            return False

    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,
                                        user=self.user)
        rc = True
        if ret == 0:
            if self.mounttype == "smb":
                if not (('deleted successfully' in out) or
                        ('command completed successfully' in out) or
                        ('There are no entries in the list' in out) or
                        ('The network connection could not be found')):
                    rc = False
                else:
                    self.mountpoint = "*"
        else:
            rc = False

        return rc


def is_mounted(volname, mpoint, mserver, mclient, mtype, user='root'):
    """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|nfs|smb|cifs)

    Kwargs:
        user (str): Super user of the node mclient

    Returns:
        bool: True if mounted and False otherwise.
    """
    # python will error on missing arg, so just checking for empty args here
    if not volname or not mpoint or not mserver or not mclient or not mtype:
        g.log.error("Missing arguments for mount.")
        return False

    if mtype == "smb":
        if mpoint == "*":
            return False
        cmd = powershell("net use %s" % mpoint)
        ret, out, err = g.run(mclient, cmd, user)
        if ret != 0:
            return False
        expected_output = (r"\\\%s\gluster-%s" %
                           (mserver, volname))
        if "Remote name" in out and expected_output in out:
            return True
        return False
    else:
        ret, _, _ = g.run(mclient, "mount | grep %s | grep %s | grep \"%s\""
                          % (volname, mpoint, mserver), user)
        if ret == 0:
            g.log.debug("Volume %s is mounted at %s:%s" % (volname, mclient,
                                                           mpoint))
            return True
        else:
            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='',
                 smbuser=None, smbpasswd=None, user='root'):
    """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.
        smbuser (str): SMB USERNAME. Used with mtype = 'cifs'
        smbpasswd (str): SMB PASSWD. Used with mtype = 'cifs'
        user (str): Super user of the node mclient


    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            (0, '', '') if already mounted.
            (1, '', '') if setup_samba_service fails in case of smb.
            (ret, out, err) of mount commnd execution otherwise.
    """
    if is_mounted(volname, mpoint, mserver, mclient, mtype, user):
        g.log.debug("Volume %s is already mounted at %s" %
                    (volname, mpoint))
        return (0, '', '')

    if options != '':
        options = "-o %s" % options

    if mtype == 'smb':
        if smbuser is None or smbpasswd is None:
            g.log.error("smbuser and smbpasswd to be passed as parameters "
                        "for cifs mounts")
            return (1, '', '')

        mcmd = ("net use %s \\\\%s\\gluster-%s " % (mpoint, mserver, volname) +
                " /user:%s " % (smbuser) + '"' + smbpasswd + '"')

        mcmd = powershell(mcmd)

        ret, out, err = g.run(mclient, mcmd, user=user)
        if ret != 0:
            g.log.error("net use command failed on windows client %s "
                        "failed: %s" % (mclient, err))
            return (ret, out, err)

        if out.startswith('Drive'):
            drv_ltr = out.split(' ')[1]
            g.log.info("Samba share mount success on windows client %s. "
                       "Share is : %s" % (mclient, drv_ltr))
            return (ret, drv_ltr, err)

        g.log.error("net use command successful but error in mount of samba "
                    " share for windows client %s for reason %s" %
                    (mclient, err))
        return (1, out, err)

    if mtype == 'nfs':
        if not options:
            options = "-o vers=3"

        elif options and 'vers' not in options:
            options = options + ",vers=3"

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

    if mtype == 'cifs':
        if smbuser is None or smbpasswd is None:
            g.log.error("smbuser and smbpasswd to be passed as parameters "
                        "for cifs mounts")
            return (1, '', '')

        # Check if client is running rhel. If so add specific options
        cifs_options = ""
        try:
            conn = g.rpyc_get_connection(mclient, user=user)
            if conn is None:
                g.log.error("Unable to get connection to %s on node %s"
                            " in mount_volume()", user, mclient)
                return (1, '', '')

            os, version, name = conn.modules.platform.linux_distribution()
            if "Santiago" in name:
                cifs_options = "sec=ntlmssp"
        except Exception as e:
            g.log.error("Exception occurred while getting the platform "
                        "of node %s: %s", mclient, str(e))
            return (1, '', '')
        finally:
            g.rpyc_close_connection(host=mclient, user=user)

        mcmd = ("mount -t cifs -o username=%s,password=%s,%s "
                "\\\\\\\\%s\\\\gluster-%s %s" % (smbuser, smbpasswd,
                                                 cifs_options, mserver,
                                                 volname, mpoint))

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

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


def umount_volume(mclient, mpoint, mtype='', user='root'):
    """Unmounts the mountpoint.

    Args:
        mclient (str): Client from which it has to be mounted.
        mpoint (str): Mountpoint dir.

    Kwargs:
        mtype (str): Mounttype. Defaults to ''.
        user (str): Super user of the node mclient. Defaults to 'root'

    Returns:
        tuple: Tuple containing three elements (ret, out, err) as returned by
            umount command execution.
    """
    if mtype == "smb":
        cmd = "net use %s /d /Y" % mpoint
        cmd = powershell(cmd)
    else:
        cmd = ("umount %s || umount -f %s || umount -l %s" %
               (mpoint, mpoint, mpoint))
    return g.run(mclient, cmd, user=user)


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': 'testvoi',
                 'options': '',
                 'num_of_mounts': 2},

                {'protocol': 'nfs',
                 'mountpoint': '/mnt/n1',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': {'host': 'def.lab.eng.xyz.com'},
                 'volname': 'testvoi',
                 'options': ''}

                {'protocol': 'smb',
                 'mountpoint': '',
                 'server': 'abc.lab.eng.xyz.com',
                 'client': {
                     'host': 'def.lab.eng.xyz.com',
                     'super_user': 'Admin'
                     },
                 'volname': 'testvoi',
                 'options': '',
                 'smbuser': 'abc',
                 'smbpasswd': 'def',
                 '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" or mount['protocol'] == "nfs" or
                mount['protocol'] == "cifs"):
            if 'mountpoint' in mount and mount['mountpoint']:
                temp_mount['mountpoint'] = mount['mountpoint']
            else:
                temp_mount['mountpoint'] = ("/mnt/%s_%s" %
                                            (mount['volname'],
                                             mount['protocol']))
        elif mount['protocol'] == "smb":
            if 'mountpoint' in mount and mount['mountpoint']:
                temp_mount['mountpoint'] = mount['mountpoint']
            else:
                temp_mount['mountpoint'] = "*"

        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 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))
    """
    rc = True
    for mount_obj in mount_objs:
        ret = mount_obj.mount()
        if not ret:
            rc = False
    return rc


def unmount_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 unmounting the mount for all mount_objs is successful.
            False otherwise.

    Example:
        ret = unmount_mounts(create_mount_objs(mounts))
    """
    rc = True
    for mount_obj in mount_objs:
        ret = mount_obj.unmount()
        if not ret:
            rc = False
    return rc