cluster-discovery 0.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.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +9 -0
  5. data/.rubocop_todo.yml +62 -0
  6. data/Gemfile +26 -0
  7. data/Gemfile.lock +141 -0
  8. data/Guardfile +47 -0
  9. data/LICENSE +22 -0
  10. data/README.md +89 -0
  11. data/Rakefile +51 -0
  12. data/Thorfile +3 -0
  13. data/cluster-discovery.gemspec +26 -0
  14. data/lib/cluster/discovery/consul.rb +46 -0
  15. data/lib/cluster/discovery/ec2/auto_scaling.rb +27 -0
  16. data/lib/cluster/discovery/ec2/tag.rb +64 -0
  17. data/lib/cluster/discovery/errors.rb +21 -0
  18. data/lib/cluster/discovery/version.rb +6 -0
  19. data/lib/cluster/discovery.rb +38 -0
  20. data/spec/cassettes/Cluster_Discovery/_discover/can_discover_nodes/discovery_with_consul.yml +38 -0
  21. data/spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_all_nodes/can_discover_nodes_with_tags.yml +38 -0
  22. data/spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_all_nodes/can_discover_nodes_without_tags.yml +38 -0
  23. data/spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_leader_node/can_discover_nodes_with_tags.yml +38 -0
  24. data/spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_leader_node/can_discover_nodes_without_tags.yml +38 -0
  25. data/spec/cassettes/Cluster_Discovery_EC2_AutoScaling/_discover/can_reuse_the_tag_provider/can_find_instances_by_aws_auto_scaling_group.yml +191 -0
  26. data/spec/cassettes/Cluster_Discovery_EC2_Tag/_discover/passing_aws_region_arg/can_find_instances_by_tag.yml +862 -0
  27. data/spec/lib/cluster/discovery/consul_spec.rb +119 -0
  28. data/spec/lib/cluster/discovery/ec2/auto_scaling_spec.rb +19 -0
  29. data/spec/lib/cluster/discovery/ec2/tag_spec.rb +81 -0
  30. data/spec/lib/cluster/discovery_spec.rb +23 -0
  31. data/spec/spec_helper.rb +111 -0
  32. metadata +144 -0
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Cluster::Discovery::Consul' do
4
+ describe '.initialize' do
5
+ context 'configuration is correct' do
6
+ it 'has the default url' do
7
+ consul = Cluster::Discovery::Consul.new
8
+ config = consul.instance_variable_get(:@configuration)
9
+ expect(config.url).to eq('http://localhost:8500')
10
+ end
11
+
12
+ it 'has a custom url' do
13
+ consul = Cluster::Discovery::Consul.new(
14
+ consul_url: 'http://127.0.0.1:8500')
15
+ config = consul.instance_variable_get(:@configuration)
16
+ expect(config.url).to eq('http://127.0.0.1:8500')
17
+ end
18
+ end
19
+ end
20
+
21
+ describe '.build_extra_opts' do
22
+ before(:each) do
23
+ @consul = Cluster::Discovery::Consul.new
24
+ end
25
+ context 'opts are correct without tags' do
26
+ let(:noleader_notags) do
27
+ [:all]
28
+ end
29
+
30
+ let(:leader_notags) do
31
+ [:first]
32
+ end
33
+
34
+ it 'can return only leader without tags' do
35
+ expect(
36
+ @consul.build_extra_opts(leader: false, tags: [])
37
+ ).to eq(noleader_notags)
38
+ end
39
+
40
+ it 'can return only all nodes' do
41
+ expect(
42
+ @consul.build_extra_opts(leader: true, tags: [])
43
+ ).to eq(leader_notags)
44
+ end
45
+ end
46
+
47
+ context 'opts are correct with tags' do
48
+ let(:noleader_tags) do
49
+ [:all, { tag: ['master'] }]
50
+ end
51
+
52
+ let(:leader_tags) do
53
+ [:first, { tag: ['master'] }]
54
+ end
55
+
56
+ it 'can return only leader without tags' do
57
+ expect(
58
+ @consul.build_extra_opts(leader: false, tags: ['master'])
59
+ ).to eq(noleader_tags)
60
+ end
61
+
62
+ it 'can return only all nodes' do
63
+ expect(
64
+ @consul.build_extra_opts(leader: true, tags: ['master'])
65
+ ).to eq(leader_tags)
66
+ end
67
+ end
68
+ end
69
+
70
+ describe '.discover', vcr: true do
71
+ before(:each) do
72
+ @consul = Cluster::Discovery::Consul.new(
73
+ consul_url: "http://#{test_consul_host}:8500")
74
+ end
75
+
76
+ def map_resp(obj)
77
+ obj.map(&:Address)
78
+ end
79
+
80
+ let(:consul_notags_leader) do
81
+ map_resp(@consul.discover(consul_service: 'redis', leader: true))
82
+ end
83
+
84
+ let(:consul_notags_noleader) do
85
+ map_resp(@consul.discover(consul_service: 'redis'))
86
+ end
87
+
88
+ let(:consul_tags_leader) do
89
+ map_resp(@consul.discover(
90
+ consul_service: 'redis',
91
+ leader: true,
92
+ tags: 'master'))
93
+ end
94
+
95
+ let(:consul_tags_noleader) do
96
+ map_resp(@consul.discover(consul_service: 'redis', tags: 'master'))
97
+ end
98
+
99
+ context 'can discover leader node' do
100
+ it 'can discover nodes without tags' do
101
+ expect(consul_notags_leader.first).to eq('192.168.10.10')
102
+ end
103
+
104
+ it 'can discover nodes with tags' do
105
+ expect(consul_tags_leader.first).to eq('192.168.10.10')
106
+ end
107
+ end
108
+
109
+ context 'can discover all nodes' do
110
+ it 'can discover nodes without tags' do
111
+ expect(consul_notags_noleader.first).to eq('192.168.10.10')
112
+ end
113
+
114
+ it 'can discover nodes with tags' do
115
+ expect(consul_tags_noleader.length).to eq(3)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Cluster::Discovery::EC2::AutoScaling' do
4
+ describe '.discover' do
5
+ context 'can reuse the tag provider', vcr: true do
6
+ before(:each) do
7
+ @discovery = Cluster::Discovery::EC2::AutoScaling.new(
8
+ aws_region: 'us-east-1'
9
+ )
10
+ end
11
+
12
+ it 'can find instances by aws_auto_scaling_group' do
13
+ instances = @discovery.discover(aws_asg: 'foo-prod-v000')
14
+ instances = instances.map(&:instance_id)
15
+ expect(instances.length).to eq(1)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Cluster::Discovery::EC2::Tag' do
4
+ describe '.discover' do
5
+ context 'not passing aws_region arg' do
6
+ let(:discovery) { Cluster::Discovery::EC2::Tag }
7
+ it 'requires aws_region' do
8
+ expect { discovery.new }.to(
9
+ raise_error(ArgumentError),
10
+ 'missing keyword: aws_region'
11
+ )
12
+ end
13
+ end
14
+
15
+ context 'passing aws_region arg', vcr: true do
16
+ before(:each) do
17
+ @discovery = Cluster::Discovery::EC2::Tag.new(aws_region: 'us-east-1')
18
+ end
19
+
20
+ it 'has a discover method' do
21
+ expect(@discovery).to respond_to(:discover)
22
+ end
23
+
24
+ it 'tags cannot be empty' do
25
+ expect { @discovery.discover }.to(
26
+ raise_error(Cluster::Discovery::EmptyTagsError),
27
+ 'Tags cannot be empty'
28
+ )
29
+ end
30
+
31
+ it 'can find instances by tag' do
32
+ instances = @discovery.discover(
33
+ aws_tags: [{ key: 'Service', value: 'router' }]
34
+ )
35
+ instances = instances.map(&:instance_id)
36
+ expect(instances.length).to eq(6)
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '.build_tags', vcr: true do
42
+ before(:each) do
43
+ @discovery = Cluster::Discovery::EC2::Tag.new(aws_region: 'us-east-1')
44
+ end
45
+
46
+ let(:filters) do
47
+ [{ name: 'instance-state-name', values: ['running'] }]
48
+ end
49
+
50
+ context 'parse default tags' do
51
+ let(:tags) { @discovery.send(:build_tags) }
52
+
53
+ it 'can format tags correctly' do
54
+ expect(tags).to eq(filters)
55
+ end
56
+ end
57
+
58
+ context 'parse input tag' do
59
+ let(:tags) do
60
+ @discovery.send(:build_tags,
61
+ [{ key: 'Service', value: 'router' }])
62
+ end
63
+
64
+ # TODO: add failing negative test for wrong key values
65
+ # Tags:
66
+ # { key: 'aws:autoscaling:groupName', values: ['foo-prod-v000'] }
67
+ # Resulting filters:
68
+ # { name: 'tag:aws:autoscaling:groupName', values: ['foo-prod-v000'] },
69
+ let(:filters) do
70
+ [
71
+ { name: 'tag:Service', values: ['router'] },
72
+ { name: 'instance-state-name', values: ['running'] }
73
+ ]
74
+ end
75
+
76
+ it 'can format tags correctly' do
77
+ expect(tags).to eq(filters)
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Cluster::Discovery' do
4
+ let(:discovery) { Cluster::Discovery }
5
+
6
+ describe '.discover' do
7
+ context 'has a discover method' do
8
+ it { expect(discovery).to respond_to(:discover) }
9
+ end
10
+
11
+ context 'can discover nodes', vcr: true do
12
+ it 'discovery with consul' do
13
+ consul = Cluster::Discovery.discover(
14
+ 'consul',
15
+ consul_url: "http://#{test_consul_host}:8500",
16
+ consul_service: 'redis',
17
+ leader: true,
18
+ tags: 'master')
19
+ expect(consul.first.Address).to eq('192.168.10.10')
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,111 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+
20
+ require_relative '../lib/cluster/discovery'
21
+ require 'vcr'
22
+
23
+ def test_consul_host
24
+ ENV['TEST_CONSUL_HOST'] || '172.28.128.104'
25
+ end
26
+
27
+ VCR.configure do |c|
28
+ c.cassette_library_dir = 'spec/cassettes'
29
+ c.hook_into :webmock
30
+ c.configure_rspec_metadata!
31
+ c.allow_http_connections_when_no_cassette = true
32
+ end
33
+
34
+ RSpec.configure do |config|
35
+ # rspec-expectations config goes here. You can use an alternate
36
+ # assertion/expectation library such as wrong or the stdlib/minitest
37
+ # assertions if you prefer.
38
+ config.expect_with :rspec do |expectations|
39
+ # This option will default to `true` in RSpec 4. It makes the `description`
40
+ # and `failure_message` of custom matchers include text for helper methods
41
+ # defined using `chain`, e.g.:
42
+ # be_bigger_than(2).and_smaller_than(4).description
43
+ # # => "be bigger than 2 and smaller than 4"
44
+ # ...rather than:
45
+ # # => "be bigger than 2"
46
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
47
+ end
48
+
49
+ # rspec-mocks config goes here. You can use an alternate test double
50
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
51
+ config.mock_with :rspec do |mocks|
52
+ # Prevents you from mocking or stubbing a method that does not exist on
53
+ # a real object. This is generally recommended, and will default to
54
+ # `true` in RSpec 4.
55
+ mocks.verify_partial_doubles = true
56
+ end
57
+
58
+ # The settings below are suggested to provide a good initial experience
59
+ # with RSpec, but feel free to customize to your heart's content.
60
+ =begin
61
+ # These two settings work together to allow you to limit a spec run
62
+ # to individual examples or groups you care about by tagging them with
63
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
64
+ # get run.
65
+ config.filter_run :focus
66
+ config.run_all_when_everything_filtered = true
67
+
68
+ # Allows RSpec to persist some state between runs in order to support
69
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
70
+ # you configure your source control system to ignore this file.
71
+ config.example_status_persistence_file_path = "spec/examples.txt"
72
+
73
+ # Limits the available syntax to the non-monkey patched syntax that is
74
+ # recommended. For more details, see:
75
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
76
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
77
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
78
+ config.disable_monkey_patching!
79
+
80
+ # This setting enables warnings. It's recommended, but in some cases may
81
+ # be too noisy due to issues in dependencies.
82
+ config.warnings = true
83
+
84
+ # Many RSpec users commonly either run the entire suite or an individual
85
+ # file, and it's useful to allow more verbose output when running an
86
+ # individual spec file.
87
+ if config.files_to_run.one?
88
+ # Use the documentation formatter for detailed output,
89
+ # unless a formatter has already been configured
90
+ # (e.g. via a command-line flag).
91
+ config.default_formatter = 'doc'
92
+ end
93
+
94
+ # Print the 10 slowest examples and example groups at the
95
+ # end of the spec run, to help surface which specs are running
96
+ # particularly slow.
97
+ config.profile_examples = 10
98
+
99
+ # Run specs in random order to surface order dependencies. If you find an
100
+ # order dependency and want to debug it, you can fix the order by providing
101
+ # the seed, which is printed after each run.
102
+ # --seed 1234
103
+ config.order = :random
104
+
105
+ # Seed global randomization in this process using the `--seed` CLI option.
106
+ # Setting this allows you to use `--seed` to deterministically reproduce
107
+ # test failures related to randomization by passing the same `--seed` value
108
+ # as the one that triggered the failure.
109
+ Kernel.srand config.seed
110
+ =end
111
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cluster-discovery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Thompson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-19 00:00:00.000000000 Z
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: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: diplomat
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.15'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.15'
69
+ description: 'This is a library that provides a generic interface to multiple cluster
70
+ providers. Cluster providers include: EC2 Tags, EC2 AutoScaling Groups, and Consul'
71
+ email:
72
+ - Andrew_Thompson@rapid7.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".rubocop_todo.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - Guardfile
84
+ - LICENSE
85
+ - README.md
86
+ - Rakefile
87
+ - Thorfile
88
+ - cluster-discovery.gemspec
89
+ - lib/cluster/discovery.rb
90
+ - lib/cluster/discovery/consul.rb
91
+ - lib/cluster/discovery/ec2/auto_scaling.rb
92
+ - lib/cluster/discovery/ec2/tag.rb
93
+ - lib/cluster/discovery/errors.rb
94
+ - lib/cluster/discovery/version.rb
95
+ - spec/cassettes/Cluster_Discovery/_discover/can_discover_nodes/discovery_with_consul.yml
96
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_all_nodes/can_discover_nodes_with_tags.yml
97
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_all_nodes/can_discover_nodes_without_tags.yml
98
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_leader_node/can_discover_nodes_with_tags.yml
99
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_leader_node/can_discover_nodes_without_tags.yml
100
+ - spec/cassettes/Cluster_Discovery_EC2_AutoScaling/_discover/can_reuse_the_tag_provider/can_find_instances_by_aws_auto_scaling_group.yml
101
+ - spec/cassettes/Cluster_Discovery_EC2_Tag/_discover/passing_aws_region_arg/can_find_instances_by_tag.yml
102
+ - spec/lib/cluster/discovery/consul_spec.rb
103
+ - spec/lib/cluster/discovery/ec2/auto_scaling_spec.rb
104
+ - spec/lib/cluster/discovery/ec2/tag_spec.rb
105
+ - spec/lib/cluster/discovery_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.4
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Cluster Discovery Library
131
+ test_files:
132
+ - spec/cassettes/Cluster_Discovery/_discover/can_discover_nodes/discovery_with_consul.yml
133
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_all_nodes/can_discover_nodes_with_tags.yml
134
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_all_nodes/can_discover_nodes_without_tags.yml
135
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_leader_node/can_discover_nodes_with_tags.yml
136
+ - spec/cassettes/Cluster_Discovery_Consul/_discover/can_discover_leader_node/can_discover_nodes_without_tags.yml
137
+ - spec/cassettes/Cluster_Discovery_EC2_AutoScaling/_discover/can_reuse_the_tag_provider/can_find_instances_by_aws_auto_scaling_group.yml
138
+ - spec/cassettes/Cluster_Discovery_EC2_Tag/_discover/passing_aws_region_arg/can_find_instances_by_tag.yml
139
+ - spec/lib/cluster/discovery/consul_spec.rb
140
+ - spec/lib/cluster/discovery/ec2/auto_scaling_spec.rb
141
+ - spec/lib/cluster/discovery/ec2/tag_spec.rb
142
+ - spec/lib/cluster/discovery_spec.rb
143
+ - spec/spec_helper.rb
144
+ has_rdoc: