Best Python code snippet using localstack_python
lambda_executors.py
Source:lambda_executors.py
...149 return cmd150 def startup(self):151 self.cleanup()152 # start a process to remove idle containers153 self.start_idle_container_destroyer_interval()154 def cleanup(self, arn=None):155 if arn:156 self.function_invoke_times.pop(arn, None)157 return self.destroy_docker_container(arn)158 self.function_invoke_times = {}159 return self.destroy_existing_docker_containers()160 def prime_docker_container(self, runtime, func_arn, env_vars, lambda_cwd):161 """162 Prepares a persistent docker container for a specific function.163 :param runtime: Lamda runtime environment. python2.7, nodejs6.10, etc.164 :param func_arn: The ARN of the lambda function.165 :param env_vars: The environment variables for the lambda.166 :param lambda_cwd: The local directory containing the code for the lambda function.167 :return: ContainerInfo class containing the container name and default entry point.168 """169 with self.docker_container_lock:170 # Get the container name and id.171 container_name = self.get_container_name(func_arn)172 LOG.debug('Priming docker container: %s' % container_name)173 status = self.get_docker_container_status(func_arn)174 # Container is not running or doesn't exist.175 if status < 1:176 # Make sure the container does not exist in any form/state.177 self.destroy_docker_container(func_arn)178 env_vars_str = ' '.join(['-e {}={}'.format(k, cmd_quote(v)) for (k, v) in env_vars])179 # Create and start the container180 LOG.debug('Creating container: %s' % container_name)181 cmd = (182 'docker create'183 ' --name "%s"'184 ' --entrypoint /bin/bash' # Load bash when it starts.185 ' --interactive' # Keeps the container running bash.186 ' -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY"'187 ' -e HOSTNAME="$HOSTNAME"'188 ' -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME"'189 ' %s' # env_vars190 ' lambci/lambda:%s'191 ) % (container_name, env_vars_str, runtime)192 LOG.debug(cmd)193 run(cmd, stderr=subprocess.PIPE, outfile=subprocess.PIPE)194 LOG.debug('Copying files to container "%s" from "%s".' % (container_name, lambda_cwd))195 cmd = (196 'docker cp'197 ' "%s/." "%s:/var/task"'198 ) % (lambda_cwd, container_name)199 LOG.debug(cmd)200 run(cmd, stderr=subprocess.PIPE, outfile=subprocess.PIPE)201 LOG.debug('Starting container: %s' % container_name)202 cmd = 'docker start %s' % (container_name)203 LOG.debug(cmd)204 run(cmd, stderr=subprocess.PIPE, outfile=subprocess.PIPE)205 # give the container some time to start up206 time.sleep(1)207 # Get the entry point for the image.208 LOG.debug('Getting the entrypoint for image: lambci/lambda:%s' % runtime)209 cmd = (210 'docker image inspect'211 ' --format="{{ .ContainerConfig.Entrypoint }}"'212 ' lambci/lambda:%s'213 ) % (runtime)214 LOG.debug(cmd)215 run_result = run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)216 entry_point = run_result.strip('[]\n\r ')217 LOG.debug('Using entrypoint "%s" for container "%s".' % (entry_point, container_name))218 return ContainerInfo(container_name, entry_point)219 def destroy_docker_container(self, func_arn):220 """221 Stops and/or removes a docker container for a specific lambda function ARN.222 :param func_arn: The ARN of the lambda function.223 :return: None224 """225 with self.docker_container_lock:226 status = self.get_docker_container_status(func_arn)227 # Get the container name and id.228 container_name = self.get_container_name(func_arn)229 if status == 1:230 LOG.debug('Stopping container: %s' % container_name)231 cmd = (232 'docker stop -t0 %s'233 ) % (container_name)234 LOG.debug(cmd)235 run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)236 status = self.get_docker_container_status(func_arn)237 if status == -1:238 LOG.debug('Removing container: %s' % container_name)239 cmd = (240 'docker rm %s'241 ) % (container_name)242 LOG.debug(cmd)243 run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)244 def get_all_container_names(self):245 """246 Returns a list of container names for lambda containers.247 :return: A String[] localstack docker container names for each function.248 """249 with self.docker_container_lock:250 LOG.debug('Getting all lambda containers names.')251 cmd = 'docker ps -a --filter="name=localstack_lambda_*" --format "{{.Names}}"'252 LOG.debug(cmd)253 cmd_result = run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE).strip()254 if len(cmd_result) > 0:255 container_names = cmd_result.split('\n')256 else:257 container_names = []258 return container_names259 def destroy_existing_docker_containers(self):260 """261 Stops and/or removes all lambda docker containers for localstack.262 :return: None263 """264 with self.docker_container_lock:265 container_names = self.get_all_container_names()266 LOG.debug('Removing %d containers.' % len(container_names))267 for container_name in container_names:268 cmd = 'docker rm -f %s' % container_name269 LOG.debug(cmd)270 run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)271 def get_docker_container_status(self, func_arn):272 """273 Determine the status of a docker container.274 :param func_arn: The ARN of the lambda function.275 :return: 1 If the container is running,276 -1 if the container exists but is not running277 0 if the container does not exist.278 """279 with self.docker_container_lock:280 # Get the container name and id.281 container_name = self.get_container_name(func_arn)282 # Check if the container is already running.283 LOG.debug('Getting container status: %s' % container_name)284 cmd = (285 'docker ps'286 ' -a'287 ' --filter name="%s"'288 ' --format "{{ .Status }}"'289 ) % (container_name)290 LOG.debug(cmd)291 cmd_result = run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)292 # If the container doesn't exist. Create and start it.293 container_status = cmd_result.strip()294 if len(container_status) == 0:295 return 0296 if container_status.lower().startswith('up '):297 return 1298 return -1299 def idle_container_destroyer(self):300 """301 Iterates though all the lambda containers and destroys any container that has302 been inactive for longer than MAX_CONTAINER_IDLE_TIME.303 :return: None304 """305 LOG.info('Checking if there are idle containers.')306 current_time = time.time()307 for func_arn, last_run_time in self.function_invoke_times.items():308 duration = current_time - last_run_time309 # not enough idle time has passed310 if duration < MAX_CONTAINER_IDLE_TIME:311 continue312 # container has been idle, destroy it.313 self.destroy_docker_container(func_arn)314 def start_idle_container_destroyer_interval(self):315 """316 Starts a repeating timer that triggers start_idle_container_destroyer_interval every 60 seconds.317 Thus checking for idle containers and destroying them.318 :return: None319 """320 self.idle_container_destroyer()321 threading.Timer(60.0, self.start_idle_container_destroyer_interval).start()322 def get_container_name(self, func_arn):323 """324 Given a function ARN, returns a valid docker container name.325 :param func_arn: The ARN of the lambda function.326 :return: A docker compatible name for the arn.327 """328 return 'localstack_lambda_' + re.sub(r'[^a-zA-Z0-9_.-]', '_', func_arn)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!