Skip to content

Commit

Permalink
set ownership of /var/lib/rabbitmq to rabbitmq user
Browse files Browse the repository at this point in the history
For certain storage providers, the mounted directory
/var/lib/rabbitmq is created with root:root as owner.
This causes issues to start the rabbitmq server.
Change ownership of /var/lib/rabbitmq to rabbitmq:rabbitmq.
  • Loading branch information
hemanthnakkina committed Apr 1, 2024
1 parent 815938a commit 83c1fc0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
RABBITMQ_SERVICE = "rabbitmq"
RABBITMQ_USER = "rabbitmq"
RABBITMQ_GROUP = "rabbitmq"
RABBITMQ_DATA_DIR = "/var/lib/rabbitmq"
RABBITMQ_COOKIE_PATH = "/var/lib/rabbitmq/.erlang.cookie"

SELECTOR_ALL = "all"
Expand Down Expand Up @@ -201,6 +202,9 @@ def _on_config_changed(self, event: EventBase) -> None:
event.defer()
return

# Change ownership of /var/lib/rabbitmq
self._set_ownership_on_data_dir()

# Render and push configuration files
self._render_and_push_config_files()

Expand Down Expand Up @@ -711,6 +715,34 @@ def _initialize_operator_user(self) -> None:
logging.warning("Deleting the guest user.")
api.delete_user("guest")

def _set_ownership_on_data_dir(self) -> None:
"""Set ownership on /var/lib/rabbitmq."""
container = self.unit.get_container(RABBITMQ_CONTAINER)
paths = container.list_files(RABBITMQ_DATA_DIR, itself=True)
if len(paths) == 0:
return

logger.debug(
f"rabbitmq lib directory ownership: {paths[0].user}:{paths[0].group}"
)
if paths[0].user != RABBITMQ_USER or paths[0].group != RABBITMQ_GROUP:
logger.debug(
f"Changing ownership to {RABBITMQ_USER}:{RABBITMQ_GROUP}"
)
try:
container.exec(
[
"chown",
"-R",
f"{RABBITMQ_USER}:{RABBITMQ_GROUP}",
RABBITMQ_DATA_DIR,
]
)
except ExecError as e:
logger.error(
f"Exited with code {e.exit_code}. Stderr:\n{e.stderr}"
)

def _render_and_push_config_files(self) -> None:
"""Render and push configuration files.
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_rabbitmq_pebble_ready(self):
"""Test pebble handler."""
# self.harness.charm._render_and_push_config_files = Mock()
# self.harness.charm._render_and_push_plugins = Mock()
self.harness.charm._set_ownership_on_data_dir = Mock()
# Check the initial Pebble plan is empty
self.harness.set_can_connect("rabbitmq", True)
initial_plan = self.harness.get_container_pebble_plan("rabbitmq")
Expand Down

0 comments on commit 83c1fc0

Please sign in to comment.