Skip to content

Commit c9eb814

Browse files
authored
Merge pull request #407 from Limmen/cli_command
trafficmanagers cli command added
2 parents d1cf7d6 + cead37a commit c9eb814

File tree

1 file changed

+179
-31
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+179
-31
lines changed

simulation-system/libs/csle-cli/src/csle_cli/cli.py

+179-31
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
685685
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
686686
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
687687
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
688-
"| snortmanager | elkmanager")
688+
"| snortmanager | elkmanager | trafficmanagers | trafficmanager")
689689
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
690690
"""
691691
Stops an entity
@@ -741,6 +741,10 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
741741
stop_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
742742
elif entity == "elkmanager":
743743
stop_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
744+
elif entity == "trafficmanagers":
745+
stop_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
746+
elif entity == "trafficmanager":
747+
stop_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
744748
else:
745749
container_stopped = False
746750
for node in config.cluster_config.cluster_nodes:
@@ -1059,6 +1063,58 @@ def stop_elk_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
10591063
bold=False)
10601064

10611065

1066+
def stop_traffic_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
1067+
"""
1068+
Utility function for stopping the traffic managers
1069+
1070+
:param ip: the ip of the node to stop the traffic mangers
1071+
:param emulation: the emulation of the execution
1072+
:param ip_first_octet: the ID of the execution
1073+
:return: None
1074+
"""
1075+
import csle_common.constants.constants as constants
1076+
from csle_common.metastore.metastore_facade import MetastoreFacade
1077+
config = MetastoreFacade.get_config(id=1)
1078+
for node in config.cluster_config.cluster_nodes:
1079+
if node.ip == ip or ip == "":
1080+
stopped = ClusterController.stop_traffic_managers(
1081+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1082+
ip_first_octet=ip_first_octet)
1083+
if stopped.outcome:
1084+
click.secho(f"Stopping traffic managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1085+
else:
1086+
click.secho(f"Traffic managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1087+
bold=False)
1088+
1089+
1090+
def stop_traffic_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1091+
"""
1092+
Utility function for stopping the traffic manager
1093+
1094+
:param ip: the ip of the node to stop the traffic manager
1095+
:param container_ip: the ip of the host that traffic is running on
1096+
:param emulation: the emulation of the execution
1097+
:param ip_first_octet: the ID of the execution
1098+
:return: None
1099+
"""
1100+
import csle_common.constants.constants as constants
1101+
from csle_common.metastore.metastore_facade import MetastoreFacade
1102+
config = MetastoreFacade.get_config(id=1)
1103+
for node in config.cluster_config.cluster_nodes:
1104+
if node.ip == ip or ip == "":
1105+
stopped = ClusterController.stop_traffic_manager(
1106+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1107+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1108+
if stopped.outcome:
1109+
click.secho(
1110+
f"Stopping traffic manager with ip {container_ip} on port:"
1111+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1112+
else:
1113+
click.secho(f"Traffic manager with ip {container_ip} is not "
1114+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1115+
bold=False)
1116+
1117+
10621118
@click.argument('max_workers', default=10, type=int)
10631119
@click.argument('log_file', default="docker_statsmanager.log", type=str)
10641120
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1250,7 +1306,8 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
12501306
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
12511307
"container-name | emulation-name | all | statsmanager | training_job "
12521308
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
1253-
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager")
1309+
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
1310+
"| trafficmanagers | trafficmanager")
12541311
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
12551312
container_ip: str, no_beats: bool) -> None:
12561313
"""
@@ -1317,6 +1374,10 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
13171374
start_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
13181375
elif entity == "elkmanager":
13191376
start_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
1377+
elif entity == "trafficmanagers":
1378+
start_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
1379+
elif entity == "trafficmanager":
1380+
start_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
13201381
else:
13211382
container_started = False
13221383
for node in config.cluster_config.cluster_nodes:
@@ -1515,6 +1576,34 @@ def start_host_managers(ip: str, emulation: str, ip_first_octet: int):
15151576
bold=False)
15161577

15171578

1579+
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
1580+
"""
1581+
Utility function for starting host manager
1582+
1583+
:param ip: the ip of the node to start host manager
1584+
:param container_ip: the ip of the host to start
1585+
:param emulation: the emulation of the execution
1586+
:param ip_first_octet: the ID of the execution
1587+
:return: None
1588+
"""
1589+
import csle_common.constants.constants as constants
1590+
from csle_common.metastore.metastore_facade import MetastoreFacade
1591+
config = MetastoreFacade.get_config(id=1)
1592+
for node in config.cluster_config.cluster_nodes:
1593+
if node.ip == ip or ip == "":
1594+
operation_outcome = ClusterController.start_host_manager(ip=ip,
1595+
port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1596+
emulation=emulation, ip_first_octet=ip_first_octet,
1597+
container_ip=container_ip)
1598+
if operation_outcome.outcome:
1599+
click.secho(f"Started host manager with ip {container_ip} on "
1600+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1601+
else:
1602+
click.secho(f"Host manager with ip {container_ip} is not "
1603+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1604+
bold=False)
1605+
1606+
15181607
def start_client_manager(ip: str, emulation: str, ip_first_octet: int):
15191608
"""
15201609
Utility function for starting client manager
@@ -1558,34 +1647,6 @@ def start_client_manager(ip: str, emulation: str, ip_first_octet: int):
15581647
bold=False)
15591648

15601649

1561-
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
1562-
"""
1563-
Utility function for starting host manager
1564-
1565-
:param ip: the ip of the node to start host manager
1566-
:param container_ip: the ip of the host to start
1567-
:param emulation: the emulation of the execution
1568-
:param ip_first_octet: the ID of the execution
1569-
:return: None
1570-
"""
1571-
import csle_common.constants.constants as constants
1572-
from csle_common.metastore.metastore_facade import MetastoreFacade
1573-
config = MetastoreFacade.get_config(id=1)
1574-
for node in config.cluster_config.cluster_nodes:
1575-
if node.ip == ip or ip == "":
1576-
operation_outcome = ClusterController.start_host_manager(ip=ip,
1577-
port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1578-
emulation=emulation, ip_first_octet=ip_first_octet,
1579-
container_ip=container_ip)
1580-
if operation_outcome.outcome:
1581-
click.secho(f"Started host manager with ip {container_ip} on "
1582-
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1583-
else:
1584-
click.secho(f"Host manager with ip {container_ip} is not "
1585-
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1586-
bold=False)
1587-
1588-
15891650
def start_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int):
15901651
"""
15911652
Utility function for starting snort managers
@@ -1661,6 +1722,57 @@ def start_elk_manager(ip: str, emulation: str, ip_first_octet: int):
16611722
bold=False)
16621723

