Skip to content

Commit

Permalink
saves more info in .mem file
Browse files Browse the repository at this point in the history
  • Loading branch information
XaverStiensmeier committed Nov 13, 2024
1 parent 244a730 commit 3c80f1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions bibigrid/core/actions/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def get_identifier(identifier, cluster_id, additional=""):
VPN_WORKER_IDENTIFIER = partial(get_identifier, identifier="vpngtw")

KEY_PREFIX = "tempKey_bibi"
KEY_FOLDER = os.path.expanduser("~/.config/bibigrid/keys/")
CONFIG_FOLDER = os.path.expanduser("~/.config/bibigrid/")
KEY_FOLDER = os.path.join(CONFIG_FOLDER, "keys/")
AC_NAME = "ac" + SEPARATOR + "{cluster_id}"
KEY_NAME = KEY_PREFIX + SEPARATOR + "{cluster_id}"
CLUSTER_MEMORY_FOLDER = KEY_FOLDER
CLUSTER_MEMORY_FILE = ".bibigrid.mem"
CLUSTER_MEMORY_PATH = os.path.join(CLUSTER_MEMORY_FOLDER, CLUSTER_MEMORY_FILE)
CLUSTER_MEMORY_PATH = os.path.join(CONFIG_FOLDER, CLUSTER_MEMORY_FILE)
DEFAULT_SECURITY_GROUP_NAME = "default" + SEPARATOR + "{cluster_id}"
WIREGUARD_SECURITY_GROUP_NAME = "wireguard" + SEPARATOR + "{cluster_id}"

Expand All @@ -64,7 +65,8 @@ class Create: # pylint: disable=too-many-instance-attributes,too-many-arguments
The class Create holds necessary methods to execute the Create-Action
"""

def __init__(self, providers, configurations, config_path, log, debug=False, # pylint: disable=too-many-positional-arguments
def __init__(self, providers, configurations, config_path, log, debug=False,
# pylint: disable=too-many-positional-arguments
cluster_id=None):
"""
Additionally sets (unique) cluster_id, public_key_commands (to copy public keys to master) and key_name.
Expand Down Expand Up @@ -96,7 +98,7 @@ def __init__(self, providers, configurations, config_path, log, debug=False, #
# permanents holds groups or single nodes that ansible playbook should be run for during startup
self.permanents = ["vpn"]
self.vpn_counter = 0
self.vpn_master_thread_lock = threading.Lock()
self.vpn_counter_thread_lock = threading.Lock()
self.worker_thread_lock = threading.Lock()
self.use_master_with_public_ip = not configurations[0].get("gateway") and configurations[0].get(
"useMasterWithPublicIp", True)
Expand Down Expand Up @@ -197,12 +199,12 @@ def start_vpn_or_master(self, configuration, provider): # pylint: disable=too-m
"""
identifier, instance = self.prepare_vpn_or_master_args(configuration)
external_network = provider.get_external_network(configuration["network"])
with self.vpn_master_thread_lock:
if identifier == MASTER_IDENTIFIER: # pylint: disable=comparison-with-callable
name = identifier(cluster_id=self.cluster_id)
else:
name = identifier(cluster_id=self.cluster_id, # pylint: disable=redundant-keyword-arg
additional=self.vpn_counter) # pylint: disable=redundant-keyword-arg
if identifier == MASTER_IDENTIFIER: # pylint: disable=comparison-with-callable
name = identifier(cluster_id=self.cluster_id)
else:
name = identifier(cluster_id=self.cluster_id, # pylint: disable=redundant-keyword-arg
additional=self.vpn_counter) # pylint: disable=redundant-keyword-arg
with self.vpn_counter_thread_lock:
self.vpn_counter += 1
self.log.info(f"Starting server {name} on {provider.cloud_specification['identifier']}")
flavor = instance["type"]
Expand Down Expand Up @@ -239,6 +241,11 @@ def start_vpn_or_master(self, configuration, provider): # pylint: disable=too-m
if identifier == VPN_WORKER_IDENTIFIER or (identifier == MASTER_IDENTIFIER and self.use_master_with_public_ip):
configuration["floating_ip"] = \
provider.attach_available_floating_ip(network=external_network, server=server)["floating_ip_address"]
if identifier == MASTER_IDENTIFIER:
with open(CLUSTER_MEMORY_PATH, mode="w+", encoding="UTF-8") as cluster_memory_file:
yaml.safe_dump(
data={"cluster_id": self.cluster_id, "floating_ip": configuration["floating_ip"], "name": name},
stream=cluster_memory_file)
self.log.debug(f"Added floating ip {configuration['floating_ip']} to {name}.")
elif identifier == MASTER_IDENTIFIER:
configuration["floating_ip"] = server["private_v4"] # pylint: enable=comparison-with-callable
Expand Down
2 changes: 2 additions & 0 deletions bibigrid/core/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ def get_cluster_id_from_mem():
@return: cluster_id. If no mem file can be found, the file is not a valid yaml file or doesn't contain a cluster_id,
it returns none.
"""
print(create.CLUSTER_MEMORY_PATH)
if os.path.isfile(create.CLUSTER_MEMORY_PATH):
try:
with open(create.CLUSTER_MEMORY_PATH, mode="r", encoding="UTF-8") as cluster_memory_file:
mem_dict = yaml.safe_load(stream=cluster_memory_file)
return mem_dict.get("cluster_id")
except yaml.YAMLError as exc:
LOG.warning("Couldn't read configuration %s: %s", create.CLUSTER_MEMORY_PATH, exc)
LOG.warning(f"Couldn't find cluster memory path {create.CLUSTER_MEMORY_PATH}")
return None


Expand Down

0 comments on commit 3c80f1e

Please sign in to comment.