Skip to content

Commit

Permalink
Fix branch used to trigger release to 5/edge (#221)
Browse files Browse the repository at this point in the history
* Split cluster initialization into two patrs:

1. Cluster initialization
2. User creation

* Fix lint issue

* Fix lint issue

* Fix unit tests

* Fix branch used to trigger release to 5/edge

* Update CI build scripts to use 'build withoug cache' workflow

* Free disk space

* Update src/charm.py

Updated doc string for `_initialise_users` function

Co-authored-by: Mia Altieri <[email protected]>

* Fix lint issues

---------

Co-authored-by: Mia Altieri <[email protected]>
  • Loading branch information
dmitry-ratushnyy and MiaAltieri authored Nov 22, 2023
1 parent 5acba15 commit 7e659d0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ jobs:
run: tox run -e unit

build:
name: Build charms
uses: canonical/data-platform-workflows/.github/workflows/build_charms_with_cache.yaml@v5
strategy:
fail-fast: true
matrix:
charms: [".", "tests/integration/ha_tests/application_charm", "tests/integration/relation_tests/application-charm"]
name: Build ${{matrix.charms}} charm
uses: canonical/data-platform-workflows/.github/workflows/build_charm_without_cache.yaml@v5
with:
charmcraft-snap-channel: "latest/edge"
path-to-charm-directory: ${{matrix.charms}}


integration-test:
strategy:
Expand Down Expand Up @@ -78,6 +84,17 @@ jobs:
uses: actions/download-artifact@v3
with:
name: ${{ needs.build.outputs.artifact-name }}
- name: Free disk space
run: |
echo "Free disk space before cleanup"
df -T
# free space in the runner
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf /usr/local/share/boost
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
echo "Free disk space after cleanup"
df -T
- name: Select tests
id: select-tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_5_edge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Release to 5/edge
on:
push:
branches:
- main
- 5/edge

jobs:
lib-check:
Expand Down
64 changes: 58 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ def db_initialised(self, value):
f"'db_initialised' must be a boolean value. Proivded: {value} is of type {type(value)}"
)

@property
def users_initialized(self) -> bool:
"""Check if MongoDB users are created."""
return "users_initialized" in self.app_peer_data

@users_initialized.setter
def users_initialized(self, value):
"""Set the users_initialized flag."""
if isinstance(value, bool):
self.app_peer_data["users_initialized"] = str(value)
else:
raise ValueError(
f"'users_initialized' must be a boolean value. Proivded: {value} is of type {type(value)}"
)

# END: properties

# BEGIN: generic helper methods
Expand Down Expand Up @@ -387,6 +402,7 @@ def _on_start(self, event) -> None:
return

self._initialise_replica_set(event)
self._initialise_users(event)

# mongod is now active
self.unit.status = ActiveStatus()
Expand Down Expand Up @@ -590,6 +606,48 @@ def _on_secret_changed(self, event):
# END: actions

# BEGIN: user management
@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(5),
reraise=True,
before=before_log(logger, logging.DEBUG),
)
def _initialise_users(self, event: StartEvent) -> None:
"""Create users.
User creation can only be completed after the replica set has
been initialised which requires some time.
In race conditions this can lead to failure to initialise users.
To prevent these race conditions from breaking the code, retry on failure.
"""
if not self.db_initialised:
return

if self.users_initialized:
return

# only leader should create users
if not self.unit.is_leader():
return

logger.info("User initialization")

try:
self._init_operator_user()
self._init_backup_user()
self._init_monitor_user()
logger.info("Reconcile relations")
self.client_relations.oversee_users(None, event)
self.users_initialized = True
except ExecError as e:
logger.error("Deferring on_start: exit code: %i, stderr: %s", e.exit_code, e.stderr)
event.defer()
return
except PyMongoError as e:
logger.error("Deferring on_start since: error=%r", e)
event.defer()
return

@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(5),
Expand Down Expand Up @@ -757,12 +815,6 @@ def _initialise_replica_set(self, event: StartEvent) -> None:
try:
logger.info("Replica Set initialization")
direct_mongo.init_replset()
logger.info("User initialization")
self._init_operator_user()
self._init_backup_user()
self._init_monitor_user()
logger.info("Reconcile relations")
self.client_relations.oversee_users(None, event)
except ExecError as e:
logger.error(
"Deferring on_start: exit code: %i, stderr: %s", e.exit_code, e.stderr
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def test_start_already_initialised(self, connection, init_user, provider, defer)
self.harness.charm.unit.get_container = mock_container

self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["users_initialized"] = "True"

self.harness.charm.on.start.emit()

Expand Down Expand Up @@ -394,7 +395,7 @@ def test_start_mongod_error_initalising_user(self, connection, init_user, provid
defer.assert_called()

# verify app data
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
self.assertEqual("users_initialized" in self.harness.charm.app_peer_data, False)

@patch("ops.framework.EventBase.defer")
@patch("charm.MongoDBProvider")
Expand Down Expand Up @@ -423,7 +424,7 @@ def test_start_mongod_error_overseeing_users(self, connection, init_user, provid
defer.assert_called()

# verify app data
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
self.assertEqual("users_initialized" in self.harness.charm.app_peer_data, False)

@patch("ops.framework.EventBase.defer")
@patch("charm.MongoDBConnection")
Expand Down

0 comments on commit 7e659d0

Please sign in to comment.