From 6edcb30cbe088fa4b6b07131d3887726c6e6cf37 Mon Sep 17 00:00:00 2001 From: vktrrdk Date: Thu, 15 Aug 2024 11:42:13 +0000 Subject: [PATCH 1/2] fix(Security Groups): Locking mechanisms for checking security groups project wise --- .../openstack_connector/openstack_connector.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index 6b6895a..8bc6a5c 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -6,6 +6,7 @@ import sys import urllib import urllib.parse +import threading from contextlib import closing from typing import Union from uuid import uuid4 @@ -54,6 +55,9 @@ ALL_TEMPLATES = [BIOCONDA] +lock_dict = {} +lock_access = threading.Lock() + class OpenStackConnector: def __init__(self, config_file: str): @@ -1034,7 +1038,14 @@ def get_or_create_vm_security_group(self, openstack_id): def get_or_create_project_security_group(self, project_name, project_id): security_group_name = f"{project_name}_{project_id}" - logger.info( + + with lock_access: + if security_group_name not in lock_dict: + lock_dict[security_group_name] = threading.Lock() + lock = lock_dict[security_group_name] + + with lock: + logger.info( f"Check if Security Group for project - [{project_name}-{project_id}] exists... " ) sec = self.openstack_connection.get_security_group( @@ -1061,6 +1072,7 @@ def get_or_create_project_security_group(self, project_name, project_id): remote_group_id=new_security_group["id"], ) return new_security_group["id"] + def get_limits(self) -> dict[str, str]: logger.info("Get Limits") From eeff645a192a2bc6d4c84fd756623cc2cac75052 Mon Sep 17 00:00:00 2001 From: vktrrdk Date: Thu, 15 Aug 2024 14:56:27 +0000 Subject: [PATCH 2/2] wrong indent --- .../openstack_connector.py | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index 8bc6a5c..297fe26 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -1046,32 +1046,32 @@ def get_or_create_project_security_group(self, project_name, project_id): with lock: logger.info( - f"Check if Security Group for project - [{project_name}-{project_id}] exists... " - ) - sec = self.openstack_connection.get_security_group( - name_or_id=security_group_name - ) - if sec: - logger.info( - f"Security group [{project_name}-{project_id}] already exists." + f"Check if Security Group for project - [{project_name}-{project_id}] exists... " ) - return sec["id"] + sec = self.openstack_connection.get_security_group( + name_or_id=security_group_name + ) + if sec: + logger.info( + f"Security group [{project_name}-{project_id}] already exists." + ) + return sec["id"] - logger.info( - f"No security Group for [{project_name}-{project_id}] exists. Creating.. " - ) - new_security_group = self.openstack_connection.create_security_group( - name=security_group_name, description=f"{project_name} Security Group" - ) - self.openstack_connection.network.create_security_group_rule( - direction="ingress", - protocol="tcp", - port_range_max=22, - port_range_min=22, - security_group_id=new_security_group["id"], - remote_group_id=new_security_group["id"], - ) - return new_security_group["id"] + logger.info( + f"No security Group for [{project_name}-{project_id}] exists. Creating.. " + ) + new_security_group = self.openstack_connection.create_security_group( + name=security_group_name, description=f"{project_name} Security Group" + ) + self.openstack_connection.network.create_security_group_rule( + direction="ingress", + protocol="tcp", + port_range_max=22, + port_range_min=22, + security_group_id=new_security_group["id"], + remote_group_id=new_security_group["id"], + ) + return new_security_group["id"] def get_limits(self) -> dict[str, str]: