From e6ca0fd3b618b6b965c66c57b33c01ef11493e85 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 25 Aug 2021 10:07:41 +0200 Subject: [PATCH] Zoo: Parallelize start and stop of gcp machines --- .../blackbox/gcp_test_machine_list.py | 1 - .../blackbox/utils/gcp_machine_handlers.py | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py index 52efeb670..86999ab6d 100644 --- a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py +++ b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py @@ -7,7 +7,6 @@ GCP_TEST_MACHINE_LIST = { "hadoop-2", "hadoop-3", "mssql-16", - "powershell-3-45", "mimikatz-14", "mimikatz-15", "struts2-23", diff --git a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py index 26b4b18a5..9c01c72c7 100644 --- a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py +++ b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py @@ -1,6 +1,7 @@ import logging import os import subprocess +from multiprocessing.dummy import Pool LOGGER = logging.getLogger(__name__) @@ -44,24 +45,24 @@ def start_machines(machine_list): """ LOGGER.info("Setting up all GCP machines...") try: + arglist = [] for zone in machine_list: - subprocess.call( # noqa: DUO116 - (MACHINE_STARTING_COMMAND % (" ".join(machine_list[zone]), zone)), - shell=True, - ) - LOGGER.info("GCP machines successfully started.") + arglist.append((MACHINE_STARTING_COMMAND, machine_list, zone)) + with Pool(2) as pool: + pool.map(run_gcp_command, arglist) + LOGGER.info("GCP machines successfully started.") except Exception as e: LOGGER.error("GCP Handler failed to start GCP machines: %s" % e) def stop_machines(machine_list): try: + arglist = [] for zone in machine_list: - subprocess.call( # noqa: DUO116 - (MACHINE_STOPPING_COMMAND % (" ".join(machine_list[zone]), zone)), - shell=True, - ) - LOGGER.info("GCP machines stopped successfully.") + arglist.append((MACHINE_STOPPING_COMMAND, machine_list, zone)) + with Pool(2) as pool: + pool.map(run_gcp_command, arglist) + LOGGER.info("GCP machines stopped successfully.") except Exception as e: LOGGER.error("GCP Handler failed to stop network machines: %s" % e) @@ -72,3 +73,11 @@ def get_auth_command(key_path): def get_set_project_command(project): return SET_PROPERTY_PROJECT % project + + +def run_gcp_command(arglist): + gcp_cmd, machine_list, zone = arglist + subprocess.call( # noqa DUO116 + (gcp_cmd % (" ".join(machine_list[zone]), zone)), + shell=True, + )