consul_watcher 0.0.2 → 0.0.4

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: 9e50318d48e6289402eb0458fe4e4223a548810de65f592400dc3e7012f4443b
4
- data.tar.gz: 4e25cb74e2e614f9a874aa07a9e8627a2733233cc3503ebb61ea4bc94a09afcd
3
+ metadata.gz: 1154bc525d36859510163c742472b9d58b850ff800e9e07bba309e7796fd9385
4
+ data.tar.gz: a8f35ed4aca91c81e57050343aa26354fa86a6c11a1bc0fe9522356492c7711c
5
5
  SHA512:
6
- metadata.gz: a5e77b1018fce3771ccc803f0341067761ea55bfb19330332f9152d9beef7ad47971ca3a28e61e56b787ee7b9b902bdb898177b08c598083efdbf3858fb5885c
7
- data.tar.gz: 18fac72357f24ab0e88516c79439fcef5bab11cc1c7f766e03dc63ace7897ed8c30c673706c3cfcf2bc363c8bee9ced936255f200c339fb8e6a1aca671d716c7
6
+ metadata.gz: a0ca51f1b7e53edc15384239730a5fb30a4d9dbecd44fd3323e560c43339e1d690c0d070d28af9ad189745c599cc2e681444d76dec66915bf9885eec0f0760f7
7
+ data.tar.gz: 81e309621849f103fe61495df030f6c33988e5a3ee0d423f23f3894dd7e22da2ea283a3901d3cb22a95956210126db901f34e6b87226088003615d33a492479d
data/Rakefile CHANGED
@@ -2,63 +2,57 @@
2
2
 
3
3
  require 'bundler/gem_tasks'
4
4
  require 'open3'
5
- require_relative 'lib/consul_watcher/rake_helper'
5
+ require 'flazm_ruby_helpers/os'
6
+ require 'flazm_ruby_helpers/project'
7
+ require_relative 'test/lib/rake_helper'
6
8
 
7
9
  spec_file = Gem::Specification.load('consul_watcher.gemspec')
8
10
 
9
11
  task default: :docker_build
10
12
 
11
- task :docker_tag, [:version, :docker_image_id] do |_task, args|
12
- puts "Docker id #{args['docker_image_id']} => tag rfortman/consul_watcher:#{args['version']}"
13
- tag_cmd = "docker tag #{args['docker_image_id']} rfortman/consul_watcher:#{args['version']}"
14
- Open3.popen3(tag_cmd) do |_stdin, _stdout, stderr, wait_thr|
15
- error = stderr.read
16
- puts error unless wait_thr.value.success?
17
- end
18
- end
19
-
20
13
  task docker_build: [:build] do
21
- docker_image_id = nil
22
14
  build_cmd = "docker build --build-arg gem_file=consul_watcher-#{spec_file.version}.gem ."
23
- threads = []
24
- Open3.popen3(build_cmd) do |_stdin, stdout, stderr, wait_thr|
25
- { out: stdout, err: stderr }.each do |key, stream|
26
- threads << Thread.new do
27
- until (raw_line = stream.gets).nil?
28
- match = raw_line.match(/Successfully built (.*)$/i)
29
- docker_image_id = match.captures[0] if match
30
- puts raw_line.to_s
31
- end
32
- end
33
- end
34
- threads.each(&:join)
35
- if wait_thr.value.success?
36
- Rake::Task['docker_tag'].invoke(spec_file.version, docker_image_id)
37
- Rake::Task['docker_tag'].reenable
38
- Rake::Task['docker_tag'].invoke('latest', docker_image_id)
39
- end
40
- end
15
+ image_id = FlazmRubyHelpers::Project::Docker.build(build_cmd)
16
+ FlazmRubyHelpers::Project::Docker.tag(spec_file.metadata['docker_image_name'],
17
+ spec_file.version,
18
+ image_id)
19
+ FlazmRubyHelpers::Project::Docker.tag(spec_file.metadata['docker_image_name'],
20
+ 'latest',
21
+ image_id)
41
22
  end
42
23
 
43
24
  task :start_deps do
44
25
  cmd = 'docker-compose --file test/docker-compose.yml up -d consul rabbitmq'
45
- ConsulWatcher::RakeHelper.exec(cmd)
26
+ FlazmRubyHelpers::Os.exec(cmd)
46
27
  urls = [
47
28
  'http://localhost:8500/v1/status/leader',
48
29
  'http://localhost:15672'
49
30
  ]
50
- ConsulWatcher::RakeHelper.wait_for_urls(urls)
51
- ConsulWatcher::RakeHelper.config_rabbitmq
31
+ FlazmRubyHelpers::Http.wait_for_urls(urls)
52
32
  end
53
33
 
54
34
  task up: [:start_deps] do
55
- cmd = 'docker-compose --file test/docker-compose.yml up -d consul-watcher'
56
- _output, _status = ConsulWatcher::RakeHelper.exec(cmd)
35
+ _output, _success = FlazmRubyHelpers::Os.exec('docker-compose --file test/docker-compose.yml up -d consul-watcher')
36
+ end
37
+
38
+ task :consume do
39
+ ConsulWatcher::RakeHelper.config_rabbitmq
57
40
  puts 'Starting queue consumer'
58
41
  ConsulWatcher::RakeHelper.consumer_start
59
42
  end
60
43
 
61
44
  task :down do
62
- cmd = 'docker-compose --file test/docker-compose.yml down'
63
- _output, _status = ConsulWatcher::RakeHelper.exec(cmd)
45
+ _output, _success = FlazmRubyHelpers::Os.exec('docker-compose --file test/docker-compose.yml down')
46
+ end
47
+
48
+ task publish: [:build, :docker_build] do
49
+ FlazmRubyHelpers::Project::Git.publish(spec_file.version.to_s, 'origin', 'master')
50
+ FlazmRubyHelpers::Project::Docker.publish(spec_file.metadata['docker_image_name'], spec_file.version.to_s)
51
+ FlazmRubyHelpers::Project::Gem.publish(spec_file.name.to_s, spec_file.version.to_s)
52
+ end
53
+
54
+ task :unpublish do
55
+ _output, _success = FlazmRubyHelpers::Os.exec("git tag --delete #{spec_file.version.to_s}")
56
+ _output, _success = FlazmRubyHelpers::Os.exec("gem yank #{spec_file.name.to_s} -v #{spec_file.version.to_s}")
57
+ puts "Please delete the tag from dockerhub at https://cloud.docker.com/repository/registry-1.docker.io/#{spec_file.metadata['docker_image_name']}/tags"
64
58
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.4
@@ -4,13 +4,15 @@ lib = File.expand_path('lib', '..')
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = 'consul_watcher'
8
- spec.version = IO.read('VERSION').chomp
9
- spec.authors = ['Ryan Fortman']
10
- spec.email = ['r.fortman.dev@gmail.com']
11
-
12
- spec.summary = 'Send consul watch events to an amqp.'
13
- spec.homepage = 'https://github.com/fortman/consul_watcher'
7
+ spec.name = 'consul_watcher'
8
+ spec.version = IO.read('VERSION').chomp
9
+ spec.authors = ['Ryan Fortman']
10
+ spec.email = ['r.fortman.dev@gmail.com']
11
+ spec.summary = 'Send consul watch events to an amqp.'
12
+ spec.homepage = 'https://github.com/fortman/consul_watcher'
13
+ spec.metadata = {
14
+ 'docker_image_name' => 'rfortman/consul_watcher'
15
+ }
14
16
 
15
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
18
  f.match(%r{^(test|spec|features)/})
@@ -19,11 +21,11 @@ Gem::Specification.new do |spec|
19
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
22
  spec.require_paths = ['lib']
