Skip to content

Commit 643084e

Browse files
[DPE-4118] Port mysql lib changes from k8s + increment tls lib for local changes (#487)
* Port mysql lib changes from k8s + increment tls lib for local changes * Fix lint warnings * Add a kwargs to force remove instances from cluster + add unit test
1 parent 5327a7a commit 643084e

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

lib/charms/mysql/v0/mysql.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def wait_until_mysql_connection(self) -> None:
128128
# Increment this major API version when introducing breaking changes
129129
LIBAPI = 0
130130

131-
LIBPATCH = 63
131+
LIBPATCH = 64
132132

133133
UNIT_TEARDOWN_LOCKNAME = "unit-teardown"
134134
UNIT_ADD_LOCKNAME = "unit-add"
@@ -1778,6 +1778,27 @@ def _get_host_ip(host: str) -> str:
17781778

17791779
return ",".join(rw_endpoints), ",".join(ro_endpoints), ",".join(no_endpoints)
17801780

1781+
def execute_remove_instance(
1782+
self, connect_instance: Optional[str] = None, force: bool = False
1783+
) -> None:
1784+
"""Execute the remove_instance() script with mysqlsh.
1785+
1786+
Args:
1787+
connect_instance: (optional) The instance from where to run the remove_instance()
1788+
force: (optional) Whether to force the removal of the instance
1789+
"""
1790+
remove_instance_options = {
1791+
"password": self.cluster_admin_password,
1792+
"force": "true" if force else "false",
1793+
}
1794+
remove_instance_commands = (
1795+
f"shell.connect('{self.cluster_admin_user}:{self.cluster_admin_password}@{connect_instance or self.instance_address}')",
1796+
f"cluster = dba.get_cluster('{self.cluster_name}')",
1797+
"cluster.remove_instance("
1798+
f"'{self.cluster_admin_user}@{self.instance_address}', {remove_instance_options})",
1799+
)
1800+
self._run_mysqlsh_script("\n".join(remove_instance_commands))
1801+
17811802
@retry(
17821803
retry=retry_if_exception_type(MySQLRemoveInstanceRetryError),
17831804
stop=stop_after_attempt(15),
@@ -1841,17 +1862,7 @@ def remove_instance(self, unit_label: str, lock_instance: Optional[str] = None)
18411862
)
18421863

18431864
# Just remove instance
1844-
remove_instance_options = {
1845-
"password": self.cluster_admin_password,
1846-
"force": "true",
1847-
}
1848-
remove_instance_commands = (
1849-
f"shell.connect('{self.cluster_admin_user}:{self.cluster_admin_password}@{self.instance_address}')",
1850-
f"cluster = dba.get_cluster('{self.cluster_name}')",
1851-
"cluster.remove_instance("
1852-
f"'{self.cluster_admin_user}@{self.instance_address}', {remove_instance_options})",
1853-
)
1854-
self._run_mysqlsh_script("\n".join(remove_instance_commands))
1865+
self.execute_remove_instance(force=True)
18551866
except MySQLClientError as e:
18561867
# In case of an error, raise an error and retry
18571868
logger.warning(

lib/charms/mysql/v0/tls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
LIBID = "eb73947deedd4380a3a90d527e0878eb"
5454
LIBAPI = 0
55-
LIBPATCH = 5
55+
LIBPATCH = 6
5656

5757
SCOPE = "unit"
5858

poetry.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/unit/test_mysql.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,19 @@ def test_is_instance_configured_for_innodb_exceptions(self, _run_mysqlsh_script)
488488
)
489489
self.assertFalse(is_instance_configured)
490490

491+
@patch("charms.mysql.v0.mysql.MySQLBase._run_mysqlsh_script")
492+
def test_execute_remove_instance(self, _run_mysqlsh_script):
493+
expected_remove_instance_commands = (
494+
"shell.connect('clusteradmin:[email protected]')\n"
495+
"cluster = dba.get_cluster('test_cluster')\n"
496+
"cluster.remove_instance('[email protected]', "
497+
"{'password': 'clusteradminpassword', 'force': 'false'})"
498+
)
499+
500+
self.mysql.execute_remove_instance(connect_instance="1.2.3.4", force=False)
501+
502+
_run_mysqlsh_script.assert_called_once_with(expected_remove_instance_commands)
503+
491504
@patch("charms.mysql.v0.mysql.MySQLBase.get_cluster_node_count")
492505
@patch("charms.mysql.v0.mysql.MySQLBase.get_cluster_primary_address")
493506
@patch("charms.mysql.v0.mysql.MySQLBase._acquire_lock")

0 commit comments

Comments
 (0)