summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/glusterfind_ops.py
blob: 90cd3305d4f7f8e708481f96cd509c876a31502b (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
533
534
535
536
#!/usr/bin/env python
#  Copyright (C) 2019  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 glusterfind operations.
"""

from glusto.core import Glusto as g


def gfind_create(mnode, volname, sessname, debug=False, resetsesstime=False,
                 force=False):
    """Creates a glusterfind session for the given volume.

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        sessname (str): session name

    Kwargs:
        debug (bool): If this option is set to True, then
            the command will be run with debug mode. If this option is
            set to False, then the command will not be run with debug mode.
        resetsesstime (bool): If this option is set to True, then
            the session time will be forced to be reset to the current time
            and the next incremental will start from this time. If this option
            is set to False then the session time will not be reset.
        force (bool): If this option is set to True, then glusterfind
            create will get execute with force option. If it is set to False,
            then glusterfind create will get executed without force option.

    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.

    Example:
        gfind_create("abc.com", testvol, testsession)
        >>> (0, 'Session testsession created with volume alpha\n', '')
    """

    params = ''
    if debug:
        params = params + ' --debug'

    if resetsesstime:
        params = params + ' --reset-session-time'

    if force:
        params = params + ' --force'

    cmd = "glusterfind create %s %s %s" % (sessname, volname, params)
    return g.run(mnode, cmd)


def gfind_delete(mnode, volname, sessname, debug=False):
    """Deletes the given session

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        sessname (str): session name

    Kwargs:
        debug (bool): If this option is set to True, then
            the command will be run with debug mode. If this option is
            set to False, then the command will not be run with debug mode.

    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.

    Example:
        gfind_delete("abc.com", testvol, testsession)
    """

    params = ''
    if debug:
        params = params + ' --debug'

    cmd = "glusterfind delete %s %s %s" % (sessname, volname, params)
    return g.run(mnode, cmd)


def gfind_list(mnode, volname=None, sessname=None, debug=False):
    """Lists the sessions created

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

    Kwargs:
        volname (str): volume name. If this option is provided then
            the command will be run with the '--volume volname' option.
        sessname (str): session name. If this option is provided then
            the command will be run with the '--session sessname' option.
        debug (bool): If this option is set to True, then
            the command will be run with debug mode. If this option is
            set to False, then the command will not be run with debug mode.

    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.

    Example:
        gfind_list("abc.com", testvol, testsession)
    """

    params = ''

    if not volname:
        volname = ''

    if volname:
        params = params + (" --volume %s" % volname)

    if not sessname:
        sessname = ''

    if sessname:
        params = params + (" --session %s" % sessname)

    if debug:
        params = params + ' --debug'

    cmd = "glusterfind list %s" % params
    return g.run(mnode, cmd)


def gfind_pre(mnode, volname, sessname, outfile='', **kwargs):
    """Retrieve the modified files and directories and store it in the outfile.

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        sessname (str): session name
        outfile (str): This is the incremental list of modified files.

    Kwargs:

        **kwargs
            The keys, values in kwargs are:
                - full: (bool)|False
                - tagforfullfind: (str)|None
                - gftype: (str)|None
                - outprefix: (str)|None
                - fieldsep: (str)|None
                - debug: (bool)|False
                - noencode: (bool)|False
                - disablepartial: (bool)|False
                - namespace: (bool)|False
                - regenoutfile: (bool)|False

        Where:
        full (bool): If this option is set to True, then the command will be
            run with '--full' option and a full find will be performed.
            If this option is set to False, then the command will be run
            without the '--full' option.
        tagforfullfind (str): When running the command with '--full' option,
            a subset of files can be retrieved according to a tag.
        gftype (str): 'Type' option specifies the finding the list of files
            or directories only. If the value is set to 'f' then only the file
            types will be listed. If the value is set to 'd' then only the
            directory types will be listed. If the value is set to 'both' then
            the files and directories both will be listed.
        outprefix (str): Prefix to the path/name specified in the outfile.
        fieldsep (str): field-separator specifies the character/s that
            glusterfind output uses to separate fields
        debug (bool): If this option is set to True, then
            the command will be run with debug mode. If this option is
            set to False, then the command will not be run with debug mode.
        noencode (bool): If this option is set to True, then it disables
            encoding of file paths. If this option is set to False, then the
            command will run without --no-encode option.
        disablepartial (bool): If this option is set to True, then the
            partial-find feature will be disabled. If this option is set to
            False, then the default value will be respected.
        namespace (bool): If this option is set to True, then the command
            will be run with '--N' option and only namespace changes will
            be listed. If this option is set to False, then the command will
            be run without the '--N' option.
        regenoutfile (bool): If this option is set to True, then the outfile
            will be regenerated. If this option is set to False, then the
            outfile will not be regenerated.

    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.

            (-1, None, None): If an invalid option is used in the command.

    Example:
        gfind_pre("abc.com", testvol, testsession, outfile=/newoutfile.txt)
    """

    outprefix = fieldsep = tagforfullfind = gftype = None
    full = debug = noencode = disablepartial = regenoutfile = namespace = False
    params = ''

    if 'outprefix' in kwargs:
        outprefix = str(kwargs['outprefix'])

    if 'fieldsep' in kwargs:
        fieldsep = str(kwargs['fieldsep'])

    if 'full' in kwargs:
        full = bool(kwargs['full'])

    if 'tagforfullfind' in kwargs:
        tagforfullfind = str(kwargs['tagforfullfind'])

    if 'gftype' in kwargs:
        gftype = str(kwargs['gftype'])

    if 'debug' in kwargs:
        debug = bool(kwargs['debug'])

    if 'noencode' in kwargs:
        noencode = bool(kwargs['noencode'])

    if 'disablepartial' in kwargs:
        disablepartial = bool(kwargs['disablepartial'])

    if 'regenoutfile' in kwargs:
        regenoutfile = bool(kwargs['regenoutfile'])

    if 'namespace' in kwargs:
        namespace = bool(kwargs['namespace'])

    if outfile == '':
        g.log.error("Invalid command: Outfile needs to be provided in order"
                    " for the pre command to run")
        return (-1, None, None)

    if outfile != '':
        params = params + (" %s" % outfile)

    if outprefix:
        params = params + (" --output-prefix %s" % outprefix)

    if fieldsep:
        params = params + (" --field-separator '%s'" % fieldsep)

    if not full and gftype:
        if gftype == 'both':
            params = params + ' --type both'
        else:
            g.log.error("Invalid command: The '--type' option with 'f' or "
                        "'d' as values can only be used along with "
                        "'--full' option")
            return (-1, None, None)

    if not gftype:
        gftype = ''

    if full:
        params = params + ' --full'

        gftypelist = ['f', 'd', 'both', '']
        if gftype in gftypelist:
            if gftype != '':
                params = params + (" --type %s" % gftype)
        else:
            g.log.error("Invalid value for the '--type' option of the "
                        "glusterfind pre command. Choose among 'f/d/both'.")
            return (-1, None, None)

        if tagforfullfind:
            params = params + (" --tag-for-full-find %s" % tagforfullfind)

    if debug:
        params = params + ' --debug'

    if noencode:
        params = params + ' --no-encode'

    if disablepartial:
        params = params + ' --disable-partial'

    if regenoutfile:
        params = params + ' --regenerate-outfile'

    if namespace:
        params = params + ' -N'

    cmd = "glusterfind pre %s %s %s" % (sessname, volname, params)
    return g.run(mnode, cmd)


def gfind_post(mnode, volname, sessname, debug=False):
    """Run to update the session time

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        sessname (str): session name

    Kwargs:
        debug (bool): If this option is set to True, then
            the command will be run with debug mode. If this option is
            set to False, then the command will not be run with debug mode.

    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.

    Example:
        gfind_post("abc.com", testvol, testsession)
    """

    params = ''
    if debug:
        params = params + ' --debug'

    cmd = "glusterfind post %s %s %s" % (sessname, volname, params)
    return g.run(mnode, cmd)


def gfind_query(mnode, volname, outfile='', since='', end='', **kwargs):
    """Get a list of changed files based on a specific timestamp.

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        outfile (str): This is the incremental list of modified files.

    Kwargs:
        since (int): Timestamp from which the files need to be retrieved.
        end (int): Timestamp until which the files need to be retrieved.

        **kwargs:
            The keys, values in kwargs are:
                - full: (bool)|False
                - tagforfullfind: (str)|None
                - gftype: (str)|None
                - outprefix: (str)|None
                - fieldsep: (str)|None
                - debug: (bool)|False
                - noencode: (bool)|False
                - disablepartial: (bool)|False
                - namespace: (bool)|False

        Where:
        full (bool): If this option is set to True, then the command will be
            run with '--full' option and a full find will be performed.
            If this option is set to False, then the command will be run
            without the '--full' option.
        tagforfullfind (str): When running the command with '--full' option,
            a subset of files can be retrieved according to a tag.
        gftype (str): 'Type' option specifies the finding the list of files
            or directories only. If the value is set to 'f' then only the file
            types will be listed. If the value is set to 'd' then only the
            directory types will be listed. If the value is set to 'both' then
            the files and directories both will be listed.
        outprefix (str): Prefix to the path/name specified in the outfile.
        fieldsep (str): field-separator specifies the character/s that
            glusterfind output uses to separate fields
        debug (bool): If this option is set to True, then
            the command will be run with debug mode. If this option is
            set to False, then the command will not be run with debug mode.
        noencode (bool): If this option is set to True, then it disables
            encoding of file paths. If this option is set to False, then the
            command will run without --no-encode option.
        disablepartial (bool): If this option is set to True, then the
            partial-find feature will be disabled. If this option is set to
            False, then the default value will be respected.
        namespace (bool): If this option is set to True, then the command
            will be run with '--N' option and only namespace changes will
            be listed. If this option is set to False, then the command will
            be run without the '--N' option.

    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.

            (-1, None, None): If an invalid option is used in the command.

    Example1:
        gfind_query("abc.com", testvol, outfile=/newoutfile.txt,
                    since=timestamp1, end=timestamp2, full=False)
    Example2:
        gfind_query("abc.com", testvol, outfile=/newoutfile.txt, gftype='f')
            The above example will fail because the
            'full' option is not provided.
    """

    outprefix = fieldsep = tagforfullfind = gftype = None
    full = debug = noencode = disablepartial = namespace = False
    params = ''

    if 'outprefix' in kwargs:
        outprefix = str(kwargs['outprefix'])

    if 'fieldsep' in kwargs:
        fieldsep = str(kwargs['fieldsep'])

    if 'full' in kwargs:
        full = bool(kwargs['full'])

    if 'tagforfullfind' in kwargs:
        tagforfullfind = str(kwargs['tagforfullfind'])

    if 'gftype' in kwargs:
        gftype = str(kwargs['gftype'])

    if 'debug' in kwargs:
        debug = bool(kwargs['debug'])

    if 'noencode' in kwargs:
        noencode = bool(kwargs['noencode'])

    if 'disablepartial' in kwargs:
        disablepartial = bool(kwargs['disablepartial'])

    if 'namespace' in kwargs:
        namespace = bool(kwargs['namespace'])

    if full and since != "" and end != "":
        g.log.error("Invalid command: Glusterfind query accepts either full or"
                    " the since/end timestamps")
        return (-1, None, None)

    if outfile == '':
        g.log.error("Invalid command: Outfile needs to be provided in order"
                    " for the query command to run")
        return (-1, None, None)

    if outfile != '':
        params = params + (" %s" % outfile)

    if not full:
        if since != '':
            params = params + (" --since-time %s" % since)
        if end != '':
            params = params + (" --end-time %s" % end)
        if gftype:
            if gftype == 'both':
                params = params + ' --type both'
            else:
                g.log.error("Invalid command: The '--type' option with 'f' or "
                            "'d' as values can only be used along with "
                            "'--full' option")
                return (-1, None, None)

    if not gftype:
        gftype = ''

    if full:
        params = params + ' --full'

        gftypelist = ['f', 'd', 'both', '']
        if gftype in gftypelist:
            if gftype != '':
                params = params + (" --type %s" % gftype)
        else:
            g.log.error("Invalid value for the '--type' option of the "
                        "glusterfind query command. Choose among 'f/d/both'.")
            return (-1, None, None)

        if tagforfullfind:
            params = params + (" --tag-for-full-find %s" % tagforfullfind)

    if outprefix:
        params = params + (" --output-prefix %s" % outprefix)

    if fieldsep:
        params = params + (" --field-separator '%s'" % fieldsep)

    if debug:
        params = params + ' --debug'

    if noencode:
        params = params + ' --no-encode'

    if disablepartial:
        params = params + ' --disable-partial'

    if namespace:
        params = params + ' -N'

    cmd = "glusterfind query %s %s" % (volname, params)
    return g.run(mnode, cmd)