21
23
 
22
- # spec.add_development_dependency 'bundler', '~> 2.0'
24
+ spec.add_development_dependency 'bundler', '~> 2.0'
23
25
  spec.add_development_dependency 'rake', '~> 10.0'
24
- spec.add_runtime_dependency 'bundler', '~> 2.0'
26
+ spec.add_dependency 'flazm_ruby_helpers', '~> 0.0.1'
27
+ spec.add_runtime_dependency 'bunny', '~> 1.7.0'
25
28
  spec.add_runtime_dependency 'diplomat', '~> 2.2.4'
26
- spec.add_dependency 'bunny', '~> 1.7.0'
27
- spec.add_dependency 'hashdiff', '~> 0.3'
28
- spec.add_dependency 'slop', '~> 4.6'
29
+ spec.add_runtime_dependency 'hashdiff', '~> 0.3'
30
+ spec.add_runtime_dependency 'slop', '~> 4.6'
29
31
  end
data/docs/TODO.md CHANGED
@@ -12,11 +12,12 @@
12
12
  | PROP | proposed |
13
13
 
14
14
  ## High level tasks
15
- | task | status |
16
- |:---------------------------------- |:------ |
17
- | Create documentation | IP |
18
- | Publish docker image and ruby gem | IP |
19
- | Write unit tests | PL |
15
+ | task | status |
16
+ |:-------------------------------------------- |:------ |
17
+ | Create documentation | IP |
18
+ | Publish docker image and ruby gem | IP |
19
+ | Write unit tests | PL |
20
+ | Write super classes for shared functionality | PL |
20
21
 
21
22
  ## Functional ruby classes implementation
22
23
 
@@ -33,10 +34,10 @@
33
34
  | ---------------- |:----------- |
34
35
  | key/keyprefix | FUNC |
35
36
  | checks | IP |
36
- | services | planned |
37
- | service | planned |
38
- | nodes | planned |
39
- | event | planned |
37
+ | services | PL |
38
+ | service | PL |
39
+ | nodes | PL |
40
+ | event | PL |
40
41
 
41
42
  #### Destination class implementation status
42
43
 
@@ -15,6 +15,7 @@ module ConsulWatcher
15
15
  current_watch_json = $stdin.read
16
16
  previous_watch_json = @storage.fetch
17
17
  changes = @watch_type.get_changes(previous_watch_json, current_watch_json)
18
+ # @watch_type.filters.print_filters
18
19
  changes.each do |change|
19
20
  @destination.send(change)
20
21
  end
@@ -16,11 +16,7 @@ module ConsulWatcher
16
16
  end
17
17
 
18
18
  def setup_rabbitmq
19
- @conn = Bunny.new(host: @rabbitmq_server,
20
- port: @rabbitmq_port,
21
- vhost: @rabbitmq_vhost,
22
- username: @rabbitmq_username,
23
- password: @rabbitmq_password)
19
+ @conn = Bunny.new(rabbitmq_opts)
24
20
  @conn.start
25
21
  @ch = @conn.create_channel
26
22
  @ex = Bunny::Exchange.new(@ch,
@@ -29,6 +25,23 @@ module ConsulWatcher
29
25
  durable: true)
30
26
  end
31
27
 
28
+ def rabbitmq_opts
29
+ opts = {}
30
+ opts[:vhost] = @rabbitmq_vhost
31
+ opts[:username] = @rabbitmq_username
32
+ opts[:password] = @rabbitmq_password
33
+ if @rabbitmq_addresses
34
+ opts[:addresses] = @rabbitmq_addresses
35
+ elsif @rabbitmq_hosts
36
+ opts[:hosts] = @rabbitmq_hosts
37
+ opts[:port] = @rabbitmq_port if @rabbitmq_port
38
+ elsif @rabbitmq_host
39
+ opts[:host] = @rabbitmq_host
40
+ opts[:port] = @rabbitmq_port if @rabbitmq_port
41
+ end
42
+ opts
43
+ end
44
+
32
45
  def send(change)
