Skip to content

Commit

Permalink
Fix cached clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
phvalguima committed Jun 10, 2024
1 parent 370327c commit 752212c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
30 changes: 18 additions & 12 deletions lib/charms/opensearch/v0/opensearch_keystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
This module manages OpenSearch keystore access and lifecycle.
"""
import functools
import logging
import os
from abc import ABC
Expand All @@ -13,7 +14,6 @@
from charms.opensearch.v0.opensearch_exceptions import (
OpenSearchCmdError,
OpenSearchError,
OpenSearchHttpError,
)

# The unique Charmhub library identifier, never change it
Expand Down Expand Up @@ -134,11 +134,11 @@ def delete(self, entries: List[str]) -> None:
for key in entries:
self._delete(key)

def list(self, alias: str = None) -> List[str]:
@functools.cached_property
def list(self) -> List[str]:
"""Lists the keys available in opensearch's keystore."""
if not os.path.exists(self.keystore):
raise OpenSearchKeystoreNotReadyYetError()

try:
return self._opensearch.run_bin(self._keytool, "list").split("\n")
except OpenSearchCmdError as e:
Expand All @@ -151,12 +151,16 @@ def _add(self, key: str, value: str):
# Add newline to the end of the key, if missing
value += "" if value.endswith("\n") else "\n"
self._opensearch.run_bin(self._keytool, f"add --force {key}", stdin=value)

self._clean_cache_if_needed()
except OpenSearchCmdError as e:
raise OpenSearchKeystoreError(str(e))

def _delete(self, key: str) -> None:
try:
self._opensearch.run_bin(self._keytool, f"remove {key}")

self._clean_cache_if_needed()
except OpenSearchCmdError as e:
if "does not exist in the keystore" in str(e):
logger.info(
Expand All @@ -166,13 +170,15 @@ def _delete(self, key: str) -> None:
return
raise OpenSearchKeystoreError(str(e))

def _clean_cache_if_needed(self):
if self.list:
del self.list

def reload_keystore(self) -> None:
"""Updates the keystore value (adding or removing) and reload."""
try:
# Reload the security settings and return if opensearch needs restart
response = self._opensearch.request("POST", "_nodes/reload_secure_settings")
logger.debug(f"_update_keystore_and_reload: response received {response}")
except OpenSearchHttpError as e:
raise OpenSearchKeystoreError(
f"Failed to reload keystore: error code: {e.response_code}, error body: {e.response_body}"
)
"""Updates the keystore value (adding or removing) and reload.
Raises:
OpenSearchHttpError: If the reload fails.
"""
response = self._opensearch.request("POST", "_nodes/reload_secure_settings")
logger.debug(f"_update_keystore_and_reload: response received {response}")
18 changes: 14 additions & 4 deletions lib/charms/opensearch/v0/opensearch_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def reset_event_scope(self) -> None:
"""Resets the event scope of the plugin manager to the default value."""
self._event_scope = OpenSearchPluginEventScope.DEFAULT

@property
@functools.cached_property
def plugins(self) -> List[OpenSearchPlugin]:
"""Returns List of installed plugins."""
plugins_list = []
Expand Down Expand Up @@ -222,7 +222,7 @@ def _install_plugin(self, plugin: OpenSearchPlugin) -> bool:
Returns True if the plugin was installed.
"""
installed_plugins = self._installed_plugins()
installed_plugins = self._installed_plugins
if plugin.dependencies:
missing_deps = [dep for dep in plugin.dependencies if dep not in installed_plugins]
if missing_deps:
Expand All @@ -242,6 +242,7 @@ def _install_plugin(self, plugin: OpenSearchPlugin) -> bool:
raise OpenSearchPluginMissingDepsError(plugin.name, missing_deps)

self._opensearch.run_bin("opensearch-plugin", f"install --batch {plugin.name}")
self._clean_cache_if_needed()
except KeyError as e:
raise OpenSearchPluginMissingConfigError(e)
except OpenSearchCmdError as e:
Expand Down Expand Up @@ -423,7 +424,7 @@ def status(self, plugin: OpenSearchPlugin) -> PluginState:

def _is_installed(self, plugin: OpenSearchPlugin) -> bool:
"""Returns true if plugin is installed."""
return plugin.name in self._installed_plugins()
return plugin.name in self._installed_plugins

def _user_requested_to_enable(self, plugin: OpenSearchPlugin) -> bool:
"""Returns True if user requested plugin to be enabled."""
Expand Down Expand Up @@ -454,7 +455,7 @@ def _is_enabled(self, plugin: OpenSearchPlugin) -> bool:
if plugin.config().secret_entries_to_add or plugin.config().secret_entries_to_del:
# Need to check keystore
# If the keystore is not yet set, then an exception will be raised here
keys_available = self._keystore.list()
keys_available = self._keystore.list
keys_to_add = plugin.config().secret_entries_to_add
if any(k not in keys_available for k in keys_to_add):
return False
Expand Down Expand Up @@ -495,13 +496,22 @@ def _remove_plugin(self, plugin: OpenSearchPlugin) -> bool:
"""Remove a plugin without restarting the node."""
try:
self._opensearch.run_bin("opensearch-plugin", f"remove {plugin.name}")
self._clean_cache_if_needed()

except OpenSearchCmdError as e:
if "not found" in str(e):
logger.info(f"Plugin {plugin.name} to be deleted, not found. Continuing...")
return False
raise OpenSearchPluginRemoveError(plugin.name)
return True

def _clean_cache_if_needed(self):
if self.plugins:
del self.plugins
if self._installed_plugins:
del self._installed_plugins

@functools.cached_property
def _installed_plugins(self) -> List[str]:
"""List plugins."""
try:
Expand Down

0 comments on commit 752212c

Please sign in to comment.