Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add options to start and stop group replication #93

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- mysql_replication - added options to `start_group_replication` and `stop_group_replication`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- mysql_replication - added options to `start_group_replication` and `stop_group_replication`
- mysql_replication - add ``start_group_replication`` and ``stop_group_replication`` options (https://github.com/ansible-collections/community.mysql/pull/93).

60 changes: 58 additions & 2 deletions plugins/modules/mysql_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
author:
- Balazs Pocze (@banyek)
- Andrew Klychkov (@Andersson007)
- Sebastian Pfahl (@eryx12o45)
options:
mode:
description:
Expand All @@ -31,6 +32,8 @@
C(resetmaster) (RESET MASTER) - supported since community.mysql 0.1.0,
C(resetslave) (RESET SLAVE),
C(resetslaveall) (RESET SLAVE ALL).
C(start_group_replication) (START GROUP_REPLICATION).
C(stop_group_replication) (STOP GROUP_REPLICATION).
type: str
choices:
- changemaster
Expand All @@ -41,6 +44,8 @@
- resetmaster
- resetslave
- resetslaveall
- start_group_replication
- stop_group_replication
default: getslave
master_host:
description:
Expand Down Expand Up @@ -225,6 +230,10 @@
mode: changemaster
fail_on_error: yes

- name: Start mysql group replication
community.mysql.mysql_replication:
mode: start_group_replication

'''

RETURN = r'''
Expand Down Expand Up @@ -401,12 +410,45 @@ def changemaster(cursor, chm, connection_name='', channel=''):
cursor.execute(query)


def start_group_replication(module, cursor, fail_on_error=False):
query = 'START GROUP_REPLICATION'

try:
executed_queries.append(query)
cursor.execute(query)
started = True
except mysql_driver.Warning as e:
started = False
except Exception as e:
if fail_on_error:
module.fail_json(msg="START GROUP_REPLICATION failed: %s" % to_native(e))
started = False
return started


def stop_group_replication(module, cursor, fail_on_error=False, term='GROUP_REPLICATION'):
query = 'STOP GROUP_REPLICATION'

try:
executed_queries.append(query)
cursor.execute(query)
stopped = True
except mysql_driver.Warning as e:
stopped = False
except Exception as e:
if fail_on_error:
module.fail_json(msg="STOP GROUP_REPLICATION failed: %s" % to_native(e))
stopped = False
return stopped


def main():
argument_spec = mysql_common_argument_spec()
argument_spec.update(
mode=dict(type='str', default='getslave', choices=[
'getmaster', 'getslave', 'changemaster', 'stopslave',
'startslave', 'resetmaster', 'resetslave', 'resetslaveall']),
'startslave', 'resetmaster', 'resetslave', 'resetslaveall',
'start_group_replication', 'stop_group_replication']),
master_auto_position=dict(type='bool', default=False),
master_host=dict(type='str'),
master_user=dict(type='str'),
Expand Down Expand Up @@ -561,7 +603,8 @@ def main():
if started is True:
module.exit_json(msg="Slave started ", changed=True, queries=executed_queries)
else:
module.exit_json(msg="Slave already started (Or cannot be started)", changed=False, queries=executed_queries)
module.exit_json(msg="Slave already started (Or cannot be started)", changed=False,
queries=executed_queries)
elif mode in "stopslave":
stopped = stop_slave(module, cursor, connection_name, channel, fail_on_error, replica_term)
if stopped is True:
Expand All @@ -586,6 +629,19 @@ def main():
module.exit_json(msg="Slave reset", changed=True, queries=executed_queries)
else:
module.exit_json(msg="Slave already reset", changed=False, queries=executed_queries)
elif mode in "start_group_replication":
started = start_group_replication(module, cursor, fail_on_error)
if started is True:
module.exit_json(msg="Group replication started ", changed=True, queries=executed_queries)
else:
module.exit_json(msg="Group replication already started (Or cannot be started)", changed=False,
ueries=executed_queries)
elif mode in "stop_group_replication":
stopped = stop_group_replication(module, cursor, channel, fail_on_error)
if stopped is True:
module.exit_json(msg="Group replication stopped", changed=True, queries=executed_queries)
else:
module.exit_json(msg="Group replication already stopped", changed=False, queries=executed_queries)

warnings.simplefilter("ignore")

Expand Down