diff --git a/example/hello-world.project/conf/test.txt b/example/hello-world.project/conf/test.txt new file mode 100644 index 0000000..484ba93 --- /dev/null +++ b/example/hello-world.project/conf/test.txt @@ -0,0 +1 @@ +This is a test. diff --git a/src/images/rsync.Dockerfile b/src/images/rsync.Dockerfile new file mode 100644 index 0000000..47c262e --- /dev/null +++ b/src/images/rsync.Dockerfile @@ -0,0 +1,2 @@ +FROM alpine:latest +RUN apk --no-cache add rsync \ No newline at end of file diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index 69d75d3..e31437b 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -1,5 +1,6 @@ # local from .cmd import CmdCommand +from .copy_conf import CopyConfCommand from .down import DownCommand from .init import InitCommand from .logs import LogsCommand @@ -10,6 +11,7 @@ from .up import UpCommand __all__ = [ 'CmdCommand', + 'CopyConfCommand', 'DownCommand', 'InitCommand', 'LogsCommand', diff --git a/src/kiwi/subcommands/copy_conf.py b/src/kiwi/subcommands/copy_conf.py new file mode 100644 index 0000000..805640b --- /dev/null +++ b/src/kiwi/subcommands/copy_conf.py @@ -0,0 +1,70 @@ +# system +import logging +import os +import subprocess + +# parent +from .._constants import KIWI_ROOT + +# local +from ._subcommand import SubCommand +from .utils.dockercommand import DockerCommand +from .utils.project import list_projects, get_project_dir + + +def _add_prefix(prefix, path): + abs_path = os.path.abspath(path) + return os.path.realpath(prefix + '/' + abs_path) + + +class CopyConfCommand(SubCommand): + """kiwi copy-conf""" + + def __init__(self): + super().__init__( + 'copy-conf', + description="Synchronize all config files to target directory" + ) + + def run(self, runner, config, args): + logging.info("Building image kiwi-config/auxiliary:rsync") + DockerCommand('docker').run( + config, args, + [ + 'build', + '-t', 'kiwi-config/auxiliary:rsync', + '-f', f"{KIWI_ROOT}/images/rsync.Dockerfile", + f"{KIWI_ROOT}/images" + ], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + + conf_sources = [] + + for project_name in list_projects(config): + project_conf = f"{get_project_dir(config, project_name)}/conf" + + if os.path.isdir(project_conf): + conf_sources.append(project_conf) + + if conf_sources: + print(f"Syncing {conf_sources} to '{config['runtime:storage']}'") + conf_sources = [f"'{_add_prefix('/mnt', src)}'" for src in conf_sources] + conf_sources = ' '.join(conf_sources) + + conf_target = f"'{_add_prefix('/mnt', config['runtime:storage'])}'" + logging.debug(f"Config sources {conf_sources}, Config target {conf_target}") + + DockerCommand('docker').run( + config, args, + [ + 'run', '--rm', + '-v', '/:/mnt', + '-u', 'root', + 'kiwi-config/auxiliary:rsync', + 'ash', '-c', f"rsync -r {conf_sources} {conf_target}" + ], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + + return True diff --git a/src/kiwi/subcommands/utils/project.py b/src/kiwi/subcommands/utils/project.py index ae02225..12f9a9a 100644 --- a/src/kiwi/subcommands/utils/project.py +++ b/src/kiwi/subcommands/utils/project.py @@ -19,6 +19,12 @@ def get_project_dir(config, project_name): return f"{project_name}{config['markers:project']}" +def get_target_dir(config, project_name): + """get project's target directory""" + + return f"{config['runtime:storage']}{get_project_dir(config, project_name)}" + + def list_projects(config): """list projects in current instance"""