Skip to content

Commit d5f9e5d

Browse files
authored
Merge pull request #400 from Limmen/cli_commands
hostmanager CLI
2 parents d79cbf8 + 2ce71c5 commit d5f9e5d

File tree

1 file changed

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

1 file changed

+73
-7
lines changed

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

+73-7
Original file line numberDiff line numberDiff line change
@@ -678,13 +678,14 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
678678

679679

680680
@click.option('--ip', default="", type=str)
681+
@click.option('--container_ip', default="", type=str)
681682
@click.argument('id', default=-1)
682683
@click.argument('name', default="", type=str)
683684
@click.argument('entity', default="", shell_complete=stop_shell_complete)
684685
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
685686
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
686-
"| docker | clustermanager | hostmanagers")
687-
def stop(entity: str, name: str, id: int = -1, ip: str = "") -> None:
687+
"| docker | clustermanager | hostmanagers | hostmanager")
688+
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
688689
"""
689690
Stops an entity
690691
@@ -729,6 +730,8 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "") -> None:
729730
stop_emulation_executions()
730731
elif entity == "hostmanagers":
731732
stop_host_managers(ip=ip, emulation=name, ip_first_octet=id)
733+
elif entity == "hostmanager":
734+
stop_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
732735
else:
733736
container_stopped = False
734737
for node in config.cluster_config.cluster_nodes:
@@ -923,6 +926,32 @@ def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
923926
bold=False)
924927

925928

929+
def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
930+
"""
931+
Utility function for stopping the Docker statsmanager
932+
933+
:param ip: the ip of the node to stop the Docker statsmanager
934+
:param container_ip: the ip of the host to be stopped
935+
:param emulation: the emulation of the execution
936+
:param ip_first_octet: the ID of the execution
937+
:return: None
938+
"""
939+
import csle_common.constants.constants as constants
940+
from csle_common.metastore.metastore_facade import MetastoreFacade
941+
config = MetastoreFacade.get_config(id=1)
942+
for node in config.cluster_config.cluster_nodes:
943+
if node.ip == ip or ip == "":
944+
stopped = ClusterController.stop_host_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
945+
emulation=emulation, ip_first_octet=ip_first_octet,
946+
container_ip=container_ip)
947+
if stopped:
948+
click.secho(f"Stopping host with ip {container_ip} on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
949+
else:
950+
click.secho(f"Host with ip {container_ip} is not "
951+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
952+
bold=False)
953+
954+
926955
@click.argument('max_workers', default=10, type=int)
927956
@click.argument('log_file', default="docker_statsmanager.log", type=str)
928957
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1103,6 +1132,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
11031132

11041133

11051134
@click.option('--ip', default="", type=str)
1135+
@click.option('--container_ip', default="", type=str)
11061136
@click.option('--id', default=None, type=int)
11071137
@click.option('--no_clients', is_flag=True, help='skip starting the client population')
11081138
@click.option('--no_traffic', is_flag=True, help='skip starting the traffic generators')
@@ -1112,9 +1142,10 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
11121142
@click.argument('entity', default="", type=str, shell_complete=start_shell_complete)
11131143
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
11141144
"container-name | emulation-name | all | statsmanager | training_job "
1115-
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanager")
1145+
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
1146+
"| hostmanager")
11161147
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
1117-
no_beats: bool) -> None:
1148+
container_ip: str, no_beats: bool) -> None:
11181149
"""
11191150
Starts an entity, e.g., a container or the management system
11201151
@@ -1126,6 +1157,7 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
11261157
:param no_network: a boolean parameter that is True if the network should be skipped when creating a container
11271158
:param id: (optional) an id parameter to identify the entity to start
11281159
:param ip: ip when stopping a service on a specific physical server (empty ip means all servers)
1160+
:param container_ip: ip of the host to be started
11291161
:return: None
11301162
"""
11311163
from csle_agents.job_controllers.training_job_manager import TrainingJobManager
@@ -1166,8 +1198,10 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
11661198
data_collection_job=system_id_job)
11671199
elif entity == "flask":
11681200
start_flask(ip=ip)
1201+
elif entity == "hostmanagers":
1202+
start_host_managers(ip=ip, emulation=name, ip_first_octet=id)
11691203
elif entity == "hostmanager":
1170-
start_host_manager(ip=ip, emulation=name, ip_first_octet=id)
1204+
start_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
11711205
else:
11721206
container_started = False
11731207
for node in config.cluster_config.cluster_nodes:
@@ -1341,7 +1375,7 @@ def start_statsmanager(ip: str) -> None:
13411375
ClusterController.start_docker_statsmanager(ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT)
13421376

13431377

1344-
def start_host_manager(ip: str, emulation: str, ip_first_octet: int):
1378+
def start_host_managers(ip: str, emulation: str, ip_first_octet: int):
13451379
"""
13461380
Utility function for starting host manager
13471381
@@ -1355,9 +1389,41 @@ def start_host_manager(ip: str, emulation: str, ip_first_octet: int):
13551389
config = MetastoreFacade.get_config(id=1)
13561390
for node in config.cluster_config.cluster_nodes:
13571391
if node.ip == ip or ip == "":
1358-
ClusterController.start_host_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1392+
started = ClusterController.start_host_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
13591393
emulation=emulation, ip_first_octet=ip_first_octet)
13601394

1395+
if started:
1396+
click.secho(f"Starting host managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1397+
else:
1398+
click.secho(f"Host managers are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1399+
bold=False)
1400+
1401+
def start_host_manager(ip: str, container_ip:str, emulation: str, ip_first_octet: int):
1402+
"""
1403+
Utility function for starting host manager
1404+
1405+
:param ip: the ip of the node to start host manager
1406+
:param container_ip: the ip of the host to start
1407+
:param emulation: the emulation of the execution
1408+
:param ip_first_octet: the ID of the execution
1409+
:return: None
1410+
"""
1411+
import csle_common.constants.constants as constants
1412+
from csle_common.metastore.metastore_facade import MetastoreFacade
1413+
config = MetastoreFacade.get_config(id=1)
1414+
for node in config.cluster_config.cluster_nodes:
1415+
if node.ip == ip or ip == "":
1416+
started = ClusterController.start_host_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1417+
emulation=emulation, ip_first_octet=ip_first_octet,
1418+
container_ip=container_ip)
1419+
if started:
1420+
click.secho(f"Starting host with ip {container_ip} on "
1421+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1422+
else:
1423+
click.secho(f"Host with ip {container_ip} is not "
1424+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1425+
bold=False)
1426+
13611427

13621428
def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
13631429
"""

0 commit comments

Comments
 (0)