Skip to content

Commit f913a02

Browse files
committed
Fix #1512
1 parent 23a1c02 commit f913a02

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

IM/connectors/Lambda.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
import threading
1718
import os
1819
import re
1920
import yaml
@@ -36,6 +37,8 @@ class LambdaCloudConnector(CloudConnector):
3637
Cloud Launcher to create Lambda functions.
3738
"""
3839

40+
_lock = threading.Lock()
41+
"""Threading Lock to avoid concurrency problems."""
3942
type = "Lambda"
4043
"""str with the name of the provider."""
4144

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

101104
@staticmethod
102105
def _free_scar_env():
103-
if 'AWS_ACCESS_KEY_ID' in os.environ:
104-
del os.environ["AWS_ACCESS_KEY_ID"]
105-
if 'AWS_SECRET_ACCESS_KEY' in os.environ:
106-
del os.environ["AWS_SECRET_ACCESS_KEY"]
106+
del os.environ["AWS_ACCESS_KEY_ID"]
107+
del os.environ["AWS_SECRET_ACCESS_KEY"]
107108
os.unlink(os.environ['SCAR_TMP_CFG'])
108-
if 'SCAR_TMP_CFG' in os.environ:
109-
if os.path.exists(os.environ['SCAR_TMP_CFG']):
110-
os.unlink(os.environ['SCAR_TMP_CFG'])
111-
del os.environ['SCAR_TMP_CFG']
109+
del os.environ['SCAR_TMP_CFG']
112110

113111
@staticmethod
114112
def _get_region_from_image(image_url):
@@ -178,9 +176,10 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
178176
inf.add_vm(vm)
179177

180178
try:
181-
self._set_scar_env(radl.systems[0], auth_data)
182-
AWS("init")
183-
self._free_scar_env()
179+
with LambdaCloudConnector._lock:
180+
self._set_scar_env(radl.systems[0], auth_data)
181+
AWS("init")
182+
self._free_scar_env()
184183
vm.destroy = False
185184
vm.state = VirtualMachine.RUNNING
186185
res.append((True, vm))
@@ -196,10 +195,11 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
196195

197196
def finalize(self, vm, last, auth_data):
198197
try:
199-
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
200-
Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
201-
AWS("rm")
202-
self._free_scar_env()
198+
with LambdaCloudConnector._lock:
199+
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
200+
Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
201+
AWS("rm")
202+
self._free_scar_env()
203203
except ClientError as ce:
204204
# Function not found
205205
if ce.response['Error']['Code'] == 'ResourceNotFoundException':
@@ -229,10 +229,11 @@ def update_system_info_from_function_conf(system, func_conf):
229229

230230
def updateVMInfo(self, vm, auth_data):
231231
try:
232-
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
233-
func_conf = Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
234-
self.update_system_info_from_function_conf(vm.info.systems[0], func_conf)
235-
self._free_scar_env()
232+
with LambdaCloudConnector._lock:
233+
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
234+
func_conf = Lambda(aws_resources["functions"]["aws"][0]).get_function_configuration(vm.id)
235+
self.update_system_info_from_function_conf(vm.info.systems[0], func_conf)
236+
self._free_scar_env()
236237
return True, vm
237238
except ClientError as ce:
238239
# Function not found
@@ -255,11 +256,12 @@ def alterVM(self, vm, radl, auth_data):
255256

256257
if new_memory and new_memory != memory:
257258
try:
258-
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
259-
Lambda(aws_resources["functions"]["aws"][0]).client.update_function_configuration(
260-
MemorySize=new_memory, FunctionName=vm.id)
261-
self.update_system_info_from_function_conf(vm.info.systems[0], {"MemorySize": new_memory})
262-
self._free_scar_env()
259+
with LambdaCloudConnector._lock:
260+
aws_resources = self._set_scar_env(vm.info.systems[0], auth_data)
261+
Lambda(aws_resources["functions"]["aws"][0]).client.update_function_configuration(
262+
MemorySize=new_memory, FunctionName=vm.id)
263+
self.update_system_info_from_function_conf(vm.info.systems[0], {"MemorySize": new_memory})
264+
self._free_scar_env()
263265
return True, vm
264266
except ClientError as ce:
265267
# Function not found

0 commit comments

Comments
 (0)