consul_syncer 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6354e9ee4ccdf46a22fd97263f8008da145c27a92ff766b501b15f8431303585
4
- data.tar.gz: a93e69e00dbcba1cb4d7ee4f5ab4f0fe7cd32ec1e8d3521ca6e11c6278868675
3
+ metadata.gz: 875c3e3eee8a06872dde88a612d7dfb8ae33ac59377d441e54afa6585da787b7
4
+ data.tar.gz: 4e1d1d3701fc464772745858109ff04260b0a8568e7ce8ad8eeab6ad6de26032
5
5
  SHA512:
6
- metadata.gz: 87b9d3375f8e6aa9ce4e7f7055117ddc2e743b86e410f2367ab748ed3795e0f48a763a4b620e40fe703fab7b250a99d2855016c2d2e4170244061ef7aa54b2dc
7
- data.tar.gz: a53abb227735ead7f9c0d7f44a95c856cbce43a6e346f40a0977ed4feaad22caa76c9342888d6d135fa99722fd508c30e0743fd44dea426fd1dd35e5200a4c48
6
+ metadata.gz: ae7f93e5464309a3ed828ba18e7f43bd8ff2ec8a111b513d225f598d2a84c6df80c480722d3f1545a7813c2a34fbf5de10083357432d6f3f9336bf4923a9f082
7
+ data.tar.gz: 3cdeb2e0566669014aa37ae01cebcb453593e993cf9cee5a4155d26649fddb8f5ca38e179cd911979824c08a5fa98d23345bf441c5b2eb8e5937001263aedba0
@@ -1,3 +1,3 @@
1
1
  class ConsulSyncer
2
- VERSION = "1.0.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/consul_syncer.rb CHANGED
@@ -17,16 +17,21 @@ class ConsulSyncer
17
17
 
18
18
  # changing tags means all previous services need to be removed manually since
19
19
  # they can no longer be found
20
- def sync(expected_definitions, tags)
21
- raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if tags.empty?
20
+ def sync(expected_definitions, tags, dry: false)
21
+ planned = plan(expected_definitions, tags)
22
+ execute planned unless dry
23
+ planned.size # let users know what we did and keep legacy api working
24
+ end
22
25
 
23
- modified = 0
26
+ def plan(expected_definitions, tags)
27
+ raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if tags.empty?
24
28
 
25
29
  # ensure consistent tags to find the endpoints after adding
26
30
  expected_definitions = expected_definitions.dup
27
31
  expected_definitions.each do |d|
28
32
  d[:tags] += tags
29
33
  d[:tags].sort!
34
+ d[:tags].uniq!
30
35
  end
31
36
 
32
37
  actual_definitions = consul_endpoints(tags).map do |consul_endpoint|
@@ -43,6 +48,7 @@ class ConsulSyncer
43
48
 
44
49
  identifying = [:node, :service_id]
45
50
  interesting = [*identifying, :service, :service_address, :address, :tags, :port]
51
+ planned = []
46
52
 
47
53
  expected_definitions.each do |expected|
48
54
  description = "#{expected[:service] || "*"} / #{expected[:service_id] || "*"} on #{expected.fetch(:node)} in Consul"
@@ -59,23 +65,30 @@ class ConsulSyncer
59
65
  @logger.debug "Found #{description}"
60
66
  elsif remove_matching_service!(actual_definitions, expected, identifying)
61
67
  @logger.info "Updating #{description}"
62
- modified += 1
63
- register expected
68
+ planned << [:register, [], expected]
64
69
  else
65
70
  @logger.info "Adding #{description}"
66
- modified += 1
67
- register expected
71
+ planned << [:register, [], expected]
68
72
  end
69
73
  end
70
74
 
71
75
  # all definitions that are left did not match any expected definitions and are no longer needed
72
76
  actual_definitions.each do |actual|
73
77
  @logger.info "Removing #{actual.fetch(:service)} / #{actual.fetch(:service_id)} on #{actual.fetch(:node)} in Consul"
74
- modified += 1
75
- deregister actual.fetch(:node), actual.fetch(:service_id)
78
+ planned << [:deregister, [actual.fetch(:node), actual.fetch(:service_id)], nil]
76
79
  end
77
80
 
78
- modified
81
+ planned
82
+ end
83
+
84
+ def execute(planned)
85
+ planned.each do |m, args, kwargs|
86
+ if kwargs
87
+ send m, *args, **kwargs
88
+ else
89
+ send m, *args
90
+ end
91
+ end
79
92
  end
80
93
 
81
94
  private
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: 1.0.0
4
+ version: 1.2.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: 2020-02-20 00:00:00.000000000 Z
11
+ date: 2021-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -47,14 +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
- rubygems_version: 3.0.3
57
+ rubygems_version: 3.2.16
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Sync remote services into consul