def get_logs(logs_start_dt, logs_end_dt): wa_containers = docker_utils.get_wa_containers() log_files = [] errors = [] for wa_container in wa_containers: try: container_log_filename = get_container_logs( wa_container, logs_start_dt, logs_end_dt) log_files.append(container_log_filename) inspect_log_filename = get_container_inspect_logs(wa_container) log_files.append(inspect_log_filename) core_dump_filename = get_corecontainer_coredumps_logs(wa_container) if core_dump_filename is not None: log_files.append(core_dump_filename) webapp_log, webapp_error_log = get_webcontainer_logs(wa_container) if webapp_log is not None and webapp_error_log is not None: log_files.append(webapp_log) log_files.append(webapp_error_log) except Exception as e: print(e) errors.append((wa_container.container, e)) if errors: err_str = "Container: {}\nException: {}" exception_msg = "Some logs could not be obtained:\n{}".format( "\n".join([err_str.format(err[0].name, err) for err in errors])) raise exceptions.LogsNotCompleteError(exception_msg) return [lf for lf in log_files if lf is not None]
def _run(cls, config, *args, **kwargs): all_wa_containers = docker_utils.get_wa_containers() stopped_wa_container_ids = [ wa_container.short_id for wa_container in all_wa_containers if not wa_container.is_running() ] remediation_msg = ( "List all containers on the current host with `docker ps -a` and " "run `docker start #container_id` to start a container. " "If a container stops due to errors, " "refer to messages from other checks to diagnose or contact support " "https://developers.facebook.com/docs/whatsapp/contact-support" ) if len(all_wa_containers) == len(stopped_wa_container_ids): return results.Problem( cls, "No WA container is running", "All WA containers are stopped.", remediation_msg, ) if len(stopped_wa_container_ids) > 0: return results.Warning( cls, "Not all WA containers are running.", f"WA containers with id {', '.join(stopped_wa_container_ids)}" " not running.", f"If this is expected, please ignore. {remediation_msg}", ) return results.OK(cls)
def get_all_running_wa_containers(): wa_containers = docker_utils.get_wa_containers() versions_to_wa_containers_dict = defaultdict( lambda: defaultdict(list)) for wa_container in wa_containers: container = wa_container.container container_type = wa_container.get_container_type() if docker_utils.is_container_running(container): if wa_container.is_webapp() or wa_container.is_coreapp(): version = docker_utils.get_wa_version_from_container( container) versions_to_wa_containers_dict[version][ container_type].append(container) return versions_to_wa_containers_dict
def test_should_only_return_wa_containers(self, mocker): mock_containers = [ MockContainer([docker_utils.WA_COREAPP_CONTAINER_TAG]), MockContainer([docker_utils.WA_WEBAPP_CONTAINER_TAG]), MockContainer(["a random image"]) ] mocker.patch.object( docker_utils, 'get_all_containers', return_value=mock_containers) wa_containers = docker_utils.get_wa_containers() assert len( wa_containers) == 2, 'Expected to get exactly 2 WA containers' assert not mock_containers[2] in wa_containers, 'Non-WA containers should not be returned'
def test_should_only_return_wa_containers(self): mock_containers = [ MockContainer([docker_utils.WA_COREAPP_CONTAINER_TAG]), MockContainer([docker_utils.WA_WEBAPP_CONTAINER_TAG]), MockContainer(["a random image"]), ] with patch.object( docker_utils, "get_all_containers", return_value=mock_containers ): wa_containers = docker_utils.get_wa_containers() assert len(wa_containers) == 2, "Expected to get exactly 2 WA containers" assert ( not mock_containers[2] in wa_containers ), "Non-WA containers should not be returned"
def test_should_only_return_wa_containers(self): mock_containers = [ MockWACoreappContainer(), MockWAWebContainer(), MockDockerContainer(), ] with patch.object( docker_utils, "get_all_containers", return_value=mock_containers ): wa_containers = docker_utils.get_wa_containers() assert len(wa_containers) == 2, "Expected to get exactly 2 WA containers" assert ( not mock_containers[2] in wa_containers ), "Non-WA containers should not be returned"
def get_all_running_wa_containers(): wa_containers = docker_utils.get_wa_containers() versions_to_wa_containers_dict = defaultdict(lambda: defaultdict(list)) for wa_container in wa_containers: container = wa_container.container container_type = wa_container.container_type if docker_utils.is_container_running(container): if ( docker_utils.WA_WEBAPP_CONTAINER_TAG == container_type or docker_utils.WA_COREAPP_CONTAINER_TAG == container_type ): version = docker_utils.get_wa_version_from_container(container) versions_to_wa_containers_dict[version][container_type].append( container ) return versions_to_wa_containers_dict