Skip to content

Commit

Permalink
[DPE-2074] sync libs from MongoDB VM charm (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
delgod authored Jul 12, 2023
1 parent 45ae2a9 commit 0d50315
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ wait-remove:

remove:
# remove app from current model
juju remove-application --destroy-storage $(app)
juju remove-application $(app)
juju status --storage --format=json 2>/dev/null \
| jq -r '.storage.volumes[] | select( .status.current == "detached" ) | .storage' \
| xargs juju remove-storage

# redeployment chain
redeploy: remove build wait-remove deploy-local
Expand Down
13 changes: 8 additions & 5 deletions lib/charms/mongodb/v0/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# See LICENSE file for licensing details.

import logging
import os
import secrets
import string
import subprocess
Expand All @@ -20,7 +21,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 6
LIBPATCH = 7


# path to store mongodb ketFile
Expand Down Expand Up @@ -166,13 +167,13 @@ def build_unit_status(mongodb_config: MongoDBConfiguration, unit_ip: str) -> Sta
replica_status = replset_status[unit_ip]

if replica_status == "PRIMARY":
return ActiveStatus("Replica set primary")
return ActiveStatus("Primary")
elif replica_status == "SECONDARY":
return ActiveStatus("Replica set secondary")
return ActiveStatus("")
elif replica_status in ["STARTUP", "STARTUP2", "ROLLBACK", "RECOVERING"]:
return WaitingStatus("Member is syncing..")
return WaitingStatus("Member is syncing...")
elif replica_status == "REMOVED":
return WaitingStatus("Member is removing..")
return WaitingStatus("Member is removing...")
else:
return BlockedStatus(replica_status)
except ServerSelectionTimeoutError as e:
Expand All @@ -188,6 +189,8 @@ def build_unit_status(mongodb_config: MongoDBConfiguration, unit_ip: str) -> Sta

def copy_licenses_to_unit():
"""Copies licenses packaged in the snap to the charm's licenses directory."""
os.makedirs("src/licenses", exist_ok=True)
subprocess.check_output("cp LICENSE src/licenses/LICENSE-charm", shell=True)
subprocess.check_output(
"cp -r /snap/charmed-mongodb/current/licenses/* src/licenses", shell=True
)
7 changes: 4 additions & 3 deletions lib/charms/mongodb/v0/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import re
from dataclasses import dataclass
from typing import Dict, List, Set
from typing import Dict, List, Optional, Set
from urllib.parse import quote_plus

from bson.json_util import dumps
Expand All @@ -29,7 +29,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 6
LIBPATCH = 7

# path to store mongodb ketFile
logger = logging.getLogger(__name__)
Expand All @@ -49,7 +49,7 @@ class MongoDBConfiguration:
"""

replset: str
database: str
database: Optional[str]
username: str
password: str
hosts: Set[str]
Expand Down Expand Up @@ -284,6 +284,7 @@ def create_user(self, config: MongoDBConfiguration):
config.username,
pwd=config.password,
roles=self._get_roles(config),
mechanisms=["SCRAM-SHA-256"],
)

def update_user(self, config: MongoDBConfiguration):
Expand Down
10 changes: 8 additions & 2 deletions lib/charms/mongodb/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def _on_relation_event(self, event):
return

# legacy relations have auth disabled, which new relations require
legacy_relation_users = self._get_users_from_relations(None, rel=LEGACY_REL_NAME)
if len(legacy_relation_users) > 0:
if self.model.get_relation(LEGACY_REL_NAME):
self.charm.unit.status = BlockedStatus("cannot have both legacy and new relations")
logger.error("Auth disabled due to existing connections to legacy relations")
return
Expand Down Expand Up @@ -117,6 +116,13 @@ def oversee_users(self, departed_relation_id: Optional[int], event):
relation is still on the list of all relations. Therefore, for proper
work of the function, we need to exclude departed relation from the list.
"""
# This hook gets called from other contexts within the charm so it is necessary to check
# for legacy relations which have auth disabled, which new relations require
if self.model.get_relation(LEGACY_REL_NAME):
self.charm.unit.status = BlockedStatus("cannot have both legacy and new relations")
logger.error("Auth disabled due to existing connections to legacy relations")
return

with MongoDBConnection(self.charm.mongodb_config) as mongo:
database_users = mongo.get_users()
relation_users = self._get_users_from_relations(departed_relation_id)
Expand Down
2 changes: 1 addition & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def _set_user_created(self, user: MongoDBUser) -> None:
self.app_peer_data[f"{user.get_username()}-user-created"] = "True"

def _get_mongodb_config_for_user(
self, user: MongoDBUser, hosts: list[str]
self, user: MongoDBUser, hosts: List[str]
) -> MongoDBConfiguration:
external_ca, _ = self.tls.get_tls_files("unit")
internal_ca, _ = self.tls.get_tls_files("app")
Expand Down

0 comments on commit 0d50315

Please sign in to comment.