Skip to content

Commit

Permalink
Add --exclude option + Make exclude alternative to selected (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skazza94 committed May 15, 2024
1 parent 0ad23f3 commit 3fe2ef3
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 237 deletions.
11 changes: 10 additions & 1 deletion src/Kathara/cli/command/LcleanCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def __init__(self) -> None:
required=False,
help='Specify the folder containing the network scenario.'
)
self.parser.add_argument(
'--exclude',
dest='excluded_machines',
metavar='DEVICE_NAME',
nargs='*',
default=[],
help='Exclude specified devices from clean.'
)
self.parser.add_argument(
'machine_names',
metavar='DEVICE_NAME',
Expand All @@ -55,5 +63,6 @@ def run(self, current_path: str, argv: List[str]) -> None:

Kathara.get_instance().undeploy_lab(
lab_hash=lab.hash,
selected_machines=set(args['machine_names']) if args['machine_names'] else None
selected_machines=set(args['machine_names']) if args['machine_names'] else None,
excluded_machines=set(args['excluded_machines']) if args['excluded_machines'] else None,
)
10 changes: 10 additions & 0 deletions src/Kathara/cli/command/LrestartCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def __init__(self) -> None:
const=True,
help='Mount "/shared" directory inside devices.'
)
self.parser.add_argument(
'--exclude',
dest='excluded_machines',
metavar='DEVICE_NAME',
nargs='*',
default=[],
help='Exclude specified devices.'
)
self.parser.add_argument(
'machine_name',
metavar='DEVICE_NAME',
Expand All @@ -123,6 +131,8 @@ def run(self, current_path: str, argv: List[str]) -> None:

lclean_argv = ['-d', args['directory']] if args['directory'] else []

if args['excluded_machines']:
lclean_argv.extend(['--exclude'] + args['excluded_machines'])
if args['machine_name']:
lclean_argv.extend(args['machine_name'])

Expand Down
12 changes: 11 additions & 1 deletion src/Kathara/cli/command/LstartCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ def __init__(self) -> None:
const=True,
help='Mount "/shared" directory inside devices.'
)
self.parser.add_argument(
'--exclude',
dest='excluded_machines',
metavar='DEVICE_NAME',
nargs='*',
default=[],
help='Exclude specified devices from startup.'
)
self.parser.add_argument(
'machine_name',
metavar='DEVICE_NAME',
Expand Down Expand Up @@ -216,7 +224,9 @@ def run(self, current_path: str, argv: List[str]) -> Lab:
self.console.print("[yellow]\u26a0 Running devices with privileged capabilities, terminals won't open!")
Setting.get_instance().open_terminals = False

Kathara.get_instance().deploy_lab(lab, selected_machines=set(args['machine_name']))
Kathara.get_instance().deploy_lab(
lab, selected_machines=set(args['machine_name']), excluded_machines=set(args['excluded_machines'])
)

if args['list']:
with self.console.status(
Expand Down
23 changes: 17 additions & 6 deletions src/Kathara/manager/docker/DockerLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .stats.DockerLinkStats import DockerLinkStats
from ... import utils
from ...event.EventDispatcher import EventDispatcher
from ...exceptions import PrivilegeError
from ...exceptions import PrivilegeError, InvocationError
from ...model.ExternalLink import ExternalLink
from ...model.Lab import Lab
from ...model.Link import BRIDGE_LINK_NAME, Link
Expand All @@ -40,11 +40,22 @@ def deploy_links(self, lab: Lab, selected_links: Set[str] = None, excluded_links
Returns:
None
Raises:
InvocationError: If both `selected_links` and `excluded_links` are specified.
"""
links = {
k: v for k, v in lab.links.items()
if (not selected_links or k in selected_links) and (not excluded_links or k not in excluded_links)
}.items()
if selected_links and excluded_links:
raise InvocationError(f"You can either specify `selected_links` or `excluded_links`.")

links = lab.links.items()
if selected_links:
links = {
k: v for k, v in links if k in selected_links
}.items()
elif excluded_links:
links = {
k: v for k, v in links if k not in excluded_links
}.items()

if len(links) > 0:
pool_size = utils.get_pool_size()
Expand Down Expand Up @@ -151,7 +162,7 @@ def undeploy(self, lab_hash: str, selected_links: Optional[Set[str]] = None) ->
None
"""
networks = self.get_links_api_objects_by_filters(lab_hash=lab_hash)
if selected_links is not None and len(selected_links) > 0:
if selected_links:
networks = [item for item in networks if item.attrs["Labels"]["name"] in selected_links]

for item in networks:
Expand Down
37 changes: 24 additions & 13 deletions src/Kathara/manager/docker/DockerMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ... import utils
from ...event.EventDispatcher import EventDispatcher
from ...exceptions import MountDeniedError, MachineAlreadyExistsError, DockerPluginError, \
MachineBinaryError, MachineNotRunningError, PrivilegeError
MachineBinaryError, MachineNotRunningError, PrivilegeError, InvocationError
from ...model.Interface import Interface
from ...model.Lab import Lab
from ...model.Link import Link, BRIDGE_LINK_NAME
Expand Down Expand Up @@ -122,15 +122,23 @@ def deploy_machines(self, lab: Lab, selected_machines: Set[str] = None, excluded
Raises:
PrivilegeError: If the privileged mode is active and the user does not have root privileges.
InvocationError: If both `selected_machines` and `excluded_machines` are specified.
"""
if lab.general_options['privileged_machines'] and not utils.is_admin():
raise PrivilegeError("You must be root in order to start Kathara devices in privileged mode.")

machines = {
k: v for k, v in lab.machines.items()
if (not selected_machines or k in selected_machines) and
(not excluded_machines or k not in excluded_machines)
}.items()
if selected_machines and excluded_machines:
raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.")

machines = lab.machines.items()
if selected_machines:
machines = {
k: v for k, v in machines if k in selected_machines
}.items()
elif excluded_machines:
machines = {
k: v for k, v in machines if k not in excluded_machines
}.items()

# Check and pulling machine images
lab_images = set(map(lambda x: x[1].get_image(), machines))
Expand Down Expand Up @@ -482,15 +490,18 @@ def undeploy(self, lab_hash: str, selected_machines: Set[str] = None, excluded_m
Returns:
None
Raises:
InvocationError: If both `selected_machines` and `excluded_machines` are specified.
"""
containers = self.get_machines_api_objects_by_filters(lab_hash=lab_hash, user=utils.get_current_user_name())
if selected_machines and excluded_machines:
raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.")

if selected_machines or excluded_machines:
containers = [
item for item in containers
if (selected_machines is None or item.labels["name"] in selected_machines) and
(excluded_machines is None or item.labels["name"] not in excluded_machines)
]
containers = self.get_machines_api_objects_by_filters(lab_hash=lab_hash, user=utils.get_current_user_name())
if selected_machines:
containers = [item for item in containers if item.labels["name"] in selected_machines]
elif excluded_machines:
containers = [item for item in containers if item.labels["name"] not in excluded_machines]

if len(containers) > 0:
pool_size = utils.get_pool_size()
Expand Down
16 changes: 14 additions & 2 deletions src/Kathara/manager/docker/DockerManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,13 @@ def deploy_lab(self, lab: Lab, selected_machines: Optional[Set[str]] = None,
OSError: If any link in the network scenario is attached to external interfaces and the host OS is not LINUX.
PrivilegeError: If the user start the network scenario in privileged mode without having root privileges.
MachineNotFoundError: If the specified devices are not in the network scenario.
InvocationError: If both `selected_machines` and `excluded_machines` are specified.
"""
lab.check_integrity()

if selected_machines and excluded_machines:
raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.")

if selected_machines and not lab.has_machines(selected_machines):
machines_not_in_lab = selected_machines - set(lab.machines.keys())
raise MachineNotFoundError(f"The following devices are not in the network scenario: {machines_not_in_lab}.")
Expand All @@ -155,7 +159,11 @@ def deploy_lab(self, lab: Lab, selected_machines: Optional[Set[str]] = None,

excluded_links = None
if excluded_machines:
excluded_links = lab.get_links_from_machines(excluded_machines)
# Get the links of remaining machines
running_links = lab.get_links_from_machines(set(lab.machines.keys()) - excluded_machines)
# Get the links of the excluded machines and get the diff with the running ones
# The remaining are the ones to delete
excluded_links = lab.get_links_from_machines(excluded_machines) - running_links

# Deploy all lab links.
self.docker_link.deploy_links(lab, selected_links=selected_links, excluded_links=excluded_links)
Expand Down Expand Up @@ -303,14 +311,18 @@ def undeploy_lab(self, lab_hash: Optional[str] = None, lab_name: Optional[str] =
None
Raises:
InvocationError: If a running network scenario hash or name is not specified.
InvocationError: If a running network scenario hash or name is not specified,
or if both `selected_machines` and `excluded_machines` are specified.
"""
check_required_single_not_none_var(lab_hash=lab_hash, lab_name=lab_name, lab=lab)
if lab:
lab_hash = lab.hash
elif lab_name:
lab_hash = utils.generate_urlsafe_hash(lab_name)

if selected_machines and excluded_machines:
raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.")

self.docker_machine.undeploy(lab_hash, selected_machines=selected_machines, excluded_machines=excluded_machines)

self.docker_link.undeploy(lab_hash)
Expand Down
22 changes: 17 additions & 5 deletions src/Kathara/manager/kubernetes/KubernetesLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .stats.KubernetesLinkStats import KubernetesLinkStats
from ... import utils
from ...event.EventDispatcher import EventDispatcher
from ...exceptions import InvocationError
from ...model.Lab import Lab
from ...model.Link import Link
from ...setting.Setting import Setting
Expand Down Expand Up @@ -48,11 +49,22 @@ def deploy_links(self, lab: Lab, selected_links: Set[str] = None, excluded_links
Returns:
None
Raises:
InvocationError: If both `selected_links` and `excluded_links` are specified.
"""
links = {
k: v for k, v in lab.links.items()
if (not selected_links or k in selected_links) and (not excluded_links or k not in excluded_links)
}.items()
if selected_links and excluded_links:
raise InvocationError(f"You can either specify `selected_links` or `excluded_links`.")

links = lab.links.items()
if selected_links:
links = {
k: v for k, v in links if k in selected_links
}.items()
elif excluded_links:
links = {
k: v for k, v in links if k not in excluded_links
}.items()

if len(links) > 0:
pool_size = utils.get_pool_size()
Expand Down Expand Up @@ -128,7 +140,7 @@ def undeploy(self, lab_hash: str, selected_links: Optional[Set[str]] = None) ->
None
"""
networks = self.get_links_api_objects_by_filters(lab_hash=lab_hash)
if selected_links is not None:
if selected_links:
networks = [item for item in networks if item["metadata"]["name"] in selected_links]

if len(networks) > 0:
Expand Down
40 changes: 27 additions & 13 deletions src/Kathara/manager/kubernetes/KubernetesMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from .stats.KubernetesMachineStats import KubernetesMachineStats
from ... import utils
from ...event.EventDispatcher import EventDispatcher
from ...exceptions import MachineAlreadyExistsError, MachineNotReadyError, MachineNotRunningError, MachineBinaryError
from ...exceptions import MachineAlreadyExistsError, MachineNotReadyError, MachineNotRunningError, MachineBinaryError, \
InvocationError
from ...model.Lab import Lab
from ...model.Machine import Machine
from ...setting.Setting import Setting
Expand Down Expand Up @@ -148,12 +149,22 @@ def deploy_machines(self, lab: Lab, selected_machines: Set[str] = None, excluded
Returns:
None
Raises:
InvocationError: If both `selected_machines` and `excluded_machines` are specified.
"""
machines = {
k: v for k, v in lab.machines.items()
if (not selected_machines or k in selected_machines) and
(not excluded_machines or k not in excluded_machines)
}.items()
if selected_machines and excluded_machines:
raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.")

machines = lab.machines.items()
if selected_machines:
machines = {
k: v for k, v in machines if k in selected_machines
}.items()
elif excluded_machines:
machines = {
k: v for k, v in machines if k not in excluded_machines
}.items()

if lab.general_options['privileged_machines']:
logging.warning('Privileged option is not supported on Megalos. It will be ignored.')
Expand Down Expand Up @@ -495,17 +506,20 @@ def undeploy(self, lab_hash: str, selected_machines: Set[str] = None, excluded_m
Returns:
None
Raises:
InvocationError: If both `selected_machines` and `excluded_machines` are specified.
"""
if selected_machines and excluded_machines:
raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.")

pods = self.get_machines_api_objects_by_filters(lab_hash=lab_hash)
machines_to_watch = {item.metadata.labels["name"] for item in pods}
if selected_machines or excluded_machines:
pods = [
item for item in pods
if (selected_machines is None or item.metadata.labels["name"] in selected_machines) and
(excluded_machines is None or item.metadata.labels["name"] not in excluded_machines)
]

if selected_machines:
pods = [item for item in pods if item.metadata.labels["name"] in selected_machines]
machines_to_watch = selected_machines if selected_machines else machines_to_watch
elif excluded_machines:
pods = [item for item in pods if item.metadata.labels["name"] not in excluded_machines]
machines_to_watch = machines_to_watch if not excluded_machines else machines_to_watch - excluded_machines

if len(pods) > 0:
Expand Down
Loading

0 comments on commit 3fe2ef3

Please sign in to comment.