Skip to content

Commit

Permalink
Add more details to the plugin manager exceptions, remove the backup …
Browse files Browse the repository at this point in the history
…plugin from the manager as it is managed directly by the opensearch_backups
  • Loading branch information
phvalguima committed Feb 21, 2024
1 parent e30b4ce commit c608ff3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
29 changes: 12 additions & 17 deletions lib/charms/opensearch/v0/opensearch_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import logging
from typing import Any, Dict, List, Optional

from charms.opensearch.v0.opensearch_backups import OpenSearchBackupPlugin
from charms.opensearch.v0.opensearch_exceptions import (
OpenSearchCmdError,
OpenSearchNotFullyReadyError,
Expand Down Expand Up @@ -53,11 +52,6 @@
"config": "plugin_opensearch_knn",
"relation": None,
},
"repository-s3": {
"class": OpenSearchBackupPlugin,
"config": None,
"relation": "s3-credentials",
},
}


Expand Down Expand Up @@ -163,7 +157,12 @@ def run(self) -> bool:
restart_needed,
]
)
except OpenSearchPluginMissingConfigError as e:
except (
OpenSearchPluginMissingDepsError,
OpenSearchPluginMissingConfigError,
OpenSearchPluginInstallError,
OpenSearchPluginRemoveError,
) as e:
# This is a more serious issue, as we are missing some input from
# the user. The charm should block.
raise e
Expand All @@ -183,9 +182,7 @@ def _install_plugin(self, plugin: OpenSearchPlugin) -> bool:
if plugin.dependencies:
missing_deps = [dep for dep in plugin.dependencies if dep not in installed_plugins]
if missing_deps:
raise OpenSearchPluginMissingDepsError(
f"Failed to install {plugin.name}, missing dependencies: {missing_deps}"
)
raise OpenSearchPluginMissingDepsError(plugin.name, missing_deps)

# Add the plugin
try:
Expand All @@ -198,9 +195,7 @@ def _install_plugin(self, plugin: OpenSearchPlugin) -> bool:
# Check for dependencies
missing_deps = [dep for dep in plugin.dependencies if dep not in installed_plugins]
if missing_deps:
raise OpenSearchPluginMissingDepsError(
f"Failed to install {plugin.name}, missing dependencies: {missing_deps}"
)
raise OpenSearchPluginMissingDepsError(plugin.name, missing_deps)

self._opensearch.run_bin("opensearch-plugin", f"install --batch {plugin.name}")
except KeyError as e:
Expand All @@ -210,7 +205,7 @@ def _install_plugin(self, plugin: OpenSearchPlugin) -> bool:
logger.info(f"Plugin {plugin.name} already installed, continuing...")
# Nothing installed, as plugin already exists
return False
raise OpenSearchPluginInstallError(f"Failed to install plugin {plugin.name}: {e}")
raise OpenSearchPluginInstallError(plugin.name)
# Install successful
return True

Expand Down Expand Up @@ -240,7 +235,7 @@ def _configure_if_needed(self, plugin: OpenSearchPlugin) -> bool:
return False
return self.apply_config(plugin.config())
except KeyError as e:
raise OpenSearchPluginMissingConfigError(e)
raise OpenSearchPluginMissingConfigError(plugin.name, configs=[f"{e}"])

def _disable_if_needed(self, plugin: OpenSearchPlugin) -> bool:
"""If disabled, removes plugin configuration or sets it to other values."""
Expand All @@ -255,7 +250,7 @@ def _disable_if_needed(self, plugin: OpenSearchPlugin) -> bool:
return False
return self.apply_config(plugin.disable())
except KeyError as e:
raise OpenSearchPluginMissingConfigError(e)
raise OpenSearchPluginMissingConfigError(plugin.name, configs=[f"{e}"])

def apply_config(self, config: OpenSearchPluginConfig) -> bool:
"""Runs the configuration changes as passed via OpenSearchPluginConfig.
Expand Down Expand Up @@ -373,7 +368,7 @@ def _remove_plugin(self, plugin: OpenSearchPlugin) -> bool:
if "not found" in str(e):
logger.info(f"Plugin {plugin.name} to be deleted, not found. Continuing...")
return False
raise OpenSearchPluginRemoveError(f"Failed to remove plugin {plugin.name}: {e}")
raise OpenSearchPluginRemoveError(plugin.name)
return True

def _installed_plugins(self) -> List[str]:
Expand Down
21 changes: 20 additions & 1 deletion lib/charms/opensearch/v0/opensearch_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,35 @@ def __init__(self, msg):
class OpenSearchPluginMissingDepsError(OpenSearchPluginError):
"""Exception thrown when an opensearch plugin misses installed dependencies."""

def __init__(self, name, deps):
super().__init__(f"Failed to install plugin: {name} - missing dependencies {deps}")
self.deps = deps


class OpenSearchPluginInstallError(OpenSearchPluginError):
"""Exception thrown when opensearch plugin installation fails."""

def __init__(self, name):
super().__init__("Failed to install plugin: {}".format(name))


class OpenSearchPluginRemoveError(OpenSearchPluginError):
"""Exception thrown when opensearch plugin removal fails."""

def __init__(self, name):
super().__init__("Failed to remove plugin: {}".format(name))


class OpenSearchPluginMissingConfigError(OpenSearchPluginError):
"""Exception thrown when config() or disable() fails to find a config key.
The plugin itself should raise a KeyError, to avoid burden in the plugin development.
"""

def __init__(self, name, configs: List[str]):
super().__init__(f"Plugin {name} is missing configs: {configs}")
self.configs = configs


class OpenSearchPluginEventScope(BaseStrEnum):
"""Defines the scope of the plugin manager."""
Expand Down Expand Up @@ -472,7 +486,12 @@ def config(self) -> OpenSearchPluginConfig:
"""
if not self._extra_config.get("access-key") or not self._extra_config.get("secret-key"):
raise OpenSearchPluginMissingConfigError(
"Missing AWS access-key and secret-key configuration"
self.name,
[
conf
for conf in ["access-key", "secret-key"]
if not self._extra_config.get(conf)
],
)

return OpenSearchPluginConfig(
Expand Down

0 comments on commit c608ff3

Please sign in to comment.