monkey/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py

77 lines
2.8 KiB
Python
Raw Normal View History

2019-10-01 15:42:51 +08:00
import logging
import os
import subprocess
2019-10-01 15:42:51 +08:00
2019-10-01 21:11:53 +08:00
LOGGER = logging.getLogger(__name__)
2019-08-27 23:12:14 +08:00
class GCPHandler(object):
AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s"
2019-08-28 19:56:35 +08:00
SET_PROPERTY_PROJECT = "gcloud config set project %s"
2019-08-27 23:12:14 +08:00
MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s"
MACHINE_STOPPING_COMMAND = "gcloud compute instances stop %s --zone=%s"
# Key path location relative to this file's directory
RELATIVE_KEY_PATH = "../../gcp_keys/gcp_key.json"
DEFAULT_ZONE = "europe-west3-a"
DEFAULT_PROJECT = "guardicore-22050661"
2021-04-06 21:19:27 +08:00
def __init__(
self,
zone=DEFAULT_ZONE,
project_id=DEFAULT_PROJECT,
2021-04-06 21:19:27 +08:00
):
2019-08-27 23:12:14 +08:00
self.zone = zone
abs_key_path = GCPHandler.get_absolute_key_path()
subprocess.call(GCPHandler.get_auth_command(abs_key_path), shell=True) # noqa: DUO116
LOGGER.info("GCP Handler passed key")
subprocess.call(GCPHandler.get_set_project_command(project_id), shell=True) # noqa: DUO116
LOGGER.info("GCP Handler set project")
LOGGER.info("GCP Handler initialized successfully")
@staticmethod
def get_absolute_key_path() -> str:
file_dir = os.path.dirname(os.path.realpath(__file__))
absolute_key_path = os.path.join(file_dir, GCPHandler.RELATIVE_KEY_PATH)
absolute_key_path = os.path.realpath(absolute_key_path)
if not os.path.isfile(absolute_key_path):
raise FileNotFoundError(
"GCP key not found. " "Add a service key to envs/monkey_zoo/gcp_keys/gcp_key.json"
)
return absolute_key_path
2019-08-27 23:12:14 +08:00
def start_machines(self, machine_list):
"""
Start all the machines in the list.
:param machine_list: A space-separated string with all the machine names. Example:
start_machines(`" ".join(["elastic-3", "mssql-16"])`)
"""
2019-10-01 21:11:53 +08:00
LOGGER.info("Setting up all GCP machines...")
2019-08-27 23:12:14 +08:00
try:
subprocess.call( # noqa: DUO116
2021-04-06 21:19:27 +08:00
(GCPHandler.MACHINE_STARTING_COMMAND % (machine_list, self.zone)), shell=True
)
2019-10-01 21:11:53 +08:00
LOGGER.info("GCP machines successfully started.")
2019-08-27 23:12:14 +08:00
except Exception as e:
2019-10-01 21:11:53 +08:00
LOGGER.error("GCP Handler failed to start GCP machines: %s" % e)
2019-08-27 23:12:14 +08:00
def stop_machines(self, machine_list):
try:
subprocess.call( # noqa: DUO116
2021-04-06 21:19:27 +08:00
(GCPHandler.MACHINE_STOPPING_COMMAND % (machine_list, self.zone)), shell=True
)
2019-10-01 21:11:53 +08:00
LOGGER.info("GCP machines stopped successfully.")
2019-08-27 23:12:14 +08:00
except Exception as e:
2019-10-01 21:11:53 +08:00
LOGGER.error("GCP Handler failed to stop network machines: %s" % e)
2019-08-27 23:12:14 +08:00
@staticmethod
def get_auth_command(key_path):
return GCPHandler.AUTHENTICATION_COMMAND % key_path
2019-08-28 19:56:35 +08:00
@staticmethod
def get_set_project_command(project):
return GCPHandler.SET_PROPERTY_PROJECT % project