2020-08-17 14:47:08 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# local
|
2020-08-18 10:28:59 +00:00
|
|
|
from .._constants import CONF_DIRECTORY_NAME
|
2020-08-20 12:15:38 +00:00
|
|
|
from ..executable import Executable
|
2020-08-19 15:21:38 +00:00
|
|
|
from ..subcommand import SubCommand
|
2020-08-19 14:10:56 +00:00
|
|
|
from ..config import LoadedConfig
|
2020-08-19 15:22:40 +00:00
|
|
|
from ..projects import Projects
|
2020-08-19 15:15:00 +00:00
|
|
|
from ..rootkit import Rootkit, prefix_path_mnt
|
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-20 12:15:38 +00:00
|
|
|
action="Syncing all configs for", add_parser=False,
|
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-25 13:41:27 +00:00
|
|
|
if project.has_configs()
|
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([
|
2020-08-25 13:41:27 +00:00
|
|
|
'rsync', '-rpt', '--delete', *prefix_path_mnt(conf_dirs)
|
2020-08-19 14:10:56 +00:00
|
|
|
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
2020-08-17 14:47:08 +00:00
|
|
|
|
|
|
|
return True
|
2020-08-18 10:24:55 +00:00
|
|
|
|
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
def _find_net(net_name):
|
|
|
|
ps = Executable('docker').run([
|
|
|
|
'network', 'ls', '--filter', f"name={net_name}", '--format', '{{.Name}}'
|
|
|
|
], stdout=subprocess.PIPE)
|
2020-08-18 15:49:49 +00:00
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
net_found = str(ps.stdout, 'utf-8').strip()
|
2020-08-18 15:49:49 +00:00
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
return net_found == net_name
|
2020-08-18 15:49:49 +00:00
|
|
|
|
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
class NetUpCommand(SubCommand):
|
|
|
|
"""kiwi net-up"""
|
2020-08-18 15:49:49 +00:00
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
|
|
|
'net-up',
|
|
|
|
action="Creating the local network hub for", add_parser=False,
|
|
|
|
description="Create the local network hub for this instance"
|
|
|
|
)
|
2020-08-18 15:49:49 +00:00
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
def _run_instance(self, runner, args):
|
|
|
|
config = LoadedConfig.get()
|
|
|
|
net_name = config['network:name']
|
|
|
|
net_cidr = config['network:cidr']
|
|
|
|
|
|
|
|
if _find_net(net_name):
|
|
|
|
logging.info(f"Network '{net_name}' already exists")
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
|
|
|
Executable('docker').run([
|
|
|
|
'network', 'create',
|
|
|
|
'--driver', 'bridge',
|
|
|
|
'--internal',
|
|
|
|
'--subnet', net_cidr,
|
|
|
|
net_name
|
|
|
|
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
logging.info(f"Network '{net_name}' created")
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
logging.error(f"Error creating network '{net_name}'")
|
|
|
|
return False
|
2020-08-18 15:49:49 +00:00
|
|
|
|
2020-08-20 12:15:38 +00:00
|
|
|
return True
|