Example #1
0
    def test_yum_is_dnf(self):
        # Setup for yum not being the same as dnf, modeled after fb
        with temp_dir() as td:
            yum_path = Path(td / 'yum').touch()

            with mock.patch('shutil.which') as mock_which:
                mock_which.return_value = None
                self.assertFalse(yum_is_dnf())
                mock_which.return_value = yum_path.decode()
                self.assertFalse(yum_is_dnf())

        # Setup for yum being the same as dnf, modeled after fedora
        # where `/bin/yum -> dnf-3`
        with temp_dir() as td:
            dnf_name = 'dnf-3'
            dnf_path = Path(td / dnf_name).touch()
            yum_path = td / 'yum'
            # Symlink to the name for a relative symlink that ends up
            # as yum -> dnf-3
            os.symlink(dnf_name, yum_path)

            with mock.patch('shutil.which') as mock_which:
                mock_paths = {dnf_name: dnf_path, 'yum': yum_path}
                mock_which.side_effect = lambda p: mock_paths[p].decode()

                self.assertTrue(yum_is_dnf())
Example #2
0
def _yum_using_build_appliance(
    *,
    build_appliance: Subvol,
    nspawn_args: List[str],
    install_root: Path,
    protected_paths: Iterable[str],
    yum_args: List[str],
    preserve_yum_cache: bool,
) -> None:
    work_dir = '/work' + base64.urlsafe_b64encode(
        uuid.uuid4().bytes  # base64 instead of hex saves 10 bytes
    ).decode().strip('=')
    mount_var_cache_yum = '' if preserve_yum_cache else f'''
                         mkdir -p {work_dir}/var/cache/yum ; \
                         mount --bind /var/cache/yum {work_dir}/var/cache/yum ;
                         '''
    opts = nspawn_in_subvol_parse_opts([
        '--layer',
        'UNUSED',
        '--user',
        'root',
        # You can see below --no-private-network in conjunction with
        # --cap-net-admin.  CAP_NET_ADMIN is not intended to administer the
        # host's network stack.  See how yum_from_snapshot() brings loopback
        # interface up under protection of "unshare --net".
        '--cap-net-admin',
        '--no-private-network',
        '--bindmount-rw',
        install_root.decode(),
        work_dir,
        *nspawn_args,
        '--',
        'sh',
        '-uec',
        f'''
        {mount_var_cache_yum}
        /yum-from-snapshot \
            {' '.join(
                '--protected-path=' + shlex.quote(p) for p in protected_paths
            )} \
            --install-root {work_dir} \
            -- {' '.join(shlex.quote(arg) for arg in yum_args)}
        ''',
    ])
    # Don't pollute stdout with logging
    nspawn_in_subvol(build_appliance, opts, stdout=sys.stderr)