diff --git a/lib/charms/mongodb/v0/config_server_interface.py b/lib/charms/mongodb/v0/config_server_interface.py index a0316ce33..58908bada 100644 --- a/lib/charms/mongodb/v0/config_server_interface.py +++ b/lib/charms/mongodb/v0/config_server_interface.py @@ -22,6 +22,7 @@ KEY_FILE = "keyFile" HOSTS_KEY = "host" CONFIG_SERVER_DB_KEY = "config-server-db" +MONGOS_SOCKET_URI_FMT = "%2Fvar%2Fsnap%2Fcharmed-mongodb%2Fcommon%2Fvar%2Fmongodb-27018.sock" # The unique Charmhub library identifier, never change it LIBID = "58ad1ccca4974932ba22b97781b9b2a0" @@ -146,11 +147,10 @@ def _on_relation_changed(self, event) -> None: ) # avoid restarting mongos when possible - if not updated_keyfile and not updated_config and self.charm.monogs_initialised: + if not updated_keyfile and not updated_config and self.is_mongos_running(): return # mongos is not available until it is using new secrets - del self.charm.unit_peer_data["mongos_initialised"] logger.info("Restarting mongos with new secrets") self.charm.unit.status = MaintenanceStatus("starting mongos") self.charm.restart_mongos_service() @@ -163,12 +163,11 @@ def _on_relation_changed(self, event) -> None: return # TODO: Follow up PR. Add a user for mongos once it has been started - self.charm.unit_peer_data["mongos_initialised"] = json.dumps(True) self.charm.unit.status = ActiveStatus() def is_mongos_running(self) -> bool: """Returns true if mongos service is running.""" - with MongosConnection(None, "mongodb://localhost:27018") as mongo: + with MongosConnection(None, f"mongodb://{MONGOS_SOCKET_URI_FMT}") as mongo: return mongo.is_ready def update_config_server_db(self, config_server_db) -> bool: diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 6140a7a11..96fe00970 100644 --- a/lib/charms/mongodb/v1/helpers.py +++ b/lib/charms/mongodb/v1/helpers.py @@ -90,15 +90,21 @@ def get_mongos_args( Returns: A string representing the arguments to be passed to mongos. """ + # suborinate charm which provides its own config_server_db, should only use unix domain socket + binding_ips = ( + f"--bind_ip {MONGODB_COMMON_DIR}/var/mongodb-27018.sock" + if config_server_db + else "--bind_ip_all" + ) + # mongos running on the config server communicates through localhost - # use constant for port - config_server_db = config_server_db or f"{config.replset}/localhost:27017" + config_server_db = config_server_db or f"{config.replset}/localhost:{Config.MONGODB_PORT}" full_conf_dir = f"{MONGODB_SNAP_DATA_DIR}{CONF_DIR}" if snap_install else CONF_DIR cmd = [ # mongos on config server side should run on 0.0.0.0 so it can be accessed by other units # in the sharded cluster - "--bind_ip_all", + binding_ips, f"--configdb {config_server_db}", # config server is already using 27017 f"--port {Config.MONGOS_PORT}",