<% # This template can be configure the following way with environment variables # Environment variables to filter services/instances # OUTPUT_FORMAT: `JSON | YAML` to output to YAML or JSON # SERVICES_TAG_FILTER: basic tag filter for service (default HTTP) # INSTANCE_MUST_TAG: Second level of filtering (optional, default to SERVICES_TAG_FILTER) # INSTANCE_EXCLUDE_TAG: Exclude instances having the given tag (default: canary) # EXCLUDE_SERVICES: comma-separated services regexps to exclude (default: lbl7.*,netsvc-probe.*,consul-probed.*) service_tag_filter = ENV['SERVICES_TAG_FILTER'] || 'http' instance_must_tag = ENV['INSTANCE_MUST_TAG'] || service_tag_filter instance_exclude_tag = ENV['INSTANCE_EXCLUDE_TAG'] || 'canary' # Services to hide services_blacklist_raw = (ENV['EXCLUDE_SERVICES'] || 'lbl7.*,netsvc-probe.*,consul-probed.*').split(',') services_blacklist = services_blacklist_raw.map { |v| Regexp.new(v) } # Compute the health of a Service # Compute the health of a Service backends = {} services(tag: service_tag_filter).each do |service_name, tags| if !services_blacklist.any? {|r| r.match(service_name)} && tags.include?(instance_must_tag) the_backends = [] service(service_name, tag:'http').sort {|a,b| a['Node']['Node'] <=> b['Node']['Node'] }.each do |snode| tags_of_instance = snode['Service']['Tags'] if tags_of_instance.include?(instance_must_tag) && !tags_of_instance.include?(instance_exclude_tag) the_backends << snode if snode['Service']['Port'] end end # We add the backend ONLY if at least one valid instance does exists backends[service_name] = the_backends end end %><% json_backends = {} backends.each_pair do |service_name, nodes| service = { name: service_name, count: nodes.count, instances: [] } json_backends[service_name] = service nodes.each do |snode| server = { frontend_id: "backend_http__#{service_name}", id: snode['Service']['ID'], addr: snode.service_address, port: snode['Service']['Port'], tags: snode['Service']['Tags'], weights:snode.weights, } service[:instances] << server end end json = { services: json_backends} %><%= case (ENV['OUTPUT_FORMAT'] || 'JSON').upcase when 'JSON' JSON.pretty_generate(json) when 'YAML', 'YML' YAML.dump(json) else raise "Cannot serialize to #{ENV['OUTPUT_FORMAT']}" end %>