Best Python code snippet using localstack_python
lambda_executors.py
Source:lambda_executors.py  
...273            ) % (runtime)274            LOG.debug(cmd)275            run_result = run(cmd)276            entry_point = run_result.strip('[]\n\r ')277            container_network = self.get_docker_container_network(func_arn)278            LOG.debug('Using entrypoint "%s" for container "%s" on network "%s".'279                % (entry_point, container_name, container_network))280            return ContainerInfo(container_name, entry_point)281    def destroy_docker_container(self, func_arn):282        """283        Stops and/or removes a docker container for a specific lambda function ARN.284        :param func_arn: The ARN of the lambda function.285        :return: None286        """287        with self.docker_container_lock:288            status = self.get_docker_container_status(func_arn)289            # Get the container name and id.290            container_name = self.get_container_name(func_arn)291            if status == 1:292                LOG.debug('Stopping container: %s' % container_name)293                cmd = (294                    'docker stop -t0 %s'295                ) % (container_name)296                LOG.debug(cmd)297                run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)298                status = self.get_docker_container_status(func_arn)299            if status == -1:300                LOG.debug('Removing container: %s' % container_name)301                cmd = (302                    'docker rm %s'303                ) % (container_name)304                LOG.debug(cmd)305                run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)306    def get_all_container_names(self):307        """308        Returns a list of container names for lambda containers.309        :return: A String[] localstack docker container names for each function.310        """311        with self.docker_container_lock:312            LOG.debug('Getting all lambda containers names.')313            cmd = 'docker ps -a --filter="name=localstack_lambda_*" --format "{{.Names}}"'314            LOG.debug(cmd)315            cmd_result = run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE).strip()316            if len(cmd_result) > 0:317                container_names = cmd_result.split('\n')318            else:319                container_names = []320            return container_names321    def destroy_existing_docker_containers(self):322        """323        Stops and/or removes all lambda docker containers for localstack.324        :return: None325        """326        with self.docker_container_lock:327            container_names = self.get_all_container_names()328            LOG.debug('Removing %d containers.' % len(container_names))329            for container_name in container_names:330                cmd = 'docker rm -f %s' % container_name331                LOG.debug(cmd)332                run(cmd, asynchronous=False, stderr=subprocess.PIPE, outfile=subprocess.PIPE)333    def get_docker_container_status(self, func_arn):334        """335        Determine the status of a docker container.336        :param func_arn: The ARN of the lambda function.337        :return: 1 If the container is running,338        -1 if the container exists but is not running339        0 if the container does not exist.340        """341        with self.docker_container_lock:342            # Get the container name and id.343            container_name = self.get_container_name(func_arn)344            # Check if the container is already running345            # Note: filtering by *exact* name using regex filter '^...$' seems unstable on some346            # systems. Therefore, we use a combination of filter and grep to get the results.347            cmd = ('docker ps -a --filter name=\'%s\' '348                   '--format "{{ .Status }} - {{ .Names }}" '349                   '| grep -w "%s" | cat') % (container_name, container_name)350            LOG.debug('Getting status for container "%s": %s' % (container_name, cmd))351            cmd_result = run(cmd)352            # If the container doesn't exist. Create and start it.353            container_status = cmd_result.strip()354            if len(container_status) == 0:355                return 0356            if container_status.lower().startswith('up '):357                return 1358            return -1359    def get_docker_container_network(self, func_arn):360        """361        Determine the network of a docker container.362        :param func_arn: The ARN of the lambda function.363        :return: name of the container network364        """365        with self.docker_container_lock:366            status = self.get_docker_container_status(func_arn)367            # container does not exist368            if status == 0:369                return ''370            # Get the container name.371            container_name = self.get_container_name(func_arn)372            # Get the container network373            LOG.debug('Getting container network: %s' % container_name)...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!!
