Skip to content

Commit

Permalink
Support separate data dir for log files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Ratushnyy committed Feb 8, 2024
1 parent cf191c9 commit 82e4700
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
26 changes: 17 additions & 9 deletions lib/charms/mongodb/v1/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@

DATA_DIR = "/var/lib/mongodb"
LOG_DIR = "/var/log/mongodb"
LOG_TO_SYSLOG = True
CONF_DIR = "/etc/mongod"
MONGODB_LOG_FILENAME = "mongodb.log"
logger = logging.getLogger(__name__)


def _get_logging_options(snap_install: bool) -> str:
# TODO sending logs to syslog until we have a separate mount point for logs
if LOG_TO_SYSLOG:
return ""
# in k8s the default logging options that are used for the vm charm are ignored and logs are
# the output of the container. To enable logging to a file it must be set explicitly
return f"--logpath={LOG_DIR}/{MONGODB_LOG_FILENAME}" if snap_install else ""
log_path = f"{LOG_DIR}/{MONGODB_LOG_FILENAME}"
if snap_install:
log_path = f"{MONGODB_COMMON_DIR}{log_path}"
return f"--logpath={log_path}"


def _get_audit_log_settings(snap_install: bool) -> List[str]:
log_path = f"{LOG_DIR}/{Config.AuditLog.FILE_NAME}"
if snap_install:
log_path = f"{MONGODB_COMMON_DIR}{log_path}"
return [
f"--auditDestination={Config.AuditLog.DESTINATION}",
f"--auditFormat={Config.AuditLog.FORMAT}",
]


# noinspection GrazieInspection
Expand Down Expand Up @@ -143,6 +150,7 @@ def get_mongod_args(
full_data_dir = f"{MONGODB_COMMON_DIR}{DATA_DIR}" if snap_install else DATA_DIR
full_conf_dir = f"{MONGODB_SNAP_DATA_DIR}{CONF_DIR}" if snap_install else CONF_DIR
logging_options = _get_logging_options(snap_install)
audit_log_settings = _get_audit_log_settings(snap_install)
cmd = [
# bind to localhost and external interfaces
"--bind_ip_all",
Expand All @@ -153,10 +161,10 @@ def get_mongod_args(
# for simplicity we run the mongod daemon on shards, configsvrs, and replicas on the same
# port
f"--port={Config.MONGODB_PORT}",
"--auditDestination=syslog", # TODO sending logs to syslog until we have a separate mount point for logs
f"--auditFormat={Config.AuditLog.FORMAT}",
"--setParameter processUmask=037", # required for log files perminission (g+r)
logging_options,
]
cmd.extend(audit_log_settings)
if auth:
cmd.extend(["--auth"])

Expand Down
3 changes: 3 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ storage:
mongodb:
type: filesystem
location: /var/snap/charmed-mongodb/common
logs:
type: filesystem
location: /var/snap/charmed-mongodb/common/var/log/mongodb

peers:
database-peers:
Expand Down
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AuditLog:
"""Audit log related configuration."""

FORMAT = "JSON"
DESTINATION = "file"
FILE_NAME = "audit.log"

class Backup:
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/test_mongodb_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def test_get_mongod_args(self):
"--replSet=my_repl_set",
"--dbpath=/var/snap/charmed-mongodb/common/var/lib/mongodb",
"--port=27017",
"--auditDestination=syslog",
"--logpath=/var/snap/charmed-mongodb/common/var/log/mongodb/mongodb.log",
"--auditDestination=file",
"--auditFormat=JSON",
"--auth",
"--clusterAuthMode=keyFile",
Expand All @@ -38,7 +39,8 @@ def test_get_mongod_args(self):
"--replSet=my_repl_set",
"--dbpath=/var/snap/charmed-mongodb/common/var/lib/mongodb",
"--port=27017",
"--auditDestination=syslog",
"--logpath=/var/snap/charmed-mongodb/common/var/log/mongodb/mongodb.log",
"--auditDestination=file",
"--auditFormat=JSON",
]

Expand All @@ -53,8 +55,9 @@ def test_get_mongod_args(self):
# part of replicaset
"--replSet=my_repl_set",
"--dbpath=/var/lib/mongodb",
"--logpath=/var/snap/charmed-mongodb/common/var/log/mongodb/mongodb.log",
"--port=27017",
"--auditDestination=syslog",
"--auditDestination=file",
"--auditFormat=JSON",
]

Expand Down

0 comments on commit 82e4700

Please sign in to comment.