From 5efcae64c1c2b268444e9a587bb3154cc5fdeaeb Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 20 Aug 2023 13:31:13 +0200 Subject: [PATCH] Better Dockerfile logic to retrieve sources. If the /install folder on the container doesn't contain a copy of the source files, then the git repository will be cloned under that folder. The user can specify via `-r/--ref` option which tag/branch/commit they want to install. --- platypush/platydock/__init__.py | 49 +++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/platypush/platydock/__init__.py b/platypush/platydock/__init__.py index 86557e445..e2acabff6 100755 --- a/platypush/platydock/__init__.py +++ b/platypush/platydock/__init__.py @@ -7,7 +7,9 @@ import argparse import inspect import os import pathlib +import re import sys +import textwrap from typing import Iterable from platypush.config import Config @@ -34,9 +36,10 @@ class DockerfileGenerator: BaseImage.UBUNTU: PackageManagers.APT, } - def __init__(self, cfgfile: str, image: BaseImage) -> None: + def __init__(self, cfgfile: str, image: BaseImage, gitref: str) -> None: self.cfgfile = os.path.abspath(os.path.expanduser(cfgfile)) self.image = image + self.gitref = gitref def generate(self) -> str: """ @@ -70,7 +73,12 @@ class DockerfileGenerator: file_lines = [line.rstrip() for line in f.readlines()] for line in file_lines: - if line.startswith('RUN cd /install '): + if re.match( + r'RUN /install/platypush/install/scripts/[A-Za-z0-9_-]+/install.sh', + line.strip(), + ): + new_file_lines.append(self._generate_git_clone_command()) + elif line.startswith('RUN cd /install '): for new_line in deps.before: new_file_lines.append('RUN ' + new_line) @@ -93,6 +101,24 @@ class DockerfileGenerator: return '\n'.join(new_file_lines) + def _generate_git_clone_command(self) -> str: + pkg_manager = self._pkg_manager_by_base_image[self.image] + install_cmd = ' '.join(pkg_manager.value.install) + uninstall_cmd = ' '.join(pkg_manager.value.uninstall) + return textwrap.dedent( + f""" + RUN if [ ! -f "/install/setup.py" ]; then \\ + echo "Platypush source not found under the current directory, downloading it" && \\ + {install_cmd} git && \\ + rm -rf /install && \\ + git clone https://github.com/BlackLight/platypush.git /install && \\ + cd /install && \\ + git checkout {self.gitref} && \\ + {uninstall_cmd} git; \\ + fi + """ + ) + @staticmethod def _get_exposed_ports() -> Iterable[int]: """ @@ -123,6 +149,7 @@ def main(): parser.add_argument( '-h', '--help', dest='show_usage', action='store_true', help='Show usage' ) + parser.add_argument( 'cfgfile', type=str, @@ -130,6 +157,7 @@ def main(): help='The path to the configuration file. If not specified a minimal ' 'Dockerfile with no extra dependencies will be generated.', ) + parser.add_argument( '--image', '-i', @@ -141,6 +169,18 @@ def main(): help='Base image to use for the Dockerfile.', ) + parser.add_argument( + '--ref', + '-r', + dest='gitref', + required=False, + type=str, + default='master', + help='If platydock is not run from a Platypush installation directory, ' + 'it will clone the source via git. You can specify through this ' + 'option which branch, tag or commit hash to use. Defaults to master.', + ) + opts, _ = parser.parse_known_args(sys.argv[1:]) if opts.show_usage: parser.print_help() @@ -157,7 +197,10 @@ def main(): file=sys.stderr, ) - dockerfile = DockerfileGenerator(opts.cfgfile, image=opts.image).generate() + dockerfile = DockerfileGenerator( + opts.cfgfile, image=opts.image, gitref=opts.gitref + ).generate() + print(dockerfile) return 0