Skip to content

Commit

Permalink
Promote via existing action
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Dec 24, 2024
1 parent 55a7a7e commit dd88198
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
9 changes: 6 additions & 3 deletions actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
32 changes: 21 additions & 11 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("/", "-")
Expand Down Expand Up @@ -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."""
Expand Down
5 changes: 1 addition & 4 deletions src/relations/async_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd88198

Please sign in to comment.