Skip to content

Commit 6440523

Browse files
authored
Merge pull request #401 from Limmen/cli_command
Clientmanager CLI start/stop
2 parents 1963720 + acfa224 commit 6440523

File tree

1 file changed

+142
-7
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+142
-7
lines changed

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

+142-7
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
684684
@click.argument('entity', default="", shell_complete=stop_shell_complete)
685685
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
686686
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
687-
"| docker | clustermanager | hostmanagers | hostmanager")
687+
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager")
688688
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
689689
"""
690690
Stops an entity
@@ -732,6 +732,8 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
732732
stop_host_managers(ip=ip, emulation=name, ip_first_octet=id)
733733
elif entity == "hostmanager":
734734
stop_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
735+
elif entity == "clientmanager":
736+
stop_client_manager(ip=ip, emulation=name, ip_first_octet=id)
735737
else:
736738
container_stopped = False
737739
for node in config.cluster_config.cluster_nodes:
@@ -905,9 +907,9 @@ def stop_statsmanager(ip: str) -> None:
905907

906908
def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
907909
"""
908-
Utility function for stopping the Docker statsmanager
910+
Utility function for stopping the host managers
909911
910-
:param ip: the ip of the node to stop the Docker statsmanager
912+
:param ip: the ip of the node to stop the host mangers
911913
:param emulation: the emulation of the execution
912914
:param ip_first_octet: the ID of the execution
913915
:return: None
@@ -928,9 +930,9 @@ def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
928930

929931
def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
930932
"""
931-
Utility function for stopping the Docker statsmanager
933+
Utility function for stopping the host manager
932934
933-
:param ip: the ip of the node to stop the Docker statsmanager
935+
:param ip: the ip of the node to stop the host manager
934936
:param container_ip: the ip of the host to be stopped
935937
:param emulation: the emulation of the execution
936938
:param ip_first_octet: the ID of the execution
@@ -953,6 +955,29 @@ def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet
953955
bold=False)
954956

955957

958+
def stop_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
959+
"""
960+
Utility function for stopping the client manager
961+
962+
:param ip: the ip of the node to stop the client manger
963+
:param emulation: the emulation of the execution
964+
:param ip_first_octet: the ID of the execution
965+
:return: None
966+
"""
967+
import csle_common.constants.constants as constants
968+
from csle_common.metastore.metastore_facade import MetastoreFacade
969+
config = MetastoreFacade.get_config(id=1)
970+
for node in config.cluster_config.cluster_nodes:
971+
if node.ip == ip or ip == "":
972+
stopped = ClusterController.stop_client_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
973+
emulation=emulation, ip_first_octet=ip_first_octet)
974+
if stopped:
975+
click.secho(f"Stopping client manager on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
976+
else:
977+
click.secho(f"Client manager is not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
978+
bold=False)
979+
980+
956981
@click.argument('max_workers', default=10, type=int)
957982
@click.argument('log_file', default="docker_statsmanager.log", type=str)
958983
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1144,7 +1169,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
11441169
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
11451170
"container-name | emulation-name | all | statsmanager | training_job "
11461171
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
1147-
"| hostmanager")
1172+
"| hostmanager | clientmanager")
11481173
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
11491174
container_ip: str, no_beats: bool) -> None:
11501175
"""
@@ -1203,6 +1228,8 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
12031228
start_host_managers(ip=ip, emulation=name, ip_first_octet=id)
12041229
elif entity == "hostmanager":
12051230
start_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
1231+
elif entity == "clientmanager":
1232+
start_client_manager(ip=ip, emulation=name, ip_first_octet=id)
12061233
else:
12071234
container_started = False
12081235
for node in config.cluster_config.cluster_nodes:
@@ -1401,6 +1428,49 @@ def start_host_managers(ip: str, emulation: str, ip_first_octet: int):
14011428
bold=False)
14021429

14031430

