Skip to content

Commit

Permalink
more feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Aug 19, 2024
1 parent 1455165 commit d1e94d5
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
import json

from exceptions import MissingSecretError

from ops.pebble import PathError, ProtocolError, Layer, APIError


from ops.pebble import PathError, ProtocolError, Layer
from typing import Set, Optional, Dict

from charms.mongodb.v0.config_server_interface import ClusterRequirer
from tenacity import (
Retrying,
retry,
stop_after_attempt,
wait_fixed,
)

from charms.mongos.v0.set_status import MongosStatusHandler

from charms.mongos.v0.set_status import MongosStatusHandler
from charms.mongodb.v0.mongodb_tls import MongoDBTLS
from charms.mongodb.v0.mongodb_secrets import SecretCache
from charms.mongodb.v0.mongodb_secrets import generate_secret_label
Expand Down Expand Up @@ -218,6 +220,11 @@ def remove_secret(self, scope, key) -> None:
content[key] = Config.Secrets.SECRET_DELETED_LABEL
secret.set_content(content)

@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(2),
reraise=True,
)
def stop_mongos_service(self):
"""Stop mongos service."""
container = self.unit.get_container(Config.CONTAINER_NAME)
Expand All @@ -226,14 +233,11 @@ def stop_mongos_service(self):
def restart_charm_services(self):
"""Restart mongos service."""
container = self.unit.get_container(Config.CONTAINER_NAME)
try:
container.stop(Config.SERVICE_NAME)
except APIError:
# stopping a service that is not running results in an APIError which can be ignored.
pass
container.stop(Config.SERVICE_NAME)

container.add_layer(Config.CONTAINER_NAME, self._mongos_layer, combine=True)
container.replan()
for _ in Retrying(stop=stop_after_attempt(3), wait=wait_fixed(2), reraise=True):
container.add_layer(Config.CONTAINER_NAME, self._mongos_layer, combine=True)
container.replan()

def set_database(self, database: str) -> None:
"""Updates the database requested for the mongos user."""
Expand Down Expand Up @@ -308,6 +312,18 @@ def get_mongos_host(self) -> str:
"""
return self.unit_host(self.unit)

def get_mongos_hosts(self) -> Set:
"""Returns the host for mongos as a str.
The host for mongos can be either the Unix Domain Socket or an IP address depending on how
the client wishes to connect to mongos (inside Juju or outside).
"""
hosts = {self.unit_host(self.unit)}
for unit in self.peers_units:
hosts.add(self.unit_host(unit))

return hosts

@staticmethod
def _generate_relation_departed_key(rel_id: int) -> str:
"""Generates the relation departed key for a specified relation id."""
Expand Down Expand Up @@ -411,6 +427,14 @@ def unit_host(self, unit: Unit) -> str:
# END: helper functions

# BEGIN: properties
@property
def peers_units(self) -> list[Unit]:
"""Get peers units in a safe way."""
if not self._peers:
return []
else:
return self._peers.units

@property
def _mongos_layer(self) -> Layer:
"""Returns a Pebble configuration layer for mongos."""
Expand Down Expand Up @@ -487,7 +511,7 @@ def extra_user_roles(self) -> Set[str]:
@property
def mongos_config(self) -> MongosConfiguration:
"""Generates a MongoDBConfiguration object for mongos in the deployment of MongoDB."""
hosts = [self.get_mongos_host()]
hosts = self.get_mongos_hosts()
external_ca, _ = self.tls.get_tls_files(internal=False)
internal_ca, _ = self.tls.get_tls_files(internal=True)

Expand Down

0 comments on commit d1e94d5

Please sign in to comment.