33
46
  @logger.debug('publishing message')
34
47
  routing_key = change['id']
@@ -47,7 +60,12 @@ module ConsulWatcher
47
60
  logger.level = Logger::DEBUG
48
61
  {
49
62
  logger: logger,
50
- rabbitmq_server: 'localhost',
63
+ #rabbitmq_host: nil,
64
+ rabbitmq_host: 'rabbitmq',
65
+ #rabbitmq_hosts: ['rabbitmq', 'localhost'],
66
+ #rabbitmq_host: 'localhost',
67
+ #rabbitmq_hosts: nil,
68
+ rabbitmq_addresses: nil,
51
69
  rabbitmq_port: '5672',
52
70
  rabbitmq_vhost: '/',
53
71
  rabbitmq_username: 'guest',
@@ -31,14 +31,9 @@ module ConsulWatcher
31
31
  logger = Logger.new(STDOUT)
32
32
  logger.level = Logger::INFO
33
33
  {
34
- logger: logger,
35
- rabbitmq_server: 'localhost',
36
- rabbitmq_port: '5672',
37
- rabbitmq_vhost: '/',
38
- rabbitmq_username: 'guest',
39
- rabbitmq_password: 'guest',
40
- rabbitmq_exchange: 'amq.topic'
34
+ logger: logger
41
35
  }
36
+ end
42
37
  end
43
38
  end
44
39
  end
@@ -11,20 +11,29 @@ module ConsulWatcher
11
11
  end
12
12
 
13
13
  def add_filters(filters_to_add)
14
- @filters.merge!(filters_to_add)
14
+ @filters = @filters.deep_merge(filters_to_add)
15
15
  end
16
16
 
17
17
  def filter?(change)
18
- @filters.each do |attribute, regex|
19
- match = change.key?(attribute) ? change[attribute].match?(/#{regex}/) : false
18
+ @filters.each do |attribute, regexs|
19
+ next unless change.key?(attribute) && !change[attribute].nil?
20
+
21
+ match = match?(change[attribute], regexs)
20
22
  if match
21
- @logger.debug("filtered #{change['id']} #{attribute} on regex #{regex}")
22
- return true
23
+ @logger.debug("filtered #{attribute} '#{change[attribute]}' against regular expressions #{regexs}")
24
+ return true
23
25
  end
24
26
  end
25
27
  false
26
28
  end
27
29
 
30
+ def match?(value, regexs)
31
+ results = regexs.each.collect do |regex|
32
+ value.match?(/#{regex}/)
33
+ end
34
+ results.any?
35
+ end
36
+
28
37
  def print_filters
29
38
  @filters.each do |filter|
30
39
  @logger.debug("filter: #{filter}")
@@ -35,7 +44,7 @@ module ConsulWatcher
35
44
 
36
45
  def defaults
37
46
  logger = Logger.new(STDOUT)
38
- logger.level = Logger::WARN
47
+ logger.level = Logger::DEBUG
39
48
  {
40
49
  logger: logger,
41
50
  filters: {}
@@ -31,7 +31,7 @@ module ConsulWatcher
31
31
  end
32
32
 
33
33
  def get_filters
34
- { 'key_path' => cache_file_name }
34
+ { 'key_path' => [ cache_file_name ] }
35
35
  end
36
36
 
37
37
  private
@@ -22,6 +22,7 @@ module ConsulWatcher
22
22
  formatted_change
23
23
  end.compact
24
24
  changes = changes.each.collect.reject {|change| @filters.filter?(change)}
25
+ puts "size: #{changes.size}"
25
26
  changes
26
27
  end
27
28
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul_watcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Fortman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-13 00:00:00.000000000 Z
11
+ date: 2019-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -25,47 +39,47 @@ dependencies:
25
39
  - !ruby/object:Gem::Version
26
40
  version: '10.0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: bundler
42
+ name: flazm_ruby_helpers
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '2.0'
47
+ version: 0.0.1
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '2.0'
54
+ version: 0.0.1
41
55
  - !ruby/object:Gem::Dependency
42
- name: diplomat
56
+ name: bunny
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: 2.2.4
61
+ version: 1.7.0
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: 2.2.4
68
+ version: 1.7.0
55
69
  - !ruby/object:Gem::Dependency
56
- name: bunny
70
+ name: diplomat
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: 1.7.0
75
+ version: 2.2.4
62
76
  type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: 1.7.0
82
+ version: 2.2.4
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: hashdiff
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -134,14 +148,14 @@ files:
134
148
  - lib/consul_watcher/destination/jq.rb
135
149
  - lib/consul_watcher/diff.rb
136
150
  - lib/consul_watcher/filters.rb
137
- - lib/consul_watcher/rake_helper.rb
138
151
  - lib/consul_watcher/storage/consul.rb
139
152
  - lib/consul_watcher/storage/disk.rb
140
153
  - lib/consul_watcher/watch_type/checks.rb
141
154
  - lib/consul_watcher/watch_type/key.rb
142
155
  homepage: https://github.com/fortman/consul_watcher
143
156
  licenses: []
144
- metadata: {}
157
+ metadata:
158
+ docker_image_name: rfortman/consul_watcher
145
159
  post_install_message:
146
160
  rdoc_options: []
147
161
  require_paths:
@@ -1,76 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # frozen_string_literal: true
4
-
5
- require 'open3'
6
- require 'net/http'
7
- require 'bunny'
8
- require 'json'
9
-
10
- module ConsulWatcher
11
- module RakeHelper
12
- def self.exec(command, stream: true)
13
- output = [] ; threads = [] ; status = nil
14
- Open3.popen3(command) do |_stdin, stdout, stderr, wait_thr|
15
- { out: stdout, err: stderr }.each do |_key, stream|
16
- threads << Thread.new do
17
- until (raw_line = stream.gets).nil?
18
- output << raw_line.to_s
19
- puts raw_line.to_s if stream
20
- end
21
- end
22
- end
23
- threads.each(&:join)
24
- status = wait_thr.value.success?
25
- end
26
- return output, status
27
- end
28
-
29
- def self.wait_for_urls(urls)
30
- urls.each do |url|
31
- uri = URI(url)
32
- error = true
33
- Net::HTTP.start(uri.host, uri.port, read_timeout: 5, max_retries: 12) do |http|
34
- while error
35
- begin
36
- response = http.request(Net::HTTP::Get.new(uri))
37
- error = false
38
- rescue EOFError
39
- retry
40
- end
41
- end
42
- raise Exception unless response.code == '200'
43
-
44
- puts "up: #{url}"
45
- end
46
- end
47
- end
48
-
49
- def self.config_rabbitmq
50
- conn = Bunny.new(host: 'localhost', port: '5672', vhost: '/', \
51
- username: 'guest', password: 'guest')
52
- conn.start
53
- ch = conn.create_channel
54
- ex = Bunny::Exchange.new(ch, :topic, 'amq.topic', durable: true)
55
- @queue = ch.queue('consul_watcher', durable: true).bind(ex, routing_key: 'consul_watcher.key.#')
56
- nil
57
- end
58
-
59
- def self.process_message(delivery_info, properties, body)
60
- puts "routing_key: #{delivery_info[:routing_key]}"
61
- puts "properties: #{properties}"
62
- puts "body:\n#{body}"
63
- puts
64
- end
65
-
66
- def self.consumer_start
67
- @consumer ||= @queue.subscribe(block: true, &itself.method(:process_message))
68
- nil
69
- end
70
-
71
- def self.consumer_stop
72
- @consumer&.cancel
73
- @consumer = nil
74
- end
75
- end
76
- end