Adding bootstrap-mysqlrouter action.

Change-Id: I02770615cd2cce4ef8e12546c70e9df5a74848b3
This commit is contained in:
Seyeong Kim 2023-06-23 14:28:55 +09:00
parent b9173b52e9
commit 74bdf5b5d1
3 changed files with 63 additions and 1 deletions

View File

@ -7,3 +7,6 @@ start-mysqlrouter:
restart-mysqlrouter:
description: |
Restart the mysqlrouter daemon
bootstrap-mysqlrouter:
description: |
Bootstrap the mysqlrouter

View File

@ -35,6 +35,7 @@ _add_path(_reactive)
import charms_openstack.charm as charm
import charmhelpers.core as ch_core
import charms_openstack.bus
import charms.reactive as reactive
charms_openstack.bus.discover()
@ -113,11 +114,68 @@ def restart_mysqlrouter(args):
ch_core.hookenv.action_fail("Retart MySQLRouter failed.")
def bootstrap_mysqlrouter(args):
"""Display cluster status
Return cluster.status() as a JSON encoded dictionary
:param args: sys.argv
:type args: sys.argv
:side effect: Calls instance.get_cluster_status
:returns: This function is called for its side effect
:rtype: None
:action return: Dictionary with command output
"""
with charm.provide_charm_instance() as instance:
try:
releations = ch_core.hookenv.relation_ids('db-router')
password = None
db_host = None
for relid in relations:
related_units = ch_core.hookenv.related_units(relid=relid)
for related_unit in related_units:
data = ch_core.hookenv.relation_get(
rid=relid,
unit=related_unit)
if "mysqlrouter_password" in data:
password = data["mysqlrouter_password"].replace('"', '')
if "db_host" in data:
db_host = data["db_host"].replace('"', '')
cmd = [
"/usr/bin/mysqlrouter",
"--user", "mysql",
"--name", ch_core.hookenv.service_name(),
"--bootstrap",
"{}:{}@{}".format("mysql",
password,
db_host),
"--directory", instance.mysqlrouter_working_dir(),
"--conf-use-sockets",
"--conf-bind-address", "127.0.0.1",
"--report-host", instnace.db_router_address(),
"--conf-base-port", str(instance.mysqlrouter_port),
"--force",
]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
instance.assess_status()
ch_core.hookenv.action_set({"outcome": "Success"})
except subprocess.CalledProcessError as e:
ch_core.hookenv.action_set({
"output": e.output,
"return-code": e.returncode,
"traceback": traceback.format_exc()})
ch_core.hookenv.action_fail("Bootstrap MySQLRouter failed.")
# A dictionary of all the defined actions to callables (which take
# parsed arguments).
ACTIONS = {"stop-mysqlrouter": stop_mysqlrouter,
"start-mysqlrouter": start_mysqlrouter,
"restart-mysqlrouter": restart_mysqlrouter}
"restart-mysqlrouter": restart_mysqlrouter,
"bootstrap-mysqlrouter": bootstrap_mysqlrouter,}
def main(args):

View File

@ -0,0 +1 @@
actions.py