Skip to content

Commit

Permalink
Fix #1512
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Oct 24, 2023
1 parent 23a1c02 commit f913a02
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions IM/connectors/Lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import threading
import os
import re
import yaml
Expand All @@ -36,6 +37,8 @@ class LambdaCloudConnector(CloudConnector):
Cloud Launcher to create Lambda functions.
"""

_lock = threading.Lock()
"""Threading Lock to avoid concurrency problems."""
type = "Lambda"
"""str with the name of the provider."""

Expand Down Expand Up @@ -100,15 +103,10 @@ def _set_scar_env(self, system, auth_data, supervisor_version="1.5.4"):

@staticmethod
def _free_scar_env():
if 'AWS_ACCESS_KEY_ID' in os.environ:
del os.environ["AWS_ACCESS_KEY_ID"]
if 'AWS_SECRET_ACCESS_KEY' in os.environ:
del os.environ["AWS_SECRET_ACCESS_KEY"]
del os.environ["AWS_ACCESS_KEY_ID"]
del os.environ["AWS_SECRET_ACCESS_KEY"]
os.unlink(os.environ['SCAR_TMP_CFG'])
if 'SCAR_TMP_CFG' in os.environ:
if os.path.exists(os.environ['SCAR_TMP_CFG']):
os.unlink(os.environ['SCAR_TMP_CFG'])
del os.environ['SCAR_TMP_CFG']
del os.environ['SCAR_TMP_CFG']

@staticmethod
def _get_region_from_image(image_url):
Expand Down Expand Up @@ -178,9 +176,10 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
inf.add_vm(vm)

try:
self._set_scar_env(radl.systems[0], auth_data)
AWS("init")
self._free_scar_env()
with LambdaCloudConnector._lock:
self._set_scar_env(radl.systems[0], auth_data)
AWS("init")
self._free_scar_env()
vm.destroy = False
vm.state = VirtualMachine.RUNNING
res.append((True, vm))
Expand All @@ -196,10 +195,11 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):

def finalize(self, vm, last, auth_data):
try:
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
AWS("rm")
self._free_scar_env()
with LambdaCloudConnector._lock:
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
AWS("rm")
self._free_scar_env()
except ClientError as ce:
# Function not found
if ce.response['Error']['Code'] == 'ResourceNotFoundException':
Expand Down Expand Up @@ -229,10 +229,11 @@ def update_system_info_from_function_conf(system, func_conf):

def updateVMInfo(self, vm, auth_data):
try:
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
func_conf = Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
self.update_system_info_from_function_conf(vm.info.systems[0], func_conf)
self._free_scar_env()
with LambdaCloudConnector._lock:
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
func_conf = Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
self.update_system_info_from_function_conf(vm.info.systems[0], func_conf)
self._free_scar_env()
return True, vm
except ClientError as ce:
# Function not found
Expand All @@ -255,11 +256,12 @@ def alterVM(self, vm, radl, auth_data):

if new_memory and new_memory != memory:
try:
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
Lambda(aws_resources["functions"]["aws"][0]).client.update_function_configuration(
MemorySize=new_memory, FunctionName=vm.id)
self.update_system_info_from_function_conf(vm.info.systems[0], {"MemorySize": new_memory})
self._free_scar_env()
with LambdaCloudConnector._lock:
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
Lambda(aws_resources["functions"]["aws"][0]).client.update_function_configuration(
MemorySize=new_memory, FunctionName=vm.id)
self.update_system_info_from_function_conf(vm.info.systems[0], {"MemorySize": new_memory})
self._free_scar_env()
return True, vm
except ClientError as ce:
# Function not found
Expand Down

0 comments on commit f913a02

Please sign in to comment.