|
18 | 18 | from ... import utils |
19 | 19 | from ...event.EventDispatcher import EventDispatcher |
20 | 20 | from ...exceptions import MountDeniedError, MachineAlreadyExistsError, DockerPluginError, \ |
21 | | - MachineBinaryError, MachineNotRunningError, PrivilegeError |
| 21 | + MachineBinaryError, MachineNotRunningError, PrivilegeError, InvocationError |
22 | 22 | from ...model.Interface import Interface |
23 | 23 | from ...model.Lab import Lab |
24 | 24 | from ...model.Link import Link, BRIDGE_LINK_NAME |
@@ -109,24 +109,36 @@ def __init__(self, client: DockerClient, docker_image: DockerImage) -> None: |
109 | 109 | self._engine_version: str = client.version()['Version'] |
110 | 110 | self.docker_image: DockerImage = docker_image |
111 | 111 |
|
112 | | - def deploy_machines(self, lab: Lab, selected_machines: Set[str] = None) -> None: |
| 112 | + def deploy_machines(self, lab: Lab, selected_machines: Set[str] = None, excluded_machines: Set[str] = None) -> None: |
113 | 113 | """Deploy all the network scenario devices as Docker containers. |
114 | 114 |
|
115 | 115 | Args: |
116 | 116 | lab (Kathara.model.Lab.Lab): A Kathara network scenario. |
117 | 117 | selected_machines (Set[str]): A set containing the name of the devices to deploy. |
| 118 | + excluded_machines (Set[str]): A set containing the name of the devices to exclude. |
118 | 119 |
|
119 | 120 | Returns: |
120 | 121 | None |
121 | 122 |
|
122 | 123 | Raises: |
123 | 124 | PrivilegeError: If the privileged mode is active and the user does not have root privileges. |
| 125 | + InvocationError: If both `selected_machines` and `excluded_machines` are specified. |
124 | 126 | """ |
125 | 127 | if lab.general_options['privileged_machines'] and not utils.is_admin(): |
126 | 128 | raise PrivilegeError("You must be root in order to start Kathara devices in privileged mode.") |
127 | 129 |
|
128 | | - machines = {k: v for (k, v) in lab.machines.items() if k in selected_machines}.items() if selected_machines \ |
129 | | - else lab.machines.items() |
| 130 | + if selected_machines and excluded_machines: |
| 131 | + raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.") |
| 132 | + |
| 133 | + machines = lab.machines.items() |
| 134 | + if selected_machines: |
| 135 | + machines = { |
| 136 | + k: v for k, v in machines if k in selected_machines |
| 137 | + }.items() |
| 138 | + elif excluded_machines: |
| 139 | + machines = { |
| 140 | + k: v for k, v in machines if k not in excluded_machines |
| 141 | + }.items() |
130 | 142 |
|
131 | 143 | # Check and pulling machine images |
132 | 144 | lab_images = set(map(lambda x: x[1].get_image(), machines)) |
@@ -471,22 +483,30 @@ def start(self, machine: Machine) -> None: |
471 | 483 |
|
472 | 484 | machine.api_object.reload() |
473 | 485 |
|
474 | | - def undeploy(self, lab_hash: str, selected_machines: Set[str] = None) -> None: |
| 486 | + def undeploy(self, lab_hash: str, selected_machines: Set[str] = None, excluded_machines: Set[str] = None) -> None: |
475 | 487 | """Undeploy the devices contained in the network scenario defined by the lab_hash. |
476 | 488 |
|
477 | 489 | If a set of selected_machines is specified, undeploy only the specified devices. |
478 | 490 |
|
479 | 491 | Args: |
480 | 492 | lab_hash (str): The hash of the network scenario to undeploy. |
481 | | - selected_machines (Optional[Set[str]]): If not None, undeploy only the specified devices. |
| 493 | + selected_machines (Set[str]): A set containing the name of the devices to undeploy. |
| 494 | + excluded_machines (Set[str]): A set containing the name of the devices to exclude. |
482 | 495 |
|
483 | 496 | Returns: |
484 | 497 | None |
| 498 | +
|
| 499 | + Raises: |
| 500 | + InvocationError: If both `selected_machines` and `excluded_machines` are specified. |
485 | 501 | """ |
486 | | - containers = self.get_machines_api_objects_by_filters(lab_hash=lab_hash, user=utils.get_current_user_name()) |
| 502 | + if selected_machines and excluded_machines: |
| 503 | + raise InvocationError(f"You can either specify `selected_machines` or `excluded_machines`.") |
487 | 504 |
|
488 | | - if selected_machines is not None and len(selected_machines) > 0: |
| 505 | + containers = self.get_machines_api_objects_by_filters(lab_hash=lab_hash, user=utils.get_current_user_name()) |
| 506 | + if selected_machines: |
489 | 507 | containers = [item for item in containers if item.labels["name"] in selected_machines] |
| 508 | + elif excluded_machines: |
| 509 | + containers = [item for item in containers if item.labels["name"] not in excluded_machines] |
490 | 510 |
|
491 | 511 | if len(containers) > 0: |
492 | 512 | pool_size = utils.get_pool_size() |
|
0 commit comments