summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/samba_libs.py
blob: bffe6a12c56e51c431812c5ffbdaed15add24e1b (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
#  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: Library for samba operations.
"""

from glusto.core import Glusto as g
from glustolibs.gluster.volume_libs import is_volume_exported
from glustolibs.gluster.mount_ops import GlusterMount


def start_smb_service(mnode):
    """Start smb service on the specified node.

    Args:
        mnode (str): Node on which smb service has to be started

    Returns:
        bool: True on successfully starting smb service. False otherwise.
    """
    g.log.info("Starting SMB Service on %s", mnode)

    # Enable Samba to start on boot
    ret, _, _ = g.run(mnode, "chkconfig smb on")
    if ret != 0:
        g.log.error("Unable to set chkconfig smb on")
        return False
    g.log.info("chkconfig smb on successful")

    # Start smb service
    ret, _, _ = g.run(mnode, "service smb start")
    if ret != 0:
        g.log.error("Unable to start the smb service")
        return False
    g.log.info("Successfully started smb service")

    return True


def smb_service_status(mnode):
    """Status of smb service on the specified node.

    Args:
        mnode (str): Node on which smb service has to be started

    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.
    """
    g.log.info("Getting SMB Service status on %s", mnode)
    return g.run(mnode, "service smb status")


def is_smb_service_running(mnode):
    """Check if smb service is running on node

    Args:
        mnode (str): Node on which smb service status has to be verified.

    Returns:
        bool: True if smb service is running. False otherwise.
    """
    g.log.info("Check if SMB service is running on %s", mnode)
    ret, out, _ = smb_service_status(mnode)
    if ret != 0:
        return False
    if "Active: active (running)" in out:
        return True
    else:
        return False


def stop_smb_service(mnode):
    """Stop smb service on the specified node.

    Args:
        mnode (str): Node on which smb service has to be stopped.

    Returns:
        bool: True on successfully stopping smb service. False otherwise.
    """
    g.log.info("Stopping SMB Service on %s", mnode)

    # Disable Samba to start on boot
    ret, _, _ = g.run(mnode, "chkconfig smb off")
    if ret != 0:
        g.log.error("Unable to set chkconfig smb off")
        return False
    g.log.info("chkconfig smb off successful")

    # Stop smb service
    ret, _, _ = g.run(mnode, "service smb stop")
    if ret != 0:
        g.log.error("Unable to stop the smb service")
        return False
    g.log.info("Successfully stopped smb service")

    return True


def list_smb_shares(mnode):
    """List all the gluster volumes that are exported as SMB Shares

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

    Returns:
        list: List of all volume names that are exported as SMB Shares.
             Empty list if no volumes are exported as SMB Share.
    """
    g.log.info("List all SMB Shares")
    smb_shares_list = []
    cmd = "smbclient -L localhost"
    ret, out, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to find the SMB Shares")
        return smb_shares_list
    else:
        out = out.splitlines()
        for line in out:
            if 'gluster-' in line:
                smb_shares_list.append(line.split(" ")[0].strip())

    return smb_shares_list


def enable_mounting_volume_over_smb(mnode, volname, smb_users_info):
    """Enable mounting volume over SMB. Set ACL's for non-root users.

    Args:
        mnode (str): Node on which commands are executed.
        volname (str): Name of the volume on which acl's has to be set.
        smb_users_info (dict): Dict containing users info. Example:
            smb_users_info = {
                'root': {'password': 'foobar',
                         'acl': ''
                         },
                'user1': {'password': 'abc',
                          'acl': ''
                          },
                'user2': {'password': 'xyz',
                          'acl': ''
                          }
                }
    Returns:
        bool: True on successfully enabling to mount volume using SMB.
            False otherwise.
    """
    g.log.info("Enable mounting volume over SMB")
    # Create a temp mount to provide required permissions to the smb user
    mount = {
        'protocol': 'glusterfs',
        'server': mnode,
        'volname': volname,
        'client': {
            'host': mnode
            },
        'mountpoint': '/tmp/gluster_smb_set_user_permissions_%s' % volname,
        'options': 'acl'
        }
    mount_obj = GlusterMount(mount)
    ret = mount_obj.mount()
    if not ret:
        g.log.error("Unable to create temporary mount for providing "
                    "required permissions to the smb users")
        return False
    g.log.info("Successfully created temporary mount for providing "
               "required permissions to the smb users")

    # Provide required permissions to the smb user
    for smb_user in smb_users_info.keys():
        if smb_user != 'root':
            if 'acl' in smb_users_info[smb_user]:
                acl = smb_users_info[smb_user]['acl']
                if not acl:
                    acl = "rwx"
            else:
                acl = "rwx"

            cmd = ("setfacl -m user:%s:%s %s" % (smb_user, acl,
                                                 mount_obj.mountpoint))
            ret, _, _ = g.run(mnode, cmd)
            if ret != 0:
                g.log.error("Unable to provide required permissions to the "
                            "smb user %s ", smb_user)
                return False
            g.log.info("Successfully provided required permissions to the "
                       "smb user %s ", smb_user)

    # Verify SMB/CIFS share  can be accessed by the user

    # Unmount the temp mount created
    ret = mount_obj.unmount()
    if not ret:
        g.log.error("Unable to unmount the temp mount")
    g.log.info("Successfully unmounted the temp mount")

    return True


def share_volume_over_smb(mnode, volname, smb_users_info):
    """Sharing volumes over SMB

    Args:
        mnode (str): Node on which commands has to be executed.
        volname (str): Name of the volume to be shared.
        smb_users_info (dict): Dict containing users info. Example:
            smb_users_info = {
                'root': {'password': 'foobar',
                         'acl': ''
                         },
                'user1': {'password': 'abc',
                          'acl': ''
                          },
                'user2': {'password': 'xyz',
                          'acl': ''
                          }
                }

    Returns:
        bool : True on successfully sharing the volume over SMB.
            False otherwise
    """
    g.log.info("Start sharing the volume over SMB")

    # Set volume option 'stat-prefetch' to 'on'.
    cmd = "gluster volume set %s stat-prefetch on" % volname
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option stat-prefetch on")
        return False
    g.log.info("Successfully set 'stat-prefetch' to 'on' on %s", volname)

    # Set volume option 'server.allow-insecure' to 'on'.
    cmd = "gluster volume set %s server.allow-insecure on" % volname
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option server-allow-insecure")
        return False
    g.log.info("Successfully set 'server-allow-insecure' to 'on' on %s",
               volname)

    # Set 'storage.batch-fsync-delay-usec' to 0.
    # This is to ensure ping_pong's lock and I/O coherency tests works on CIFS.
    cmd = ("gluster volume set %s storage.batch-fsync-delay-usec 0" % volname)
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option "
                    "'storage.batch-fsync-delay-usec' to 0 on %s", volname)
        return False
    g.log.info("Successfully set 'storage.batch-fsync-delay-usec' to 0 on %s",
               volname)

    # Verify if the volume can be accessed from the SMB/CIFS share.
    cmd = ("smbclient -L localhost -U | grep -i -Fw gluster-%s " % volname)
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("volume '%s' not accessible via SMB/CIFS share", volname)
        return False
    g.log.info("volume '%s' can be accessed from SMB/CIFS share", volname)

    # To verify if the SMB/CIFS share can be accessed by the root/non-root user
    # TBD

    # Enable mounting volumes over SMB
    ret = enable_mounting_volume_over_smb(mnode, volname, smb_users_info)
    if not ret:
        g.log.error("Failed to enable mounting volumes using SMB")
        return False
    g.log.info("Successfully enabled mounting volumes using SMV for the "
               "smbusers: %s", str(smb_users_info.keys()))

    # Verify if volume is shared
    ret = is_volume_exported(mnode, volname, "smb")
    if not ret:
        g.log.info("Volume %s is not exported as 'cifs/smb' share", volname)
        return False
    g.log.info("Volume %s is exported as 'cifs/smb' share", volname)

    return True