diff --git a/src/actions.yaml b/src/actions.yaml index 9a7f3bb..b7feeb7 100644 --- a/src/actions.yaml +++ b/src/actions.yaml @@ -7,3 +7,6 @@ start-mysqlrouter: restart-mysqlrouter: description: | Restart the mysqlrouter daemon +bootstrap-mysqlrouter: + description: | + Bootstrap the mysqlrouter diff --git a/src/actions/actions.py b/src/actions/actions.py index b76cede..5b1a7c5 100755 --- a/src/actions/actions.py +++ b/src/actions/actions.py @@ -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): diff --git a/src/actions/bootstrap-mysqlrouter b/src/actions/bootstrap-mysqlrouter new file mode 120000 index 0000000..405a394 --- /dev/null +++ b/src/actions/bootstrap-mysqlrouter @@ -0,0 +1 @@ +actions.py \ No newline at end of file