Skip to content

Commit 4d80989

Browse files
authored
Merge pull request #409 from Limmen/cli_command
Ossec and ryu manager CLI start/stop added
2 parents f76022a + b79aa2d commit 4d80989

File tree

1 file changed

+236
-3
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+236
-3
lines changed

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

+236-3
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ 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 | trafficmanagers | trafficmanager | kafkamanager")
688+
"| snortmanager | elkmanager | trafficmanagers | trafficmanager | kafkamanager "
689+
"| ossecmanagers | ossecmanager | ryumanager")
689690
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
690691
"""
691692
Stops an entity
@@ -747,6 +748,12 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
747748
stop_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
748749
elif entity == "kafkamanager":
749750
stop_kafka_manager(ip=ip, emulation=name, ip_first_octet=id)
751+
elif entity == "ossecmanagers":
752+
stop_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
753+
elif entity == "ossecmanager":
754+
stop_ossec_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
755+
elif entity == "ryumanager":
756+
stop_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
750757
else:
751758
container_stopped = False
752759
for node in config.cluster_config.cluster_nodes:
@@ -941,6 +948,54 @@ def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
941948
bold=False)
942949

943950

951+
def stop_ryu_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
952+
"""
953+
Utility function for stopping the ryu manager
954+
955+
:param ip: the ip of the node to stop the ryu manger
956+
:param emulation: the emulation of the execution
957+
:param ip_first_octet: the ID of the execution
958+
:return: None
959+
"""
960+
import csle_common.constants.constants as constants
961+
from csle_common.metastore.metastore_facade import MetastoreFacade
962+
config = MetastoreFacade.get_config(id=1)
963+
for node in config.cluster_config.cluster_nodes:
964+
if node.ip == ip or ip == "":
965+
stopped = ClusterController.stop_ryu_manager(
966+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
967+
ip_first_octet=ip_first_octet)
968+
if stopped.outcome:
969+
click.secho(f"Stopping host managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
970+
else:
971+
click.secho(f"Host managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
972+
bold=False)
973+
974+
975+
def stop_ossec_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
976+
"""
977+
Utility function for stopping the ossec managers
978+
979+
:param ip: the ip of the node to stop the ossec mangers
980+
:param emulation: the emulation of the execution
981+
:param ip_first_octet: the ID of the execution
982+
:return: None
983+
"""
984+
import csle_common.constants.constants as constants
985+
from csle_common.metastore.metastore_facade import MetastoreFacade
986+
config = MetastoreFacade.get_config(id=1)
987+
for node in config.cluster_config.cluster_nodes:
988+
if node.ip == ip or ip == "":
989+
stopped = ClusterController.stop_ossec_ids_managers(
990+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
991+
ip_first_octet=ip_first_octet)
992+
if stopped.outcome:
993+
click.secho(f"Stopping ossec managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
994+
else:
995+
click.secho(f"Ossec managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
996+
bold=False)
997+
998+
944999
def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
9451000
"""
9461001
Utility function for stopping the host manager
@@ -968,6 +1023,34 @@ def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet
9681023
bold=False)
9691024

9701025

