Esempio n. 1
0
    def run(self):
        """run analysis"""
        if self.conf.version:
            print ("Preupgrade Assistant version: %s" % VERSION)
            return 0

        if not self.conf.scan and not self.conf.contents:
            cnt = 0
            is_dir = lambda x: os.path.isdir(os.path.join(self.conf.source_dir, x))
            dirs = os.listdir(self.conf.source_dir)
            for dir_name in filter(is_dir, dirs):
                if utils.get_assessment_version(dir_name):
                    self.conf.scan = dir_name
                    cnt += 1

            if int(cnt) < 1:
                log_message("There were no contents found in directory %s. \
If you would like to use this tool, you have to install some." % settings.source_dir)
                return 1
            if int(cnt) > 1:
                log_message("Preupgrade assistant detects more then 1 set of contents in directory%s. \
If you would like to use this tool, you have to specify correct upgrade path parameter like -s RHEL6_7." % settings.source_dir)
                return 1

        if self.conf.list_contents_set:
            for dir_name, dummy_content in six.iteritems(list_contents(self.conf.source_dir)):
                log_message("{0}".format(dir_name))
            return 0

        if self.conf.list_rules:
            log_message(settings.list_rules % '\n'.join(utils.get_list_rules(self.conf.scan)))
            return 0

        if self.conf.upload and self.conf.results:
            self.upload_results()
            return 0

        if self.conf.mode and self.conf.select_rules:
            log_message(settings.options_not_allowed)
            return 1

        if not self.conf.riskcheck and not self.conf.apply 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 and not show_message(settings.warning_text):
                # We do not want to continue
                return 0

        if self.conf.text:
            # Test whether w3m, lynx and elinks packages are installed
            found = False
            for pkg in utils.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(utils.get_convertors())))
                return 0

        if os.geteuid() != 0:
            print("Need to be root", end="\n")
            if not self.conf.debug:
                return 2

        if self.conf.cleanup:
            self.clean_preupgrade_environment()
            sys.exit(0)

        if self.conf.riskcheck:
            return_val = xccdf.check_inplace_risk(self.get_default_xml_result_path(), self.conf.verbose)
            return return_val

        if self.conf.kickstart:
            if not os.path.exists(self.get_default_xml_result_path()):
                log_message("'preupg' command was not run yet. Run them before kickstart generation.")
                return 1
            kg = KickstartGenerator(settings.KS_DIR, self.get_preupgrade_kickstart())
            KickstartGenerator.copy_kickstart_templates()
            dummy_ks = kg.generate()
            if dummy_ks:
                log_message(settings.kickstart_text % self.get_preupgrade_kickstart())
            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 correct upgrade path parameter like -s RHEL6_7')
                log_message('Upgrade path is provided by command preupg --list')
                return 1
            if not os.path.isdir(os.path.join(self.conf.source_dir, self.conf.scan)):
                log_message('Specify correct upgrade path parameter like -s RHEL6_7')
                log_message('Upgrade path is provided by command preupg --list')
                return 1

        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 1

        if self.conf.scan or self.conf.contents:
            if not os.path.exists(self.binary):
                log_message("Oscap with SCE enabled is not installed")
                return 1
            if not os.access(self.binary, os.X_OK):
                log_message("Oscap with SCE %s is not executable" % self.binary)
                return 1

            current_dir = os.getcwd()
            os.chdir("/tmp")
            tarball_path = self.scan_system()
            self.summary_report(tarball_path)
            self.common.copy_common_files()
            KickstartGenerator.kickstart_scripts()
            utils.remove_home_issues()
            if self.conf.upload:
                self.upload_results(tarball_path)
            os.chdir(current_dir)
            return 0

        log_message('Nothing to do. Give me a task, please.')
        self.conf.settings[2].parser.print_help()
        return 0
 def setUp(self):
     kickstart_file = "preupgrade.ks"
     self.ks = KickstartGenerator(os.path.join(os.getcwd(), "tests", "partition_data"), kickstart_file)
 def setUp(self):
     kickstart_file = 'preupgrade.ks'
     self.ks = KickstartGenerator(
         os.path.join(os.getcwd(), 'tests', 'partition_data'),
         kickstart_file)
