Merge "Add extended parameter for cluster-status"

This commit is contained in:
Zuul 2024-04-03 09:14:20 +00:00 committed by Gerrit Code Review
commit fab3470389
4 changed files with 30 additions and 6 deletions

View File

@ -36,6 +36,12 @@ cluster-status:
description: | description: |
JSON dump of the cluster schema and status. This action can be used to JSON dump of the cluster schema and status. This action can be used to
determine the RW and RO instances. determine the RW and RO instances.
params:
extended:
type: integer
default: 0
description: |
Level of information to report back, valid values between 0 and 3 inclusive
reboot-cluster-from-complete-outage: reboot-cluster-from-complete-outage:
description: | description: |
In the case of a complete outage, reboot the cluster from this instance's In the case of a complete outage, reboot the cluster from this instance's

View File

@ -139,7 +139,9 @@ def cluster_status(args):
""" """
try: try:
with charm.provide_charm_instance() as instance: with charm.provide_charm_instance() as instance:
_status = json.dumps(instance.get_cluster_status()) _extended = ch_core.hookenv.action_get("extended")
_status = json.dumps(
instance.get_cluster_status(extended=_extended))
ch_core.hookenv.action_set({"cluster-status": _status}) ch_core.hookenv.action_set({"cluster-status": _status})
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
ch_core.hookenv.action_set({ ch_core.hookenv.action_set({
@ -147,6 +149,13 @@ def cluster_status(args):
"return-code": e.returncode, "return-code": e.returncode,
"traceback": traceback.format_exc()}) "traceback": traceback.format_exc()})
ch_core.hookenv.action_fail("Cluster status failed") ch_core.hookenv.action_fail("Cluster status failed")
except ValueError as e:
ch_core.hookenv.action_set({
"error": str(e),
"traceback": traceback.format_exc()
})
ch_core.hookenv.action_fail(
"Cluster status failed, invalid values for extended parameter")
def reboot_cluster_from_complete_outage(args): def reboot_cluster_from_complete_outage(args):

View File

@ -1214,7 +1214,7 @@ class MySQLInnoDBClusterCharm(
self.configure_instance(address) self.configure_instance(address)
self.add_instance_to_cluster(address) self.add_instance_to_cluster(address)
def get_cluster_status(self, nocache=False): def get_cluster_status(self, nocache=False, extended=0):
"""Get cluster status """Get cluster status
Return cluster.status() as a dictionary. If cached data exists and is Return cluster.status() as a dictionary. If cached data exists and is
@ -1223,6 +1223,8 @@ class MySQLInnoDBClusterCharm(
:param nocache: Do not return cached data :param nocache: Do not return cached data
:type nocache: Boolean :type nocache: Boolean
:param extended: Extended output for cluster-status
:type extended: integer
:side effect: Calls self.check_mysql_connection :side effect: Calls self.check_mysql_connection
:returns: Dictionary cluster status output :returns: Dictionary cluster status output
:rtype: Union[None, dict] :rtype: Union[None, dict]
@ -1251,12 +1253,18 @@ class MySQLInnoDBClusterCharm(
.format(self._error_str(e)), "ERROR") .format(self._error_str(e)), "ERROR")
return return
if extended < 0 or extended > 3:
raise ValueError(
'The value of extended parameter needs to be between '
'0 and 3 inclusive')
_script = ( _script = (
"shell.connect('{}:{}@{}')\n" "shell.connect('{}:{}@{}')\n"
"cluster = dba.get_cluster('{}')\n" "cluster = dba.get_cluster('{}')\n"
"print(cluster.status())" "print(cluster.status({{'extended': {}}}))"
.format(self.cluster_user, self.cluster_password, .format(self.cluster_user, self.cluster_password,
self.cluster_address, self.cluster_name)) self.cluster_address, self.cluster_name,
extended))
try: try:
output = self.run_mysqlsh_script(_script) output = self.run_mysqlsh_script(_script)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
@ -1264,7 +1272,8 @@ class MySQLInnoDBClusterCharm(
"Failed checking cluster status: {}" "Failed checking cluster status: {}"
.format(self._error_str(e)), "ERROR") .format(self._error_str(e)), "ERROR")
return return
self._cached_cluster_status = json.loads(output.decode("UTF-8")) output = ''.join(str(output.decode('UTF-8')).split('\n'))
self._cached_cluster_status = json.loads(output)
return self._cached_cluster_status return self._cached_cluster_status
@staticmethod @staticmethod

View File

@ -1313,7 +1313,7 @@ class TestMySQLInnoDBClusterCharm(test_utils.PatchHelper):
_script = ( _script = (
"shell.connect('{}:{}@{}')\n" "shell.connect('{}:{}@{}')\n"
"cluster = dba.get_cluster('{}')\n" "cluster = dba.get_cluster('{}')\n"
"print(cluster.status())" "print(cluster.status({{'extended': 0}}))"
.format( .format(
midbc.cluster_user, midbc.cluster_password, midbc.cluster_user, midbc.cluster_password,
midbc.cluster_address, midbc.cluster_name)) midbc.cluster_address, midbc.cluster_name))