Beispiel #1
0
    def generate_report(self):
        """Function generates report"""
        scenario = self.get_scenario()
        scenario_path = os.path.join(self.conf.source_dir, scenario)
        self.assessment_dir = os.path.join(self.conf.assessment_results_dir,
                                           self.get_proper_scenario(scenario))
        dir_util.copy_tree(scenario_path, self.assessment_dir)
        # Try copy directory with contents to /root/preupgrade
        # Call xccdf_compose API for generating all-xccdf.xml
        if not self.conf.contents:
            xccdf_compose = XCCDFCompose(self.assessment_dir)
            if xccdf_compose.generate_xml(generate_from_ini=False) != 0:
                return ReturnValues.SCENARIO
            if os.path.isdir(self.assessment_dir):
                shutil.rmtree(self.assessment_dir)
            shutil.move(xccdf_compose.get_compose_dir_name(),
                        self.assessment_dir)

        self.common.prep_symlinks(self.assessment_dir,
                                  scenario=self.get_proper_scenario(scenario))
        if not self.conf.contents:
            XccdfHelper.update_platform(
                os.path.join(self.assessment_dir, settings.content_file))
        else:
            XccdfHelper.update_platform(self.content)
            self.assessment_dir = os.path.dirname(self.content)
        return 0
 def _update_xccdf_file(self, return_value, risk):
     temp_file = self._copy_xccdf_file(update_return_value=return_value,
                                       update_text=risk)
     return_value = XccdfHelper.check_inplace_risk(
         os.path.join(os.path.dirname(temp_file), 'all-xccdf.xml'), 0)
     shutil.rmtree(os.path.dirname(temp_file))
     return return_value
 def update_inplace_risk(self, scanning_progress, rule, res):
     """Function updates inplace risk"""
     inplace_risk = XccdfHelper.get_check_import_inplace_risk(rule)
     if inplace_risk:
         logger_report.debug("Update_inplace_risk '%s'", inplace_risk)
         return_value = XccdfHelper.get_and_print_inplace_risk(0, inplace_risk)
         logger_report.debug("Get and print inplace risk return code '%s'", return_value)
         if int(return_value)/2 == 1:
             res.text = ReportHelper.get_needs_inspection()
         elif int(return_value)/2 == 2:
             res.text = ReportHelper.get_needs_action()
         for index, row in enumerate(scanning_progress.output_data):
             if self.get_nodes_text(rule, "title") in row:
                 scanning_progress.output_data[index] = "{0}:{1}".format(
                     self.get_nodes_text(rule, "title"),
                     res.text)
    def summary_report(self, tarball_path):
        """Function prints a summary report"""
        command = settings.ui_command.format(tarball_path)
        if self.conf.text:
            path = self.openscap_helper.get_default_txt_result_path()
        else:
            path = self.openscap_helper.get_default_html_result_path()

        report_dict = {
            0: settings.risks_found_warning.format(path),
            1: settings.risks_found_warning.format(path),
            2: 'We have found some critical issues. In-place upgrade or migration is not advised.\n' +
               "Read the file {0} for more details.".format(path),
            3: 'We have found some error issues. In-place upgrade or migration is not advised.\n' +
               "Read the file {0} for more details.".format(path)
        }
        report_return_value = XccdfHelper.check_inplace_risk(
            self.openscap_helper.get_default_xml_result_path(),
            0)
        try:
            if report_dict[int(report_return_value)]:
                log_message('Summary information:')
                log_message(report_dict[int(report_return_value)])
        except KeyError:
            # We do not want to print anything in case of testing contents
            pass
        if not self.conf.mode or self.conf.mode == "upgrade":
            # User either has not specied mode (upgrade and migration both
            # together by default) or has chosen upgrade only = print warning
            # to backup the system before doing the in-place upgrade
            log_message(settings.upgrade_backup_warning)
        log_message("Upload results to UI by the command:\ne.g. {0} .".format(command))
        return report_return_value
