Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ratushnyy committed Nov 28, 2023
1 parent 5d118b3 commit 93500d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
40 changes: 23 additions & 17 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
ServiceInfo,
)
from pymongo.errors import PyMongoError
from tenacity import before_log, retry, stop_after_attempt, wait_fixed
from tenacity import Retrying, before_log, retry, stop_after_attempt, wait_fixed

from config import Config
from exceptions import AdminUserCreationError, MissingSecretError
Expand Down Expand Up @@ -604,20 +604,6 @@ def _on_secret_changed(self, event):
# END: actions

# BEGIN: user management

@retry(
stop=_user_creation_stop_condition,
wait=wait_fixed(USER_CREATION_COOLDOWN),
reraise=True,
before_sleep=_before_sleep_user_creation,
)
def _initialize_users(self, event: StartEvent) -> None:
logger.info("User initialization")
self._init_operator_user()
self._init_monitor_user()
self._init_backup_user()
self.client_relations.oversee_users(None, event)

@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(5),
Expand Down Expand Up @@ -788,8 +774,23 @@ def _initialise_replica_set(self, event: StartEvent) -> None:

# Check replica set status before creating users
while not direct_mongo.client.admin.command("hello")["isWritablePrimary"]:
logger.info("Mongo replica set is not ready yet. Sleeping for %d", REPLICA_SET_INIT_CHECK_TIMEOUT)
time.sleep(REPLICA_SET_INIT_CHECK_TIMEOUT)
self._initialize_users(event)

for attempt in Retrying(
stop=_user_creation_stop_condition,
wait=wait_fixed(USER_CREATION_COOLDOWN),
reraise=True,
before_sleep=_before_sleep_user_creation,
):
with attempt:
logger.info("User initialization. Attempt #%d", attempt.retry_state.attempt_number)
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
Expand Down Expand Up @@ -853,7 +854,12 @@ def set_secret(self, scope: Scopes, key: str, value: Optional[str]) -> Optional[
else:
content = secret.get_content()
content.update({key: value})
secret.set_content(content)
try:
secret.set_content(content)
except Exception as e:
logger.exception(e)
logger.exception("CONTENT: %s", content)
raise e
return label

def remove_secret(self, scope, key) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async def test_no_password_change_on_invalid_password(ops_test: OpsTest) -> None
password1 = await get_password(ops_test, unit_id=leader_id, username="monitor")

# The password has to be minimum 3 characters
await set_password(ops_test, unit_id=leader_id, username="monitor", password="ca" * 1000000)
await set_password(ops_test, unit_id=leader_id, username="monitor", password="ca" * 2048)
password2 = await get_password(ops_test, unit_id=leader_id, username="monitor")

# The password didn't change
Expand Down

0 comments on commit 93500d6

Please sign in to comment.