14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
17
+ import threading
17
18
import os
18
19
import re
19
20
import yaml
@@ -36,6 +37,8 @@ class LambdaCloudConnector(CloudConnector):
36
37
Cloud Launcher to create Lambda functions.
37
38
"""
38
39
40
+ _lock = threading .Lock ()
41
+ """Threading Lock to avoid concurrency problems."""
39
42
type = "Lambda"
40
43
"""str with the name of the provider."""
41
44
@@ -100,15 +103,10 @@ def _set_scar_env(self, system, auth_data, supervisor_version="1.5.4"):
100
103
101
104
@staticmethod
102
105
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" ]
107
108
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' ]
112
110
113
111
@staticmethod
114
112
def _get_region_from_image (image_url ):
@@ -178,9 +176,10 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
178
176
inf .add_vm (vm )
179
177
180
178
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 ()
184
183
vm .destroy = False
185
184
vm .state = VirtualMachine .RUNNING
186
185
res .append ((True , vm ))
@@ -196,10 +195,11 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
196
195
197
196
def finalize (self , vm , last , auth_data ):
198
197
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 ()
203
203
except ClientError as ce :
204
204
# Function not found
205
205
if ce .response ['Error' ]['Code' ] == 'ResourceNotFoundException' :
@@ -229,10 +229,11 @@ def update_system_info_from_function_conf(system, func_conf):
229
229
230
230
def updateVMInfo (self , vm , auth_data ):
231
231
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 ()
236
237
return True , vm
237
238
except ClientError as ce :
238
239
# Function not found
@@ -255,11 +256,12 @@ def alterVM(self, vm, radl, auth_data):
255
256
256
257
if new_memory and new_memory != memory :
257
258
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 ()
263
265
return True , vm
264
266
except ClientError as ce :
265
267
# Function not found
0 commit comments