consul_syncer 0.6.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62318b7ff11cb47fbcf16d8517e94680743ccbb2f49601ecf5ac2acd5556d096
4
- data.tar.gz: d0cf1cbd28f2e7bb21d3ead832171ab3bd18f65000f10bac54326be427dac9e1
3
+ metadata.gz: 59b87094f77b404c59b018035020eab02be5075c3b3d9dc3e68f28a64d8f70c0
4
+ data.tar.gz: c61781655c3a8a67e266da1e97578b9e996d7c4b43248c777ef43e35608ba8ff
5
5
  SHA512:
6
- metadata.gz: 300ae25519fc5e00460750324613dc883e0b1b9eec3223a3a2d4f8ac5be5e73414500bebdfc6fed9f70e6fb5e09bef67641c9f8fc52a855f5e830a96edbe1063
7
- data.tar.gz: 6a8310a1fbe7dcf69fdb4f3712aae357f97328faa2056d44245e1b05997ca8ab41849c111aeff9512244c3093342794a1fcba342b6c2b9188ebc3ed1cddbe7dd
6
+ metadata.gz: 4f164174999daa718fe237edbff4a0b1ca3389ab56db5af2ce57cfd7876c20b0d5563de016b649677e0bd14b3d7e7c544c8857a27bb271eae1291fe6a43f02da
7
+ data.tar.gz: bfd88ffae1281a70b942fb6f766fbabcc20f4ec49aca1df311a5cf79e4f6c08dc4dbd13f77110b8f38be4260e3e7a4daba8431d5996279739f475d1d0a71de6f
data/lib/consul_syncer.rb CHANGED
@@ -5,18 +5,19 @@ require 'consul_syncer/endpoint'
5
5
  require 'consul_syncer/wrapper'
6
6
 
7
7
  # syncs a given list of endpoints into consul
8
+ # - sorts tags
8
9
  # - adds missing
9
10
  # - updates changed
10
11
  # - removes deprecated
11
12
  class ConsulSyncer
12
13
  def initialize(url, logger: Logger.new(STDOUT), params: {})
13
14
  @logger = logger
14
- @consul = Wrapper.new(Faraday.new(url), params: params, logger: @logger)
15
+ @consul = Wrapper.new(url, params: params, logger: @logger)
15
16
  end
16
17
 
17
18
  # changing tags means all previous services need to be removed manually since
18
19
  # they can no longer be found
19
- def sync(expected_definitions, tags)
20
+ def sync(expected_definitions, tags, dry: false)
20
21
  raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if tags.empty?
21
22
 
22
23
  modified = 0
@@ -26,6 +27,7 @@ class ConsulSyncer
26
27
  expected_definitions.each do |d|
27
28
  d[:tags] += tags
28
29
  d[:tags].sort!
30
+ d[:tags].uniq!
29
31
  end
30
32
 
31
33
  actual_definitions = consul_endpoints(tags).map do |consul_endpoint|
@@ -59,11 +61,11 @@ class ConsulSyncer
59
61
  elsif remove_matching_service!(actual_definitions, expected, identifying)
60
62
  @logger.info "Updating #{description}"
61
63
  modified += 1
62
- register expected
64
+ register **expected unless dry
63
65
  else
64
66
  @logger.info "Adding #{description}"
65
67
  modified += 1
66
- register expected
68
+ register **expected unless dry
67
69
  end
68
70
  end
69
71
 
@@ -71,7 +73,7 @@ class ConsulSyncer
71
73
  actual_definitions.each do |actual|
72
74
  @logger.info "Removing #{actual.fetch(:service)} / #{actual.fetch(:service_id)} on #{actual.fetch(:node)} in Consul"
73
75
  modified += 1
74
- deregister actual.fetch(:node), actual.fetch(:service_id)
76
+ deregister actual.fetch(:node), actual.fetch(:service_id) unless dry
75
77
  end
76
78
 
77
79
  modified
@@ -80,7 +82,7 @@ class ConsulSyncer
80
82
  private
81
83
 
82
84
  def consul_endpoints(requested_tags)
83
- services = @consul.request(:get, "/v1/catalog/services?tag=#{requested_tags.first}")
85
+ services = @consul.request(:get, "/v1/catalog/services?cached&stale&tag=#{requested_tags.first}")
84
86
  services.each_with_object([]) do |(name, tags), all|
85
87
  # cannot query for multiple tags via query, so handle multi-matching manually
86
88
  next if (requested_tags - tags).any?
@@ -1,3 +1,3 @@
1
1
  class ConsulSyncer
2
- VERSION = "0.6.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -11,8 +11,9 @@ class ConsulSyncer
11
11
  class ConsulError < StandardError
12
12
  end
13
13
 
14
- def initialize(consul, params:, logger:)
15
- @consul = consul
14
+ def initialize(consul_url, params:, logger:)
15
+ consul_url = "http://#{consul_url}" unless consul_url.include?("://")
16
+ @consul = Faraday.new(consul_url)
16
17
  @params = params
17
18
  @logger = logger
18
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul_syncer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-22 00:00:00.000000000 Z
11
+ date: 2021-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -47,15 +47,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
47
  requirements:
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- version: 2.2.0
50
+ version: 2.4.0
51
51
  required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubyforge_project:
58
- rubygems_version: 2.7.6
57
+ rubygems_version: 3.2.16
59
58
  signing_key:
60
59
  specification_version: 4
61
60
  summary: Sync remote services into consul