Skip to content

Commit

Permalink
Add "cobbler-tftp setup" command to install configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
affenull2345 committed Jan 18, 2024
1 parent ddfc818 commit 6c0eb91
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/cobbler_tftp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import os
import sys
from pathlib import Path
from signal import SIGCHLD, SIGTERM
from typing import List, Optional
Expand All @@ -16,8 +17,14 @@
except ImportError: # use backport for Python versions older than 3.8
import importlib_metadata

try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

from cobbler_tftp.server import run_server
from cobbler_tftp.settings import SettingsFactory
from cobbler_tftp.utils import copy_file

try:
__version__ = importlib_metadata.version("cobbler_tftp")
Expand Down Expand Up @@ -57,7 +64,11 @@ def cli(ctx):
help="Enable or disable auto migration of settings.",
)
@click.option(
"--config", "-c", type=click.Path(), help="Set location of configuration file."
"--config",
"-c",
default="/etc/cobbler-tftp/settings.yml",
type=click.Path(),
help="Set location of configuration file.",
)
@click.option(
"--settings",
Expand Down Expand Up @@ -149,7 +160,67 @@ def stop(config: Optional[str], pid_file: Optional[str]):
pid_file_path.unlink()


@cli.command()
@click.option(
"--systemd-dir",
type=click.Path(),
default="/etc/systemd/system",
help="Where to install systemd unit files",
)
@click.option(
"--config-dir",
type=click.Path(),
default="/etc/cobbler-tftp",
help="Where to install the configuration files for cobbler-tftp",
)
@click.option(
"--systemd/--no-systemd",
is_flag=True,
default=True,
help="Whether to install systemd unit files or not",
)
@click.option(
"--install-prefix",
type=click.Path(),
default=None,
help="Installation prefix for the file locations that will be ignored during runtime",
)
def setup(
systemd_dir: str,
config_dir: str,
systemd: bool,
install_prefix: Optional[str],
):
if install_prefix is not None:
systemd_path = Path(install_prefix, systemd_dir.strip("/"))
config_path = Path(install_prefix, config_dir.strip("/"))
else:
systemd_path = Path(systemd_dir)
config_path = Path(config_dir)
config_dir = str(config_path.absolute())
try:
config_path.mkdir(parents=True, exist_ok=True)
source_path = files("cobbler_tftp.settings.data")
if systemd:
systemd_path.mkdir(parents=True, exist_ok=True)
copy_file(source_path, systemd_path, "cobbler-tftp.service")
copy_file(
source_path,
config_path,
"settings.yml",
[("/etc/cobbler-tftp", config_dir)],
)
copy_file(source_path, config_path, "logging.conf")
except PermissionError as err:
click.echo(err, err=True)
click.echo(
"Try changing the --systemd-dir/--config-dir parameters or running as root."
)
sys.exit(1)


cli.add_command(start)
cli.add_command(version)
cli.add_command(print_default_config)
cli.add_command(stop)
cli.add_command(setup)
31 changes: 31 additions & 0 deletions src/cobbler_tftp/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Various utility functions for cobbler-tftp
"""

from pathlib import Path
from typing import List, Tuple

try:
from importlib.abc import Traversable
except ImportError:
from importlib_resources.abc import Traversable


def copy_file(
src_dir: Traversable, dst_dir: Path, name: str, patch: List[Tuple[str, str]] = []
):
"""
Copy a file to a different directory, preserving the name and
possibly replacing strings in it.
:param src_dir: Directory that contains the file to copy.
:param dst_dir: Directory to copy the file into.
:param name: Name of the file (in both directories).
:param patch: List of (old, new) strings to replace in the file.
"""
src = src_dir / name
dst = dst_dir / name
contents: bytes = src.read_bytes() # type: ignore
for old, new in patch:
contents = contents.replace(old.encode("UTF-8"), new.encode("UTF-8"))
dst.write_bytes(contents)

0 comments on commit 6c0eb91

Please sign in to comment.