Skip to content

Commit

Permalink
Copy policies from one host to another. (#257)
Browse files Browse the repository at this point in the history
- This PR allows for copying of all policies (roles) from one host to another via a new command, `policy host_copy`.
  • Loading branch information
terjekv authored Jun 4, 2024
1 parent b922b50 commit 02681f0
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion mreg_cli/commands/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import argparse
from typing import Any

from mreg_cli.api.history import HistoryResource
from mreg_cli.api.models import Atom, Host, HostPolicy, Role
from mreg_cli.commands.base import BaseCommand
from mreg_cli.commands.registry import CommandRegistry
Expand Down Expand Up @@ -312,6 +311,33 @@ def host_add(args: argparse.Namespace) -> None:
cli_info(f"Added host {host.name!r} to role {role_name!r}", print_msg=True)


@command_registry.register_command(
prog="host_copy",
description="Copy roles from one host to another",
short_desc="Copy roles between hosts",
flags=[
Flag("source", description="Source host", metavar="SOURCE"),
Flag("destination", description="Destination host", metavar="DESTINATION"),
],
)
def host_copy(args: argparse.Namespace) -> None:
"""Copy roles from one host to another.
:param args: argparse.Namespace (source, destination)
"""
source_name: str = args.source
destination_name: str = args.destination

source = Host.get_by_any_means_or_raise(source_name)
destination = Host.get_by_any_means_or_raise(destination_name)

for role in source.roles():
role.add_host(destination.name.hostname)
OutputManager().add_line(
f"Copied role {role.name} from {source_name} to {destination_name}"
)


@command_registry.register_command(
prog="host_list",
description="List roles for host(s)",
Expand Down

0 comments on commit 02681f0

Please sign in to comment.