Refactor/documentation round for platydock.

This commit is contained in:
Fabio Manganiello 2023-08-22 02:49:05 +02:00
parent 10c0e5fcad
commit ce68250b4d
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -10,7 +10,7 @@ import pathlib
import re import re
import sys import sys
import textwrap import textwrap
from typing import Iterable from typing import Iterable, Sequence
from platypush.config import Config from platypush.config import Config
from platypush.utils.manifest import ( from platypush.utils.manifest import (
@ -70,7 +70,6 @@ class DockerfileGenerator:
""" """
Generate a Dockerfile based on a configuration file. Generate a Dockerfile based on a configuration file.
:param cfgfile: Path to the configuration file.
:return: The content of the generated Dockerfile. :return: The content of the generated Dockerfile.
""" """
import platypush import platypush
@ -131,6 +130,11 @@ class DockerfileGenerator:
return '\n'.join(new_file_lines) return '\n'.join(new_file_lines)
def _generate_git_clone_command(self) -> str: def _generate_git_clone_command(self) -> str:
"""
Generates a git clone command in Dockerfile that checks out the repo
and the right git reference, if the application sources aren't already
available under /install.
"""
pkg_manager = self._pkg_manager_by_base_image[self.image] pkg_manager = self._pkg_manager_by_base_image[self.image]
install_cmd = ' '.join(pkg_manager.value.install) install_cmd = ' '.join(pkg_manager.value.install)
uninstall_cmd = ' '.join(pkg_manager.value.uninstall) uninstall_cmd = ' '.join(pkg_manager.value.uninstall)
@ -148,26 +152,13 @@ class DockerfileGenerator:
""" """
) )
@staticmethod @classmethod
def _get_exposed_ports() -> Iterable[int]: def from_cmdline(cls, args: Sequence[str]) -> 'DockerfileGenerator':
""" """
:return: The listen ports used by the backends enabled in the configuration Create a DockerfileGenerator instance from command line arguments.
file.
"""
backends_config = Config.get_backends()
return {
int(port)
for port in (
backends_config.get('http', {}).get('port'),
backends_config.get('tcp', {}).get('port'),
)
if port
}
:param args: Command line arguments.
def main(): :return: A DockerfileGenerator instance.
"""
Generates a Dockerfile based on the configuration file.
""" """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='platydock', prog='platydock',
@ -210,10 +201,10 @@ def main():
'option which branch, tag or commit hash to use. Defaults to master.', '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(args)
if opts.show_usage: if opts.show_usage:
parser.print_help() parser.print_help()
return 0 sys.exit(0)
if not opts.cfgfile: if not opts.cfgfile:
opts.cfgfile = os.path.join( opts.cfgfile = os.path.join(
@ -226,11 +217,30 @@ def main():
file=sys.stderr, file=sys.stderr,
) )
dockerfile = DockerfileGenerator( return cls(opts.cfgfile, image=opts.image, gitref=opts.gitref)
opts.cfgfile, image=opts.image, gitref=opts.gitref
).generate()
print(dockerfile) @staticmethod
def _get_exposed_ports() -> Iterable[int]:
"""
:return: The listen ports used by the backends enabled in the configuration
file.
"""
backends_config = Config.get_backends()
return {
int(port)
for port in (
backends_config.get('http', {}).get('port'),
backends_config.get('tcp', {}).get('port'),
)
if port
}
def main():
"""
Generates a Dockerfile based on the configuration file.
"""
print(DockerfileGenerator.from_cmdline(sys.argv[1:]).generate())
return 0 return 0