summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/geo_rep_ops.py
blob: aa89f19de3b24a91397f127fce0c9baeedc51a54 (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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#  Copyright (C) 2017-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: Library for gluster geo-replication operations
"""

from glusto.core import Glusto as g


def create_shared_storage(mnode):
    """Create shared volume which is necessary for the setup of
       a geo-rep session

    Args:
        mnode(str): Node on which command is to be executed

    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.

    """
    cmd = "gluster volume set all cluster.enable-shared-storage enable"
    return g.run(mnode, cmd)


def georep_createpem(mnode):
    """ Creates a common pem pub file on all the nodes in the master and
        is used to implement the passwordless SSH connection
    Args:
        mnode (str): Node on which cmd is to be executed
    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.

    """
    cmd = "gluster system:: execute gsec_create"
    return g.run(mnode, cmd)


def georep_ssh_keygen(mnode):
    """ Creates a pair of ssh private and public key if not present

    Args:
        mnode (str): Node on which cmd is to be executed
    Returns:
        bool : True if ssh-keygen is successful on all servers.
            False otherwise. It also returns True if ssh key
            is already present

    """
    cmd = 'echo -e "n" | ssh-keygen -f ~/.ssh/id_rsa -q -N ""'
    ret, out, _ = g.run(mnode, cmd)
    if ret and "already exists" not in out:
        return False
    return True


def georep_ssh_copyid(mnode, tonode, user, passwd):
    """ Copies the default ssh public key onto tonode's
        authorized_keys file

    Args:
        mnode (str): Node on which cmd is to be executed
        tonode (str): Node to which ssh key is to be copied
        user (str): user of tonode
        passwd (str): passwd of the user of tonode
    Returns:
        bool : True if ssh-copy-id is successful to tonode.
            False otherwise. It also returns True if ssh key
            is already present

    """
    cmd = ('sshpass -p "%s" ssh-copy-id -o StrictHostKeyChecking=no %s@%s' %
           (passwd, user, tonode))
    ret, _, _ = g.run(mnode, cmd)
    if ret:
        return False
    return True


def georep_groupadd(servers, groupname):
    """ Creates a group in all the slave nodes where a user will be added
        to set up a non-root session

    Args:
        servers (list): list of nodes on which cmd is to be executed
        groupname (str): Specifies a groupname

    Returns:
        bool : True if add group is successful on all servers.
            False otherwise.

    """
    cmd = "groupadd %s" % groupname
    results = g.run_parallel(servers, cmd)

    _rc = True
    for server, ret_value in results.iteritems():
        retcode, _, err = ret_value
        if retcode != 0 and "already exists" not in err:
            g.log.error("Unable to add group %s on server %s",
                        groupname, server)
            _rc = False
    if not _rc:
        return False

    return True


def georep_geoaccount(servers, groupname, groupaccount):
    """ Creates a user account with which the geo-rep session can be securely
        set up

    Args:
        servers (list): list of nodes on which cmd is to be executed
        groupname (str): Specifies a groupname
        groupaccount (str): Specifies the user account to set up geo-rep

    Returns:
        bool : True if user add is successful on all servers.
            False otherwise.

    """
    cmd = "useradd -G %s %s" % (groupname, groupaccount)
    results = g.run_parallel(servers, cmd)

    _rc = True
    for server, ret_value in results.iteritems():
        retcode, _, err = ret_value
        if retcode != 0 and "already exists" not in err:
            g.log.error("Unable to add user on %s", server)
            _rc = False
    if not _rc:
        return False

    return True


def georep_geoaccount_setpasswd(servers, groupname, groupaccount, passwd):
    """ Creates a user account with which the geo-rep session can be securely
        set up

    Args:
        servers (list): list of nodes on which cmd is to be executed
        groupname (str): Specifies a groupname
        groupaccount (str): Specifies the user account to set up geo-rep
        passwd (str): Specifies password for they groupaccount

    Returns:
        bool : True if password set is successful on all servers.
            False otherwise.

    """
    cmd = "echo %s:%s | chpasswd" % (groupaccount, passwd)
    results = g.run_parallel(servers, cmd)

    _rc = True
    for server, ret_value in results.iteritems():
        retcode, _, err = ret_value
        if retcode != 0:
            g.log.error("Unable to set passwd for user %s on %s",
                        groupaccount, server)
            _rc = False
    if not _rc:
        return False

    return True


def georep_mountbroker_setup(mnode, groupname, directory):
    """ Sets up mountbroker root directory and group

    Args:
        mnode (str): Node on which command is to be executed
        groupname (str) : Specifies the groupname used
        directory (str) : Specifies mountbroker root directory

    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.

    """
    cmd = "gluster-mountbroker setup %s %s" % (directory, groupname)
    return g.run(mnode, cmd)


def georep_mountbroker_adduser(mnode, slavevol, useraccount):
    """ Adds the volume and user to the mountbroker

    Args:
        mnode (str): Node on which command is to be executed
        slavevol (str) : The slave volume name
        useraccount (str): The user with which geo-rep is to be set up

    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.

    """
    cmd = "gluster-mountbroker add %s %s" % (slavevol, useraccount)
    return g.run(mnode, cmd)


def georep_mountbroker_status(mnode):
    """ Displays the status of every peer node in the slave cluster

    Args:
        mnode (str): Node on which command is to be executed


    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.

    """
    cmd = "gluster-mountbroker status"
    return g.run(mnode, cmd)


def georep_set_pemkeys(mnode, useraccount, mastervol, slavevol):
    """ Sets geo-rep pem keys

    Args:
        mnode (str): Node on which command is to be executed
        useraccount (str) : User with which geo-rep is to be set up
        mastervol (str) : The master volume
        slavevol (str): The slave volume

    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.

    """
    cmd = ("/usr/libexec/glusterfs/set_geo_rep_pem_keys.sh %s %s %s" %
           (useraccount, mastervol, slavevol))
    return g.run(mnode, cmd)


def georep_status(mnode, mastervol, slaveip, slavevol, user=None):
    """Shows the status of the geo-replication session
    Args:
        mnode (str): Node on which cmd is to be executed
        mastervol (str):The name of the master volume
        slaveip (str): SlaveIP
        slavevol(str): The name of the slave volume
    Kwargs:
        user (str): If not set, the default is a root-user
            If specified, non-root user participates in geo-rep
            session
    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.

    """
    if user:
        cmd = ("gluster volume geo-replication %s %s@%s::%s status" %
               (mastervol, user, slaveip, slavevol))
    else:
        cmd = ("gluster volume geo-replication %s %s::%s status" %
               (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)


def georep_create(mnode, mastervol, slaveip, slavevol, user=None, force=False):
    """Pushes the keys to all the slave nodes and creates a geo-rep session
    Args:
        mnode (str) : Node on which cmd is to be executed
        mastervol (str) : The name of the mastervol
        slaveip (str): SlaveIP
        slavevol (str) The name of the slave volume
    kwargs:
        force (bool): If this option is set to True, then create geo-rep
            session will be executed with the force option.
            If it is set to False, then the geo-rep session is created
            without the force option
        user (str): If not set, the default is a root-user
            If specified, non-root user participates in the geo-rep
            session

    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.

    """
    if user:
        if force:
            cmd = ("gluster volume geo-replication %s %s@%s::%s create "
                   "push-pem force" % (mastervol, user, slaveip, slavevol))
        else:
            cmd = ("gluster volume geo-replication %s %s@%s::%s create "
                   "push-pem" % (mastervol, user, slaveip, slavevol))
    else:
        if force:
            cmd = ("gluster volume geo-replication %s %s::%s create "
                   "push-pem force" % (mastervol, slaveip, slavevol))
        else:
            cmd = ("gluster volume geo-replication %s %s::%s create push-pem" %
                   (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)


def georep_config_get(mnode, mastervol, slaveip, slavevol, config_key):
    """ All the available configurable geo-rep options can be got
        using the config_key and seeing what it has been set to

    Args:
        mnode (str) : Node on which cmd is to be executed
        mastervol (str) : The name of the mastervol
        slaveip (str): SlaveIP
        slavevol (str) The name of the slave volume
        config_key (str): The configurable options available in geo-replication
    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. In this case, it contains value of
            config.

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

    """
    cmd = ("gluster volume geo-replication %s %s::%s config %s" %
           (mastervol, slaveip, slavevol, config_key))
    return g.run(mnode, cmd)


def georep_config_set(mnode, mastervol, slaveip, slavevol, config, value):
    """ All the available configurable geo-rep options can be set with a
        specific command if required or
        just with the config parameter
    Args:
        mnode (str) : Node on which cmd is to be executed
        mastervol (str) : The name of the mastervol
        slaveip (str): SlaveIP
        slavevol (str) The name of the slave volume
        config (str): The configurable options available in geo-replication
    Kwargs:
        value (str): The value for the geo-rep config
    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.

    """
    cmd = ("gluster volume geo-replication %s %s::%s config %s %s" %
           (mastervol, slaveip, slavevol, config, value))
    return g.run(mnode, cmd)


def georep_start(mnode, mastervol, slaveip, slavevol, user=None, force=False):
    """Starts the Geo-replication session
    Args:
        mnode (str): Node on which cmd is to be executed
        mastervol (str):The name of the master volume
        slaveip (str): SlaveIP
        slavevol (str): The name of the slave volume

    kwargs:
        force (bool): If this option is set to True, then the geo-rep
            session will be started with the force option.
            If it is set to False, then the session will be started
            without the force option -- which is the default option
        user (str): If not set, the default is a root-user
            If specified, non-root user participates in the geo-rep
            session

    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.

    """
    if user:
        if force:
            cmd = ("gluster volume geo-replication %s %s@%s::%s start force" %
                   (mastervol, user, slaveip, slavevol))
        else:
            cmd = ("gluster volume geo-replication %s %s@%s::%s start" %
                   (mastervol, user, slaveip, slavevol))
    else:
        if force:
            cmd = ("gluster volume geo-replication %s %s::%s start force" %
                   (mastervol, slaveip, slavevol))
        else:
            cmd = ("gluster volume geo-replication %s %s::%s start" %
                   (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)


def georep_stop(mnode, mastervol, slaveip, slavevol, user=None, force=False):
    """Stops a geo-repication session

    Args:
        mnode (str): Node on which cmd is to be executed
        mastervol (str):The name of the master volume
        slaveip (str): SlaveIP
        slavevol (str): The name of the slave volume
    kwargs:
        force (bool): If this option is set to True, then the geo-rep
            session will be stopped with the force option.
            If it is set to False, then the session will be stopped
            without the force option --which is the default option
        user (str): If not set, the default is a root-user
            If specified, non-root user participates in the geo-rep
            session

    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.

    """
    if user:
        if force:
            cmd = ("gluster volume geo-replication %s %s@%s::%s stop force" %
                   (mastervol, user, slaveip, slavevol))
        else:
            cmd = ("gluster volume geo-replication %s %s@%s::%s stop" %
                   (mastervol, user, slaveip, slavevol))
    else:
        if force:
            cmd = ("gluster volume geo-replication %s %s::%s stop force" %
                   (mastervol, slaveip, slavevol))
        else:
            cmd = ("gluster volume geo-replication %s %s::%s stop" %
                   (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)


def georep_pause(mnode, mastervol, slaveip, slavevol, user=None):
    """Pauses the geo-replication session
    Args:
        mnode (str): Node on which cmd is to be executed
        mastervol (str):The name of the master volume
        slaveip (str): SlaveIP
        slavevol (str): The name of the slave volume
    Kwargs:
        user (str): If not set, the default is a root-user
        If specified, non-root user participates in geo-rep
        session
    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.
    """
    if user:
        cmd = ("gluster volume geo-replication %s %s@%s::%s pause" %
               (mastervol, user, slaveip, slavevol))
    else:
        cmd = ("gluster volume geo-replication %s %s::%s pause" %
               (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)


def georep_resume(mnode, mastervol, slaveip, slavevol, user=None):
    """Resumes the geo-replication session
    Args:
        mnode (str): Node on which cmd is to be executed
        mastervol (str):The name of the master volume
        slaveip (str): SlaveIP
        slavevol (str): The name of the slave volume
    Kwargs:
        user (str): If not set, the default is a root-user
        If specified, non-root user participates in geo-rep
        session
    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.

    """
    if user:
        cmd = ("gluster volume geo-replication %s %s@%s::%s resume" %
               (mastervol, user, slaveip, slavevol))
    else:
        cmd = ("gluster volume geo-replication %s %s::%s resume" %
               (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)


def georep_delete(mnode, mastervol, slaveip, slavevol, user=None):
    """Deletes the geo-replication session
    Args:
        mnode (str): Node on which cmd is to be executed
        mastervol (str):The name of the master volume
        slaveip (str): SlaveIP
        slavevol (str): The name of the slave volume
    Kwargs:
        user (str): If not set, the default is a root-user
            If specified, non-root user participates in geo-rep
            session
    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.

    """
    if user:
        cmd = ("gluster volume geo-replication %s %s@%s::%s delete" %
               (mastervol, user, slaveip, slavevol))
    else:
        cmd = ("gluster volume geo-replication %s %s::%s delete" %
               (mastervol, slaveip, slavevol))
    return g.run(mnode, cmd)