elbtool 0.1.0

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: fc09aa0abb9a97ca45b828af77d9fa090fbb77b3
4
+ data.tar.gz: 59c8ead9002d223ebcc626e0eafb40ec9d7d43f5
5
+ SHA512:
6
+ metadata.gz: 99af78cc533e2078671ae821d17e463791f3f451d09b87352ab14bdada5b01acc1620a5e9515512e218ef6fef51cd1790e72ef08d7d5377f13b7bf2555891f8c
7
+ data.tar.gz: f0e7c6a2860b2d4b2e7255945a64ee360164880eff5f94f637c65c2c358810c456f4f15138593fc530e0e8b099ed863c71f6247b88d1cb95f17e44a1d8675ffe
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ LATEST_CHANGELOG.md
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ LineLength:
2
+ Max: 100
3
+
4
+ MethodLength:
5
+ Max: 50
6
+
7
+ AbcSize:
8
+ Max: 30
9
+
10
+ Documentation:
11
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elbtool.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Eric Herot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Elbtool
2
+
3
+ Register and deregister instance from an AWS ELB
4
+
5
+ # Requirements
6
+
7
+ * Instance must be a member of an IAM role with the proper permissions
8
+ * `aws-sdk`
9
+
10
+ # Usage
11
+
12
+ Originally this was intended to be used by automatic reboot scripts to deregister instances before shutting them down, but it can also be triggered manually.
13
+
14
+ Note that if you are **NOT** running on the EC2 instance you want to (de-)register, you will need to specify `--instance-id INSTANCE_ID`.
15
+
16
+ ## To register an instance
17
+
18
+ ```
19
+ $ elbtool --register LOAD_BALANCER_NAME
20
+ ```
21
+
22
+ ## To de-register an instance
23
+
24
+ ```
25
+ $ elbtool --deregister LOAD_BALANCER_NAME
26
+ ```
27
+
28
+ ## Other important options
29
+
30
+ * `--register-timeout` -- Number of seconds to wait when registering an instance before giving up and returning an error. By default this is set to the health check `interval` times `healthy_threshold` (the number of times an instance must return a successful health check to be considered health) plus 15 seconds (for buffer)
31
+ * `--deregister-timeout` -- Number of seconds to wait when de-registering an instance before giving up and returning an error. If connection draining is enabled, this waits for `connection_draining` + 5 seconds. If not, it just waits 5 seconds (for buffer).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elbtool"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/elbtool.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elbtool/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elbtool"
8
+ spec.version = Elbtool::VERSION
9
+ spec.authors = ["Eric Herot"]
10
+ spec.email = ["eric.github@herot.com"]
11
+
12
+ spec.summary = %q{Register and reregister EC2 instances from an ELB}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'aws-sdk'
23
+ spec.add_dependency 'trollop'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "byebug"
30
+ end
data/exe/elbtool ADDED
@@ -0,0 +1,3 @@
1
+ require 'elbtool'
2
+
3
+ Elbtool.run
@@ -0,0 +1,3 @@
1
+ class Elbtool
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/elbtool.rb ADDED
@@ -0,0 +1,126 @@
1
+ require 'elbtool/version'
2
+ require 'trollop'
3
+ require 'aws-sdk'
4
+ require 'net/http'
5
+ require 'byebug'
6
+
7
+ class Elbtool
8
+ def self.run
9
+ Elbtool.new.run
10
+ end
11
+
12
+ def run
13
+ return register if opts[:register]
14
+ deregister
15
+ puts 'Done'
16
+ end
17
+
18
+ private
19
+
20
+ def register
21
+ elb.register_instances_with_load_balancer(
22
+ instances: [{ instance_id: instance_id }],
23
+ load_balancer_name: load_balancer_name
24
+ )
25
+
26
+ print 'Waiting for instance to become healthy...'
27
+
28
+ register_timeout.times do
29
+ return if instance_health.state == 'InService'
30
+ print '.'
31
+ sleep 1
32
+ end
33
+
34
+ h_o = instance_health
35
+
36
+ fail "Instance failed to enter state 'InService' within #{register_timeout} seconds \n" \
37
+ "(state: #{h_o.state}; reason_code: #{h_o.reason_code}; description: #{h_o.description})"
38
+ end
39
+
40
+ def deregister
41
+ elb.deregister_instances_from_load_balancer(
42
+ instances: [{ instance_id: instance_id }],
43
+ load_balancer_name: load_balancer_name
44
+ )
45
+
46
+ print 'Waiting for instance to deregister...'
47
+
48
+ deregister_timeout.times do
49
+ return unless instance_health
50
+ print '.'
51
+ sleep 1
52
+ end
53
+
54
+ h_o = instance_health
55
+
56
+ fail "Instance failed to deregister within #{deregister_timeout} seconds \n" \
57
+ "(state: #{h_o.state}; reason_code: #{h_o.reason_code}; description: #{h_o.description})"
58
+ end
59
+
60
+ def instance_id
61
+ @instance_id ||=
62
+ opts[:instance_id] || begin
63
+ uri = URI 'http://169.254.169.254/2016-06-30/meta-data/instance-id'
64
+ Net::HTTP.start(uri.host, uri.port, open_timeout: 1) { |http| http.get uri.path }.body
65
+ rescue Net::OpenTimeout
66
+ puts '`instance_id` could not be retrieved from instance metadata. Please specify ' \
67
+ '--instance-id if you are not running on an ec2 instance.'
68
+ exit 1
69
+ end
70
+ end
71
+
72
+ def instance_health
73
+ elb.describe_instance_health(load_balancer_name: load_balancer_name).instance_states.find do |i|
74
+ i['instance_id'] == instance_id
75
+ end
76
+ end
77
+
78
+ def elb
79
+ @elb ||= Aws::ElasticLoadBalancing::Client.new
80
+ end
81
+
82
+ def load_balancer_name
83
+ @load_balancer_name ||= (opts[:register] || opts[:deregister])
84
+ end
85
+
86
+ def register_timeout
87
+ @register_timeout ||= begin
88
+ return opts[:register_timeout] if opts[:register_timeout]
89
+ health_check_obj = elb.describe_load_balancers(
90
+ load_balancer_names: [load_balancer_name]
91
+ ).load_balancer_descriptions.first.health_check
92
+
93
+ # Base the register_timeout on the existing load balancer configuration
94
+ health_check_obj.interval * health_check_obj.healthy_threshold + 15
95
+ end
96
+ end
97
+
98
+ def deregister_timeout
99
+ @deregister_timeout ||= begin
100
+ return opts[:deregister_timeout] if opts[:deregister_timeout]
101
+ c_d = elb.describe_load_balancer_attributes(
102
+ load_balancer_name: load_balancer_name
103
+ ).load_balancer_attributes.connection_draining
104
+ (c_d.enabled ? c_d.timeout : 0) + 5
105
+ end
106
+ end
107
+
108
+ def opts
109
+ @opts ||= Trollop.options do
110
+ opt :register, 'Register node with the specified ELB', short: 'r', type: String
111
+ opt :deregister, 'De-register node from the specified ELB', short: 'd', type: String
112
+ opt :instance_id, 'Instance ID (retrieved from local metadata by default)',
113
+ short: 'i',
114
+ type: String
115
+ opt :register_timeout,
116
+ 'Registration timeout in seconds (defaults to `interval` * `healthy_threshold` + 15)',
117
+ type: Integer
118
+ opt :deregister_timeout,
119
+ 'De-registration timeout in seconds (defaults to `connection_draining` timeout + 5)',
120
+ type: Integer
121
+ end
122
+
123
+ return @opts if @opts[:register] || @opts[:deregister]
124
+ Trollop.die 'Please specify either --register or --deregister'
125
+ end
126
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elbtool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Herot
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: trollop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - eric.github@herot.com
114
+ executables:
115
+ - elbtool
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - elbtool.gemspec
130
+ - exe/elbtool
131
+ - lib/elbtool.rb
132
+ - lib/elbtool/version.rb
133
+ homepage:
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Register and reregister EC2 instances from an ELB
157
+ test_files: []