1026+
def stop_ossec_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1027+
"""
1028+
Utility function for stopping the ossec manager
1029+
1030+
:param ip: the ip of the node to stop the ossec manager
1031+
:param container_ip: the ip of the host to be stopped
1032+
:param emulation: the emulation of the execution
1033+
:param ip_first_octet: the ID of the execution
1034+
:return: None
1035+
"""
1036+
import csle_common.constants.constants as constants
1037+
from csle_common.metastore.metastore_facade import MetastoreFacade
1038+
config = MetastoreFacade.get_config(id=1)
1039+
for node in config.cluster_config.cluster_nodes:
1040+
if node.ip == ip or ip == "":
1041+
stopped = ClusterController.stop_ossec_ids_manager(
1042+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1043+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1044+
if stopped.outcome:
1045+
click.secho(
1046+
f"Stopping ossec manager with ip {container_ip} on port:"
1047+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1048+
else:
1049+
click.secho(f"Ossec manager with ip {container_ip} is not "
1050+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1051+
bold=False)
1052+
1053+
9711054
def stop_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
9721055
"""
9731056
Utility function for stopping the client manager
@@ -1333,7 +1416,8 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
13331416
"container-name | emulation-name | all | statsmanager | training_job "
13341417
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
13351418
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
1336-
"| trafficmanagers | trafficmanager | kafkamanager")
1419+
"| trafficmanagers | trafficmanager | kafkamanager | ossecmanagers | ossecmanager "
1420+
"| ryumanager")
13371421
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
13381422
container_ip: str, no_beats: bool) -> None:
13391423
"""
@@ -1406,6 +1490,12 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
14061490
start_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
14071491
elif entity == "kafkamanager":
14081492
start_kafka_manager(ip=ip, emulation=name, ip_first_octet=id)
1493+
elif entity == "ossecmanagers":
1494+
start_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
1495+
elif entity == "ossecmanager":
1496+
start_ossec_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
1497+
elif entity == "ryumanager":
1498+
start_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
14091499
else:
14101500
container_started = False
14111501
for node in config.cluster_config.cluster_nodes:
@@ -1604,6 +1694,30 @@ def start_host_managers(ip: str, emulation: str, ip_first_octet: int):
16041694
bold=False)
16051695

16061696

1697+
def start_ryu_manager(ip: str, emulation: str, ip_first_octet: int):
1698+
"""
1699+
Utility function for starting ryu manager
1700+
1701+
:param ip: the ip of the node to start ryu manager
1702+
:param emulation: the emulation of the execution
1703+
:param ip_first_octet: the ID of the execution
1704+
:return: None
1705+
"""
1706+
import csle_common.constants.constants as constants
1707+
from csle_common.metastore.metastore_facade import MetastoreFacade
1708+
config = MetastoreFacade.get_config(id=1)
1709+
for node in config.cluster_config.cluster_nodes:
1710+
if node.ip == ip or ip == "":
1711+
operation_outcome = ClusterController.start_ryu_manager(
1712+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1713+
ip_first_octet=ip_first_octet)
1714+
if operation_outcome.outcome:
1715+
click.secho(f"Starting ryu managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1716+
else:
1717+
click.secho(f"Ryu managers are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1718+
bold=False)
1719+
1720+
16071721
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
16081722
"""
16091723
Utility function for starting host manager
@@ -1726,6 +1840,57 @@ def start_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first
17261840
bold=False)
17271841

17281842

1843+
def start_ossec_ids_managers(ip: str, emulation: str, ip_first_octet: int):
1844+
"""
1845+
Utility function for starting ossec managers
1846+
1847+
:param ip: the ip of the node to start ossec managers
1848+
:param emulation: the emulation of the execution
1849+
:param ip_first_octet: the ID of the execution
1850+
:return: None
1851+
"""
1852+
import csle_common.constants.constants as constants
1853+
from csle_common.metastore.metastore_facade import MetastoreFacade
1854+
config = MetastoreFacade.get_config(id=1)
1855+
for node in config.cluster_config.cluster_nodes:
1856+
if node.ip == ip or ip == "":
1857+
operation_outcome = ClusterController.start_ossec_ids_managers(
1858+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1859+
ip_first_octet=ip_first_octet)
1860+
if operation_outcome.outcome:
1861+
click.secho(f"Starting ossec ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1862+
else:
1863+
click.secho(f"Ossec ids managers are not started:"
1864+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}", bold=False)
1865+
1866+
1867+
def start_ossec_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
1868+
"""
1869+
Utility function for starting ossec ids manager
1870+
1871+
:param ip: the ip of the node to start ossec ids manager
1872+
:param container_ip: the ip of the host to start
1873+
:param emulation: the emulation of the execution
1874+
:param ip_first_octet: the ID of the execution
1875+
:return: None
1876+
"""
1877+
import csle_common.constants.constants as constants
1878+
from csle_common.metastore.metastore_facade import MetastoreFacade
1879+
config = MetastoreFacade.get_config(id=1)
1880+
for node in config.cluster_config.cluster_nodes:
1881+
if node.ip == ip or ip == "":
1882+
operation_outcome = ClusterController.start_ossec_ids_manager(
1883+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1884+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1885+
if operation_outcome.outcome:
1886+
click.secho(f"Started ossec ids manager with ip {container_ip} on "
1887+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1888+
else:
1889+
click.secho(f"Ossec ids manager with ip {container_ip} is not "
1890+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1891+
bold=False)
1892+
1893+
17291894
def start_elk_manager(ip: str, emulation: str, ip_first_octet: int):
17301895
"""
17311896
Utility function for starting elk manager
@@ -2112,7 +2277,8 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
21122277
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
21132278
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
21142279
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
2115-
"clientmanager | snortmanagers | elkmanager | trafficmanagers | kafkamanager")
2280+
"clientmanager | snortmanagers | elkmanager | trafficmanagers | kafkamanager | "
2281+
"ossecmanagers | ryumanager")
21162282
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
21172283
@click.option('--all', is_flag=True, help='list all')
21182284
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -2189,6 +2355,10 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
21892355
list_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
21902356
elif entity == "kafkamanager":
21912357
list_kafka_managers(ip=ip, emulation=name, ip_first_octet=id)
2358+
elif entity == "ossecmanagers":
2359+
list_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
2360+
elif entity == "ryumanager":
2361+
list_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
21922362
else:
21932363
container = get_running_container(name=entity)
21942364
if container is not None:
@@ -2223,6 +2393,32 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
22232393
click.secho(f"entity: {entity} is not recognized", fg="red", bold=True)
22242394

