elasticsearch-drain 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53c5321ac669ce1bf6ca87ed31a698e36e60d2c8
4
+ data.tar.gz: b252c88074cb2fb623c4de01b4991b7bac8da95c
5
+ SHA512:
6
+ metadata.gz: 2d81aec24db08902298fe06dc33a72738e426d5b0f560e9dcf53fe8ea4acbca5fbe6a329136587dbed255a49a410431faf290fa2e0681510011cef4d17256b49
7
+ data.tar.gz: 7400aebaa436eeb4f52d24f9c13bc221cd8ec4ff0b39ba585c8b508c200792a82aa4ea7d1608008e47d453691220e23152128857c6bf84a4e797fd4262ab184c
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+ /test/tmp/
15
+ es_client.log*
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,88 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2015-11-03 12:49:27 -0500 using RuboCop version 0.34.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ Lint/AmbiguousRegexpLiteral:
11
+ Exclude:
12
+ - 'test/elasticsearch/drain/test_autoscaling.rb'
13
+
14
+ # Offense count: 2
15
+ Lint/ParenthesesAsGroupedExpression:
16
+ Exclude:
17
+ - 'test/elasticsearch/drain/test_cluster.rb'
18
+ - 'test/elasticsearch/test_drain.rb'
19
+
20
+ # Offense count: 1
21
+ # Cop supports --auto-correct.
22
+ Lint/UnusedMethodArgument:
23
+ Exclude:
24
+ - 'lib/elasticsearch/drain/nodes.rb'
25
+
26
+ # Offense count: 1
27
+ Metrics/AbcSize:
28
+ Max: 63
29
+
30
+ # Offense count: 1
31
+ Metrics/CyclomaticComplexity:
32
+ Max: 10
33
+
34
+ # Offense count: 16
35
+ # Configuration parameters: AllowURI, URISchemes.
36
+ Metrics/LineLength:
37
+ Max: 125
38
+
39
+ # Offense count: 1
40
+ Metrics/PerceivedComplexity:
41
+ Max: 12
42
+
43
+ # Offense count: 1
44
+ # Cop supports --auto-correct.
45
+ Style/CommentIndentation:
46
+ Exclude:
47
+ - 'test/test_helper.rb'
48
+
49
+ # Offense count: 8
50
+ # Configuration parameters: Exclude.
51
+ Style/Documentation:
52
+ Exclude:
53
+ - 'lib/elasticsearch/drain.rb'
54
+ - 'lib/elasticsearch/drain/autoscaling.rb'
55
+ - 'lib/elasticsearch/drain/base.rb'
56
+ - 'lib/elasticsearch/drain/cli.rb'
57
+ - 'lib/elasticsearch/drain/cluster.rb'
58
+ - 'lib/elasticsearch/drain/node.rb'
59
+ - 'lib/elasticsearch/drain/nodes.rb'
60
+ - 'lib/elasticsearch/drain/version.rb'
61
+ - 'test/**/*'
62
+
63
+ # Offense count: 1
64
+ # Cop supports --auto-correct.
65
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
66
+ Style/EmptyLinesAroundClassBody:
67
+ Exclude:
68
+ - 'lib/elasticsearch/drain/autoscaling.rb'
69
+
70
+ # Offense count: 2
71
+ # Cop supports --auto-correct.
72
+ # Configuration parameters: PreferredDelimiters.
73
+ Style/PercentLiteralDelimiters:
74
+ Exclude:
75
+ - 'elasticsearch-drain.gemspec'
76
+
77
+ # Offense count: 1
78
+ # Cop supports --auto-correct.
79
+ # Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
80
+ Style/RegexpLiteral:
81
+ Exclude:
82
+ - 'lib/elasticsearch/drain/node.rb'
83
+
84
+ # Offense count: 2
85
+ # Cop supports --auto-correct.
86
+ Style/UnneededPercentQ:
87
+ Exclude:
88
+ - 'elasticsearch-drain.gemspec'
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.3
5
+ - 2.1.5
6
+ - 2.1.6
7
+ - 2.2.0
8
+ - 2.2.1
9
+ - 2.2.2
10
+ sudo: false
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elasticsearch-drain.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rubyzip', '~> 1.1'
8
+ gem 'elasticsearch-extensions', '0.0.18'
9
+ gem 'yard', '~> 0.8'
10
+ gem 'thor', '~> 0.19.1'
11
+ gem 'thor-scmversion', '= 1.7.0'
12
+ gem 'guard'
13
+ gem 'guard-minitest'
14
+ end
15
+
16
+ group :development, :test do
17
+ gem 'minitest', '~> 5.8'
18
+ gem 'rubocop', '~> 0.34'
19
+ gem 'simplecov', '~> 0.10'
20
+ gem 'vcr', '~> 2.9'
21
+ gem 'webmock', '~> 1.21'
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,129 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ elasticsearch-drain (0.0.1)
5
+ aws-sdk (~> 2)
6
+ elasticsearch (~> 1.0)
7
+ thor (~> 0.19)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.3.8)
13
+ ansi (1.5.0)
14
+ ast (2.1.0)
15
+ astrolabe (1.3.1)
16
+ parser (~> 2.2)
17
+ aws-sdk (2.1.30)
18
+ aws-sdk-resources (= 2.1.30)
19
+ aws-sdk-core (2.1.30)
20
+ jmespath (~> 1.0)
21
+ aws-sdk-resources (2.1.30)
22
+ aws-sdk-core (= 2.1.30)
23
+ coderay (1.1.0)
24
+ crack (0.4.2)
25
+ safe_yaml (~> 1.0.0)
26
+ docile (1.1.5)
27
+ elasticsearch (1.0.14)
28
+ elasticsearch-api (= 1.0.14)
29
+ elasticsearch-transport (= 1.0.14)
30
+ elasticsearch-api (1.0.14)
31
+ multi_json
32
+ elasticsearch-extensions (0.0.18)
33
+ ansi
34
+ ruby-prof
35
+ elasticsearch-transport (1.0.14)
36
+ faraday
37
+ multi_json
38
+ faraday (0.9.2)
39
+ multipart-post (>= 1.2, < 3)
40
+ ffi (1.9.10)
41
+ formatador (0.2.5)
42
+ guard (2.13.0)
43
+ formatador (>= 0.2.4)
44
+ listen (>= 2.7, <= 4.0)
45
+ lumberjack (~> 1.0)
46
+ nenv (~> 0.1)
47
+ notiffany (~> 0.0)
48
+ pry (>= 0.9.12)
49
+ shellany (~> 0.0)
50
+ thor (>= 0.18.1)
51
+ guard-compat (1.2.1)
52
+ guard-minitest (2.4.4)
53
+ guard-compat (~> 1.2)
54
+ minitest (>= 3.0)
55
+ hashdiff (0.2.2)
56
+ jmespath (1.1.3)
57
+ json (1.8.3)
58
+ listen (3.0.3)
59
+ rb-fsevent (>= 0.9.3)
60
+ rb-inotify (>= 0.9)
61
+ lumberjack (1.0.9)
62
+ method_source (0.8.2)
63
+ minitest (5.8.1)
64
+ mixlib-shellout (2.2.3)
65
+ multi_json (1.11.2)
66
+ multipart-post (2.0.0)
67
+ nenv (0.2.0)
68
+ notiffany (0.0.8)
69
+ nenv (~> 0.1)
70
+ shellany (~> 0.0)
71
+ parser (2.2.3.0)
72
+ ast (>= 1.1, < 3.0)
73
+ powerpack (0.1.1)
74
+ pry (0.10.3)
75
+ coderay (~> 1.1.0)
76
+ method_source (~> 0.8.1)
77
+ slop (~> 3.4)
78
+ rainbow (2.0.0)
79
+ rake (10.4.2)
80
+ rb-fsevent (0.9.6)
81
+ rb-inotify (0.9.5)
82
+ ffi (>= 0.5.0)
83
+ rubocop (0.34.2)
84
+ astrolabe (~> 1.3)
85
+ parser (>= 2.2.2.5, < 3.0)
86
+ powerpack (~> 0.1)
87
+ rainbow (>= 1.99.1, < 3.0)
88
+ ruby-progressbar (~> 1.4)
89
+ ruby-prof (0.15.8)
90
+ ruby-progressbar (1.7.5)
91
+ rubyzip (1.1.7)
92
+ safe_yaml (1.0.4)
93
+ shellany (0.0.1)
94
+ simplecov (0.10.0)
95
+ docile (~> 1.1.0)
96
+ json (~> 1.8)
97
+ simplecov-html (~> 0.10.0)
98
+ simplecov-html (0.10.0)
99
+ slop (3.6.0)
100
+ thor (0.19.1)
101
+ thor-scmversion (1.7.0)
102
+ mixlib-shellout
103
+ thor
104
+ vcr (2.9.3)
105
+ webmock (1.22.1)
106
+ addressable (>= 2.3.6)
107
+ crack (>= 0.3.2)
108
+ hashdiff
109
+ yard (0.8.7.6)
110
+
111
+ PLATFORMS
112
+ ruby
113
+
114
+ DEPENDENCIES
115
+ bundler (~> 1.7)
116
+ elasticsearch-drain!
117
+ elasticsearch-extensions (= 0.0.18)
118
+ guard
119
+ guard-minitest
120
+ minitest (~> 5.8)
121
+ rake (~> 10.0)
122
+ rubocop (~> 0.34)
123
+ rubyzip (~> 1.1)
124
+ simplecov (~> 0.10)
125
+ thor (~> 0.19.1)
126
+ thor-scmversion (= 1.7.0)
127
+ vcr (~> 2.9)
128
+ webmock (~> 1.21)
129
+ yard (~> 0.8)
data/Guardfile ADDED
@@ -0,0 +1,42 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
21
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
22
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
23
+
24
+ # with Minitest::Spec
25
+ # watch(%r{^spec/(.*)_spec\.rb$})
26
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
27
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
28
+
29
+ # Rails 4
30
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
31
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
32
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
33
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
34
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
35
+ # watch(%r{^test/.+_test\.rb$})
36
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
37
+
38
+ # Rails < 4
39
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
40
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
41
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
42
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andrew Thompson, Rapid7 LLC.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Elasticsearch Drain
2
+
3
+ The purpose of this utility is to drain documents from Elasticsearch nodes in an AutoScaling Group.
4
+
5
+ This will help you do Elasticsearch node replacement while keeping the cluster healthy. This is useful if you want to change the instance type of your nodes, or if you use custom AMIs and need to rollout a new AMI.
6
+
7
+ Consider the following deployment procedure:
8
+ * Start with an AutoScaling Group with Elasticsearch nodes in a cluster
9
+ * Create a new AutoScaling Group with Elasticsearch nodes that join the above cluster
10
+ * Drain all data off older AutoScaling Group and remove instances from the AutoScaling Group and terminate instances
11
+
12
+
13
+ ## How does it work?
14
+ 1. Get the list of instances we want to remove from the cluster
15
+ * In this case it's an entire AutoScaling Group
16
+ 2. Ask the cluster for the `_id`(s) of those instances
17
+ 3. Then, tell the cluster to exclude these nodes from routing, which effectively removes all documents from the nodes
18
+ 4. Loop on these nodes asking the cluster how many documents each node is storing, when one reaches 0 we move on to the next step
19
+ 5. Remove the instance from the AutoScaling Group
20
+ 6. Terminate the instance
21
+ 7. Wait a moment and go back to step 4 and continue until there are 0 instances in the AutoScaling Group
22
+
23
+
24
+ ## Installation
25
+ ```bash
26
+ $ gem install elasticsearch-drain
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ 1. Create a new AutoScaling Group and populate with the same number of instances as the previous AutoScaling Group
32
+ 2. Run the tool, to start draining:
33
+ ```bash
34
+ $ drain asg --asg="test-asg-0" --region="us-east-1" --host="localhost:9200"
35
+ ```
36
+
37
+ ## What's next?
38
+ * Remove a single node from the cluster
39
+ * Drain only mode
40
+
41
+
42
+ ## Testing
43
+ Install all dependencies:
44
+ ```bash
45
+ gem install bundler
46
+ bundle install
47
+ ```
48
+
49
+ Run test tests (unit and style):
50
+ ```bash
51
+ rake
52
+ ```
53
+ Or on a tight loop with guard:
54
+ ```bash
55
+ bundle exec guard
56
+ ```
57
+
58
+ If you need to make a new http request or refresh the fixtures you will need to start a test cluster.
59
+
60
+ By default the test cluster install is version `1.7.2`, this can be changed by setting the `ES_VERSION` enviroment variable.
61
+
62
+ Install and Start the Cluster:
63
+ ```bash
64
+ rake elasticsearch:install elasticsearch:start
65
+ ```
66
+
67
+ Run the tests:
68
+ ```bash
69
+ rake test
70
+ ```
71
+
72
+ Stop the Cluster:
73
+ ```bash
74
+ rake elasticsearch:stop
75
+ ```
76
+
77
+ And, to wrap all that up:
78
+ ```bash
79
+ rake refresh_fixtures
80
+ ```
81
+
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/rapid7/elasticsearch-drain/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ require 'elasticsearch/extensions/test/cluster/tasks'
2
+ require 'elasticsearch/extensions/test/cluster'
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop/rake_task'
5
+ require 'rake/testtask'
6
+ require 'net/https'
7
+ require 'fileutils'
8
+ require 'zip'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ Rake::TestTask.new do |t|
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/test_*.rb'
15
+ end
16
+
17
+ def elasticsearch_command
18
+ path = "tmp/elasticsearch-#{ENV['ES_VERSION']}/bin/elasticsearch"
19
+ path = ::File.expand_path(path, __dir__)
20
+ "bash #{path}"
21
+ end
22
+
23
+ ENV['TEST_CLUSTER_NODES'] = '1'
24
+ ENV['TEST_CLUSTER_COMMAND'] = elasticsearch_command
25
+ ENV['ES_VERSION'] ||= '1.7.2'
26
+
27
+ namespace :elasticsearch do
28
+ task :clean do
29
+ next if File.exist? 'tmp/es.lock'
30
+ FileUtils.rm_rf 'tmp'
31
+ end
32
+
33
+ directory 'tmp'
34
+
35
+ task :install_lock do
36
+ FileUtils.touch 'tmp/es.lock'
37
+ end
38
+
39
+ # based on http://snippets.dzone.com/posts/show/2469
40
+ # http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg
41
+ desc 'Download/extract Elasticsearch archive'
42
+ task download: [:tmp] do
43
+ next if File.exist? 'tmp/es.lock'
44
+ Net::HTTP.start('download.elastic.co') do |http|
45
+ resp = http.get("/elasticsearch/elasticsearch/elasticsearch-#{ENV['ES_VERSION']}.zip")
46
+ open('tmp/es.zip', 'w') { |file| file.write(resp.body) }
47
+ end
48
+ end
49
+
50
+ task :extract do
51
+ next if File.exist? 'tmp/es.lock'
52
+ Zip::File.open('tmp/es.zip') do |zip_file|
53
+ # Handle entries one by one
54
+ zip_file.each do |entry|
55
+ # Extract to file/directory/symlink
56
+ puts "Extracting #{entry.name}"
57
+ entry.extract(::File.join('tmp', entry.name))
58
+ end
59
+ end
60
+ end
61
+
62
+ desc 'Install a test Elasticsearch Cluster in project directory'
63
+ task install: [:clean, :download, :extract, :install_lock]
64
+ end
65
+
66
+ desc 'Start/Stop Elasticsearch Cluster to refresh test fixtures'
67
+ task refresh_fixtures: ['elasticsearch:install',
68
+ 'elasticsearch:start',
69
+ 'test',
70
+ 'elasticsearch:stop']
71
+
72
+ desc 'Run unit and style tests'
73
+ task default: [:test, :rubocop]
data/Thorfile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ require 'thor/scmversion'
data/bin/drain ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative File.join(File.expand_path('../../lib', __FILE__), 'elasticsearch/drain')
3
+ ::Elasticsearch::Drain::CLI.start(ARGV)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elasticsearch/drain/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'elasticsearch-drain'
8
+ spec.version = Elasticsearch::Drain::VERSION
9
+ spec.authors = ['Andrew Thompson']
10
+ spec.email = ['Andrew_Thompson@rapid7.com']
11
+ spec.summary = %q{Elasticsearch node replacement utility that tries to keep the cluster healthy}
12
+ spec.description = %q{The purpose of this utility is to drain documents from Elasticsearch nodes in an AutoScaling Group}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+
24
+ spec.add_dependency 'elasticsearch', '~> 1.0'
25
+ spec.add_dependency 'aws-sdk', '~> 2'
26
+ spec.add_dependency 'thor', '~> 0.19'
27
+ end
@@ -0,0 +1,85 @@
1
+ module Elasticsearch
2
+ class Drain
3
+ class AutoScaling
4
+
5
+ # @attribute [r]
6
+ # EC2 AutoScaling Group name
7
+ attr_reader :asg
8
+
9
+ # @attribute [r]
10
+ # EC2 Client
11
+ attr_reader :ec2_client
12
+
13
+ def initialize(asg, region)
14
+ @asg = asg
15
+ @asg_client = Aws::AutoScaling::Client.new(region: region)
16
+ @ec2_client = Aws::EC2::Client.new(region: region)
17
+ @instances = nil
18
+ @instance_ids = nil
19
+ end
20
+
21
+ def find_instances_in_asg
22
+ instances = []
23
+ @asg_client.describe_auto_scaling_instances.each do |page|
24
+ instances << page.auto_scaling_instances.map do |i|
25
+ i.instance_id if i.auto_scaling_group_name == asg
26
+ end
27
+ end
28
+ instances.flatten!
29
+ instances.compact!
30
+ @instance_ids = instances
31
+ end
32
+
33
+ def describe_instances
34
+ instances = []
35
+ find_instances_in_asg if @instance_ids.nil?
36
+ return [] if @instance_ids.empty?
37
+ @ec2_client.describe_instances(instance_ids: @instance_ids).each do |page|
38
+ instances << page.reservations.map(&:instances)
39
+ end
40
+ instances.flatten!
41
+ @instances = instances
42
+ end
43
+
44
+ def find_private_ips
45
+ instances = describe_instances.clone
46
+ return [] if instances.nil?
47
+ instances.map!(&:private_ip_address)
48
+ instances.flatten!
49
+ instances
50
+ end
51
+
52
+ def instances
53
+ find_instances_in_asg
54
+ find_private_ips
55
+ end
56
+
57
+ def instance(ipaddress)
58
+ describe_instances if @instances.nil?
59
+ instances = @instances.clone
60
+ instances.find { |i| i.private_ip_address == ipaddress }
61
+ end
62
+
63
+ # Sets the MinSize of an AutoScalingGroup
64
+ #
65
+ # @option [FixNum] count (0) The new MinSize of the AutoScalingGroup
66
+ # @return [Struct] Empty response from the sdk
67
+ def min_size(count = 0)
68
+ @asg_client.update_auto_scaling_group(
69
+ auto_scaling_group_name: asg,
70
+ min_size: count
71
+ )
72
+ end
73
+
74
+ def detach_instance(instance_id)
75
+ resp = @asg_client.detach_instances(
76
+ instance_ids: [instance_id],
77
+ auto_scaling_group_name: asg,
78
+ should_decrement_desired_capacity: true
79
+ )
80
+ resp.activities.first.status_code == 'Successful'
81
+ end
82
+ end
83
+ end
84
+ end
85
+ require 'aws-sdk'
@@ -0,0 +1,13 @@
1
+ module Elasticsearch
2
+ class Drain
3
+ class Base
4
+ # @attribute [r]
5
+ # The Elasticsearch client object
6
+ attr_reader :client
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+ end
12
+ end
13
+ end