def is_installed_oscap_ok(): """Check whether expected openscap rpms are installed.""" class GetCmdStdout(): def __init__(self): self.stdout_lines = [] def __call__(self, line): if line.strip(): self.stdout_lines.append(line.strip()) if not os.path.exists(settings.openscap_binary): log_message("Oscap with SCE enabled is not installed") return False if not os.access(settings.openscap_binary, os.X_OK): log_message("Oscap with SCE %s is not executable" % settings.openscap_binary) return False # that's generic problem that could be on various rpm-based systems url = "https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html-single/6.10_release_notes/index#BZ1804691" for pkg in settings.openscap_rpms: cmd = ["rpm", "-q", pkg, "--qf", "%{ARCH}\n"] cmdout = GetCmdStdout() ProcessHelper.run_subprocess(cmd, function=cmdout) if SystemIdentification.get_arch() not in cmdout.stdout_lines: log_message("The %s rpm is not installed for the" " %s architecture. This usually ends in a broken" " state in which all the Preupgrade Assistant modules" " are skipped (notchecked state). Please, install" " packages related for your architecture. See %s" " for more info." % (pkg, SystemIdentification.get_arch(), url)) return False return True
def update_platform(full_path): file_lines = FileHelper.get_file_content(full_path, 'rb', method=True) platform = '' platform_id = '' if not SystemIdentification.get_system(): platform = settings.CPE_RHEL else: platform = settings.CPE_FEDORA platform_id = SystemIdentification.get_assessment_version(full_path) for index, line in enumerate(file_lines): if 'PLATFORM_NAME' in line: line = line.replace('PLATFORM_NAME', platform) if 'PLATFORM_ID' in line: line = line.replace('PLATFORM_ID', platform_id[0]) file_lines[index] = line FileHelper.write_to_file(full_path, 'wb', file_lines)
def _check_available_contents(self): cnt = 0 is_dir = lambda x: os.path.isdir(os.path.join(self.conf.source_dir, x)) dirs = os.listdir(self.conf.source_dir) self.list_scans = [] for dir_name in filter(is_dir, dirs): if SystemIdentification.get_assessment_version(dir_name): self.conf.scan = dir_name self.list_scans.append(dir_name) logger_debug.debug("Scan directory '%s'", self.conf.scan) cnt += 1 if int(cnt) < 1: log_message("There were no modules found in the %s directory. \ If you would like to use this tool, you have to install some." % settings.source_dir) return ReturnValues.SCENARIO if int(cnt) > 1: log_message("Preupgrade Assistant detects more " "than one set of modules in the %s directory.\n" % settings.source_dir) log_message("The list of sets of all available modules is: \n%s" % '\n'.join(self.list_scans)) log_message( "If you would like to use the tool, " "specify the correct upgrade path mentioned above with a parameter -s." ) return ReturnValues.SCENARIO return 0
def fnc_solution_text(self, key, name): """Function updates a solution text.""" if name in key: self.solution_modification(key) else: self.update_values_list(self.rule, "{fix}", xml_tags.FIX_TEXT) self.update_values_list(self.rule, "{solution_text}", "text") self.update_values_list(self.rule, "{platform_id}", SystemIdentification.get_assessment_version(self.dirname)[1])
def fnc_solution_text(self, key, name): """Function updates a solution text.""" if name in key: self.solution_modification(key) else: self.update_values_list(self.rule, "{fix}", xml_tags.FIX_TEXT) self.update_values_list(self.rule, "{solution_text}", "text") self.update_values_list( self.rule, "{platform_id}", SystemIdentification.get_assessment_version(self.dirname)[1])
def prep_symlinks(self, assessment_dir, scenario=""): """Prepare a symlinks for relevant architecture and Server Variant""" server_variant = SystemIdentification.get_variant() if server_variant is None: return self.common_result_dir = os.path.join(assessment_dir, settings.common_name) # We need to copy /usr/share/preupgrade/RHEL6_7/common also in case of # usage --contents option. Some contents needs a /root/preupgrade/RHEL6_7/common # directory if self.conf.contents: usr_common_name = os.path.join(settings.source_dir, scenario, settings.common_name) if os.path.exists(usr_common_name): dir_util.copy_tree( usr_common_name, os.path.join(assessment_dir, settings.common_name)) # We have repositories for i386 architecture but packages are built # sometimes as i686 architecture. That's problematic in some cases # so we solve this for now by this little hack ugly. i386_dir = os.path.join(self.common_result_dir, 'i386') i386_x64_dir = os.path.join(self.common_result_dir, 'i386-x86_64') i686_x64_dir = os.path.join(self.common_result_dir, 'i686-x86_64') i686_dir = os.path.join(self.common_result_dir, 'i686') if not os.path.exists(i686_dir) and os.path.exists(i386_dir): os.symlink(i386_dir, i686_dir) if not os.path.exists(i686_x64_dir) and os.path.exists(i386_x64_dir): os.symlink(i386_x64_dir, i686_x64_dir) dir_name = os.path.join(self.common_result_dir, SystemIdentification.get_arch()) if not os.path.exists(dir_name): return server_variant_files = [ files for files in os.listdir(dir_name) if files.startswith(server_variant) or files.startswith("Common") ] self.copy_kickstart_files(self.common_result_dir, server_variant) for files in server_variant_files: # First create a default links to "ServerVariant_" if files.startswith(server_variant): self.create_common_symlink(files, server_variant) elif files.startswith("Common"): self.create_common_symlink(files, "Common")
def prep_symlinks(self, assessment_dir, scenario=""): """Prepare a symlinks for relevant architecture and Server Variant""" server_variant = SystemIdentification.get_variant() if server_variant is None: return self.common_result_dir = os.path.join(assessment_dir, settings.common_name) # We need to copy /usr/share/preupgrade/RHEL6_7/common also in case of # usage --contents option. Some contents needs a /root/preupgrade/RHEL6_7/common # directory if self.conf.contents: usr_common_name = os.path.join(settings.source_dir, scenario, settings.common_name) if os.path.exists(usr_common_name): dir_util.copy_tree(usr_common_name, os.path.join(assessment_dir, settings.common_name)) # We have repositories for i386 architecture but packages are built # sometimes as i686 architecture. That's problematic in some cases # so we solve this for now by this little hack ugly. i386_dir = os.path.join(self.common_result_dir, 'i386') i386_x64_dir = os.path.join(self.common_result_dir, 'i386-x86_64') i686_x64_dir = os.path.join(self.common_result_dir, 'i686-x86_64') i686_dir = os.path.join(self.common_result_dir, 'i686') if not os.path.exists(i686_dir) and os.path.exists(i386_dir): os.symlink(i386_dir, i686_dir) if not os.path.exists(i686_x64_dir) and os.path.exists(i386_x64_dir): os.symlink(i386_x64_dir, i686_x64_dir) dir_name = os.path.join(self.common_result_dir, SystemIdentification.get_arch()) if not os.path.exists(dir_name): return server_variant_files = [files for files in os.listdir(dir_name) if files.startswith(server_variant) or files.startswith("Common")] self.copy_kickstart_files(self.common_result_dir, server_variant) for files in server_variant_files: # First create a default links to "ServerVariant_" if files.startswith(server_variant): self.create_common_symlink(files, server_variant) elif files.startswith("Common"): self.create_common_symlink(files, "Common")
def write_list_rules(self): end_point = self.dirname.find(SystemIdentification.get_valid_scenario(self.dirname)) rule_name = '_'.join(self.dirname[end_point:].split('/')[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) else: lines = [] for values in six.itervalues(self.loaded): check_script = [v for k, v in six.iteritems(values[0]) if k == 'check_script'] if check_script: check_script = os.path.splitext(''.join(check_script))[0] lines.append(settings.xccdf_tag + rule_name + '_' + check_script + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def get_scenario(self): """The function returns scenario""" scenario = None try: sep_content = os.path.dirname(self.content).split('/') if self.conf.contents: dir_name = SystemIdentification.get_valid_scenario(self.content) if dir_name is None: return None check_name = dir_name else: check_name = self.conf.scan scenario = [x for x in sep_content if check_name in x][0] except IndexError: scenario = None return scenario
def _get_required_arch_dirname(self): """ Get expected dirname of common data for requested assessment of system. In case that dst_arch is not specified by --dst-arch option, final arch is arch of current system (e.g. x86_64). In case that destination architecture is specified by the option and it is different to source arch, specific dirname for cross-architecture is returned. E.g. for source arch "i386" and destination arch "x86_64" returns "i386-x86_64". Doesn't matter when directory exist or not. Just return expected dirname, which correspond to used convention. """ arch = src_arch = SystemIdentification.get_arch() if self.conf.dst_arch and src_arch != self.conf.dst_arch: arch = "%s-%s" % (src_arch, self.conf.dst_arch) return arch
def get_scenario(self): """The function returns scenario""" scenario = None try: sep_content = os.path.dirname(self.content).split('/') if self.conf.contents: dir_name = SystemIdentification.get_valid_scenario( self.content) if dir_name is None: return None check_name = dir_name else: check_name = self.conf.scan scenario = [x for x in sep_content if check_name in x][0] except IndexError: scenario = None return scenario
def generate_xml(self, generate_from_ini=True): if SystemIdentification.get_valid_scenario(self.dir_name) is None: print ('Use valid scenario like RHEL6_7 or CENTOS6_RHEL6') return ReturnValues.SCENARIO dir_util.copy_tree(self.result_dir, self.dir_name) target_tree = ComposeXML.run_compose(self.dir_name, generate_from_ini=generate_from_ini) report_filename = os.path.join(self.dir_name, settings.content_file) try: FileHelper.write_to_file(report_filename, "wb", ElementTree.tostring(target_tree, "utf-8"), False) if generate_from_ini: print ('Generate report file for preupgrade-assistant is:', ''.join(report_filename)) except IOError: print ("Problem with writing file ", report_filename) raise return 0
def specify_upgrade_path(self): if self.upgrade_path is None: self.upgrade_path = get_user_input(settings.upgrade_path, any_input=True) if self.upgrade_path is True or self.upgrade_path == "": print ("The scenario is mandatory. You have to specify it.") return None if not SystemIdentification.get_valid_scenario(self.upgrade_path): if self.content_path is None: self.content_path = self.upgrade_path print ("The scenario '%s' is not valid.\nIt has to be like RHEL6_7 or CentOS7_RHEL7." % self.content_path) return None message = 'The path %s already exists.\nDo you want to create a module there?' % self.upgrade_path if UIHelper.check_path(self.get_upgrade_path(), message) is None: return None return True
def generate_xml(self, generate_from_ini=True): if SystemIdentification.get_valid_scenario(self.dir_name) is None: print('Use valid scenario like RHEL6_7 or CENTOS6_RHEL6') return ReturnValues.SCENARIO dir_util.copy_tree(self.result_dir, self.dir_name) target_tree = ComposeXML.run_compose( self.dir_name, generate_from_ini=generate_from_ini) report_filename = os.path.join(self.dir_name, settings.content_file) try: FileHelper.write_to_file( report_filename, "wb", ElementTree.tostring(target_tree, "utf-8"), False) if generate_from_ini: print('Generate report file for preupgrade-assistant is:', ''.join(report_filename)) except IOError: print("Problem with writing file ", report_filename) raise return 0
def specify_upgrade_path(self): if self.upgrade_path is None: self.upgrade_path = get_user_input(settings.upgrade_path, any_input=True) if self.upgrade_path is True or self.upgrade_path == "": print("The scenario is mandatory. You have to specify it.") return None if not SystemIdentification.get_valid_scenario(self.upgrade_path): if self.content_path is None: self.content_path = self.upgrade_path print( "The scenario '%s' is not valid.\nIt has to be like RHEL6_7 or CentOS7_RHEL7." % self.content_path) return None message = 'The path %s already exists.\nDo you want to create a module there?' % self.upgrade_path if UIHelper.check_path(self.get_upgrade_path(), message) is None: return None return True
def write_list_rules(self): end_point = self.dirname.find( SystemIdentification.get_valid_scenario(self.dirname)) rule_name = '_'.join(self.dirname[end_point:].split('/')[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) else: lines = [] for values in six.itervalues(self.loaded): check_script = [ v for k, v in six.iteritems(values[0]) if k == 'check_script' ] if check_script: check_script = os.path.splitext(''.join(check_script))[0] lines.append(settings.xccdf_tag + rule_name + '_' + check_script + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def _check_available_contents(self): cnt = 0 is_dir = lambda x: os.path.isdir(os.path.join(self.conf.source_dir, x)) dirs = os.listdir(self.conf.source_dir) self.list_scans = [] for dir_name in filter(is_dir, dirs): if SystemIdentification.get_assessment_version(dir_name): self.conf.scan = dir_name self.list_scans.append(dir_name) logger_debug.debug("Scan directory '%s'", self.conf.scan) cnt += 1 if int(cnt) < 1: log_message("There were no modules found in the %s directory. \ If you would like to use this tool, you have to install some." % settings.source_dir) return ReturnValues.SCENARIO if int(cnt) > 1: log_message("Preupgrade Assistant detects more " "than one set of modules in the %s directory.\n" % settings.source_dir) log_message("The list of sets of all available modules is: \n%s" % '\n'.join(self.list_scans)) log_message("If you would like to use the tool, " "specify the correct upgrade path mentioned above with a parameter -s.") return ReturnValues.SCENARIO return 0
def run(self): """run analysis""" version_msg = "Preupgrade Assistant version: %s" % VERSION if self.conf.version: print(version_msg) return 0 logger_debug.debug(version_msg) if self.conf.list_contents_set: for dir_name, dummy_content in iter( get_installed_module_sets(self.conf.source_dir).items()): log_message("%s" % dir_name) return 0 if self.conf.riskcheck: result_xml_path = os.path.join(settings.assessment_results_dir, settings.xml_result_name) if not os.path.exists(result_xml_path): log_message("System assessment needs to be performed first.") return ReturnValues.PREUPG_BEFORE_RISKCHECK return XccdfHelper.check_inplace_risk(result_xml_path, self.conf.verbose) if self.conf.upload and self.conf.results: if not self.upload_results(): return ReturnValues.SEND_REPORT_TO_UI return 0 if self.conf.cleanup: if not self.executed_under_root(): return ReturnValues.ROOT self.clean_preupgrade_environment() return 0 if self.conf.text: # Test whether w3m, lynx and elinks packages are installed found = False for pkg in SystemIdentification.get_convertors(): if xml_manager.get_package_version(pkg): self.text_convertor = pkg found = True break if not found: log_message( settings.converter_message.format(' '.join( SystemIdentification.get_convertors()))) return ReturnValues.MISSING_TEXT_CONVERTOR return_code = self.determine_module_set_location() if return_code: return return_code self.determine_module_set_copy_location() if self.conf.list_rules: rules = [ x for x in XccdfHelper.get_list_rules(self.all_xccdf_xml_path) ] log_message('\n'.join(rules)) return 0 if self.conf.mode and self.conf.select_rules: log_message(settings.options_not_allowed) return ReturnValues.MODE_SELECT_RULES # If force option is not mentioned and user selects NO then exit if not self.conf.force: text = "" if self.conf.dst_arch: correct_option = [ x for x in settings.migration_options if self.conf.dst_arch == x ] if not correct_option: sys.stderr.write( "Error: Specify correct value for --dst-arch" " option.\nValid are: %s.\n" % ", ".join(settings.migration_options)) return ReturnValues.INVALID_CLI_OPTION if SystemIdentification.get_arch() == "i386" or \ SystemIdentification.get_arch() == "i686": if not self.conf.dst_arch: text = '\n' + settings.migration_text logger_debug.debug("Architecture '%s'. Text '%s'.", SystemIdentification.get_arch(), text) if not show_message(settings.warning_text + text): # User does not want to continue return ReturnValues.USER_ABORT self.openscap_helper = OpenSCAPHelper(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.all_xccdf_xml_path) if not self.executed_under_root(): return ReturnValues.ROOT if not os.path.exists(settings.openscap_binary): log_message("Oscap with SCE enabled is not installed") return ReturnValues.MISSING_OPENSCAP if not os.access(settings.openscap_binary, os.X_OK): log_message("Oscap with SCE %s is not executable" % settings.openscap_binary) return ReturnValues.MISSING_OPENSCAP self.execution_dir = os.getcwd() os.chdir("/tmp") retval = self.scan_system() if retval != 0: return retval retval = self.summary_report(self.tar_ball_name) self.common.copy_common_files() KickstartGenerator.kickstart_scripts() FileHelper.remove_home_issues() if self.conf.upload: if not self.upload_results(): retval = ReturnValues.SEND_REPORT_TO_UI os.chdir(self.execution_dir) return retval
def scan_system(self): """The function is used for scanning system with all steps.""" self._set_devel_mode() if int(self.prepare_scan_system()) != 0: return ReturnValues.SCENARIO if int(self.generate_report()) != 0: return ReturnValues.SCENARIO # Update source XML file in temporary directory self.content = os.path.join(self.assessment_dir, settings.content_file) self.openscap_helper.update_variables(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.content) try: self.report_parser = ReportParser(self.content) except IOError: log_message("The module {0} does not exist.".format(self.content)) return ReturnValues.SCENARIO if not self.conf.contents: version = SystemIdentification.get_assessment_version( self.conf.scan) if version is None: log_message("Your scan is in a wrong format %s." % version, level=logging.ERROR) log_message( "It should be like 'RHEL6_7' for upgrade from RHEL 6->7.", level=logging.ERROR) return ReturnValues.SCENARIO self.report_parser.modify_platform_tag(version[0]) if self.conf.mode: try: lines = [ i.rstrip() for i in FileHelper.get_file_content(os.path.join( self.assessment_dir, self.conf.mode), 'rb', method=True) ] except IOError: return self.report_parser.select_rules(lines) if self.conf.select_rules: lines = [i.strip() for i in self.conf.select_rules.split(',')] unknown_rules = self.report_parser.check_rules(lines) if unknown_rules: log_message(settings.unknown_rules % '\n'.join(unknown_rules)) self.report_parser.select_rules(lines) self.run_scan_process() main_report = self.scanning_progress.get_output_data() # This function prepare XML and generate HTML self.prepare_xml_for_html() third_party_dir_name = self.get_third_party_dir(self.assessment_dir) if os.path.exists(third_party_dir_name): self.run_third_party_modules(third_party_dir_name) self.copy_preupgrade_scripts(self.assessment_dir) ConfigFilesHelper.copy_modified_config_files( settings.assessment_results_dir) # It prints out result in table format ScanningHelper.format_rules_to_table(main_report, "main contents") for target, report in six.iteritems(self.report_data): ScanningHelper.format_rules_to_table(report, "3rdparty content " + target) self.tar_ball_name = TarballHelper.tarball_result_dir( self.conf.tarball_name, self.conf.assessment_results_dir, self.conf.verbose) log_message("The tarball with results is stored in '%s' ." % self.tar_ball_name) log_message("The latest assessment is stored in the '%s' directory." % self.conf.assessment_results_dir) # pack all configuration files to tarball return 0
def run(self): """run analysis""" version_msg = "Preupgrade Assistant version: %s" % VERSION if self.conf.version: print (version_msg) return 0 logger_debug.debug(version_msg) if self.conf.list_contents_set: for dir_name, dummy_content in six.iteritems(list_contents(self.conf.source_dir)): log_message("%s" % dir_name) return 0 if not self.conf.scan and not self.conf.contents and not self.conf.list_rules: ret_val = self._check_available_contents() if int(ret_val) != 0: return ret_val if self.conf.list_rules: ret_val = self._check_available_contents() if int(ret_val) != 0: return ret_val rules = [self.conf.scan + ':' + x for x in XccdfHelper.get_list_rules(self.conf.scan)] log_message('\n'.join(rules)) return 0 if self.conf.upload: if not self.upload_results(): return ReturnValues.SEND_REPORT_TO_UI return 0 if self.conf.mode and self.conf.select_rules: log_message(settings.options_not_allowed) return ReturnValues.MODE_SELECT_RULES if not self.conf.riskcheck and not self.conf.cleanup and not self.conf.kickstart: # If force option is not mentioned and user select NO then exits if not self.conf.force: text = "" if self.conf.dst_arch: correct_option = [x for x in settings.migration_options if self.conf.dst_arch == x] if not correct_option: log_message("Specify the correct --dst-arch option.") log_message("There are '%s' or '%s' available." % (settings.migration_options[0], settings.migration_options[1])) return ReturnValues.RISK_CLEANUP_KICKSTART if SystemIdentification.get_arch() == "i386" or SystemIdentification.get_arch() == "i686": if not self.conf.dst_arch: text = '\n' + settings.migration_text logger_debug.debug("Architecture '%s'. Text '%s'.", SystemIdentification.get_arch(), text) if not show_message(settings.warning_text + text): # We do not want to continue return ReturnValues.RISK_CLEANUP_KICKSTART if self.conf.text: # Test whether w3m, lynx and elinks packages are installed found = False for pkg in SystemIdentification.get_convertors(): if xml_manager.get_package_version(pkg): self.text_convertor = pkg found = True break if not found: log_message(settings.converter_message.format(' '.join(SystemIdentification.get_convertors()))) return ReturnValues.MISSING_TEXT_CONVERTOR if os.geteuid() != 0: print("Need to be root", end="\n") if not self.conf.debug: return ReturnValues.ROOT if self.conf.cleanup: self.clean_preupgrade_environment() return 0 self.openscap_helper = OpenSCAPHelper(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.content) if self.conf.riskcheck: if not os.path.exists(self.openscap_helper.get_default_xml_result_path()): log_message("The 'preupg' command was not run yet. Run it to check for possible risks.") return ReturnValues.PREUPG_BEFORE_KICKSTART return_val = XccdfHelper.check_inplace_risk(self.openscap_helper.get_default_xml_result_path(), self.conf.verbose) return return_val if self.conf.kickstart: if not os.path.exists(self.openscap_helper.get_default_xml_result_path()): log_message("The 'preupg' command was not run yet. Run it before the Kickstart generation.") return ReturnValues.PREUPG_BEFORE_KICKSTART kg = KickstartGenerator(self.conf, settings.KS_DIR, settings.KS_PATH) kg.main() return 0 if self.conf.scan: self.content = os.path.join(self.conf.source_dir, self.conf.scan, settings.content_file) if self.conf.scan.startswith("/"): log_message('Specify the correct upgrade path parameter like -s RHEL6_7') log_message("Upgrade path is provided by the 'preupg --list' command.") self._check_available_contents() log_message("The available upgrade paths: '%s'" % '\n'.join(self.list_scans)) return ReturnValues.SCENARIO if not os.path.isdir(os.path.join(self.conf.source_dir, self.conf.scan)): log_message('Specify the correct upgrade path parameter like -s RHEL6_7') self._check_available_contents() log_message("Upgrade path is provided by the 'preupg --list' command.") log_message("The available upgrade paths: '%s'" % '\n'.join(self.list_scans)) return ReturnValues.SCENARIO if self.conf.contents: self.content = os.path.join(os.getcwd(), self.conf.contents) # From content path like content-users/RHEL6_7 we need # to get content-users dir content_dir = self.conf.contents[:self.conf.contents.find(self.get_scenario())] self.conf.source_dir = os.path.join(os.getcwd(), content_dir) self.common = Common(self.conf) if not self.conf.skip_common: if not self.common.common_results(): return ReturnValues.SCRIPT_TXT_MISSING if self.conf.scan or self.conf.contents: if not os.path.exists(settings.openscap_binary): log_message("Oscap with SCE enabled is not installed") return ReturnValues.MISSING_OPENSCAP if not os.access(settings.openscap_binary, os.X_OK): log_message("Oscap with SCE %s is not executable" % settings.openscap_binary) return ReturnValues.MISSING_OPENSCAP current_dir = os.getcwd() os.chdir("/tmp") retval = self.scan_system() if int(retval) != 0: return retval self.summary_report(self.tar_ball_name) self.common.copy_common_files() KickstartGenerator.kickstart_scripts() FileHelper.remove_home_issues() if self.conf.upload: self.upload_results(self.tar_ball_name) os.chdir(current_dir) return self.report_return_value log_message('Nothing to do. Give me a task, please.') self.conf.settings[2].parser.print_help() return 0
def scan_system(self): """The function is used for scanning system with all steps.""" self._set_devel_mode() if int(self.prepare_scan_system()) != 0: return ReturnValues.SCENARIO if int(self.generate_report()) != 0: return ReturnValues.SCENARIO # Update source XML file in temporary directory self.content = os.path.join(self.assessment_dir, settings.content_file) self.openscap_helper.update_variables(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.content) try: self.report_parser = ReportParser(self.content) except IOError: log_message("The module {0} does not exist.".format(self.content)) return ReturnValues.SCENARIO if not self.conf.contents: version = SystemIdentification.get_assessment_version(self.conf.scan) if version is None: log_message("Your scan is in a wrong format %s." % version, level=logging.ERROR) log_message("It should be like 'RHEL6_7' for upgrade from RHEL 6->7.", level=logging.ERROR) return ReturnValues.SCENARIO self.report_parser.modify_platform_tag(version[0]) if self.conf.mode: try: lines = [i.rstrip() for i in FileHelper.get_file_content(os.path.join(self.assessment_dir, self.conf.mode), 'rb', method=True)] except IOError: return self.report_parser.select_rules(lines) if self.conf.select_rules: lines = [i.strip() for i in self.conf.select_rules.split(',')] unknown_rules = self.report_parser.check_rules(lines) if unknown_rules: log_message(settings.unknown_rules % '\n'.join(unknown_rules)) self.report_parser.select_rules(lines) self.run_scan_process() main_report = self.scanning_progress.get_output_data() # This function prepare XML and generate HTML self.prepare_xml_for_html() third_party_dir_name = self.get_third_party_dir(self.assessment_dir) if os.path.exists(third_party_dir_name): self.run_third_party_modules(third_party_dir_name) self.copy_preupgrade_scripts(self.assessment_dir) ConfigFilesHelper.copy_modified_config_files(settings.assessment_results_dir) # It prints out result in table format ScanningHelper.format_rules_to_table(main_report, "main contents") for target, report in six.iteritems(self.report_data): ScanningHelper.format_rules_to_table(report, "3rdparty content " + target) self.tar_ball_name = TarballHelper.tarball_result_dir(self.conf.tarball_name, self.conf.assessment_results_dir, self.conf.verbose) log_message("The tarball with results is stored in '%s' ." % self.tar_ball_name) log_message("The latest assessment is stored in the '%s' directory." % self.conf.assessment_results_dir) # pack all configuration files to tarball return 0
def run(self): """run analysis""" version_msg = "Preupgrade Assistant version: %s" % VERSION if self.conf.version: print (version_msg) return 0 logger_debug.debug(version_msg) if self.conf.list_contents_set: for dir_name, dummy_content in iter(get_installed_module_sets( self.conf.source_dir).items()): log_message("%s" % dir_name) return 0 if self.conf.riskcheck: result_xml_path = os.path.join(settings.assessment_results_dir, settings.xml_result_name) if not os.path.exists(result_xml_path): log_message("System assessment needs to be performed first.") return ReturnValues.PREUPG_BEFORE_RISKCHECK return XccdfHelper.check_inplace_risk(result_xml_path, self.conf.verbose) if self.conf.upload and self.conf.results: if not self.upload_results(): return ReturnValues.SEND_REPORT_TO_UI return 0 if self.conf.cleanup: if not self.executed_under_root(): return ReturnValues.ROOT self.clean_preupgrade_environment() return 0 if self.conf.text: # Test whether w3m, lynx and elinks packages are installed found = False for pkg in SystemIdentification.get_convertors(): if xml_manager.get_package_version(pkg): self.text_convertor = pkg found = True break if not found: log_message(settings.converter_message.format( ' '.join(SystemIdentification.get_convertors()))) return ReturnValues.MISSING_TEXT_CONVERTOR return_code = self.determine_module_set_location() if return_code: return return_code self.determine_module_set_copy_location() if self.conf.list_rules: rules = [x for x in XccdfHelper.get_list_rules(self.all_xccdf_xml_path)] log_message('\n'.join(rules)) return 0 if self.conf.mode and self.conf.select_rules: log_message(settings.options_not_allowed) return ReturnValues.MODE_SELECT_RULES # If force option is not mentioned and user selects NO then exit if not self.conf.force: text = "" if self.conf.dst_arch: correct_option = [x for x in settings.migration_options if self.conf.dst_arch == x] if not correct_option: sys.stderr.write( "Error: Specify correct value for --dst-arch" " option.\nValid are: %s.\n" % ", ".join(settings.migration_options) ) return ReturnValues.INVALID_CLI_OPTION if SystemIdentification.get_arch() == "i386" or \ SystemIdentification.get_arch() == "i686": if not self.conf.dst_arch: text = '\n' + settings.migration_text logger_debug.debug("Architecture '%s'. Text '%s'.", SystemIdentification.get_arch(), text) if not show_message(settings.warning_text + text): # User does not want to continue return ReturnValues.USER_ABORT self.openscap_helper = OpenSCAPHelper(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.all_xccdf_xml_path) if not self.executed_under_root(): return ReturnValues.ROOT if not os.path.exists(settings.openscap_binary): log_message("Oscap with SCE enabled is not installed") return ReturnValues.MISSING_OPENSCAP if not os.access(settings.openscap_binary, os.X_OK): log_message("Oscap with SCE %s is not executable" % settings.openscap_binary) return ReturnValues.MISSING_OPENSCAP self.execution_dir = os.getcwd() os.chdir("/tmp") retval = self.scan_system() if retval != 0: return retval retval = self.summary_report(self.tar_ball_name) self.common.copy_common_files() KickstartGenerator.kickstart_scripts() FileHelper.remove_home_issues() if self.conf.upload: if not self.upload_results(): retval = ReturnValues.SEND_REPORT_TO_UI os.chdir(self.execution_dir) return retval
def test_wrong_prefix(self): version = SystemIdentification.get_assessment_version('FOOBAR6_7') self.assertEqual(version, None)
def test_wrong_prefix(self): version = SystemIdentification.get_assessment_version("FOOBAR6_7") self.assertEqual(version, None)
def test_correct_prefix(self): version = SystemIdentification.get_assessment_version("FOOBAR6_CENTOS6") self.assertEqual(version, ["6", "6"])
def run(self): """run analysis""" version_msg = "Preupgrade Assistant version: %s" % VERSION if self.conf.version: print(version_msg) return 0 logger_debug.debug(version_msg) if self.conf.list_contents_set: for dir_name, dummy_content in six.iteritems( list_contents(self.conf.source_dir)): log_message("%s" % dir_name) return 0 if not self.conf.scan and not self.conf.contents and not self.conf.list_rules: ret_val = self._check_available_contents() if int(ret_val) != 0: return ret_val if self.conf.list_rules: ret_val = self._check_available_contents() if int(ret_val) != 0: return ret_val rules = [ self.conf.scan + ':' + x for x in XccdfHelper.get_list_rules(self.conf.scan) ] log_message('\n'.join(rules)) return 0 if self.conf.upload: if not self.upload_results(): return ReturnValues.SEND_REPORT_TO_UI return 0 if self.conf.mode and self.conf.select_rules: log_message(settings.options_not_allowed) return ReturnValues.MODE_SELECT_RULES if not self.conf.riskcheck and not self.conf.cleanup and not self.conf.kickstart: # If force option is not mentioned and user select NO then exits if not self.conf.force: text = "" if self.conf.dst_arch: correct_option = [ x for x in settings.migration_options if self.conf.dst_arch == x ] if not correct_option: log_message("Specify the correct --dst-arch option.") log_message("There are '%s' or '%s' available." % (settings.migration_options[0], settings.migration_options[1])) return ReturnValues.RISK_CLEANUP_KICKSTART if SystemIdentification.get_arch( ) == "i386" or SystemIdentification.get_arch() == "i686": if not self.conf.dst_arch: text = '\n' + settings.migration_text logger_debug.debug("Architecture '%s'. Text '%s'.", SystemIdentification.get_arch(), text) if not show_message(settings.warning_text + text): # We do not want to continue return ReturnValues.RISK_CLEANUP_KICKSTART if self.conf.text: # Test whether w3m, lynx and elinks packages are installed found = False for pkg in SystemIdentification.get_convertors(): if xml_manager.get_package_version(pkg): self.text_convertor = pkg found = True break if not found: log_message( settings.converter_message.format(' '.join( SystemIdentification.get_convertors()))) return ReturnValues.MISSING_TEXT_CONVERTOR if os.geteuid() != 0: print("Need to be root", end="\n") if not self.conf.debug: return ReturnValues.ROOT if self.conf.cleanup: self.clean_preupgrade_environment() return 0 self.openscap_helper = OpenSCAPHelper(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.content) if self.conf.riskcheck: if not os.path.exists( self.openscap_helper.get_default_xml_result_path()): log_message( "The 'preupg' command was not run yet. Run it to check for possible risks." ) return ReturnValues.PREUPG_BEFORE_KICKSTART return_val = XccdfHelper.check_inplace_risk( self.openscap_helper.get_default_xml_result_path(), self.conf.verbose) return return_val if self.conf.kickstart: if not os.path.exists( self.openscap_helper.get_default_xml_result_path()): log_message( "The 'preupg' command was not run yet. Run it before the Kickstart generation." ) return ReturnValues.PREUPG_BEFORE_KICKSTART kg = KickstartGenerator(self.conf, settings.KS_DIR, settings.KS_PATH) kg.main() return 0 if self.conf.scan: self.content = os.path.join(self.conf.source_dir, self.conf.scan, settings.content_file) if self.conf.scan.startswith("/"): log_message( 'Specify the correct upgrade path parameter like -s RHEL6_7' ) log_message( "Upgrade path is provided by the 'preupg --list' command.") self._check_available_contents() log_message("The available upgrade paths: '%s'" % '\n'.join(self.list_scans)) return ReturnValues.SCENARIO if not os.path.isdir( os.path.join(self.conf.source_dir, self.conf.scan)): log_message( 'Specify the correct upgrade path parameter like -s RHEL6_7' ) self._check_available_contents() log_message( "Upgrade path is provided by the 'preupg --list' command.") log_message("The available upgrade paths: '%s'" % '\n'.join(self.list_scans)) return ReturnValues.SCENARIO if self.conf.contents: self.content = os.path.join(os.getcwd(), self.conf.contents) # From content path like content-users/RHEL6_7 we need # to get content-users dir content_dir = self.conf.contents[:self.conf.contents. find(self.get_scenario())] self.conf.source_dir = os.path.join(os.getcwd(), content_dir) self.common = Common(self.conf) if not self.conf.skip_common: if not self.common.common_results(): return ReturnValues.SCRIPT_TXT_MISSING if self.conf.scan or self.conf.contents: if not os.path.exists(settings.openscap_binary): log_message("Oscap with SCE enabled is not installed") return ReturnValues.MISSING_OPENSCAP if not os.access(settings.openscap_binary, os.X_OK): log_message("Oscap with SCE %s is not executable" % settings.openscap_binary) return ReturnValues.MISSING_OPENSCAP current_dir = os.getcwd() os.chdir("/tmp") retval = self.scan_system() if int(retval) != 0: return retval self.summary_report(self.tar_ball_name) self.common.copy_common_files() KickstartGenerator.kickstart_scripts() FileHelper.remove_home_issues() if self.conf.upload: self.upload_results(self.tar_ball_name) os.chdir(current_dir) return self.report_return_value log_message('Nothing to do. Give me a task, please.') self.conf.settings[2].parser.print_help() return 0
def test_correct_prefix(self): version = SystemIdentification.get_assessment_version( 'FOOBAR6_CENTOS6') self.assertEqual(version, ['6', '6'])