def check_detected_os_info_parameters(cls, detected_os_info): required_fields = cls.get_required_detected_os_info_fields() missing_os_info_fields = [ field for field in required_fields if field not in detected_os_info ] if missing_os_info_fields: raise exception.InvalidDetectedOSParams( "There are parameters (%s) which are required by %s but " "are missing from the detected OS info: %s" % (missing_os_info_fields, cls.__name__, detected_os_info)) extra_os_info_fields = [ field for field in detected_os_info if field not in required_fields ] if extra_os_info_fields: raise exception.InvalidDetectedOSParams( "There were detected OS info parameters (%s) which were not " "expected by %s: %s" % (extra_os_info_fields, cls.__name__, detected_os_info)) return True
def detect_os(conn, os_type, os_root_dir, operation_timeout, tools_environment=None, custom_os_detect_tools=None): """ Iterates through all of the OS detection tools until one successfully identifies the OS/release and returns the release info from it. param custom_os_detect_tools: list: list of classes which inherit from coriolis.osmorphing.osdetect.base.BaseOSDetectTools. The custom detect tools will be run before the standard ones. """ if not tools_environment: tools_environment = {} detect_tools_classes = [] if custom_os_detect_tools: _check_custom_os_detect_tools(custom_os_detect_tools) detect_tools_classes.extend(custom_os_detect_tools) if os_type == constants.OS_TYPE_LINUX: detect_tools_classes.extend(LINUX_OS_DETECTION_TOOLS) elif os_type == constants.OS_TYPE_WINDOWS: detect_tools_classes.extend(WINDOWS_OS_DETECTION_TOOLS) else: raise exception.OSDetectToolsNotFound(os_type=os_type) tools = None detected_info = {} for detectcls in detect_tools_classes: tools = detectcls(conn, os_root_dir, operation_timeout) tools.set_environment(tools_environment) LOG.debug("Running OS detection tools class: %s" % detectcls.__name__) detected_info = tools.detect_os() if detected_info: LOG.info( "Successfully detected OS using tools '%s'. Returned " "OS info is: %s", detectcls.__name__, detected_info) break if not detected_info: raise exception.OSDetectToolsNotFound(os_type=os_type) if not isinstance(detected_info, dict): raise exception.InvalidDetectedOSParams( "OS detect tools '%s' returned a non-dict value: %s" % (type(tools), detected_info)) declared_returns = tools.returned_detected_os_info_fields() missing_returns = [ field for field in declared_returns if field not in detected_info ] if missing_returns: raise exception.InvalidDetectedOSParams( "OS detect tools class '%s' has failed to return some required " "fields (%s) in the detected OS info: %s" % (type(tools), declared_returns, detected_info)) extra_returns = [ field for field in detected_info if field not in declared_returns ] if extra_returns: raise exception.InvalidDetectedOSParams( "OS detect tools class '%s' has returned fields (%s) which it had " "declared it would return (%s) in info: %s" % (type(tools), extra_returns, declared_returns, detected_info)) return detected_info