# Copyright (C) 2005-2025 Splunk Inc. All Rights Reserved. from abc import ABCMeta, abstractmethod import time class MigrationPreCheck: __metaclass__ = ABCMeta """ Base class for migration prechecks """ @abstractmethod def __init__(self, session_key, logger, pre_checks, skip_pre_checks=[]): """ Constructor @type: string @param session_key: @type: object @param logger: itsi_upgrade_queue logger @type: list @param pre_checks: list of pre checks to be performed Each item is an object with attributes: id: the name of the precheck which will be appended to 'precheck_' to form the method name to call title: a textual description of the precheck @type: list @param skip_pre_checks: ids of pre checks to be skipped @rtype: object @return: None """ self.session_key = session_key self.logger = logger self.pre_checks = pre_checks self.skip_pre_checks = skip_pre_checks self.pre_check_results = [] timestamps = {} for precheck in self.pre_checks: timestamps[precheck['id']] = {'start_time': '', 'end_time': ''} self.pre_check_timestamps = timestamps def _run_pre_check(self, pre_check_id): """ Wrapper function to call corresponding pre_check_id function @type:string @param: pre_check_id @rtype: list @return: list of dictionaries that contain: precheck ID, passed or not and recommendations. """ pre_check_method = getattr(self, 'precheck_' + pre_check_id) try: results = pre_check_method() for r in results: r['id'] = pre_check_id return results except AttributeError: raise NotImplementedError("Precheck method {} in class `{}` does not implement" .format(pre_check_method, self.__class__.__name__)) def _make_result(self, passed, recommendation, type): """ Make a dictionary as the result. @type passed: bool @param passed: Whether or not the check is passed @type recommendation: @param recommendation: @rtype: dict @return: the dictionary as the result. """ return { 'passed': passed, 'recommendation': recommendation, 'type': type } def run(self): """ Run all prechecks that are not in the skip list """ for pre_check in self.pre_checks: self.pre_check_timestamps[pre_check['id']]['start_time'] = time.time() if pre_check['id'] in self.skip_pre_checks: self.pre_check_results.append({ 'id': pre_check['id'], 'passed': True, 'recommendation': pre_check['title'] + ' is skipped', 'type': 'Success' }) self.pre_check_timestamps[pre_check['id']]['end_time'] = time.time() self.logger.debug('The precheck %s has been skipped.' % pre_check['id']) else: results = self._run_pre_check(pre_check['id']) self.pre_check_results.extend(results) self.logger.debug('Results obtained from {0} are {1}'.format( pre_check['id'], results)) self.pre_check_timestamps[pre_check['id']]['end_time'] = time.time()