From dd8819806d41e66b77fa50d4501377570712463e Mon Sep 17 00:00:00 2001 From: Dragomir Penev Date: Tue, 24 Dec 2024 16:22:34 +0200 Subject: [PATCH] Promote via existing action --- actions.yaml | 9 ++++++--- src/charm.py | 32 ++++++++++++++++++++---------- src/relations/async_replication.py | 5 +---- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/actions.yaml b/actions.yaml index e616fe5cd3..7069c4668e 100644 --- a/actions.yaml +++ b/actions.yaml @@ -33,8 +33,13 @@ list-backups: pre-upgrade-check: description: Run necessary pre-upgrade checks and preparations before executing a charm refresh. promote-to-primary: - description: Promotes the cluster of choice to a primary cluster. Must be ran against the leader unit. + description: Promotes the cluster of choice to a primary cluster. Must be ran against the leader unit when promoting a cluster + or against the unit to be promoted within the cluster. params: + scope: + type: string + default: cluster + description: Whether to promote a unit or a cluster. Must be set to either unit or cluster. force: type: boolean description: Force the promotion of a cluster when there is already a primary cluster. @@ -65,5 +70,3 @@ set-tls-private-key: private-key: type: string description: The content of private key for communications with clients. Content will be auto-generated if this option is not specified. -experimental-set-raft-candidate: - description: (Experimental) set raft candidate diff --git a/src/charm.py b/src/charm.py index f65f289bfa..1845c50cba 100755 --- a/src/charm.py +++ b/src/charm.py @@ -185,10 +185,7 @@ def __init__(self, *args): self.framework.observe(self.on.start, self._on_start) self.framework.observe(self.on.get_password_action, self._on_get_password) self.framework.observe(self.on.set_password_action, self._on_set_password) - self.framework.observe( - self.on.experimental_set_raft_candidate_action, - self._on_experimental_set_raft_candidate, - ) + self.framework.observe(self.on.promote_to_primary_action, self._on_promote_to_primary) self.framework.observe(self.on.update_status, self._on_update_status) self.cluster_name = self.app.name self._member_name = self.unit.name.replace("/", "-") @@ -1507,13 +1504,26 @@ def _on_set_password(self, event: ActionEvent) -> None: event.set_results({"password": password}) - def _on_experimental_set_raft_candidate(self, event: ActionEvent) -> None: - if self.has_raft_keys(): - self.unit_peer_data.update({"raft_candidate": "True"}) - if self.unit.is_leader(): - self._raft_reinitialisation() - return - event.fail("Raft is not stuck.") + def _on_promote_to_primary(self, event: ActionEvent) -> None: + if event.params.get("scope") == "cluster": + return self.async_replication.promote_to_primary(event) + elif event.params.get("scope") == "unit": + return self.promote_primary_unit(event) + else: + event.fail("Scope should be either cluster or unit") + + def promote_primary_unit(self, event: ActionEvent) -> None: + """Handles promote to primary for unit scope.""" + if event.params.get("force"): + if self.has_raft_keys(): + self.unit_peer_data.update({"raft_candidate": "True"}) + if self.unit.is_leader(): + self._raft_reinitialisation() + return + event.fail("Raft is not stuck.") + else: + # TODO Regular promotion + pass def _on_update_status(self, _) -> None: """Update the unit status message and users list in the database.""" diff --git a/src/relations/async_replication.py b/src/relations/async_replication.py index ebfa06752d..db40602cea 100644 --- a/src/relations/async_replication.py +++ b/src/relations/async_replication.py @@ -106,9 +106,6 @@ def __init__(self, charm): self.framework.observe( self.charm.on.create_replication_action, self._on_create_replication ) - self.framework.observe( - self.charm.on.promote_to_primary_action, self._on_promote_to_primary - ) self.framework.observe(self.charm.on.secret_changed, self._on_secret_changed) @@ -583,7 +580,7 @@ def _on_create_replication(self, event: ActionEvent) -> None: # Set the status. self.charm.unit.status = MaintenanceStatus("Creating replication...") - def _on_promote_to_primary(self, event: ActionEvent) -> None: + def promote_to_primary(self, event: ActionEvent) -> None: """Promote this cluster to the primary cluster.""" if ( self.charm.app.status.message != READ_ONLY_MODE_BLOCKING_MESSAGE