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.
This commit is contained in:
Fabio Manganiello 2023-08-20 13:31:13 +02:00
parent 28ba042810
commit 5efcae64c1
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -7,7 +7,9 @@ import argparse
import inspect import inspect
import os import os
import pathlib import pathlib
import re
import sys import sys
import textwrap
from typing import Iterable from typing import Iterable
from platypush.config import Config from platypush.config import Config
@ -34,9 +36,10 @@ class DockerfileGenerator:
BaseImage.UBUNTU: PackageManagers.APT, 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.cfgfile = os.path.abspath(os.path.expanduser(cfgfile))
self.image = image self.image = image
self.gitref = gitref
def generate(self) -> str: def generate(self) -> str:
""" """
@ -70,7 +73,12 @@ class DockerfileGenerator:
file_lines = [line.rstrip() for line in f.readlines()] file_lines = [line.rstrip() for line in f.readlines()]
for line in file_lines: 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: for new_line in deps.before:
new_file_lines.append('RUN ' + new_line) new_file_lines.append('RUN ' + new_line)
@ -93,6 +101,24 @@ class DockerfileGenerator:
return '\n'.join(new_file_lines) 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 @staticmethod
def _get_exposed_ports() -> Iterable[int]: def _get_exposed_ports() -> Iterable[int]:
""" """
@ -123,6 +149,7 @@ def main():
parser.add_argument( parser.add_argument(
'-h', '--help', dest='show_usage', action='store_true', help='Show usage' '-h', '--help', dest='show_usage', action='store_true', help='Show usage'
) )
parser.add_argument( parser.add_argument(
'cfgfile', 'cfgfile',
type=str, type=str,
@ -130,6 +157,7 @@ def main():
help='The path to the configuration file. If not specified a minimal ' help='The path to the configuration file. If not specified a minimal '
'Dockerfile with no extra dependencies will be generated.', 'Dockerfile with no extra dependencies will be generated.',
) )
parser.add_argument( parser.add_argument(
'--image', '--image',
'-i', '-i',
@ -141,6 +169,18 @@ def main():
help='Base image to use for the Dockerfile.', 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:]) opts, _ = parser.parse_known_args(sys.argv[1:])
if opts.show_usage: if opts.show_usage:
parser.print_help() parser.print_help()
@ -157,7 +197,10 @@ def main():
file=sys.stderr, file=sys.stderr,
) )
dockerfile = DockerfileGenerator(opts.cfgfile, image=opts.image).generate() dockerfile = DockerfileGenerator(
opts.cfgfile, image=opts.image, gitref=opts.gitref
).generate()
print(dockerfile) print(dockerfile)
return 0 return 0