1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 21:03:00 +00:00
kiwi-scp/src/kiwi/subcommands/conf.py

97 lines
2.6 KiB
Python
Raw Normal View History

2020-08-17 14:47:08 +00:00
# system
import logging
import subprocess
# local
from ._subcommand import SubCommand
2020-08-18 10:28:59 +00:00
# parent
from .._constants import CONF_DIRECTORY_NAME
from ..config import LoadedConfig
2020-08-19 15:15:00 +00:00
from ..project import Projects
from ..rootkit import Rootkit, prefix_path_mnt
2020-08-17 14:47:08 +00:00
class ConfCopyCommand(SubCommand):
"""kiwi conf-copy"""
2020-08-17 14:47:08 +00:00
def __init__(self):
super().__init__(
'conf-copy',
2020-08-19 14:23:52 +00:00
action="Syncing all configs for",
2020-08-17 14:47:08 +00:00
description="Synchronize all config files to target directory"
)
def _run_instance(self, runner, args):
conf_dirs = [
project.conf_dir_name()
for project in Projects.from_dir().filter_enabled()
]
2020-08-17 14:47:08 +00:00
if conf_dirs:
# add target directory
conf_dirs.append(LoadedConfig.get()['runtime:storage'])
logging.info(f"Sync directories: {conf_dirs}")
2020-08-17 14:47:08 +00:00
Rootkit('rsync').run([
'rsync', '-r', *prefix_path_mnt(conf_dirs)
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
2020-08-17 14:47:08 +00:00
return True
class ConfPurgeCommand(SubCommand):
"""kiwi conf-purge"""
def __init__(self):
super().__init__(
'conf-purge',
2020-08-19 14:23:52 +00:00
action="Removing all configs for",
description="Remove all config files in target directory"
)
def _run_instance(self, runner, args):
conf_target = f"{LoadedConfig.get()['runtime:storage']}/{CONF_DIRECTORY_NAME}"
logging.info(f"Purging directories: {conf_target}")
Rootkit().run([
'rm', '-rf', prefix_path_mnt(conf_target)
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return True
2020-08-18 15:49:49 +00:00
class ConfCleanCommand(SubCommand):
"""kiwi conf-clean"""
def __init__(self):
super().__init__(
'conf-clean',
2020-08-19 14:23:52 +00:00
action="Cleaning all configs for",
2020-08-18 15:49:49 +00:00
description="Cleanly sync all configs to target folder, relaunch affected projects"
)
def _run_instance(self, runner, args):
2020-08-18 15:49:49 +00:00
result = True
affected_projects = [
project.conf_dir_name()
for project in Projects.from_dir()
if project.has_configs()
]
2020-08-18 15:49:49 +00:00
for project_name in affected_projects:
args.projects = project_name
result &= runner.run('down')
# cleanly sync configs
result &= runner.run('conf-purge')
result &= runner.run('conf-copy')
2020-08-18 15:49:49 +00:00
# bring projects back up
for project_name in affected_projects:
args.projects = project_name
result &= runner.run('up')
return result