Builds an image based on the components added.
| 415 | |
| 416 | |
| 417 | class Builder: |
| 418 | """Builds an image based on the components added.""" |
| 419 | |
| 420 | def __init__(self, cli_ctx): |
| 421 | self._ctx = cli_ctx |
| 422 | self._jobs = [] |
| 423 | self._include_dashboard = False |
| 424 | self._cached_py_site_packages = None |
| 425 | self._workdir = pathlib.Path( |
| 426 | tempfile.mkdtemp(suffix=".cpatch", dir=self._ctx.build_dir) |
| 427 | ) |
| 428 | log.debug("cpatch working dir: %s", self._workdir) |
| 429 | |
| 430 | def add(self, component): |
| 431 | """Request that the given component be added to the build.""" |
| 432 | log.info("Including: %s", component) |
| 433 | dispatch = { |
| 434 | Component.PY_COMMON: self._py_common_job, |
| 435 | Component.CEPHADM: self._cephadm_job, |
| 436 | Component.PY_MGR: self._py_mgr_job, |
| 437 | Component.DASHBOARD: self._py_mgr_job, |
| 438 | Component.PYBIND: self._pybind_job, |
| 439 | Component.CORE: self._core_job, |
| 440 | Component.CEPHFS: self._cephfs_job, |
| 441 | Component.RBD: self._rbd_job, |
| 442 | Component.RGW: self._rgw_job, |
| 443 | } |
| 444 | job = dispatch[component] |
| 445 | if component == Component.DASHBOARD: |
| 446 | self._include_dashboard = True |
| 447 | if job not in {j for _, j in self._jobs}: |
| 448 | self._jobs.append((component, job)) |
| 449 | |
| 450 | def build(self): |
| 451 | """Build the container image.""" |
| 452 | dlines = [f"FROM {self._ctx.base_image}"] |
| 453 | for cmd in self._ctx.run_before_commands: |
| 454 | dlines.append(f'RUN {cmd}') |
| 455 | jcount = len(self._jobs) |
| 456 | for idx, (component, job) in enumerate(self._jobs): |
| 457 | num = idx + 1 |
| 458 | log.info(f"Executing job {num}/{jcount} for {component}") |
| 459 | dresult = job(component) |
| 460 | dlines.extend(dresult) |
| 461 | |
| 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") |