99
1010import jubilant_backports
1111from jubilant_backports import Juju
12- from jubilant_backports .statustypes import Status , UnitStatus
12+ from jubilant_backports .statustypes import Status
1313from tenacity import (
1414 Retrying ,
1515 retry ,
2929JujuAppsStatusFn = Callable [[Status , str ], bool ]
3030
3131
32- def check_mysql_instances_online (juju : Juju , app_name : str ) -> bool :
32+ def check_mysql_instances_online (
33+ juju : Juju ,
34+ app_name : str ,
35+ app_units : list [str ] | None = None ,
36+ ) -> bool :
3337 """Checks whether all MySQL cluster instances are online.
3438
3539 Args:
3640 juju: The Juju instance
3741 app_name: The name of the application
42+ app_units: The list of application units to check
3843 """
39- mysql_app_leader = get_app_leader ( juju , app_name )
40- mysql_app_units = get_app_units (juju , app_name )
44+ if not app_units :
45+ app_units = get_app_units (juju , app_name )
4146
42- mysql_cluster_status = get_mysql_cluster_status (juju , mysql_app_leader )
47+ mysql_cluster_status = get_mysql_cluster_status (juju , app_units [ 0 ] )
4348 mysql_cluster_topology = mysql_cluster_status ["defaultreplicaset" ]["topology" ]
44- assert len (mysql_cluster_topology ) == len (mysql_app_units )
4549
46- return all (member ["status" ] == "online" for member in mysql_cluster_topology .values ())
50+ for unit_name in app_units :
51+ unit_label = get_mysql_instance_label (unit_name )
52+ if mysql_cluster_topology [unit_label ]["status" ] != "online" :
53+ return False
54+
55+ return True
4756
4857
4958async def check_mysql_units_writes_increment (
@@ -57,7 +66,7 @@ async def check_mysql_units_writes_increment(
5766 if not app_units :
5867 app_units = get_app_units (juju , app_name )
5968
60- app_primary = get_mysql_primary_unit (juju , app_name )
69+ app_primary = get_mysql_primary_unit (juju , app_name , app_units [ 0 ] )
6170 app_max_value = await get_mysql_max_written_value (juju , app_name , app_primary )
6271
6372 for unit_name in app_units :
@@ -94,16 +103,16 @@ def get_app_name(juju: Juju, charm_name: str) -> str | None:
94103 raise Exception ("No application name found" )
95104
96105
97- def get_app_units (juju : Juju , app_name : str ) -> dict [str , UnitStatus ]:
106+ def get_app_units (juju : Juju , app_name : str ) -> list [str ]:
98107 """Get the units for the given application."""
99108 model_status = juju .status ()
100109 app_status = model_status .apps [app_name ]
101- return app_status .units
110+ return list ( app_status .units )
102111
103112
104113def scale_app_units (juju : Juju , app_name : str , num_units : int ) -> None :
105114 """Scale a given application to a number of units."""
106- app_units = list ( get_app_units (juju , app_name ) )
115+ app_units = get_app_units (juju , app_name )
107116 app_units_diff = num_units - len (app_units )
108117
109118 if app_units_diff > 0 :
@@ -234,6 +243,11 @@ def get_mysql_cluster_status(juju: Juju, unit: str, cluster_set: bool = False) -
234243 return task .results ["status" ]
235244
236245
246+ def get_mysql_instance_label (unit_name : str ) -> str :
247+ """Builds a MySQL instance label out of a Juju unit name."""
248+ return "-" .join (unit_name .rsplit ("/" , 1 ))
249+
250+
237251def get_mysql_unit_name (instance_label : str ) -> str :
238252 """Builds a Juju unit name out of a MySQL instance label."""
239253 return "/" .join (instance_label .rsplit ("-" , 1 ))
@@ -307,9 +321,6 @@ async def get_mysql_variable_value(
307321
308322def start_mysql_process_gracefully (juju : Juju , unit_name : str ) -> None :
309323 """Start a MySQL process within a machine."""
310- # TODO:
311- # Rely on Jubilant exec command once they fix it
312- # https://github.com/canonical/jubilant/issues/206
313324 juju .ssh (
314325 command = "sudo snap start charmed-mysql.mysqld" ,
315326 target = unit_name ,
@@ -324,9 +335,6 @@ def start_mysql_process_gracefully(juju: Juju, unit_name: str) -> None:
324335
325336def stop_mysql_process_gracefully (juju : Juju , unit_name : str ) -> None :
326337 """Gracefully stop MySQL process."""
327- # TODO:
328- # Rely on Jubilant exec command once they fix it
329- # https://github.com/canonical/jubilant/issues/206
330338 juju .ssh (
331339 command = "sudo pkill mysqld --signal SIGTERM" ,
332340 target = unit_name ,
0 commit comments