def patch_img(): root_img = os.path.join(magic_consts.ACTIVE_IMG_PATH, "root.squashfs") active_metadata_path = os.path.join( magic_consts.ACTIVE_IMG_PATH, "metadata.yaml") patch_file = os.path.join(magic_consts.CWD, "patches/fuel_agent/patch") path_archname_pairs = [(os.path.join(magic_consts.ACTIVE_IMG_PATH, p), p) for p in ["vmlinuz", "initrd.img"]] with temp_util.temp_dir() as temp_dir: patched_img = os.path.join(temp_dir, "root.squashfs") patched_metadata_path = os.path.join(temp_dir, "metadata.yaml") _patch_squashfs(root_img, patched_img, patch_file) _mk_metadata(active_metadata_path, patched_metadata_path, patched_img) path_archname_pairs.append((patched_img, "root.squashfs")) path_archname_pairs.append((patched_metadata_path, "metadata.yaml")) with tempfile.NamedTemporaryFile() as archive_file: with tarfile.open(name=archive_file.name, mode="w:gz") as archive: for path, archname in path_archname_pairs: archive.add(path, archname) LOG.info("Import image using fuel-bootstrap") subprocess.call(["fuel-bootstrap", "import", archive_file.name]) LOG.info("Activate image using `fuel-bootstrap activate`")
def _patch_squashfs(root_img, patched_img, *patches): with temp_util.temp_dir() as patch_dir: LOG.info("unsquash root image to temporary directory") subprocess.call(["unsquashfs", "-f", "-d", patch_dir, root_img]) LOG.info("apply patch to root image") patch.patch_apply(patch_dir, patches) LOG.info("create new root.squashfs image") subprocess.call(["mksquashfs", patch_dir, patched_img])
def test_temp_dir(mocker, is_exception, prefix): class TestException(Exception): pass kwargs = {} if prefix is not None: kwargs['prefix'] = prefix temp_dir_name = mock.Mock() mkdtemp_mock = mocker.patch("tempfile.mkdtemp", return_value=temp_dir_name) rm_tree_mock = mocker.patch("shutil.rmtree") if is_exception: with pytest.raises(TestException): with tempfile.temp_dir(**kwargs): raise TestException else: with tempfile.temp_dir(**kwargs): pass mkdtemp_mock.assert_called_once_with(**kwargs) rm_tree_mock.assert_called_once_with(temp_dir_name)
def apply_patches(container, prefix, *patches, **kwargs): """Apply set of patches to a container's filesystem""" revert = kwargs.pop('revert', False) # TODO: review all logic here to apply all preprocessing steps to patches # beforehand files = [os.path.join(prefix, f) for f in patch.get_filenames_from_patches(prefix, *patches)] if not files: LOG.warn("Nothing to patch!") return with tempfile.temp_dir(prefix='octane_docker_patches.') as tempdir: get_files_from_docker(container, files, tempdir) patch.patch_apply(os.path.join(tempdir, prefix), patches, revert) put_files_to_docker(container, "/", tempdir)