Skip to content

Commit

Permalink
Merge pull request #407 from Limmen/cli_command
Browse files Browse the repository at this point in the history
trafficmanagers cli command added
  • Loading branch information
Limmen authored Aug 2, 2024
2 parents d1cf7d6 + cead37a commit c9eb814
Showing 1 changed file with 179 additions and 31 deletions.
210 changes: 179 additions & 31 deletions simulation-system/libs/csle-cli/src/csle_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
"| snortmanager | elkmanager")
"| snortmanager | elkmanager | trafficmanagers | trafficmanager")
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
"""
Stops an entity
Expand Down Expand Up @@ -741,6 +741,10 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
stop_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "elkmanager":
stop_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "trafficmanagers":
stop_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "trafficmanager":
stop_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
else:
container_stopped = False
for node in config.cluster_config.cluster_nodes:
Expand Down Expand Up @@ -1059,6 +1063,58 @@ def stop_elk_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
bold=False)


def stop_traffic_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the traffic managers
:param ip: the ip of the node to stop the traffic mangers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_traffic_managers(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if stopped.outcome:
click.secho(f"Stopping traffic managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Traffic managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def stop_traffic_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the traffic manager
:param ip: the ip of the node to stop the traffic manager
:param container_ip: the ip of the host that traffic is running on
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_traffic_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet, container_ip=container_ip)
if stopped.outcome:
click.secho(
f"Stopping traffic manager with ip {container_ip} on port:"
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Traffic manager with ip {container_ip} is not "
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


@click.argument('max_workers', default=10, type=int)
@click.argument('log_file', default="docker_statsmanager.log", type=str)
@click.argument('log_dir', default="/var/log/csle", type=str)
Expand Down Expand Up @@ -1250,7 +1306,8 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
"container-name | emulation-name | all | statsmanager | training_job "
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager")
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
"| trafficmanagers | trafficmanager")
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
container_ip: str, no_beats: bool) -> None:
"""
Expand Down Expand Up @@ -1317,6 +1374,10 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
start_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "elkmanager":
start_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "trafficmanagers":
start_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "trafficmanager":
start_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
else:
container_started = False
for node in config.cluster_config.cluster_nodes:
Expand Down Expand Up @@ -1515,6 +1576,34 @@ def start_host_managers(ip: str, emulation: str, ip_first_octet: int):
bold=False)


def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting host manager
:param ip: the ip of the node to start host manager
:param container_ip: the ip of the host to start
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_host_manager(ip=ip,
port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
emulation=emulation, ip_first_octet=ip_first_octet,
container_ip=container_ip)
if operation_outcome.outcome:
click.secho(f"Started host manager with ip {container_ip} on "
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Host manager with ip {container_ip} is not "
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def start_client_manager(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting client manager
Expand Down Expand Up @@ -1558,34 +1647,6 @@ def start_client_manager(ip: str, emulation: str, ip_first_octet: int):
bold=False)


def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting host manager
:param ip: the ip of the node to start host manager
:param container_ip: the ip of the host to start
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_host_manager(ip=ip,
port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
emulation=emulation, ip_first_octet=ip_first_octet,
container_ip=container_ip)
if operation_outcome.outcome:
click.secho(f"Started host manager with ip {container_ip} on "
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Host manager with ip {container_ip} is not "
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def start_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting snort managers
Expand Down Expand Up @@ -1661,6 +1722,57 @@ def start_elk_manager(ip: str, emulation: str, ip_first_octet: int):
bold=False)


def start_traffic_managers(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting traffic manager
:param ip: the ip of the node to start traffic manager
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_traffic_managers(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if operation_outcome.outcome:
click.secho(f"Starting traffic managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Traffic managers are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def start_traffic_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting traffic manager
:param ip: the ip of the node to start traffic manager
:param container_ip: the ip of the host to start
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_traffic_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet, container_ip=container_ip)
if operation_outcome.outcome:
click.secho(f"Started ttraffic manager with ip {container_ip} on "
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Traffic manager with ip {container_ip} is not "
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
"""
Runs a container with a given image
Expand Down Expand Up @@ -1948,7 +2060,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
"clientmanager | snortmanagers | elkmanager")
"clientmanager | snortmanagers | elkmanager | trafficmanagers")
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
@click.option('--all', is_flag=True, help='list all')
@click.option('--running', is_flag=True, help='list running only (default)')
Expand Down Expand Up @@ -2021,6 +2133,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
list_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "elkmanager":
list_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "trafficmanagers":
list_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
else:
container = get_running_container(name=entity)
if container is not None:
Expand Down Expand Up @@ -2091,6 +2205,40 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
click.secho('+' + '-' * 50 + '+', fg='white')


def list_traffic_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing traffic managers
:param ip: the ip of the node to list host manager
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
traffic_manager_info = ClusterController.get_traffic_managers_info(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{"Traffic manager IP":^30}', nl=False, fg='white')
click.secho(f'|{"Traffic manager status":^29}', nl=False, fg='white')
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')
for i in range(len(traffic_manager_info.ips)):
status_color = 'green' if traffic_manager_info.trafficManagersRunning[i] else 'red'
manager_status = 'Running' if traffic_manager_info.trafficManagersRunning[i] else 'Stopped'
click.secho('|', nl=False, fg='white')
click.secho(f'{traffic_manager_info.ips[i]:^30}', nl=False, fg=status_color)
click.secho('|', nl=False, fg='white')
click.secho(f'{manager_status:^29}', nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')


def list_elk_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing elk manager
Expand Down

0 comments on commit c9eb814

Please sign in to comment.