Skip to content

Commit

Permalink
pin snap revision + bug fix (#214)
Browse files Browse the repository at this point in the history
## Issue
1. snap revision wasn't pinned
2. charm starts pbm and mongodb exporter before database was started
  • Loading branch information
MiaAltieri committed May 3, 2023
1 parent 32b0045 commit 459c734
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/charms/mongodb/v0/mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def _resync_config_options(self, pbm_snap):
# if a resync is running restart the service
if isinstance(pbm_status, (WaitingStatus)):
pbm_snap.restart(services=["pbm-agent"])
raise PBMBusyError

# wait for re-sync and update charm status based on pbm syncing status. Need to wait for
# 2 seconds for pbm_agent to receive the resync command before verifying.
Expand Down
14 changes: 11 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
# We expect the MongoDB container to use the default ports
MONGODB_PORT = 27017
MONGODB_EXPORTER_PORT = 9216
SNAP_PACKAGES = [("charmed-mongodb", "5/edge")]
SNAP_PACKAGES = [("charmed-mongodb", "5/edge", 82)]
REL_NAME = "database"


Expand Down Expand Up @@ -540,13 +540,15 @@ def _install_snap_packages(self, packages: List[str]) -> None:
Args:
packages: list of packages to install.
"""
for snap_name, snap_channel in packages:
for snap_name, snap_channel, snap_revision in packages:
try:
snap_cache = snap.SnapCache()
snap_package = snap_cache[snap_name]

if not snap_package.present:
snap_package.ensure(snap.SnapState.Latest, channel=snap_channel)
snap_package.ensure(
snap.SnapState.Latest, channel=snap_channel, revision=snap_revision
)

except snap.SnapError as e:
logger.error(
Expand Down Expand Up @@ -594,6 +596,9 @@ def _push_tls_certificate_to_workload(self) -> None:

def _connect_mongodb_exporter(self) -> None:
"""Exposes the endpoint to mongodb_exporter."""
if "db_initialised" not in self.app_peer_data:
return

# must wait for leader to set URI before connecting
if not self.get_secret("app", "monitor-password"):
return
Expand All @@ -605,6 +610,9 @@ def _connect_mongodb_exporter(self) -> None:

def _connect_pbm_agent(self) -> None:
"""Updates URI for pbm-agent."""
if "db_initialised" not in self.app_peer_data:
return

# must wait for leader to set URI before any attempts to update are made
if not self.get_secret("app", "backup-password"):
return
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def test_resync_config_restart(self, pbm_status, retry_stop, retry_wait, check_o
retry_stop.return_value = wait_fixed(1)
pbm_status.return_value = WaitingStatus()
mock_snap = mock.Mock()
self.harness.charm.backups._resync_config_options(mock_snap)

with self.assertRaises(PBMBusyError):
self.harness.charm.backups._resync_config_options(mock_snap)

mock_snap.restart.assert_called()

@patch("charm.subprocess.check_output")
Expand Down

0 comments on commit 459c734

Please sign in to comment.