def get_service_list()

in deploy/docker/cp-edge/sync-routes.py [0:0]


def get_service_list(active_runs_list, pod_id, pod_run_id, pod_ip):
        service_list = {}
        run_cache = [cached_run for cached_run in active_runs_list if str(cached_run['pipelineRun']['id']) == str(pod_run_id)]
        run_cache = next(iter(run_cache), None)
        if not run_cache:
                do_log('Cannot find the RunID {} in the list of cached runs, skipping'.format(pod_run_id))
                return {}

        run_info = run_cache['pipelineRun']
        if run_info:
                if run_info.get("status") != 'RUNNING':
                        do_log('Status for pipeline with id: {}, is not RUNNING. Service urls will not be proxied'.format(pod_run_id))
                        return {}
                if 'pipelineRunParameters' in run_info:
                        edge_endpoint_tag_name = [rp for rp in run_info["pipelineRunParameters"]
                                                if 'name' in rp and rp["name"] == CP_EDGE_ENDPOINT_TAG_NAME]
                        if edge_endpoint_tag_name and len(edge_endpoint_tag_name) > 0:
                                run_info_tags = run_info.get("tags")
                                if not (run_info_tags and "value" in edge_endpoint_tag_name[0] and run_info_tags.get(edge_endpoint_tag_name[0]["value"])):
                                        do_log('Pipeline with id {} and run tag {} has not yet been initialized. '
                                        'Service urls will not be proxied'
                                        .format(pod_run_id, edge_endpoint_tag_name[0]["value"]))
                                        return {}
                pod_owner = run_info["owner"]
                docker_image = run_info["dockerImage"]
                runs_sids = run_info.get("runSids")
                pretty_url = run_info.get("prettyUrl")
                pretty_url = parse_pretty_url(pretty_url) if pretty_url else None
                sensitive = run_info.get("sensitive") or False

                cloud_region_id = run_info.get("instance", {}).get("cloudRegionId") or None
                instance_ip = run_info.get("instance", {}).get("nodeIP") or None

                do_log('Processing {} #{} by {} ({})...'.format(pod_id, pod_run_id, pod_owner, docker_image))

                shared_users_sids = run_sids_to_str(runs_sids, True)
                if shared_users_sids:
                        do_log('Detected shared user sids: {}'.format(shared_users_sids))

                shared_groups_sids = run_sids_to_str(runs_sids, False)
                if shared_groups_sids:
                        do_log('Detected shared group sids: {}'.format(shared_groups_sids))

                endpoints_data = run_cache.get('tool', {}).get('endpoints') or []
                tool_endpoints_count = len(endpoints_data)
                do_log('Detected {} tool settings endpoints.'.format(tool_endpoints_count))

                endpoints_data, overridden_endpoints_count = append_additional_endpoints(endpoints_data, run_info)
                additional_system_endpoints_count = len(endpoints_data) - tool_endpoints_count
                do_log('Detected {} run parameters endpoints.'.format(additional_system_endpoints_count))
                if overridden_endpoints_count:
                        do_log('Detected {} overridden tool settings endpoints.'.format(overridden_endpoints_count))

                if endpoints_data:
                        endpoints_count = len(endpoints_data)
                        for i in range(endpoints_count):
                                endpoint = None
                                try:
                                        endpoint = json.loads(endpoints_data[i])
                                except Exception as endpoint_parse_exception:
                                        do_log('Parsing endpoint #{} failed:\n{}'.format(str(i), str(endpoint_parse_exception)))
                                        continue
                                if endpoint["nginx"]:
                                        port = endpoint["nginx"]["port"]
                                        path = endpoint["nginx"].get("path", "")
                                        service_name = endpoint.get("name", "Default")
                                        is_default_endpoint = endpoint.get("isDefault", False)
                                        is_ssl_backend = endpoint.get("sslBackend", False)
                                        is_same_tab = endpoint.get("sameTab", False)
                                        create_dns_record = endpoint.get("customDNS", False)
                                        additional = endpoint["nginx"].get("additional", "")
                                        has_explicit_endpoint_num = "endpoint_num" in endpoint.keys()
                                        custom_endpoint_num = int(endpoint["endpoint_num"]) if has_explicit_endpoint_num else i
                                        edge_location_id = ROUTE_ID_TMPL.format(pod_id=pod_id, endpoint_port=port, endpoint_num=custom_endpoint_num)
                                        if not pretty_url or (has_explicit_endpoint_num and not is_system_endpoint_name(endpoint)):
                                                edge_location = edge_location_id
                                        else:
                                                pretty_url_path = pretty_url["path"]
                                                if endpoints_count == 1 or (str(is_default_endpoint).lower() == "true" and EDGE_DISABLE_NAME_SUFFIX_FOR_DEFAULT_ENDPOINT):
                                                        edge_location = pretty_url_path
                                                else:
                                                        pretty_url_suffix = endpoint["name"] if "name" in endpoint.keys() else str(custom_endpoint_num)
                                                        if pretty_url_path:
                                                                edge_location = '{}-{}'.format(pretty_url_path, pretty_url_suffix)
                                                        else:
                                                                edge_location = pretty_url_suffix


                                        if (pretty_url and pretty_url['domain']) or create_dns_record:
                                                edge_location_path = edge_location_id + '.inc'
                                        else:
                                                edge_location_path = edge_location_id + '.loc'

                                        if EDGE_INSTANCE_IP in additional:
                                                additional = additional.replace(EDGE_INSTANCE_IP, "")
                                                target_ip = instance_ip
                                        else:
                                                target_ip = pod_ip

                                        edge_target = \
                                                EDGE_ROUTE_TARGET_PATH_TMPL.format(pod_ip=target_ip, endpoint_port=port, endpoint_path=path) \
                                                        if path \
                                                        else EDGE_ROUTE_TARGET_TMPL.format(pod_ip=target_ip, endpoint_port=port)

                                        # If CP_EDGE_NO_PATH_CROP is present (any place) in the "additional" section of the route config
                                        # then trailing "/" is not added to the proxy pass target. This will allow to forward original requests trailing path
                                        if EDGE_ROUTE_NO_PATH_CROP in additional:
                                                additional = additional.replace(EDGE_ROUTE_NO_PATH_CROP, "")
                                        else:
                                                edge_target = edge_target + "/"

                                        if EDGE_COOKIE_NO_REPLACE in additional:
                                                additional = additional.replace(EDGE_COOKIE_NO_REPLACE, "")
                                                edge_cookie_location = "/"
                                        else:
                                                edge_cookie_location = None

                                        #######################################################
                                        # These parameters will be passed to the respective lua auth script
                                        # Only applied for the non-sensitive jobs
                                        edge_jwt_auth = True
                                        if EDGE_JWT_NO_AUTH in additional:
                                                additional = additional.replace(EDGE_JWT_NO_AUTH, "")
                                                edge_jwt_auth = False

                                        edge_pass_bearer = False
                                        if EDGE_PASS_BEARER in additional:
                                                additional = additional.replace(EDGE_PASS_BEARER, "")
                                                edge_pass_bearer = True

                                        #######################################################

                                        is_external_app = False
                                        if EDGE_EXTERNAL_APP in additional:
                                                additional = additional.replace(EDGE_EXTERNAL_APP, "")
                                                is_external_app = True

                                        for default_attribute in DEFAULT_LOCATION_ATTRIBUTES:
                                                if 'search_pattern' not in default_attribute or 'value' not in default_attribute:
                                                        continue
                                                if default_attribute['search_pattern'].lower() not in additional.lower():
                                                        additional = additional + default_attribute['value']

                                        service_list[edge_location_id] = {
                                                "edge_location_path": edge_location_path,
                                                "pod_id": pod_id,
                                                "pod_ip": target_ip,
                                                "pod_owner": pod_owner,
                                                "shared_users_sids": shared_users_sids,
                                                "shared_groups_sids": shared_groups_sids,
                                                "service_name": service_name,
                                                "is_default_endpoint": is_default_endpoint,
                                                "is_ssl_backend": is_ssl_backend,
                                                "is_same_tab": is_same_tab,
                                                "edge_num": i,
                                                "edge_location": edge_location,
                                                "custom_domain": pretty_url.get('domain') if pretty_url else None,
                                                "edge_target": edge_target,
                                                "run_id": pod_run_id,
                                                "additional": additional,
                                                "sensitive": sensitive,
                                                "create_dns_record": create_dns_record,
                                                "cloudRegionId": cloud_region_id,
                                                "external_app": is_external_app,
                                                "cookie_location": edge_cookie_location,
                                                "edge_jwt_auth": edge_jwt_auth,
                                                "edge_pass_bearer": edge_pass_bearer
                                        }
                else:
                        do_log('No endpoints required for the tool {}'.format(docker_image))
        else:
                do_log('Unable to get details of a RunID {} from API due to errors'.format(pod_run_id))
        return service_list