Beispiel #5
0
    def summary_report(self, tarball_path):
        """Function prints a summary report"""
        command = settings.ui_command.format(tarball_path)
        if self.conf.text:
            path = self.openscap_helper.get_default_txt_result_path()
        else:
            path = self.openscap_helper.get_default_html_result_path()

        report_dict = {
            0: settings.risks_found_warning.format(path),
            1: settings.risks_found_warning.format(path),
            2: 'We have found some critical issues. In-place upgrade or migration is not advised.\n' +
               "Read the file {0} for more details.".format(path),
            3: 'We have found some error issues. In-place upgrade or migration is not advised.\n' +
               "Read the file {0} for more details.".format(path)
        }
        report_return_value = XccdfHelper.check_inplace_risk(
            self.openscap_helper.get_default_xml_result_path(),
            0)
        try:
            if report_dict[int(report_return_value)]:
                log_message('Summary information:')
                log_message(report_dict[int(report_return_value)])
        except KeyError:
            # We do not want to print anything in case of testing contents
            pass
        if not self.conf.mode or self.conf.mode == "upgrade":
            # User either has not specied mode (upgrade and migration both
            # together by default) or has chosen upgrade only = print warning
            # to backup the system before doing the in-place upgrade
            log_message(settings.upgrade_backup_warning)
        log_message("Upload results to UI by the command:\ne.g. {0} .".format(command))
        return report_return_value
    def replace_inplace_risk(self, scanning_results=None):
        """
        This function has aim to replace FAILED to
        NEEDS_INSPECTION in case that risks are SLIGHT or MEDIUM
        """
        #Filter all rule-result in TestResult
        changed_fields = []
        self.remove_empty_check_import()
        inplace_dict = {
            0: ReportHelper.upd_inspection,
            1: ReportHelper.upd_action,
            2: ReportHelper.upd_extreme,
        }
        for rule in self.get_all_result_rules():
            result = [
                x for x in self.get_nodes(rule, "result") if x.text == "fail"
            ]
            # Get all affected rules and taken their names
            for res in result:
                inplace_risk = XccdfHelper.get_check_import_inplace_risk(rule)
                logger_report.debug(inplace_risk)
                # In case that report has state fail and
                # no log_risk than it should be needs_inspection
                if not inplace_risk:
                    changed = rule.get("idref")
                    res.text = "fail"
                    log_message(
                        'The %s module exits as fail but without a risk.' %
                        rule.get("idref"))
                else:
                    inplace_num = XccdfHelper.get_and_print_inplace_risk(
                        0, inplace_risk)
                    logger_report.debug("Call function '%s'",
                                        inplace_dict[inplace_num])
                    changed, res.text = inplace_dict[inplace_num](rule)
                    logger_report.debug("Replace text '%s:%s'", changed,
                                        res.text)
                if changed is not None:
                    changed_fields.append(changed + ":" + res.text)

        if scanning_results:
            scanning_results.update_data(changed_fields)

        self.write_xml()
    def replace_inplace_risk(self, scanning_results=None):
        """
        This function has aim to replace FAILED to
        NEEDS_INSPECTION in case that risks are SLIGHT or MEDIUM
        """
        #Filter all rule-result in TestResult
        changed_fields = []
        self.remove_empty_check_import()
        inplace_dict = {
            0: ReportHelper.upd_inspection,
            1: ReportHelper.upd_action,
            2: ReportHelper.upd_extreme,
        }
        for rule in self.get_all_result_rules():
            result = [x for x in self.get_nodes(rule, "result") if x.text == "fail"]
            # Get all affected rules and taken their names
            for res in result:
                inplace_risk = XccdfHelper.get_check_import_inplace_risk(rule)
                logger_report.debug(inplace_risk)
                # In case that report has state fail and
                # no log_risk than it should be needs_inspection
                if not inplace_risk:
                    changed = rule.get("idref")
                    res.text = "fail"
                    log_message('The %s module exits as fail but without a risk.' % rule.get("idref"))
                else:
                    inplace_num = XccdfHelper.get_and_print_inplace_risk(0, inplace_risk)
                    logger_report.debug("Call function '%s'", inplace_dict[inplace_num])
                    changed, res.text = inplace_dict[inplace_num](rule)
                    logger_report.debug("Replace text '%s:%s'", changed, res.text)
                if changed is not None:
                    changed_fields.append(changed+":"+res.text)

        if scanning_results:
            scanning_results.update_data(changed_fields)

        self.write_xml()
    def generate_report(self):
        """Function generates report"""
        scenario = self.get_scenario()
        scenario_path = os.path.join(self.conf.source_dir, scenario)
        self.assessment_dir = os.path.join(self.conf.assessment_results_dir, self.get_proper_scenario(scenario))
        dir_util.copy_tree(scenario_path, self.assessment_dir)
        # Try copy directory with contents to /root/preupgrade
        # Call xccdf_compose API for generating all-xccdf.xml
        if not self.conf.contents:
            xccdf_compose = XCCDFCompose(self.assessment_dir)
            if xccdf_compose.generate_xml(generate_from_ini=False) != 0:
                return ReturnValues.SCENARIO
            if os.path.isdir(self.assessment_dir):
                shutil.rmtree(self.assessment_dir)
            shutil.move(xccdf_compose.get_compose_dir_name(), self.assessment_dir)

        self.common.prep_symlinks(self.assessment_dir,
                                  scenario=self.get_proper_scenario(scenario))
        if not self.conf.contents:
            XccdfHelper.update_platform(os.path.join(self.assessment_dir, settings.content_file))
        else:
            XccdfHelper.update_platform(self.content)
            self.assessment_dir = os.path.dirname(self.content)
        return 0
