2021-10-20 12:32:45 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from ipaddress import IPv4Network
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-10-20 06:31:36 +00:00
|
|
|
import click
|
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
from .cli import KiwiCommandType, KiwiCommand
|
|
|
|
from .decorators import kiwi_command
|
2021-10-20 12:32:45 +00:00
|
|
|
from .._constants import KIWI_CONF_NAME
|
2021-10-26 13:56:58 +00:00
|
|
|
from ..config import KiwiConfig
|
2021-10-29 11:37:59 +00:00
|
|
|
from ..instance import Instance
|
2021-11-03 16:58:18 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
2021-10-20 06:31:36 +00:00
|
|
|
|
|
|
|
|
2021-10-25 11:29:12 +00:00
|
|
|
@click.option(
|
2021-11-03 16:58:18 +00:00
|
|
|
"-d",
|
|
|
|
"--directory",
|
2021-10-25 11:29:12 +00:00
|
|
|
help=f"initialize a kiwi-scp instance in another directory",
|
|
|
|
type=click.Path(
|
|
|
|
path_type=Path,
|
|
|
|
dir_okay=True,
|
|
|
|
writable=True,
|
|
|
|
),
|
|
|
|
)
|
2021-10-20 12:32:45 +00:00
|
|
|
@click.option(
|
|
|
|
"-f/-F",
|
|
|
|
"--force/--no-force",
|
|
|
|
help=f"use default values even if {KIWI_CONF_NAME} is present",
|
|
|
|
)
|
2021-11-03 16:58:18 +00:00
|
|
|
@kiwi_command(
|
2021-11-17 15:23:55 +00:00
|
|
|
cmd_type=KiwiCommandType.INSTANCE,
|
2021-11-03 16:58:18 +00:00
|
|
|
short_help="Initializes kiwi-scp",
|
2021-10-20 06:31:36 +00:00
|
|
|
)
|
2021-11-17 15:23:55 +00:00
|
|
|
class InitCommand(KiwiCommand):
|
2021-10-20 08:54:41 +00:00
|
|
|
"""Initialize or reconfigure a kiwi-scp instance"""
|
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
@classmethod
|
2021-11-17 14:06:44 +00:00
|
|
|
def run_for_instance(cls, instance: Instance, directory: Path = None, force: bool = None) -> None:
|
|
|
|
if directory is not None:
|
|
|
|
instance.directory = directory
|
2021-10-20 12:32:45 +00:00
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
current_config = KiwiConfig() if force else instance.config
|
2021-10-20 12:32:45 +00:00
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
# check force switch
|
|
|
|
if force and os.path.isfile(KIWI_CONF_NAME):
|
|
|
|
_logger.warning(f"About to overwrite an existing '{KIWI_CONF_NAME}'!")
|
2021-10-20 12:32:45 +00:00
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
# build new kiwi dict
|
|
|
|
kiwi_dict = current_config.kiwi_dict
|
|
|
|
kiwi_dict.update({
|
|
|
|
"version": KiwiCommand.user_query("kiwi-scp version to use in this instance", current_config.version),
|
|
|
|
"storage": {
|
|
|
|
"directory": KiwiCommand.user_query("local directory for service data",
|
|
|
|
current_config.storage.directory, Path),
|
|
|
|
},
|
|
|
|
"network": {
|
|
|
|
"name": KiwiCommand.user_query("name for local network hub", current_config.network.name),
|
|
|
|
"cidr": KiwiCommand.user_query("CIDRv4 block for local network hub", current_config.network.cidr,
|
|
|
|
IPv4Network),
|
|
|
|
},
|
|
|
|
})
|
2021-10-20 12:32:45 +00:00
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
# ensure output directory exists
|
|
|
|
if not os.path.isdir(instance.directory):
|
|
|
|
os.mkdir(instance.directory)
|
2021-10-25 11:29:12 +00:00
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
# write out the new kiwi.yml
|
|
|
|
cfg = KiwiConfig.parse_obj(kiwi_dict)
|
|
|
|
with open(instance.directory.joinpath(KIWI_CONF_NAME), "w") as file:
|
|
|
|
cfg.dump_kiwi_yml(file)
|