1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-12-05 01:53:03 +00:00
kiwi-scp/kiwi_scp/commands/cmd_init.py

72 lines
2.2 KiB
Python
Raw Permalink Normal View History

2021-10-20 12:32:45 +00:00
import logging
import os
from ipaddress import IPv4Network
from pathlib import Path
import click
2021-12-02 16:19:14 +00:00
from .cmd import KiwiCommandType, KiwiCommand
2021-11-03 16:58:18 +00:00
from .decorators import kiwi_command
2021-10-20 12:32:45 +00:00
from .._constants import KIWI_CONF_NAME
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-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(
short_help="Initializes kiwi-scp",
)
class InitCommand(KiwiCommand):
"""Initialize or reconfigure a kiwi-scp instance"""
2021-12-01 16:49:19 +00:00
type = KiwiCommandType.INSTANCE
2021-11-03 16:58:18 +00:00
@classmethod
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
2022-01-20 09:20:57 +00:00
instance.save_config(KiwiConfig.parse_obj(kiwi_dict))