Class: ConsulSyncer
- Inherits:
-
Object
- Object
- ConsulSyncer
- Defined in:
- lib/consul_syncer.rb,
lib/consul_syncer/version.rb,
lib/consul_syncer/wrapper.rb,
lib/consul_syncer/endpoint.rb
Overview
syncs a given list of endpoints into consul
-
sorts tags
-
adds missing
-
updates changed
-
removes deprecated
Defined Under Namespace
Constant Summary collapse
- VERSION =
"1.2.0"
Instance Method Summary collapse
- #execute(planned) ⇒ Object
-
#initialize(url, logger: Logger.new(STDOUT), params: {}) ⇒ ConsulSyncer
constructor
A new instance of ConsulSyncer.
- #plan(expected_definitions, tags) ⇒ Object
-
#sync(expected_definitions, tags, dry: false) ⇒ Object
changing tags means all previous services need to be removed manually since they can no longer be found.
Constructor Details
#initialize(url, logger: Logger.new(STDOUT), params: {}) ⇒ ConsulSyncer
Returns a new instance of ConsulSyncer.
13 14 15 16 |
# File 'lib/consul_syncer.rb', line 13 def initialize(url, logger: Logger.new(STDOUT), params: {}) @logger = logger @consul = Wrapper.new(url, params: params, logger: @logger) end |
Instance Method Details
#execute(planned) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/consul_syncer.rb', line 84 def execute(planned) planned.each do |m, args, kwargs| if kwargs send m, *args, **kwargs else send m, *args end end end |
#plan(expected_definitions, tags) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/consul_syncer.rb', line 26 def plan(expected_definitions, ) raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if .empty? # ensure consistent tags to find the endpoints after adding expected_definitions = expected_definitions.dup expected_definitions.each do |d| d[:tags] += d[:tags].sort! d[:tags].uniq! end actual_definitions = consul_endpoints().map do |consul_endpoint| { node: consul_endpoint.node, address: consul_endpoint.ip, service: consul_endpoint.name, service_id: consul_endpoint.service_id, service_address: consul_endpoint.service_address, tags: consul_endpoint..sort, port: consul_endpoint.port } end = [:node, :service_id] interesting = [*, :service, :service_address, :address, :tags, :port] planned = [] expected_definitions.each do |expected| description = "#{expected[:service] || "*"} / #{expected[:service_id] || "*"} on #{expected.fetch(:node)} in Consul" if expected[:keep] = .dup .delete(:service_id) unless expected[:service_id] if remove_matching_service!(actual_definitions, expected, ) @logger.warn "Kept #{description}" else @logger.error "Unable to keep #{description} since it was not found" end elsif remove_matching_service!(actual_definitions, expected, interesting) @logger.debug "Found #{description}" elsif remove_matching_service!(actual_definitions, expected, ) @logger.info "Updating #{description}" planned << [:register, [], expected] else @logger.info "Adding #{description}" planned << [:register, [], expected] end end # all definitions that are left did not match any expected definitions and are no longer needed actual_definitions.each do |actual| @logger.info "Removing #{actual.fetch(:service)} / #{actual.fetch(:service_id)} on #{actual.fetch(:node)} in Consul" planned << [:deregister, [actual.fetch(:node), actual.fetch(:service_id)], nil] end planned end |
#sync(expected_definitions, tags, dry: false) ⇒ Object
changing tags means all previous services need to be removed manually since they can no longer be found
20 21 22 23 24 |
# File 'lib/consul_syncer.rb', line 20 def sync(expected_definitions, , dry: false) planned = plan(expected_definitions, ) execute planned unless dry planned.size # let users know what we did and keep legacy api working end |