Beispiel #9
0
    def summary_report(self, tarball_path):
        """Function prints a summary report"""
        command = settings.ui_command.format(tarball_path)
        if self.conf.text:
            path = self.openscap_helper.get_default_txt_result_path()
        else:
            path = self.openscap_helper.get_default_html_result_path()

        report_dict = {
            0:
            settings.message.format(path),
            1:
            settings.message.format(path),
            2:
            'We found some critical issues. In-place upgrade is not advised.\n'
            + "Read the file {0} for more details.".format(path),
            3:
            'We found some error issues. In-place upgrade is not advised.\n' +
            "Read the file {0} for more details.".format(path)
        }
        self.report_return_value = XccdfHelper.check_inplace_risk(
            self.openscap_helper.get_default_xml_result_path(), 0)
        try:
            if report_dict[int(self.report_return_value)]:
                log_message('Summary information:')
                log_message(report_dict[int(self.report_return_value)])
            for report_type in settings.REPORTS:
                file_name = settings.result_prefix + '-' + report_type + '.html'
                report_name = os.path.join(
                    os.path.dirname(self.report_parser.get_path()), file_name)
                if os.path.exists(report_name):
                    log_message(
                        "Read the %s report file %s for more details." %
                        (report_type, report_name))
        except KeyError:
            # We do not want to print anything in case of testing contents
            pass
        if self.report_data:
            log_message('Summary of the third party providers:')
            for target, dummy_report in six.iteritems(self.report_data):
                self.third_party = target
                log_message(
                    "Read the third party content {0} {1} for more details.".
                    format(target, path))
        log_message(
            "Upload results to UI by the command:\ne.g. {0} .".format(command))
    def summary_report(self, tarball_path):
        """Function prints a summary report"""
        command = settings.ui_command.format(tarball_path)
        if self.conf.text:
            path = self.openscap_helper.get_default_txt_result_path()
        else:
            path = self.openscap_helper.get_default_html_result_path()

        report_dict = {
            0: settings.message.format(path),
            1: settings.message.format(path),
            2: 'We found some critical issues. In-place upgrade is not advised.\n' +
            "Read the file {0} for more details.".
            format(path),
            3: 'We found some error issues. In-place upgrade is not advised.\n' +
               "Read the file {0} for more details.".format(path)

        }
        self.report_return_value = XccdfHelper.check_inplace_risk(self.openscap_helper.get_default_xml_result_path(), 0)
        try:
            if report_dict[int(self.report_return_value)]:
                log_message('Summary information:')
                log_message(report_dict[int(self.report_return_value)])
            for report_type in settings.REPORTS:
                file_name = settings.result_prefix + '-' + report_type + '.html'
                report_name = os.path.join(os.path.dirname(self.report_parser.get_path()), file_name)
                if os.path.exists(report_name):
                    log_message("Read the %s report file %s for more details." % (report_type, report_name))
        except KeyError:
            # We do not want to print anything in case of testing contents
            pass
        if self.report_data:
            log_message('Summary of the third party providers:')
            for target, dummy_report in six.iteritems(self.report_data):
                self.third_party = target
                log_message("Read the third party content {0} {1} for more details.".
                            format(target, path))
        log_message("Upload results to UI by the command:\ne.g. {0} .".format(command))
Beispiel #11
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 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 _update_xccdf_file(self, return_value, risk):
     temp_file = self._copy_xccdf_file(update_return_value=return_value, update_text=risk)
     return_value = XccdfHelper.check_inplace_risk(os.path.join(os.path.dirname(temp_file), "all-xccdf.xml"), 0)
     shutil.rmtree(os.path.dirname(temp_file))
     return return_value
    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
Beispiel #15
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