# Copyright (C) 2005-2024 Splunk Inc. All Rights Reserved. import os from itsi.itsi_utils import ITOAInterfaceUtils from ITOA.version_check import VersionCheck from ITOA.setup_logging import getLogger from .constants import EVENTMANAGEMENT_JARS, MAD_JARS from .constants import EVENTMANAGEMENT_LIB, MAD_LIB, EVENTMANAGEMENT_PATH, PEKKO_VERSION, AKKA_APPLICATION from .file_fixer import VersionMismatchFileFixer class JavaFileFixer: ''' Class for removing older versions of Java archive files ''' def __init__(self, session_key, logger=None): self.session_key = session_key self.splunk_version = None if logger: self.logger = logger else: self.logger = getLogger() @staticmethod def create_path(filename, lib_path=EVENTMANAGEMENT_LIB): ''' Creates an absolute path to a Java archive file @param filename: Name of the Java archive file @type: basestring @param lib_path: Absolute path to Java archive file library directory @type: basestring @return: Absolute path to the Java archive file in the library directory @type: basestring ''' return os.path.join(lib_path, filename) @staticmethod def create_paths(filenames, lib_path=EVENTMANAGEMENT_LIB): ''' Creates a list of absolute paths to the Java archive files in Java library directory @param filenames: List of Java archive filenames @type: List[basestring] @param lib_path: Absolute path to Java archive file library directory @type: basestring @return: List of absolute paths to Java archive files in the library directory @type: List[basestring] ''' return [JavaFileFixer.create_path(jar, lib_path) for jar in filenames] def run(self): ''' Remove the Java archive files and restart affected services ''' try: self.splunk_version = ITOAInterfaceUtils.get_app_version(self.session_key, 'itsi', 'nobody', fetch_conf_only=True) except Exception: self.logger.error('Failed to retrieve Splunk version') return if not self.splunk_version: return for version, jars in EVENTMANAGEMENT_JARS.items(): # only run if the current version is greater than the keys in EVENTMANAGEMENT_JARS if VersionCheck.compare(version, self.splunk_version) < 0: vmf = VersionMismatchFileFixer(self.create_paths(jars), logger=self.logger) try: vmf.execute() except Exception: self.logger.error('Failed to remove older Java archive files') for version, jars in MAD_JARS.items(): # only run if the current version is greater than the keys in MAD_JARS if VersionCheck.compare(version, self.splunk_version) < 0: vmf = VersionMismatchFileFixer(self.create_paths(jars, MAD_LIB), logger=self.logger) try: vmf.execute() except Exception: self.logger.error('Failed to remove older Java archive files from SA-ITSI-MetricAD') # only run if the current version is greater than or equal to the PEKKO_VERSION if VersionCheck.compare(PEKKO_VERSION, self.splunk_version) <= 0: vmf = VersionMismatchFileFixer(self.create_paths(AKKA_APPLICATION, EVENTMANAGEMENT_PATH), logger=self.logger) try: vmf.execute() except Exception: self.logger.error('Failed to remove akka_aaplication.conf from event_management')