class TestPartitioning(base.TestCase):

    ks = None

    def setUp(self):
        kickstart_file = "preupgrade.ks"
        self.ks = KickstartGenerator(os.path.join(os.getcwd(), "tests", "partition_data"), kickstart_file)

    def test_lvm_partitions(self):
        lvm_lsblk = get_full_path("lvm_lsblk_list")
        vgs_list = get_full_path("vgs_list")
        lvdisplay = get_full_path("lvdisplay")
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = [
            "clearpart --all",
            "part /boot --size=500 --ondisk=vda",
            "part pv.01 --size=9000",
            "volgroup vg_rhel67 pv.01 --pesize=4096",
            "logvol / --vgname=vg_rhel67 --size=8000 --name=lv_root",
            "logvol swap --vgname=vg_rhel67 --size=1000 --name=lv_swap",
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_lvm_crypt_partitions(self):
        lvm_lsblk = get_full_path("lvm_crypt_lsblk_list")
        vgs_list = get_full_path("vgs_list")
        lvdisplay = get_full_path("lvdisplay")
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = [
            "clearpart --all",
            "part /boot --size=500 --ondisk=vda",
            "part pv.01 --size=9000 --encrypted",
            "volgroup vg_rhel67 pv.01 --pesize=4096",
            "logvol / --vgname=vg_rhel67 --size=8000 --name=lv_root",
            "logvol swap --vgname=vg_rhel67 --size=1000 --name=lv_swap",
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_crypt_partitions(self):
        lvm_lsblk = get_full_path("crypt_lsblk_list")
        vgs_list = None
        lvdisplay = None
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = [
            "clearpart --all",
            "part / --size=3000 --encrypted",
            "part /boot --size=200 --ondisk=vda",
            "part swap --size=2000 --ondisk=vda",
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_raid_crypt_partitions(self):
        raid_lsblk = get_full_path("raid_lsblk_list")
        self.ks.get_partition_layout(raid_lsblk, None, None)
        expected_layout = [
            "clearpart --all",
            "part /boot --size=200 --ondisk=sda",
            "part swap --size=1000 --ondisk=sda",
            "part raid.01 --grow --size=2048",
            "part raid.02 --grow --size=2048",
            "raid / --level=1 --device=md1 raid.01 raid.02",
            "part raid.03 --grow --size=2048",
            "part raid.04 --grow --size=2048",
            "raid /home --level=0 --device=md0 raid.03 raid.04 --encrypted --passphrase=",
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_raid_second_partitions(self):
        raid_lsblk = get_full_path("raid_lsblk_second_list")
        self.ks.get_partition_layout(raid_lsblk, None, None)
        expected_layout = [
            "clearpart --all",
            "part /boot --size=1000 --ondisk=vda",
            "part swap --size=1000 --ondisk=vdb",
            "part raid.01 --grow --size=2048",
            "part raid.02 --grow --size=2048",
            "raid / --level=0 --device=md0 raid.01 raid.02",
        ]
        self.assertListEqual(self.ks.part_layout, expected_layout)

    def test_native_partitioning(self):
        raid_lsblk = get_full_path("lsblk_native_list")
        self.ks.get_partition_layout(raid_lsblk, None, None)
        expected_layout = [
            "clearpart --all",
            "part / --size=5000 --ondisk=vda",
            "part /boot --size=200 --ondisk=vda",
            "part /home --size=2000 --ondisk=vda",
            "part swap --size=1000 --ondisk=vda",
        ]
        self.assertListEqual(self.ks.part_layout, expected_layout)

    """def test_lvm_complicated_partitions(self):
        lvm_lsblk = get_full_path('lvm_complicated_lsblk_list')
        vgs_list = get_full_path('vgs_list_complicated')
        lvdisplay = get_full_path('lvdisplay_complicated')
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = ['clearpart --all',
                           'part --size=500M --ondisk=vda',
                           'part pv.1 --size 9.5G --encrypted',
                           'volgroup vg_rhel67 pv.01 --pesize=4096',
                           'logvol / --vgname=vg_rhel67 --size=8000 --name=lv_root',
                           'logvol swap --vgname=vg_rhel67 --size=1000 --name=lv_swap']
        self.assertEqual(expected_layout, self.ks.part_layout)
    """

    def tearDown(self):
        pass
class TestPartitioning(base.TestCase):

    ks = None

    def setUp(self):
        kickstart_file = 'preupgrade.ks'
        self.ks = KickstartGenerator(
            os.path.join(os.getcwd(), 'tests', 'partition_data'),
            kickstart_file)

    def test_lvm_partitions(self):
        lvm_lsblk = get_full_path('lvm_lsblk_list')
        vgs_list = get_full_path('vgs_list')
        lvdisplay = get_full_path('lvdisplay')
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = [
            'clearpart --all', 'part /boot --size=500 --ondisk=vda',
            'part pv.01 --size=9000', 'volgroup vg_rhel67 pv.01 --pesize=4096',
            'logvol / --vgname=vg_rhel67 --size=8000 --name=lv_root',
            'logvol swap --vgname=vg_rhel67 --size=1000 --name=lv_swap'
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_lvm_crypt_partitions(self):
        lvm_lsblk = get_full_path('lvm_crypt_lsblk_list')
        vgs_list = get_full_path('vgs_list')
        lvdisplay = get_full_path('lvdisplay')
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = [
            'clearpart --all', 'part /boot --size=500 --ondisk=vda',
            'part pv.01 --size=9000 --encrypted',
            'volgroup vg_rhel67 pv.01 --pesize=4096',
            'logvol / --vgname=vg_rhel67 --size=8000 --name=lv_root',
            'logvol swap --vgname=vg_rhel67 --size=1000 --name=lv_swap'
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_crypt_partitions(self):
        lvm_lsblk = get_full_path('crypt_lsblk_list')
        vgs_list = None
        lvdisplay = None
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = [
            'clearpart --all', 'part / --size=3000 --encrypted',
            'part /boot --size=200 --ondisk=vda',
            'part swap --size=2000 --ondisk=vda'
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_raid_crypt_partitions(self):
        raid_lsblk = get_full_path('raid_lsblk_list')
        self.ks.get_partition_layout(raid_lsblk, None, None)
        expected_layout = [
            'clearpart --all', 'part /boot --size=200 --ondisk=sda',
            'part swap --size=1000 --ondisk=sda',
            'part raid.01 --grow --size=2048',
            'part raid.02 --grow --size=2048',
            'raid / --level=1 --device=md1 raid.01 raid.02',
            'part raid.03 --grow --size=2048',
            'part raid.04 --grow --size=2048',
            'raid /home --level=0 --device=md0 raid.03 raid.04 --encrypted --passphrase='
        ]
        self.assertEqual(expected_layout, self.ks.part_layout)

    def test_raid_second_partitions(self):
        raid_lsblk = get_full_path('raid_lsblk_second_list')
        self.ks.get_partition_layout(raid_lsblk, None, None)
        expected_layout = [
            'clearpart --all',
            'part /boot --size=1000 --ondisk=vda',
            'part swap --size=1000 --ondisk=vdb',
            'part raid.01 --grow --size=2048',
            'part raid.02 --grow --size=2048',
            'raid / --level=0 --device=md0 raid.01 raid.02',
        ]
        self.assertListEqual(self.ks.part_layout, expected_layout)

    def test_native_partitioning(self):
        raid_lsblk = get_full_path('lsblk_native_list')
        self.ks.get_partition_layout(raid_lsblk, None, None)
        expected_layout = [
            'clearpart --all',
            'part / --size=5000 --ondisk=vda',
            'part /boot --size=200 --ondisk=vda',
            'part /home --size=2000 --ondisk=vda',
            'part swap --size=1000 --ondisk=vda',
        ]
        self.assertListEqual(self.ks.part_layout, expected_layout)

    """def test_lvm_complicated_partitions(self):
        lvm_lsblk = get_full_path('lvm_complicated_lsblk_list')
        vgs_list = get_full_path('vgs_list_complicated')
        lvdisplay = get_full_path('lvdisplay_complicated')
        self.ks.get_partition_layout(lvm_lsblk, vgs_list, lvdisplay)
        expected_layout = ['clearpart --all',
                           'part --size=500M --ondisk=vda',
                           'part pv.1 --size 9.5G --encrypted',
                           'volgroup vg_rhel67 pv.01 --pesize=4096',
                           'logvol / --vgname=vg_rhel67 --size=8000 --name=lv_root',
                           'logvol swap --vgname=vg_rhel67 --size=1000 --name=lv_swap']
        self.assertEqual(expected_layout, self.ks.part_layout)
    """

    def tearDown(self):
        pass