"copy-conf" -> "conf-copy", subcommand "conf-purge", constants centralization

This commit is contained in:
Jörn-Michael Miehe 2020-08-18 12:24:55 +02:00
parent 366ed9b4fe
commit 5b489d0392
6 changed files with 72 additions and 38 deletions

View file

@ -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'

View file

@ -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:

View file

@ -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',

View file

@ -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,7 +36,28 @@ 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
)

View file

@ -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))
})

View file

@ -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
)