Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set ownership of /var/lib/rabbitmq to rabbitmq user #15

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading