Beispiel #1
0
Datei: dnf.py Projekt: D4N/kiwi
 def post_process_install_requests_bootstrap(self):
     """
     Move the rpm database to the place as it is expected by the
     rpm package installed during bootstrap phase
     """
     rpmdb = RpmDataBase(self.root_dir)
     rpmdb.set_database_to_image_path()
Beispiel #2
0
    def import_trusted_keys(self, signing_keys: List) -> None:
        """
        Imports trusted keys into the image

        :param list signing_keys: list of the key files to import
        """
        rpmdb = RpmDataBase(self.root_dir)
        for key in signing_keys:
            rpmdb.import_signing_key_to_image(key)
Beispiel #3
0
    def post_process_install_requests_bootstrap(self, root_bind=None):
        """
        Move the rpm database to the place as it is expected by the
        rpm package installed during bootstrap phase

        :param object root_bind: unused
        """
        rpmdb = RpmDataBase(self.root_dir)
        if rpmdb.has_rpm():
            rpmdb.set_database_to_image_path()
Beispiel #4
0
 def _get_rpm_database_location(self):
     shared_mount = MountManager(device='/dev',
                                 mountpoint=self.root_dir + '/dev')
     if not shared_mount.is_mounted():
         shared_mount.bind_mount()
     rpmdb = RpmDataBase(self.root_dir)
     if rpmdb.has_rpm():
         dbpath = rpmdb.rpmdb_image.expand_query('%_dbpath')
     else:
         dbpath = rpmdb.rpmdb_host.expand_query('%_dbpath')
     if shared_mount.is_mounted():
         shared_mount.umount_lazy()
     return dbpath
Beispiel #5
0
    def setup_package_database_configuration(self):
        """
        Setup rpm macros for bootstrapping and image building

        1. Create the rpm image macro which persists during the build
        2. Create the rpm bootstrap macro to make sure for bootstrapping
           the rpm database location matches the host rpm database setup.
           This macro only persists during the bootstrap phase
        3. Create zypper compat link
        """
        rpmdb = RpmDataBase(self.root_dir,
                            Defaults.get_custom_rpm_image_macro_name())
        if self.locale:
            rpmdb.set_macro_from_string(self.locale[0])
        rpmdb.write_config()

        RpmDataBase(self.root_dir).set_database_to_host_path()
        # Zypper compat code:
        #
        # Manually adding the compat link /var/lib/rpm that points to the
        # rpmdb location as it is configured in the host rpm setup. The
        # host rpm setup is taken into account because import_trusted_keys
        # is called during the bootstrap phase where rpm (respectively zypper)
        # is called from the host
        #
        # Usually it is expected that the package manager reads the
        # signing keys from the rpm database setup provisioned by rpm
        # itself (macro level) but zypper doesn't take the rpm macro
        # setup into account and relies on a hard coded path which we
        # can only provide as a symlink.
        #
        # That symlink is usually created by the rpm package when it gets
        # installed. However at that early phase when we import the
        # signing keys no rpm is installed yet nor any symlink exists.
        # Thus we have to create it here and hope to get rid of it in the
        # future.
        #
        # For further details on the motivation in zypper please
        # refer to bsc#1112357
        rpmdb.init_database()
        Path.create(os.sep.join([self.root_dir, 'var', 'lib']))
        Command.run([
            'ln', '-s', ''.join(
                ['../..', rpmdb.rpmdb_host.expand_query('%_dbpath')]),
            os.sep.join([self.root_dir, 'var', 'lib', 'rpm'])
        ],
                    raise_on_error=False)
Beispiel #6
0
    def setup_package_database_configuration(self):
        """
        Setup rpm macros for bootstrapping and image building

        1. Create the rpm image macro which persists during the build
        2. Create the rpm bootstrap macro to make sure for bootstrapping
           the rpm database location matches the host rpm database setup.
           This macro only persists during the bootstrap phase
        """
        rpmdb = RpmDataBase(self.root_dir,
                            Defaults.get_custom_rpm_image_macro_name())
        if self.locale:
            rpmdb.set_macro_from_string(self.locale[0])
        rpmdb.write_config()

        RpmDataBase(self.root_dir).set_database_to_host_path()
Beispiel #7
0
 def setup(self):
     self.rpmdb = RpmDataBase('root_dir')
     self.rpmdb.rpmdb_host = Mock()
     self.rpmdb.rpmdb_image = Mock()
