mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-21 20:33:00 +00:00
subcommand "copy-conf"
This commit is contained in:
parent
0433daa748
commit
500652c9d6
5 changed files with 81 additions and 0 deletions
1
example/hello-world.project/conf/test.txt
Normal file
1
example/hello-world.project/conf/test.txt
Normal file
|
@ -0,0 +1 @@
|
|||
This is a test.
|
2
src/images/rsync.Dockerfile
Normal file
2
src/images/rsync.Dockerfile
Normal file
|
@ -0,0 +1,2 @@
|
|||
FROM alpine:latest
|
||||
RUN apk --no-cache add rsync
|
|
@ -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',
|
||||
|
|
70
src/kiwi/subcommands/copy_conf.py
Normal file
70
src/kiwi/subcommands/copy_conf.py
Normal 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
|
|
@ -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"""
|
||||
|
||||
|
|
Loading…
Reference in a new issue