Skip to content

Commit 051a304

Browse files
committed
cpatch.py: search base image when patching files
Refactor the script so that for each component specified: binaries are copied to the /tmpbin directory libraries are copied to the /tmplib directory For the core component ensure that ALL locally built libraries are copied to the /tmplib directory rather than a subset of those that are used by the binaries If no component is specified then copy ALL locally built binaries are copied to the /tmpbin directory Add commands to the Dockerfile so that the build process does the following: For every file in the /tmpbin directory: find the file with the same name in the base container image (search /usr/bin amd /usr/sbin directories) move the file - replacing the version in the base image For every file in the /tmplib directory: find the file with the same name in the base container image (search /usr/lib64 directory) move the file - replacing the version in the base image Create some links to the libraries that were just copied because locally built binaries assume that libraries are in a different directory to those in the base image. (original script did this too). Signed-off-by: John Agombar <[email protected]>
1 parent 8730e92 commit 051a304

File tree

1 file changed

+34
-49
lines changed

1 file changed

+34
-49
lines changed

src/script/cpatch.py

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ def build(self):
459459
dresult = job(component)
460460
dlines.extend(dresult)
461461

462+
if os.path.isdir(self._workdir / "tmp_bin"):
463+
# For every binary file that was copied to the tmp_bin directory by the jobs above, search for the existing file in the container and replace it.
464+
dlines.append("ADD tmp_bin /tmpbin")
465+
dlines.append("RUN for i in tmpbin/*; do find /usr/bin /usr/sbin -name $(basename $i) -exec mv -f $i '{}' \;; echo $(basename $i); done && rm -rf tmpbin")
466+
467+
if os.path.isdir(self._workdir / "tmp_lib"):
468+
# For every library file that was copied to the tmp_lib directory by the jobs above, search for the existing file in the container and replace it.
469+
dlines.append("ADD tmp_lib /tmplib")
470+
dlines.append("RUN for i in tmplib/*; do find /usr/lib64 -name $(basename $i) -exec mv -f $i '{}' \;; echo $(basename $i); done && rm -rf tmplib")
471+
472+
if os.path.isdir(self._workdir / "tmp_bin"):
473+
# by default locally built binaries assume /usr/local
474+
dlines.append("RUN rm -rf /usr/local/lib64 && ln -sf /usr/lib64 /usr/local && ln -sf /usr/share/ceph /usr/local/share")
475+
# locally built binaries assume libceph-common.so.2 is in /usr/lib64 - create link to library that was just copied
476+
dlines.append("RUN ln -sf /usr/lib64/ceph/libceph-common.so.2 /usr/lib64/libceph-common.so.2")
477+
462478
with open(self._workdir / "Dockerfile", "w") as fout:
463479
for line in dlines:
464480
print(line, file=fout)
@@ -513,39 +529,29 @@ def _copy_binary(self, src_path, dst_path):
513529
pass
514530
os.link(src_path, dst_path)
515531

516-
def _bins_and_libs(self, prefix, bin_patterns, lib_patterns):
532+
def _bins_and_libs(self, prefix, bin_patterns, lib_patterns, exclude_prefixes=[]):
517533
out = []
518534

519535
bin_src = self._ctx.build_dir / "bin"
520536
bin_dst = self._workdir / f"{prefix}_bin"
521537
bin_dst.mkdir(parents=True, exist_ok=True)
522538
for path in bin_src.iterdir():
523539
if any(path.match(m) for m in bin_patterns):
540+
if any(path.match(f"{m}*") for m in exclude_prefixes):
541+
continue
524542
self._copy_binary(path, bin_dst / path.name)
525-
out.append(f"ADD {prefix}_bin /usr/bin")
526543

527544
lib_src = self._ctx.build_dir / "lib"
528545
lib_dst = self._workdir / f"{prefix}_lib"
529546
lib_dst.mkdir(parents=True, exist_ok=True)
530547
for path in lib_src.iterdir():
531548
if any(path.match(m) for m in lib_patterns):
549+
if any(path.match(f"{m}*") for m in exclude_prefixes):
550+
continue
532551
self._copy_binary(path, lib_dst / path.name)
533-
out.append(f"ADD {prefix}_lib /usr/lib64")
534552

535553
return out
536554

537-
def _conditional_libs(self, src_dir, name, destination, lib_patterns):
538-
lib_src = self._ctx.build_dir / src_dir
539-
lib_dst = self._workdir / name
540-
lib_dst.mkdir(parents=True, exist_ok=True)
541-
try:
542-
for path in lib_src.iterdir():
543-
if any(path.match(m) for m in lib_patterns):
544-
self._copy_binary(path, lib_dst / path.name)
545-
except FileNotFoundError as err:
546-
log.warning("skipping lib %s: %s", name, err)
547-
return f"ADD {name} {destination}"
548-
549555
def _py_site_packages(self):
550556
"""Return the correct python site packages dir for the image."""
551557
if self._cached_py_site_packages is not None:
@@ -639,60 +645,39 @@ def _core_job(self, component):
639645
# [Quoth the original script]:
640646
# binaries are annoying because the ceph version is embedded all over
641647
# the place, so we have to include everything but the kitchen sink.
642-
out = []
643-
644-
out.extend(
645-
self._bins_and_libs(
646-
prefix="core",
647-
bin_patterns=["ceph-mgr", "ceph-mon", "ceph-osd", "rados"],
648-
lib_patterns=["libceph-common.so*", "librados.so*"],
649-
)
650-
)
651-
652-
out.append(
653-
self._conditional_libs(
654-
src_dir="lib",
655-
name="eclib",
656-
destination="/usr/lib64/ceph/erasure-code",
657-
lib_patterns=["libec_*.so*"],
658-
)
659-
)
660-
out.append(
661-
self._conditional_libs(
662-
src_dir="lib",
663-
name="clslib",
664-
destination="/usr/lib64/rados-classes",
665-
lib_patterns=["libcls_*.so*"],
666-
)
667-
)
648+
if not self._ctx.components_selected:
649+
log.warning("Copying ALL locally built binaries over those that already exist in the image.")
650+
bins=['*']
651+
else:
652+
bins=["ceph-mgr", "ceph-mon", "ceph-osd", "rados"]
668653

669-
# [Quoth the original script]:
670-
# by default locally built binaries assume /usr/local
671-
out.append(
672-
"RUN rm -rf /usr/local/lib64 && ln -s /usr/lib64 /usr/local && ln -s /usr/share/ceph /usr/local/share"
654+
return self._bins_and_libs(
655+
prefix="tmp",
656+
bin_patterns=bins,
657+
lib_patterns=["*.so","*.so.*"],
658+
exclude_prefixes=["ceph_test","test_","unittest_"],
673659
)
674-
675660
return out
676661

677662
def _rgw_job(self, component):
678663
return self._bins_and_libs(
679-
prefix="rgw",
664+
prefix="tmp",
680665
bin_patterns=["radosgw", "radosgw-admin"],
681666
lib_patterns=["librados.so*", "libceph-common.so*"],
682667
)
683668
return out
684669

685670
def _cephfs_job(self, component):
686671
return self._bins_and_libs(
687-
prefix="cephfs",
672+
prefix="tmp",
688673
bin_patterns=["ceph-mds"],
689674
lib_patterns=["libcephfs.so*"],
690675
)
691676
return out
692677

693678
def _rbd_job(self, component):
694679
return self._bins_and_libs(
695-
prefix="rbd",
680+
prefix="tmp",
696681
bin_patterns=["rbd", "rbd-mirror"],
697682
lib_patterns=["librbd.so*"],
698683
)

0 commit comments

Comments
 (0)