1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-12-24 02:12:59 +00:00

subcommand "copy-conf"

This commit is contained in:
Jörn-Michael Miehe 2020-08-17 16:47:08 +02:00
parent 0433daa748
commit 500652c9d6
5 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1 @@
This is a test.

View file

@ -0,0 +1,2 @@
FROM alpine:latest
RUN apk --no-cache add rsync

View file

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

View file

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

View file

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