Beispiel #8
0
class TestRpmDataBase:
    def setup(self):
        self.rpmdb = RpmDataBase('root_dir')
        self.rpmdb.rpmdb_host = Mock()
        self.rpmdb.rpmdb_image = Mock()

    def setup_method(self, cls):
        self.setup()

    @patch('kiwi.utils.rpm_database.Path.which')
    def test_has_rpm(self, mock_Path_which):
        mock_Path_which.return_value = None
        assert self.rpmdb.has_rpm() is False
        mock_Path_which.assert_called_once_with(
            'rpmdb', access_mode=1, root_dir='root_dir'
        )
        mock_Path_which.return_value = 'rpm'
        assert self.rpmdb.has_rpm() is True

    @patch('kiwi.command.Command.run')
    def test_rebuild_database(self, mock_Command_run):
        self.rpmdb.rebuild_database()
        mock_Command_run.assert_called_once_with(
            ['chroot', 'root_dir', 'rpmdb', '--rebuilddb']
        )

    @patch('kiwi.command.Command.run')
    def test_init_database(self, mock_Command_run):
        self.rpmdb.init_database()
        mock_Command_run.assert_called_once_with(
            ['rpm', '--root', 'root_dir', '--initdb']
        )

    def test_set_database_to_host_path(self):
        self.rpmdb.set_database_to_host_path()
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_dbpath', self.rpmdb.rpmdb_host.get_query.return_value
        )
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
        self.rpmdb.rpmdb_host.get_query.assert_called_once_with('_dbpath')

    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    def test_link_database_to_host_path(self, mock_exists, mock_Command_run):
        self.rpmdb.rpmdb_image.expand_query.return_value = \
            '/var/lib/rpm'
        self.rpmdb.rpmdb_host.expand_query.return_value = \
            '/usr/lib/sysimage/rpm'
        mock_exists.return_value = False
        self.rpmdb.link_database_to_host_path()
        assert mock_Command_run.call_args_list == [
            call(['mkdir', '-p', 'root_dir/usr/lib/sysimage']),
            call(
                [
                    'ln', '-s', '../../../var/lib/rpm',
                    'root_dir/usr/lib/sysimage/rpm'
                ]
            )
        ]

    @patch('kiwi.utils.rpm_database.Path.wipe')
    @patch('kiwi.command.Command.run')
    @patch('os.path.islink')
    @patch('os.path.exists')
    @patch('os.unlink')
    def test_set_database_to_image_path(
        self, mock_os_unlink, mock_os_exists, mock_os_islink,
        mock_Command_run, mock_Path_wipe
    ):
        mock_os_exists.return_value = True

        link = [False, False]

        def mocked_islink(path):
            return link.pop()

        mock_os_islink.side_effect = mocked_islink
        self.rpmdb.rpmdb_image.expand_query.return_value = \
            '/var/lib/rpm'
        self.rpmdb.rpmdb_host.expand_query.return_value = \
            '/usr/lib/sysimage/rpm'
        self.rpmdb.set_database_to_image_path()
        self.rpmdb.rpmdb_image.expand_query.assert_called_once_with('%_dbpath')
        assert self.rpmdb.rpmdb_image.set_config_value.call_args_list == [
            call(
                '_dbpath', self.rpmdb.rpmdb_host.expand_query.return_value
            ),
            call(
                '_dbpath_rebuild', self.rpmdb.rpmdb_image.get_query.return_value
            )
        ]
        assert mock_Path_wipe.call_args_list == [
            call('root_dir/var/lib/rpm'),
            call('root_dir/usr/lib/sysimage/rpm')
        ]
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
        assert mock_Command_run.call_args_list == [
            call(
                [
                    'chroot', 'root_dir', 'rpmdb', '--rebuilddb'
                ]
            ),
            call(
                [
                    'mv', 'root_dir/usr/lib/sysimage/rpm/alternatives',
                    'root_dir/var/lib/rpm'
                ]
            )
        ]
        assert self.rpmdb.rpmdb_image.wipe_config.call_count == 2
        link = [True, False]
        mock_os_islink.side_effect = mocked_islink
        self.rpmdb.set_database_to_image_path()
        mock_os_unlink.assert_called_once_with(
            'root_dir/var/lib/rpm'
        )
        link = [True, True]
        mock_os_islink.side_effect = mocked_islink
        self.rpmdb.set_database_to_image_path()
        assert mock_Command_run.call_args_list[-1] == call(
            ['chroot', 'root_dir', 'rpmdb', '--rebuilddb']
        )

    def test_set_macro_from_string(self):
        self.rpmdb.set_macro_from_string('_install_langs%en_US:de_DE')
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_install_langs', 'en_US:de_DE'
        )

    def test_write_config(self):
        self.rpmdb.write_config()
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()

    @patch('kiwi.command.Command.run')
    def test_import_signing_key_to_image(self, mock_Command_run):
        self.rpmdb.import_signing_key_to_image('key')
        mock_Command_run.assert_called_once_with(
            [
                'rpm', '--root', 'root_dir', '--import', 'key',
                '--dbpath', self.rpmdb.rpmdb_host.expand_query.return_value
            ]
        )
class TestRpmDataBase(object):
    def setup(self):
        self.rpmdb = RpmDataBase('root_dir')
        self.rpmdb.rpmdb_host = Mock()
        self.rpmdb.rpmdb_image = Mock()

    @patch('kiwi.utils.rpm_database.Path.which')
    def test_has_rpm(self, mock_Path_which):
        mock_Path_which.return_value = None
        assert self.rpmdb.has_rpm() is False
        mock_Path_which.assert_called_once_with(
            'rpm', access_mode=1, custom_env={'PATH': 'root_dir/usr/bin'})
        mock_Path_which.return_value = 'rpm'
        assert self.rpmdb.has_rpm() is True

    @patch('kiwi.command.Command.run')
    def test_rebuild_database(self, mock_Command_run):
        self.rpmdb.rebuild_database()
        mock_Command_run.assert_called_once_with(
            ['chroot', 'root_dir', 'rpmdb', '--rebuilddb'])

    def test_set_database_to_host_path(self):
        self.rpmdb.set_database_to_host_path()
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_dbpath', self.rpmdb.rpmdb_host.get_query.return_value)
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
        self.rpmdb.rpmdb_host.get_query.assert_called_once_with('_dbpath')

    @patch('kiwi.utils.rpm_database.Path.wipe')
    @patch('kiwi.command.Command.run')
    def test_set_database_to_image_path(self, mock_Command_run,
                                        mock_Path_wipe):
        self.rpmdb.rpmdb_image.expand_query.return_value = '/var/lib/rpm'
        self.rpmdb.set_database_to_image_path()
        self.rpmdb.rpmdb_image.expand_query.assert_called_once_with('%_dbpath')
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_dbpath_rebuild', self.rpmdb.rpmdb_image.get_query.return_value)
        mock_Path_wipe.assert_called_once_with('root_dir//var/lib/rpm')
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
        mock_Command_run.assert_called_once_with(
            ['chroot', 'root_dir', 'rpmdb', '--rebuilddb'])
        assert self.rpmdb.rpmdb_image.wipe_config.call_count == 2

    def test_set_macro_from_string(self):
        self.rpmdb.set_macro_from_string('_install_langs%en_US:de_DE')
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_install_langs', 'en_US:de_DE')

    def test_write_config(self):
        self.rpmdb.write_config()
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
Beispiel #10
0
    def setup_package_database_configuration(self):
        """
        Setup rpm macros for bootstrapping and image building

        1. Create the rpm image macro which persists during the build
        2. Create the rpm bootstrap macro to make sure for bootstrapping
           the rpm database location matches the host rpm database setup.
           This macro only persists during the bootstrap phase. If the
           image was already bootstrapped a compat link is created instead.
        """
        rpmdb = RpmDataBase(self.root_dir,
                            Defaults.get_custom_rpm_image_macro_name())
        if self.locale:
            rpmdb.set_macro_from_string(self.locale[0])
        rpmdb.write_config()

        rpmdb = RpmDataBase(self.root_dir)
        if rpmdb.has_rpm():
            rpmdb.link_database_to_host_path()
        else:
            rpmdb.set_database_to_host_path()
        # SUSE compat code:
        #
        # Manually adding the compat link /var/lib/rpm that points to the
        # rpmdb location as it is configured in the host rpm setup. DNF makes
        # use of the host setup to bootstrap or to read the signing keys
        # from the rpm database setup provisioned by rpm itself (macro level).
        # It assumes the host setup is the default for the system being built.
        #
        # For SUSE this is causing a mismatch as the host setup is not the RPM
        # default (/var/lib/rpm) and SUSE distros proactively relocate the rpm
        # database from the default location to a custom one when the
        # rpm package and related macros are installed for the first time.
        rpmdb.init_database()
        image_rpm_compat_link = '/var/lib/rpm'
        host_rpm_dbpath = rpmdb.rpmdb_host.expand_query('%_dbpath')
        if host_rpm_dbpath != image_rpm_compat_link:
            Path.create(self.root_dir + os.path.dirname(image_rpm_compat_link))
            Command.run([
                'ln', '-s', '--no-target-directory', ''.join([
                    '../..', host_rpm_dbpath
                ]), self.root_dir + image_rpm_compat_link
            ],
                        raise_on_error=False)
Beispiel #11
0
class TestRpmDataBase(object):
    def setup(self):
        self.rpmdb = RpmDataBase('root_dir')
        self.rpmdb.rpmdb_host = Mock()
        self.rpmdb.rpmdb_image = Mock()

    @patch('kiwi.utils.rpm_database.Path.which')
    def test_has_rpm(self, mock_Path_which):
        mock_Path_which.return_value = None
        assert self.rpmdb.has_rpm() is False
        mock_Path_which.assert_called_once_with(
            'rpm', access_mode=1, custom_env={'PATH': 'root_dir/usr/bin'})
        mock_Path_which.return_value = 'rpm'
        assert self.rpmdb.has_rpm() is True

    @patch('kiwi.command.Command.run')
    def test_rebuild_database(self, mock_Command_run):
        self.rpmdb.rebuild_database()
        mock_Command_run.assert_called_once_with(
            ['chroot', 'root_dir', 'rpmdb', '--rebuilddb'])

    @patch('kiwi.command.Command.run')
    def test_init_database(self, mock_Command_run):
        self.rpmdb.init_database()
        mock_Command_run.assert_called_once_with(
            ['rpm', '--root', 'root_dir', '--initdb'])

    def test_set_database_to_host_path(self):
        self.rpmdb.set_database_to_host_path()
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_dbpath', self.rpmdb.rpmdb_host.get_query.return_value)
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
        self.rpmdb.rpmdb_host.get_query.assert_called_once_with('_dbpath')

    @patch('kiwi.utils.rpm_database.Path.wipe')
    @patch('kiwi.command.Command.run')
    @patch('os.path.islink')
    @patch('os.path.exists')
    @patch('os.unlink')
    def test_set_database_to_image_path(self, mock_os_unlink, mock_os_exists,
                                        mock_os_islink, mock_Command_run,
                                        mock_Path_wipe):
        mock_os_exists.return_value = True
        mock_os_islink.return_value = False
        self.rpmdb.rpmdb_image.expand_query.return_value = \
            '/var/lib/rpm'
        self.rpmdb.rpmdb_host.expand_query.return_value = \
            '/usr/lib/sysimage/rpm'
        self.rpmdb.set_database_to_image_path()
        self.rpmdb.rpmdb_image.expand_query.assert_called_once_with('%_dbpath')
        assert self.rpmdb.rpmdb_image.set_config_value.call_args_list == [
            call('_dbpath', self.rpmdb.rpmdb_host.expand_query.return_value),
            call('_dbpath_rebuild',
                 self.rpmdb.rpmdb_image.get_query.return_value)
        ]
        assert mock_Path_wipe.call_args_list == [
            call('root_dir/var/lib/rpm'),
            call('root_dir/usr/lib/sysimage/rpm')
        ]
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()
        assert mock_Command_run.call_args_list == [
            call(['chroot', 'root_dir', 'rpmdb', '--rebuilddb']),
            call([
                'mv', 'root_dir/usr/lib/sysimage/rpm/alternatives',
                'root_dir/var/lib/rpm'
            ])
        ]
        assert self.rpmdb.rpmdb_image.wipe_config.call_count == 2
        mock_os_islink.return_value = True
        self.rpmdb.set_database_to_image_path()
        mock_os_unlink.assert_called_once_with('root_dir/var/lib/rpm')

    def test_set_macro_from_string(self):
        self.rpmdb.set_macro_from_string('_install_langs%en_US:de_DE')
        self.rpmdb.rpmdb_image.set_config_value.assert_called_once_with(
            '_install_langs', 'en_US:de_DE')

    def test_write_config(self):
        self.rpmdb.write_config()
        self.rpmdb.rpmdb_image.write_config.assert_called_once_with()

    @patch('kiwi.command.Command.run')
    def test_import_signing_key_to_image(self, mock_Command_run):
        self.rpmdb.import_signing_key_to_image('key')
        mock_Command_run.assert_called_once_with([
            'rpm', '--root', 'root_dir', '--import', 'key', '--dbpath',
            self.rpmdb.rpmdb_host.expand_query.return_value
        ])