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-10-29 11:37:59 +00:00
|
|
|
from .cli import pass_instance
|
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-10-20 12:32:45 +00:00
|
|
|
from ..misc import user_query
|
2021-10-20 06:31:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command(
|
|
|
|
"init",
|
2021-10-22 15:43:29 +00:00
|
|
|
short_help="Initializes kiwi-scp",
|
2021-10-20 06:31:36 +00:00
|
|
|
)
|
2021-10-25 11:29:12 +00:00
|
|
|
@click.option(
|
|
|
|
"-o",
|
|
|
|
"--output",
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-s/-S",
|
|
|
|
"--show/--no-show",
|
|
|
|
help=f"show effective {KIWI_CONF_NAME} contents instead",
|
2021-10-20 06:31:36 +00:00
|
|
|
)
|
2021-10-25 11:29:12 +00:00
|
|
|
@pass_instance
|
|
|
|
def cmd(ctx: Instance, output: Path, force: bool, show: bool):
|
2021-10-20 08:54:41 +00:00
|
|
|
"""Initialize or reconfigure a kiwi-scp instance"""
|
|
|
|
|
2021-10-25 11:29:12 +00:00
|
|
|
if output is not None:
|
|
|
|
ctx.directory = output
|
|
|
|
|
2021-10-26 13:56:58 +00:00
|
|
|
current_config = KiwiConfig() if force else ctx.config
|
2021-10-20 12:32:45 +00:00
|
|
|
|
|
|
|
if show:
|
|
|
|
# just show the currently effective kiwi.yml
|
|
|
|
click.echo_via_pager(current_config.kiwi_yml)
|
|
|
|
return
|
|
|
|
|
|
|
|
# check force switch
|
|
|
|
if force and os.path.isfile(KIWI_CONF_NAME):
|
|
|
|
logging.warning(f"Overwriting an existing '{KIWI_CONF_NAME}'!")
|
|
|
|
|
|
|
|
# build new kiwi dict
|
|
|
|
kiwi_dict = current_config.kiwi_dict
|
|
|
|
kiwi_dict.update({
|
|
|
|
"version": user_query("kiwi-scp version to use in this instance", current_config.version),
|
|
|
|
"storage": {
|
|
|
|
"directory": user_query("local directory for service data", current_config.storage.directory, Path),
|
|
|
|
},
|
|
|
|
"network": {
|
|
|
|
"name": user_query("name for local network hub", current_config.network.name),
|
|
|
|
"cidr": user_query("CIDRv4 block for local network hub", current_config.network.cidr, IPv4Network),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-10-25 11:29:12 +00:00
|
|
|
# ensure output directory exists
|
|
|
|
if not os.path.isdir(ctx.directory):
|
|
|
|
os.mkdir(ctx.directory)
|
|
|
|
|
|
|
|
# write out the new kiwi.yml
|
|
|
|
with open(ctx.directory.joinpath(KIWI_CONF_NAME), "w") as file:
|
2021-10-26 13:56:58 +00:00
|
|
|
KiwiConfig.parse_obj(kiwi_dict).dump_kiwi_yml(file)
|