From 5b489d039279641f0fdbbf0f0ee473a8b94b9166 Mon Sep 17 00:00:00 2001 From: ldericher Date: Tue, 18 Aug 2020 12:24:55 +0200 Subject: [PATCH] "copy-conf" -> "conf-copy", subcommand "conf-purge", constants centralization --- src/kiwi/_constants.py | 26 ++++++++++++ src/kiwi/config.py | 10 +---- src/kiwi/subcommands/__init__.py | 5 ++- .../subcommands/{copy_conf.py => conf.py} | 41 +++++++++++++------ src/kiwi/subcommands/utils/dockercommand.py | 4 +- src/kiwi/subcommands/utils/rootkit.py | 24 +++++------ 6 files changed, 72 insertions(+), 38 deletions(-) rename src/kiwi/subcommands/{copy_conf.py => conf.py} (50%) diff --git a/src/kiwi/_constants.py b/src/kiwi/_constants.py index 836bedc..ade6c87 100644 --- a/src/kiwi/_constants.py +++ b/src/kiwi/_constants.py @@ -1,7 +1,33 @@ # system import os + +############# +# ENVIRONMENT + # location of "src" directory to use KIWI_ROOT = os.getenv('KIWI_ROOT', ".") # default name of kiwi-config file KIWI_CONF_NAME = os.getenv('KIWI_CONF_NAME', "kiwi.yml") + + +############ +# FILE NAMES + +# text files inside kiwi-config "src" directory +HEADER_KIWI_CONF_NAME = f"{KIWI_ROOT}/kiwi_header.yml" +DEFAULT_KIWI_CONF_NAME = f"{KIWI_ROOT}/kiwi_default.yml" +VERSION_TAG_NAME = f"{KIWI_ROOT}/version-tag" + +# special config directory in projects +CONF_DIRECTORY_NAME = 'conf' +# location for auxiliary Dockerfiles +IMAGES_DIRECTORY_NAME = f"{KIWI_ROOT}/images" + + +#################### +# DOCKER IMAGE NAMES + +# name for auxiliary docker images +LOCAL_IMAGES_NAME = 'localhost/kiwi-config/auxiliary' +DEFAULT_IMAGE_NAME = 'alpine:latest' diff --git a/src/kiwi/config.py b/src/kiwi/config.py index dcbe94a..f557c10 100644 --- a/src/kiwi/config.py +++ b/src/kiwi/config.py @@ -6,15 +6,7 @@ import re import yaml # local -from ._constants import KIWI_ROOT, KIWI_CONF_NAME - -########### -# CONSTANTS - -# text files inside kiwi-config "src" directory -HEADER_KIWI_CONF_NAME = f"{KIWI_ROOT}/kiwi_header.yml" -DEFAULT_KIWI_CONF_NAME = f"{KIWI_ROOT}/kiwi_default.yml" -VERSION_TAG_NAME = f"{KIWI_ROOT}/version-tag" +from ._constants import KIWI_CONF_NAME, HEADER_KIWI_CONF_NAME, DEFAULT_KIWI_CONF_NAME, VERSION_TAG_NAME class Config: diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index e31437b..6b37a7e 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -1,6 +1,6 @@ # local from .cmd import CmdCommand -from .copy_conf import CopyConfCommand +from .conf import ConfCopyCommand, ConfPurgeCommand from .down import DownCommand from .init import InitCommand from .logs import LogsCommand @@ -11,7 +11,8 @@ from .up import UpCommand __all__ = [ 'CmdCommand', - 'CopyConfCommand', + 'ConfCopyCommand', + 'ConfPurgeCommand', 'DownCommand', 'InitCommand', 'LogsCommand', diff --git a/src/kiwi/subcommands/copy_conf.py b/src/kiwi/subcommands/conf.py similarity index 50% rename from src/kiwi/subcommands/copy_conf.py rename to src/kiwi/subcommands/conf.py index db694ea..98c8bf2 100644 --- a/src/kiwi/subcommands/copy_conf.py +++ b/src/kiwi/subcommands/conf.py @@ -4,26 +4,20 @@ import os import subprocess # parent -from .._constants import KIWI_ROOT +from .._constants import CONF_DIRECTORY_NAME # local from ._subcommand import SubCommand -from .utils.dockercommand import DockerCommand from .utils.project import list_projects, get_project_dir -from .utils.rootkit import Rootkit, prefix_path +from .utils.rootkit import Rootkit, _prefix_path_mnt -def _add_prefix(prefix, path): - abs_path = os.path.abspath(path) - return os.path.realpath(prefix + '/' + abs_path) - - -class CopyConfCommand(SubCommand): - """kiwi copy-conf""" +class ConfCopyCommand(SubCommand): + """kiwi conf-copy""" def __init__(self): super().__init__( - 'copy-conf', + 'conf-copy', description="Synchronize all config files to target directory" ) @@ -31,7 +25,7 @@ class CopyConfCommand(SubCommand): conf_dirs = [] for project_name in list_projects(config): - project_conf = f"{get_project_dir(config, project_name)}/conf" + project_conf = f"{get_project_dir(config, project_name)}/{CONF_DIRECTORY_NAME}" if os.path.isdir(project_conf): conf_dirs.append(project_conf) @@ -42,8 +36,29 @@ class CopyConfCommand(SubCommand): logging.info(f"Sync directories: {conf_dirs}") Rootkit('rsync').run( - config, args, ['rsync', '-r', *prefix_path(conf_dirs)], + config, args, ['rsync', '-r', *_prefix_path_mnt(conf_dirs)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) return True + + +class ConfPurgeCommand(SubCommand): + """kiwi conf-purge""" + + def __init__(self): + super().__init__( + 'conf-purge', + description="Remove all config files in target directory" + ) + + def run(self, runner, config, args): + conf_target = f"{config['runtime:storage']}/{CONF_DIRECTORY_NAME}" + logging.info(f"Purging directories: {conf_target}") + + Rootkit().run( + config, args, ['rm', '-rf', _prefix_path_mnt(conf_target)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + + return True diff --git a/src/kiwi/subcommands/utils/dockercommand.py b/src/kiwi/subcommands/utils/dockercommand.py index d0426bd..eb631c7 100644 --- a/src/kiwi/subcommands/utils/dockercommand.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -1,9 +1,9 @@ # system import logging -import os import subprocess # local +from ..._constants import CONF_DIRECTORY_NAME from .executable import Executable from .project import * @@ -23,7 +23,7 @@ def _update_kwargs(config, args, **kwargs): kwargs['env'].update({ 'COMPOSE_PROJECT_NAME': project_name, 'KIWI_HUB_NAME': config['network:name'], - 'CONFDIR': os.path.join(config['runtime:storage'], 'conf'), + 'CONFDIR': os.path.join(config['runtime:storage'], CONF_DIRECTORY_NAME), 'TARGETDIR': os.path.join(config['runtime:storage'], get_project_dir(config, project_name)) }) diff --git a/src/kiwi/subcommands/utils/rootkit.py b/src/kiwi/subcommands/utils/rootkit.py index c18e5ac..c9cbf96 100644 --- a/src/kiwi/subcommands/utils/rootkit.py +++ b/src/kiwi/subcommands/utils/rootkit.py @@ -4,29 +4,29 @@ import os import subprocess # parent -from ..._constants import KIWI_ROOT +from ..._constants import IMAGES_DIRECTORY_NAME, LOCAL_IMAGES_NAME, DEFAULT_IMAGE_NAME # local from .dockercommand import DockerCommand def _prefix_path(prefix, path): - abs_path = os.path.abspath(path) - return os.path.realpath(prefix + '/' + abs_path) - - -def prefix_path(path): if isinstance(path, str): - return _prefix_path('/mnt/', path) + abs_path = os.path.abspath(path) + return os.path.realpath(f"{prefix}/{abs_path}") elif isinstance(path, list): - return [_prefix_path('/mnt/', p) for p in path] + return [_prefix_path(prefix, p) for p in path] + + +def _prefix_path_mnt(path): + return _prefix_path('/mnt/', path) def _image_name(image_tag): if image_tag is not None: - return f"kiwi-config/auxiliary:{image_tag}" + return f"{LOCAL_IMAGES_NAME}:{image_tag}" else: - return "alpine:latest" + return DEFAULT_IMAGE_NAME class Rootkit: @@ -66,8 +66,8 @@ class Rootkit: [ 'build', '-t', _image_name(self.__image_tag), - '-f', f"{KIWI_ROOT}/images/{self.__image_tag}.Dockerfile", - f"{KIWI_ROOT}/images" + '-f', f"{IMAGES_DIRECTORY_NAME}/{self.__image_tag}.Dockerfile", + f"{IMAGES_DIRECTORY_NAME}" ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL )