Skip to content

Commit dfefc76

Browse files
committed
Add options to start and stop group replication
1 parent 67f1460 commit dfefc76

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- mysql_replication - add ``startgroupreplication`` and ``stopgroupreplication`` options (https://github.com/ansible-collections/community.mysql/pull/647).

plugins/modules/mysql_replication.py

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Balazs Pocze (@banyek)
2121
- Andrew Klychkov (@Andersson007)
2222
- Dennis Urtubia (@dennisurtubia)
23+
- Sebastian Pfahl (@eryx12o45)
2324
- Laurent Indermühle (@laurent-indermuehle)
2425
options:
2526
mode:
@@ -33,7 +34,9 @@
3334
C(stopreplica) (STOP REPLICA),
3435
C(resetprimary) (RESET MASTER) - supported since community.mysql 0.1.0,
3536
C(resetreplica) (RESET REPLICA),
36-
C(resetreplicaall) (RESET REPLICA ALL).
37+
C(resetreplicaall) (RESET REPLICA ALL),
38+
C(startgroupreplication) (START GROUP_REPLICATION) - supported since community.mysql 3.10.0,
39+
C(stopgroupreplication) (STOP GROUP_REPLICATION) - supported since community.mysql 3.10.0.
3740
type: str
3841
choices:
3942
- changeprimary
@@ -45,6 +48,8 @@
4548
- resetprimary
4649
- resetreplica
4750
- resetreplicaall
51+
- startgroupreplication
52+
- stopgroupreplication
4853
default: getreplica
4954
primary_host:
5055
description:
@@ -191,7 +196,16 @@
191196
type: bool
192197
default: false
193198
version_added: '0.1.0'
194-
199+
group_replication_user:
200+
description:
201+
- User for group replication.
202+
type: str
203+
version_added: '3.10.0'
204+
group_replication_password:
205+
description:
206+
- Password for group replication user.
207+
type: str
208+
version_added: '3.10.0'
195209
notes:
196210
- Compatible with MariaDB or MySQL.
197211
- If an empty value for the parameter of string type is needed, use an empty string.
@@ -285,6 +299,17 @@
285299
community.mysql.mysql_replication:
286300
mode: changeprimary
287301
fail_on_error: true
302+
303+
- name: Start mysql group replication
304+
community.mysql.mysql_replication:
305+
mode: startgroupreplication
306+
group_replication_user: group_repl_user
307+
group_replication_password: group_repl_passwd
308+
309+
- name: Stop mysql group replication
310+
community.mysql.mysql_replication:
311+
mode: stopgroupreplication
312+
288313
'''
289314

290315
RETURN = r'''
@@ -465,6 +490,38 @@ def changereplication(cursor, chm, channel=''):
465490
cursor.execute(query)
466491

467492

493+
def startgroupreplication(module, cursor, chm, fail_on_error=False, term='GROUP_REPLICATION'):
494+
query = 'START %s %s' % (term, ','.join(chm))
495+
496+
try:
497+
executed_queries.append(query)
498+
cursor.execute(query)
499+
started = True
500+
except mysql_driver.Warning as e:
501+
started = False
502+
except Exception as e:
503+
if fail_on_error:
504+
module.fail_json(msg="START %s failed: %s" % (term, to_native(e)))
505+
started = False
506+
return started
507+
508+
509+
def stopgroupreplication(module, cursor, fail_on_error=False, term='GROUP_REPLICATION'):
510+
query = 'STOP %s' % term
511+
512+
try:
513+
executed_queries.append(query)
514+
cursor.execute(query)
515+
stopped = True
516+
except mysql_driver.Warning as e:
517+
stopped = False
518+
except Exception as e:
519+
if fail_on_error:
520+
module.fail_json(msg="STOP %s failed: %s" % (term, to_native(e)))
521+
stopped = False
522+
return stopped
523+
524+
468525
def main():
469526
argument_spec = mysql_common_argument_spec()
470527
argument_spec.update(
@@ -477,7 +534,9 @@ def main():
477534
'resetprimary',
478535
'resetreplica',
479536
'resetreplicaall',
480-
'changereplication']),
537+
'changereplication',
538+
'startgroupreplication',
539+
'stopgroupreplication']),
481540
primary_auto_position=dict(type='bool', default=False, aliases=['master_auto_position']),
482541
primary_host=dict(type='str', aliases=['master_host']),
483542
primary_user=dict(type='str', aliases=['master_user']),
@@ -501,6 +560,8 @@ def main():
501560
connection_name=dict(type='str'),
502561
channel=dict(type='str'),
503562
fail_on_error=dict(type='bool', default=False),
563+
group_replication_user=dict(type='str'),
564+
group_replication_password=dict(type='str', no_log=True),
504565
)
505566
module = AnsibleModule(
506567
argument_spec=argument_spec,
@@ -540,6 +601,8 @@ def main():
540601
connection_name = module.params["connection_name"]
541602
channel = module.params['channel']
542603
fail_on_error = module.params['fail_on_error']
604+
group_replication_user = module.params['group_replication_user']
605+
group_replication_password = module.params['group_replication_password']
543606

544607
if mysql_driver is None:
545608
module.fail_json(msg=mysql_driver_fail_msg)
@@ -742,6 +805,24 @@ def main():
742805
module.fail_json(msg='%s. Query == CHANGE REPLICATION SOURCE TO %s' % (to_native(e), chm))
743806
result['changed'] = True
744807
module.exit_json(queries=executed_queries, **result)
808+
elif mode == "startgroupreplication":
809+
chm = []
810+
if group_replication_user is not None:
811+
chm.append(" USER='%s'" % group_replication_user)
812+
if group_replication_password is not None:
813+
chm.append(" PASSWORD='%s'" % group_replication_password)
814+
started = startgroupreplication(module, cursor, chm, fail_on_error)
815+
if started:
816+
module.exit_json(msg="Group replication started ", changed=True, queries=executed_queries)
817+
else:
818+
module.exit_json(msg="Group replication already started (Or cannot be started)", changed=False,
819+
ueries=executed_queries)
820+
elif mode == "stopgroupreplication":
821+
stopped = stopgroupreplication(module, cursor, channel, fail_on_error)
822+
if stopped:
823+
module.exit_json(msg="Group replication stopped", changed=True, queries=executed_queries)
824+
else:
825+
module.exit_json(msg="Group replication already stopped", changed=False, queries=executed_queries)
745826

746827
warnings.simplefilter("ignore")
747828

tests/integration/targets/test_mysql_replication/tasks/mysql_replication_initial.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,5 @@
338338
assert:
339339
that:
340340
-
341-
"result.msg == 'value of mode must be one of: getprimary, getreplica, changeprimary, stopreplica, startreplica, resetprimary, resetreplica, resetreplicaall, changereplication, got: stopslave'"
341+
"result.msg == 'value of mode must be one of: getprimary, getreplica, changeprimary, stopreplica, startreplica, resetprimary, resetreplica, resetreplicaall, changereplication, startgroupreplication, stopgroupreplication, got: stopslave'"
342342
- result is failed

0 commit comments

Comments
 (0)