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

Fix storage mount ownership update process during charm upgrades #200

Merged
merged 1 commit into from
Mar 22, 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
30 changes: 12 additions & 18 deletions src/charm.py
jdkandersson marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ def _on_upgrade_charm(self, _event: UpgradeCharmEvent):
_event: required by ops framework, not used.
"""
self._setup_replica_data(_event)
self._change_uploads_directory_ownership(recursive=True)

def _gen_wp_config(self):
"""Generate the wp-config.php file WordPress needs based on charm config and relations.
Expand Down Expand Up @@ -1448,25 +1447,20 @@ def _storage_mounted(self) -> bool:
mount_info: str = container.pull("/proc/mounts").read()
return self._WP_UPLOADS_PATH in mount_info

def _change_uploads_directory_ownership(self, recursive=False):
"""Change uploads directory ownership.

Args:
recursive: Run chown recursively. Defaults to False.
"""
command_list = [
"chown",
f"{self._WORDPRESS_USER}:{self._WORDPRESS_GROUP}",
]

if recursive:
command_list.append("-R")
def _change_uploads_directory_ownership(self):
"""Change uploads directory ownership, noop if ownership is correct."""
dir_current = self._container().list_files(self._WP_UPLOADS_PATH, itself=True)[0]
if dir_current.user == self._WORDPRESS_USER and dir_current.group == self._WORDPRESS_GROUP:
return

command_list.append(self._WP_UPLOADS_PATH)
self._container().exec(
command_list,
timeout=120,
)
[
"chown",
f"{self._WORDPRESS_USER}:{self._WORDPRESS_GROUP}",
"-R",
self._WP_UPLOADS_PATH,
]
).wait()

def _reconciliation(self, _event: EventBase) -> None:
"""Reconcile the WordPress charm on juju event.
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/wordpress_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ def exists(self, path):

def list_files(self, path: str, itself=False):
"""Mock method for :meth:`ops.charm.model.Container.list_files`."""
if path == "/var/www/html/wp-content/uploads":
file_info_mock = unittest.mock.MagicMock()
file_info_mock.user = "_daemon_"
file_info_mock.group = "_daemon_"
return [file_info_mock]
if not path.endswith("/"):
path += "/"
file_list = []
Expand Down
Loading