22252395

2396+
def list_ryu_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
2397+
"""
2398+
Utility function for listing ryu manager
2399+
2400+
:param ip: the ip of the node to list ryu manager
2401+
:param emulation: the emulation of the execution
2402+
:param ip_first_octet: the ID of the execution
2403+
2404+
:return: None
2405+
"""
2406+
import csle_common.constants.constants as constants
2407+
from csle_common.metastore.metastore_facade import MetastoreFacade
2408+
emulation_config = MetastoreFacade.get_emulation_by_name(name=emulation)
2409+
has_sdn = emulation_config.sdn_controller_config is not None
2410+
config = MetastoreFacade.get_config(id=1)
2411+
for node in config.cluster_config.cluster_nodes:
2412+
if node.ip == ip or ip == "":
2413+
if has_sdn:
2414+
ryu_manager_info = ClusterController.get_ryu_managers_info(
2415+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2416+
ip_first_octet=ip_first_octet)
2417+
click.secho(ryu_manager_info)
2418+
else:
2419+
click.secho(f"Emulation with name: {emulation} does not have SDN.", fg="red", bold=True)
2420+
2421+
22262422
def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
22272423
"""
22282424
Utility function for listing host managers
@@ -2430,6 +2626,43 @@ def list_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> Non
24302626
click.secho('+' + '-' * 60 + '+', fg='white')
24312627

24322628

2629+
def list_ossec_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
2630+
"""
2631+
Utility function for listing ossec ids managers
2632+
2633+
:param ip: the ip of the node to list ossec ids managers
2634+
:param emulation: the emulation of the execution
2635+
:param ip_first_octet: the ID of the execution
2636+
2637+
:return: None
2638+
"""
2639+
import csle_common.constants.constants as constants
2640+
from csle_common.metastore.metastore_facade import MetastoreFacade
2641+
config = MetastoreFacade.get_config(id=1)
2642+
for node in config.cluster_config.cluster_nodes:
2643+
if node.ip == ip or ip == "":
2644+
ossec_manager_info = ClusterController.get_ossec_ids_managers_info(
2645+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2646+
ip_first_octet=ip_first_octet)
2647+
2648+
click.secho('+' + '-' * 60 + '+', fg='white')
2649+
click.secho(f'|{"Ossec ids manager IP":^30}', nl=False, fg='white')
2650+
click.secho('|', nl=False, fg='white')
2651+
click.secho(f'{"Ossec ids status":^29}', nl=False, fg='white')
2652+
click.secho('|', fg='white')
2653+
2654+
for i in range(len(ossec_manager_info.ips)):
2655+
status = "Running" if ossec_manager_info.ossecIdsManagersRunning[i] else "Stopped"
2656+
status_color = 'green' if ossec_manager_info.ossecIdsManagersRunning[i] else 'red'
2657+
2658+
click.secho('+' + '-' * 60 + '+', fg='white')
2659+
click.secho(f'|{ossec_manager_info.ips[i]:^30}', nl=False, fg='white')
2660+
click.secho('|', nl=False, fg='white')
2661+
click.secho(f'{status:^29}', nl=False, fg=status_color)
2662+
click.secho('|', fg='white')
2663+
click.secho('+' + '-' * 60 + '+', fg='white')
2664+
2665+
24332666
def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
24342667
"""
24352668
Utility function for listing client managers

0 commit comments

Comments
 (0)