Skip to content

Commit 8d8bd9e

Browse files
authored
Merge pull request #398 from Limmen/cli_commands
commands start/stop/ls host_mangers are added.
2 parents 41f391f + 4df112f commit 8d8bd9e

File tree

1 file changed

+90
-5
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+90
-5
lines changed

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

+90-5
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,12 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
679679

680680
@click.option('--ip', default="", type=str)
681681
@click.argument('id', default=-1)
682+
@click.argument('name', default="", type=str)
682683
@click.argument('entity', default="", shell_complete=stop_shell_complete)
683684
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
684685
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
685-
"| docker | clustermanager")
686-
def stop(entity: str, id: int = -1, ip: str = "") -> None:
686+
"| docker | clustermanager | hostmanagers")
687+
def stop(entity: str, name: str, id: int = -1, ip: str = "") -> None:
687688
"""
688689
Stops an entity
689690
@@ -726,6 +727,8 @@ def stop(entity: str, id: int = -1, ip: str = "") -> None:
726727
stop_statsmanager(ip=ip)
727728
elif entity == "emulation_executions":
728729
stop_emulation_executions()
730+
elif entity == "hostmanager":
731+
stop_host_managers(ip=ip, emulation=name, ip_first_octet=id)
729732
else:
730733
container_stopped = False
731734
for node in config.cluster_config.cluster_nodes:
@@ -897,6 +900,30 @@ def stop_statsmanager(ip: str) -> None:
897900
ClusterController.stop_docker_statsmanager(ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT)
898901

899902

903+
def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
904+
"""
905+
Utility function for stopping the Docker statsmanager
906+
907+
:param ip: the ip of the node to stop the Docker statsmanager
908+
:param emulation: the emulation of the execution
909+
:param ip_first_octet: the ID of the execution
910+
:return: None
911+
"""
912+
import csle_common.constants.constants as constants
913+
from csle_common.metastore.metastore_facade import MetastoreFacade
914+
config = MetastoreFacade.get_config(id=1)
915+
for node in config.cluster_config.cluster_nodes:
916+
if node.ip == ip or ip == "":
917+
stopped = ClusterController.stop_host_managers(ip=ip, port= constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
918+
emulation=emulation, ip_first_octet=ip_first_octet)
919+
if stopped:
920+
click.secho(f"Stopping host managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
921+
else:
922+
click.secho(f"Host managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
923+
bold=False)
924+
925+
926+
900927
@click.argument('max_workers', default=10, type=int)
901928
@click.argument('log_file', default="docker_statsmanager.log", type=str)
902929
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1086,7 +1113,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
10861113
@click.argument('entity', default="", type=str, shell_complete=start_shell_complete)
10871114
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
10881115
"container-name | emulation-name | all | statsmanager | training_job "
1089-
"| system_id_job | nginx | postgresql | docker | clustermanager")
1116+
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanager")
10901117
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
10911118
no_beats: bool) -> None:
10921119
"""
@@ -1140,6 +1167,8 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
11401167
data_collection_job=system_id_job)
11411168
elif entity == "flask":
11421169
start_flask(ip=ip)
1170+
elif entity == "hostmanager":
1171+
start_host_manager(ip=ip, emulation=name, ip_first_octet=id)
11431172
else:
11441173
container_started = False
11451174
for node in config.cluster_config.cluster_nodes:
@@ -1312,6 +1341,24 @@ def start_statsmanager(ip: str) -> None:
13121341
if node.ip == ip or ip == "":
13131342
ClusterController.start_docker_statsmanager(ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT)
13141343

1344+
def start_host_manager(ip: str, emulation: str, ip_first_octet: int):
1345+
"""
1346+
Utility function for starting host manager
1347+
1348+
:param ip: the ip of the node to start host manager
1349+
:param emulation: the emulation of the execution
1350+
:param ip_first_octet: the ID of the execution
1351+
1352+
:return: None
1353+
"""
1354+
import csle_common.constants.constants as constants
1355+
from csle_common.metastore.metastore_facade import MetastoreFacade
1356+
config = MetastoreFacade.get_config(id=1)
1357+
for node in config.cluster_config.cluster_nodes:
1358+
if node.ip == ip or ip == "":
1359+
ClusterController.start_host_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1360+
emulation=emulation, ip_first_octet=ip_first_octet)
1361+
13151362

13161363
def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
13171364
"""
@@ -1599,12 +1646,15 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
15991646

16001647
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
16011648
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
1602-
"simulations | emulation_executions | cluster | nginx | postgresql | docker")
1649+
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers")
16031650
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
16041651
@click.option('--all', is_flag=True, help='list all')
16051652
@click.option('--running', is_flag=True, help='list running only (default)')
16061653
@click.option('--stopped', is_flag=True, help='list stopped only')
1607-
def ls(entity: str, all: bool, running: bool, stopped: bool) -> None:
1654+
@click.option('--ip', default="", type=str)
1655+
@click.option('--id', default=None, type=int)
1656+
@click.option('--name', default="", type=str)
1657+
def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str, id: int) -> None:
16081658
"""
16091659
Lists the set of containers, networks, images, or emulations, or all
16101660
@@ -1661,6 +1711,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool) -> None:
16611711
list_simulations()
16621712
elif entity == "emulation_executions":
16631713
list_emulation_executions()
1714+
elif entity == "hostmanagers":
1715+
list_host_managers(ip=ip, emulation=name, ip_first_octet=id)
16641716
else:
16651717
container = get_running_container(name=entity)
16661718
if container is not None:
@@ -1694,6 +1746,39 @@ def ls(entity: str, all: bool, running: bool, stopped: bool) -> None:
16941746
else:
16951747
click.secho(f"entity: {entity} is not recognized", fg="red", bold=True)
16961748

1749+
def list_host_managers(ip: str, emulation: str , ip_first_octet: int) -> None:
1750+
"""
1751+
Utility function for listing host managers
1752+
1753+
:param ip: the ip of the node to start host manager
1754+
:param emulation: the emulation of the execution
1755+
:param ip_first_octet: the ID of the execution
1756+
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+
host_manage_info = ClusterController.get_host_managers_info(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1765+
emulation=emulation, ip_first_octet=ip_first_octet)
1766+
host_managers = host_manage_info.hostManagersStatuses
1767+
1768+
click.secho('+' + '-' * 50 + '+', fg='white')
1769+
click.secho(f'|{"Host IP":^30}|{"Running Status":^19}|', fg='white')
1770+
click.secho('+' + '-' * 50 + '+', fg='white')
1771+
for hosts in host_managers:
1772+
status = "Running" if hosts.monitor_running else "Stopped"
1773+
status_color = 'green' if hosts.monitor_running else 'red'
1774+
1775+
click.secho('|', nl=False, fg='white')
1776+
click.secho(f'{hosts.ip:<30}', nl=False, fg='white')
1777+
click.secho('|', nl=False, fg='white')
1778+
click.secho(f'{status:<19}', nl=False, fg=status_color)
1779+
click.secho('|', fg='white')
1780+
click.secho('+' + '-' * 50 + '+', fg='white')
1781+
16971782

16981783
def print_running_container(container: DockerContainerDTO) -> None:
16991784
"""

0 commit comments

Comments
 (0)