16631724

1725+
def start_traffic_managers(ip: str, emulation: str, ip_first_octet: int):
1726+
"""
1727+
Utility function for starting traffic manager
1728+
1729+
:param ip: the ip of the node to start traffic manager
1730+
:param emulation: the emulation of the execution
1731+
:param ip_first_octet: the ID of the execution
1732+
:return: None
1733+
"""
1734+
import csle_common.constants.constants as constants
1735+
from csle_common.metastore.metastore_facade import MetastoreFacade
1736+
config = MetastoreFacade.get_config(id=1)
1737+
for node in config.cluster_config.cluster_nodes:
1738+
if node.ip == ip or ip == "":
1739+
operation_outcome = ClusterController.start_traffic_managers(
1740+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1741+
ip_first_octet=ip_first_octet)
1742+
if operation_outcome.outcome:
1743+
click.secho(f"Starting traffic managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1744+
else:
1745+
click.secho(f"Traffic managers are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1746+
bold=False)
1747+
1748+
1749+
def start_traffic_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
1750+
"""
1751+
Utility function for starting traffic manager
1752+
1753+
:param ip: the ip of the node to start traffic manager
1754+
:param container_ip: the ip of the host to start
1755+
:param emulation: the emulation of the execution
1756+
:param ip_first_octet: the ID of the execution
1757+
:return: None
1758+
"""
1759+
import csle_common.constants.constants as constants
1760+
from csle_common.metastore.metastore_facade import MetastoreFacade
1761+
config = MetastoreFacade.get_config(id=1)
1762+
for node in config.cluster_config.cluster_nodes:
1763+
if node.ip == ip or ip == "":
1764+
operation_outcome = ClusterController.start_traffic_manager(
1765+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1766+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1767+
if operation_outcome.outcome:
1768+
click.secho(f"Started ttraffic manager with ip {container_ip} on "
1769+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1770+
else:
1771+
click.secho(f"Traffic manager with ip {container_ip} is not "
1772+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1773+
bold=False)
1774+
1775+
16641776
def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
16651777
"""
16661778
Runs a container with a given image
@@ -1948,7 +2060,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
19482060
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
19492061
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
19502062
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
1951-
"clientmanager | snortmanagers | elkmanager")
2063+
"clientmanager | snortmanagers | elkmanager | trafficmanagers")
19522064
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
19532065
@click.option('--all', is_flag=True, help='list all')
19542066
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -2021,6 +2133,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
20212133
list_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
20222134
elif entity == "elkmanager":
20232135
list_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
2136+
elif entity == "trafficmanagers":
2137+
list_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
20242138
else:
20252139
container = get_running_container(name=entity)
20262140
if container is not None:
@@ -2091,6 +2205,40 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
20912205
click.secho('+' + '-' * 50 + '+', fg='white')
20922206

20932207

2208+
def list_traffic_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
2209+
"""
2210+
Utility function for listing traffic managers
2211+
2212+
:param ip: the ip of the node to list host manager
2213+
:param emulation: the emulation of the execution
2214+
:param ip_first_octet: the ID of the execution
2215+
2216+
:return: None
2217+
"""
2218+
import csle_common.constants.constants as constants
2219+
from csle_common.metastore.metastore_facade import MetastoreFacade
2220+
config = MetastoreFacade.get_config(id=1)
2221+
for node in config.cluster_config.cluster_nodes:
2222+
if node.ip == ip or ip == "":
2223+
traffic_manager_info = ClusterController.get_traffic_managers_info(
2224+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2225+
ip_first_octet=ip_first_octet)
2226+
click.secho('+' + '-' * 60 + '+', fg='white')
2227+
click.secho(f'|{"Traffic manager IP":^30}', nl=False, fg='white')
2228+
click.secho(f'|{"Traffic manager status":^29}', nl=False, fg='white')
2229+
click.secho('|', fg='white')
2230+
click.secho('+' + '-' * 60 + '+', fg='white')
2231+
for i in range(len(traffic_manager_info.ips)):
2232+
status_color = 'green' if traffic_manager_info.trafficManagersRunning[i] else 'red'
2233+
manager_status = 'Running' if traffic_manager_info.trafficManagersRunning[i] else 'Stopped'
2234+
click.secho('|', nl=False, fg='white')
2235+
click.secho(f'{traffic_manager_info.ips[i]:^30}', nl=False, fg=status_color)
2236+
click.secho('|', nl=False, fg='white')
2237+
click.secho(f'{manager_status:^29}', nl=False, fg=status_color)
2238+
click.secho('|', fg='white')
2239+
click.secho('+' + '-' * 60 + '+', fg='white')
2240+
2241+
20942242
def list_elk_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
20952243
"""
20962244
Utility function for listing elk manager

0 commit comments

Comments
 (0)