1431+
def start_client_manager(ip: str, emulation: str, ip_first_octet: int):
1432+
"""
1433+
Utility function for starting client manager
1434+
1435+
:param ip: the ip of the node to start client manager
1436+
:param emulation: the emulation of the execution
1437+
:param ip_first_octet: the ID of the execution
1438+
:return: None
1439+
"""
1440+
import csle_common.constants.constants as constants
1441+
from csle_common.metastore.metastore_facade import MetastoreFacade
1442+
config = MetastoreFacade.get_config(id=1)
1443+
for node in config.cluster_config.cluster_nodes:
1444+
if node.ip == ip or ip == "":
1445+
operation_outcome = ClusterController.start_client_manager(ip=ip,
1446+
port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1447+
emulation=emulation,
1448+
ip_first_octet=ip_first_octet)
1449+
if operation_outcome.outcome:
1450+
click.secho(f"Starting client manager on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1451+
else:
1452+
click.secho(f"Client manager is not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1453+
bold=False)
1454+
1455+
operation_outcome = ClusterController.start_client_population(
1456+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1457+
ip_first_octet=ip_first_octet)
1458+
if operation_outcome.outcome:
1459+
click.secho(f"Starting client population on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1460+
else:
1461+
click.secho(f"Client population is not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1462+
bold=False)
1463+
1464+
operation_outcome = ClusterController.start_kafka_client_producer(
1465+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1466+
ip_first_octet=ip_first_octet)
1467+
if operation_outcome.outcome:
1468+
click.secho(f"Starting Kafka client producer on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1469+
else:
1470+
click.secho(f"Kafka client producer is not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1471+
bold=False)
1472+
1473+
14041474
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
14051475
"""
14061476
Utility function for starting host manager
@@ -1715,7 +1785,8 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
17151785

17161786
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
17171787
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
1718-
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers")
1788+
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
1789+
"clientmanager")
17191790
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
17201791
@click.option('--all', is_flag=True, help='list all')
17211792
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -1782,6 +1853,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
17821853
list_emulation_executions()
17831854
elif entity == "hostmanagers":
17841855
list_host_managers(ip=ip, emulation=name, ip_first_octet=id)
1856+
elif entity == "clientmanager":
1857+
list_client_manager(ip=ip, emulation=name, ip_first_octet=id)
17851858
else:
17861859
container = get_running_container(name=entity)
17871860
if container is not None:
@@ -1852,6 +1925,68 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
18521925
click.secho('+' + '-' * 50 + '+', fg='white')
18531926

18541927

1928+
def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
1929+
"""
1930+
Utility function for listing client managers
1931+
1932+
:param ip: the ip of the node to start host manager
1933+
:param emulation: the emulation of the execution
1934+
:param ip_first_octet: the ID of the execution
1935+
1936+
:return: None
1937+
"""
1938+
import csle_common.constants.constants as constants
1939+
from csle_common.metastore.metastore_facade import MetastoreFacade
1940+
config = MetastoreFacade.get_config(id=1)
1941+
for node in config.cluster_config.cluster_nodes:
1942+
if node.ip == ip or ip == "":
1943+
host_manager_info = ClusterController.get_client_managers_info(
1944+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1945+
ip_first_octet=ip_first_octet)
1946+
1947+
click.secho('+' + '-' * 60 + '+', fg='white')
1948+
click.secho(f'|{"Host IP":^40}', nl=False, fg='white')
1949+
for i in range(len(host_manager_info.ips)):
1950+
click.secho('|', nl=False, fg='white')
1951+
click.secho(f'{host_manager_info.ips[i]:<19}', nl=False, fg="green")
1952+
click.secho('|', fg='white')
1953+
click.secho('+' + '-' * 60 + '+', fg='white')
1954+
1955+
click.secho(f'|{"Clients number":^40}', nl=False, fg='white')
1956+
click.secho('|', nl=False, fg='white')
1957+
click.secho(f'{host_manager_info.clientManagersStatuses[i].num_clients:<19}', nl=False, fg="green")
1958+
click.secho('|', fg='white')
1959+
click.secho('+' + '-' * 60 + '+', fg='white')
1960+
1961+
click.secho(f'|{"Client process active":^40}', nl=False, fg='white')
1962+
click.secho('|', nl=False, fg='white')
1963+
click.secho(f'{bool(host_manager_info.clientManagersStatuses[i].client_process_active):<19}',
1964+
nl=False, fg="green")
1965+
click.secho('|', fg='white')
1966+
click.secho('+' + '-' * 60 + '+', fg='white')
1967+
1968+
click.secho(f'|{"Procedure active":^40}', nl=False, fg='white')
1969+
click.secho('|', nl=False, fg='white')
1970+
click.secho(f'{bool(host_manager_info.clientManagersStatuses[i].producer_active):<19}',
1971+
nl=False, fg="green")
1972+
click.secho('|', fg='white')
1973+
click.secho('+' + '-' * 60 + '+', fg='white')
1974+
1975+
click.secho(f'|{"Client time step length in seconds":^40}', nl=False, fg='white')
1976+
click.secho('|', nl=False, fg='white')
1977+
click.secho(f'{host_manager_info.clientManagersStatuses[i].clients_time_step_len_seconds:<19}',
1978+
nl=False, fg="green")
1979+
click.secho('|', fg='white')
1980+
click.secho('+' + '-' * 60 + '+', fg='white')
1981+
1982+
click.secho(f'|{"Producer time step length in seconds":^40}', nl=False, fg='white')
1983+
click.secho('|', nl=False, fg='white')
1984+
click.secho(f'{host_manager_info.clientManagersStatuses[i].producer_time_step_len_seconds:<19}',
1985+
nl=False, fg="green")
1986+
click.secho('|', fg='white')
1987+
click.secho('+' + '-' * 60 + '+', fg='white')
1988+
1989+
18551990
def print_running_container(container: DockerContainerDTO) -> None:
18561991
"""
18571992
Utility function for printing information about a running container

0 commit comments

Comments
 (0)