2020-08-17 14:47:08 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# local
|
|
|
|
from ._subcommand import SubCommand
|
2020-08-19 09:58:13 +00:00
|
|
|
from .utils.project import Projects
|
2020-08-18 10:28:59 +00:00
|
|
|
from .utils.rootkit import Rootkit, prefix_path_mnt
|
|
|
|
|
|
|
|
# parent
|
|
|
|
from .._constants import CONF_DIRECTORY_NAME
|
2020-08-19 14:10:56 +00:00
|
|
|
from ..config import LoadedConfig
|
2020-08-17 14:47:08 +00:00
|
|
|
|
|
|
|
|
2020-08-18 10:24:55 +00:00
|
|
|
class ConfCopyCommand(SubCommand):
|
|
|
|
"""kiwi conf-copy"""
|
2020-08-17 14:47:08 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
2020-08-18 10:24:55 +00:00
|
|
|
'conf-copy',
|
2020-08-17 14:47:08 +00:00
|
|
|
description="Synchronize all config files to target directory"
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
2020-08-19 09:58:13 +00:00
|
|
|
conf_dirs = [
|
|
|
|
project.conf_dir_name()
|
2020-08-19 14:10:56 +00:00
|
|
|
for project in Projects.from_dir().filter_enabled()
|
2020-08-19 09:58:13 +00:00
|
|
|
]
|
2020-08-17 14:47:08 +00:00
|
|
|
|
2020-08-17 15:56:29 +00:00
|
|
|
if conf_dirs:
|
|
|
|
# add target directory
|
2020-08-19 14:10:56 +00:00
|
|
|
conf_dirs.append(LoadedConfig.get()['runtime:storage'])
|
2020-08-17 15:56:29 +00:00
|
|
|
logging.info(f"Sync directories: {conf_dirs}")
|
2020-08-17 14:47:08 +00:00
|
|
|
|
2020-08-19 14:10:56 +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
|
2020-08-18 10:24:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfPurgeCommand(SubCommand):
|
|
|
|
"""kiwi conf-purge"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
|
|
|
'conf-purge',
|
|
|
|
description="Remove all config files in target directory"
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
|
|
|
conf_target = f"{LoadedConfig.get()['runtime:storage']}/{CONF_DIRECTORY_NAME}"
|
2020-08-18 10:24:55 +00:00
|
|
|
logging.info(f"Purging directories: {conf_target}")
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
Rootkit().run([
|
|
|
|
'rm', '-rf', prefix_path_mnt(conf_target)
|
|
|
|
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
2020-08-18 10:24:55 +00:00
|
|
|
|
|
|
|
return True
|
2020-08-18 15:49:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfCleanCommand(SubCommand):
|
|
|
|
"""kiwi conf-clean"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
|
|
|
'conf-clean',
|
|
|
|
description="Cleanly sync all configs to target folder, relaunch affected projects"
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
2020-08-18 15:49:49 +00:00
|
|
|
result = True
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
affected_projects = [
|
|
|
|
project.conf_dir_name()
|
2020-08-19 14:10:56 +00:00
|
|
|
for project in Projects.from_dir()
|
2020-08-19 09:58:13 +00:00
|
|
|
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')
|
2020-08-19 09:58:13 +00:00
|
|
|
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')
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
return result
|