Best Python code snippet using localstack_python
lambda_executors.py
Source:lambda_executors.py  
...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....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!!
