asg-rebooter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/asg-rebooter.gemspec +46 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/asg-rebooter +29 -0
- data/lib/asg/rebooter.rb +23 -0
- data/lib/asg/rebooter/configuration/auto_scaling_group.rb +69 -0
- data/lib/asg/rebooter/configuration/target_group.rb +34 -0
- data/lib/asg/rebooter/instance_rebooter.rb +68 -0
- data/lib/asg/rebooter/validate_credentials.rb +6 -0
- data/lib/asg/rebooter/version.rb +5 -0
- data/lib/asg/rebooter/wrappers/auto_scaling_group.rb +59 -0
- data/lib/asg/rebooter/wrappers/instance.rb +117 -0
- data/lib/asg/rebooter/wrappers/target_group.rb +72 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ac27241a28fdcc9c6371c1eb378a67ac6350bc7f232d50fef56228ae622aa50f
|
4
|
+
data.tar.gz: 1eb4afaa574d39eef83a5aac53de1307e25418c6733fda43970bd7f5f71853c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd8c0747be0c121dfbb86a389f7466edeec5db0fbf3eff3729c6918cbdc3bc34d63e655b69a49a11e75be086688378ab03416dd3e9220d457517ec86050be5dc
|
7
|
+
data.tar.gz: bee3f141d30672812527be0a3e43c50e7d1fd4ab29d1df55e13f8fa452f7c3b48235208524975b645b10f6ff102e9460686657485ee583965e3b16385322cce7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tom@hackerone.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
asg-rebooter (0.1.0)
|
5
|
+
aws-sdk-autoscaling (~> 1.0)
|
6
|
+
aws-sdk-ec2 (~> 1.0)
|
7
|
+
aws-sdk-elasticloadbalancingv2 (~> 1.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
aws-eventstream (1.0.3)
|
13
|
+
aws-partitions (1.256.0)
|
14
|
+
aws-sdk-autoscaling (1.30.0)
|
15
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
16
|
+
aws-sigv4 (~> 1.1)
|
17
|
+
aws-sdk-core (3.86.0)
|
18
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
19
|
+
aws-partitions (~> 1, >= 1.239.0)
|
20
|
+
aws-sigv4 (~> 1.1)
|
21
|
+
jmespath (~> 1.0)
|
22
|
+
aws-sdk-ec2 (1.126.0)
|
23
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
24
|
+
aws-sigv4 (~> 1.1)
|
25
|
+
aws-sdk-elasticloadbalancingv2 (1.39.0)
|
26
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
27
|
+
aws-sigv4 (~> 1.1)
|
28
|
+
aws-sigv4 (1.1.0)
|
29
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
30
|
+
diff-lcs (1.3)
|
31
|
+
jmespath (1.4.0)
|
32
|
+
rake (10.5.0)
|
33
|
+
rspec (3.9.0)
|
34
|
+
rspec-core (~> 3.9.0)
|
35
|
+
rspec-expectations (~> 3.9.0)
|
36
|
+
rspec-mocks (~> 3.9.0)
|
37
|
+
rspec-core (3.9.0)
|
38
|
+
rspec-support (~> 3.9.0)
|
39
|
+
rspec-expectations (3.9.0)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.9.0)
|
42
|
+
rspec-mocks (3.9.0)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.9.0)
|
45
|
+
rspec-support (3.9.0)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
ruby
|
49
|
+
|
50
|
+
DEPENDENCIES
|
51
|
+
asg-rebooter!
|
52
|
+
bundler (~> 1.17)
|
53
|
+
rake (~> 10.0)
|
54
|
+
rspec (~> 3.0)
|
55
|
+
|
56
|
+
BUNDLED WITH
|
57
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Tom de Vries
|
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,43 @@
|
|
1
|
+
# Asg::Rebooter
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/asg/rebooter`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'asg-rebooter'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install asg-rebooter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/asg-rebooter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Asg::Rebooter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/asg-rebooter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "asg/rebooter/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "asg-rebooter"
|
8
|
+
spec.version = Asg::Rebooter::VERSION
|
9
|
+
spec.authors = ["Tom de Vries"]
|
10
|
+
spec.email = ["tom@hackerone.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Gracefully restart EC2 instances within an Auto Scaling Group.}
|
13
|
+
spec.description = %q{See summary.}
|
14
|
+
spec.homepage = "https://github.com/tomdev/asg-rebooter"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
24
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_dependency "aws-sdk-autoscaling", "~> 1.0"
|
40
|
+
spec.add_dependency "aws-sdk-ec2", "~> 1.0"
|
41
|
+
spec.add_dependency "aws-sdk-elasticloadbalancingv2", "~> 1.0"
|
42
|
+
|
43
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
44
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
45
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
46
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "asg/rebooter"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/exe/asg-rebooter
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'asg-rebooter'
|
4
|
+
|
5
|
+
argument_present = ARGV.length > 0;
|
6
|
+
|
7
|
+
if argument_present
|
8
|
+
asg_name = ARGV[0]
|
9
|
+
else
|
10
|
+
puts """
|
11
|
+
asg-rebooter requires
|
12
|
+
|
13
|
+
usage: asg-rebooter <auto_scaling_group_name>
|
14
|
+
"""
|
15
|
+
|
16
|
+
exit 1
|
17
|
+
end
|
18
|
+
|
19
|
+
if ENV['REBOOT']
|
20
|
+
asg = Asg::Rebooter::Wrappers::AutoScalingGroup.new(asg_name).reboot
|
21
|
+
else
|
22
|
+
puts """
|
23
|
+
defaulting to dry-run: asg-rebooter would have rebooted AutoScalingGroup with name \"#{asg_name}\"
|
24
|
+
|
25
|
+
To start the reboot procedure use the following command:
|
26
|
+
|
27
|
+
REBOOT=true asg-rebooter #{asg_name}
|
28
|
+
"""
|
29
|
+
end
|
data/lib/asg/rebooter.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'aws-sdk-autoscaling'
|
2
|
+
require 'aws-sdk-ec2'
|
3
|
+
require 'aws-sdk-elasticloadbalancingv2'
|
4
|
+
|
5
|
+
require 'asg/rebooter/version'
|
6
|
+
require 'asg/rebooter/configuration/auto_scaling_group'
|
7
|
+
require 'asg/rebooter/configuration/target_group'
|
8
|
+
require 'asg/rebooter/instance_rebooter'
|
9
|
+
require 'asg/rebooter/wrappers/auto_scaling_group'
|
10
|
+
require 'asg/rebooter/wrappers/instance'
|
11
|
+
require 'asg/rebooter/wrappers/target_group'
|
12
|
+
|
13
|
+
module Asg
|
14
|
+
module Rebooter
|
15
|
+
class Error < StandardError; end
|
16
|
+
|
17
|
+
class Work
|
18
|
+
def execute
|
19
|
+
puts 'Doing all the hard work for you'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Asg
|
2
|
+
module Rebooter
|
3
|
+
module Configuration
|
4
|
+
class AutoScalingGroup
|
5
|
+
ASG_MIN_SIZE_DELTA = 1
|
6
|
+
AZREBALANCE = 'AZRebalance'
|
7
|
+
|
8
|
+
attr_reader :asg, :original_min_size, :original_suspended_processes
|
9
|
+
|
10
|
+
def initialize(asg)
|
11
|
+
@asg = asg
|
12
|
+
|
13
|
+
set_original_values
|
14
|
+
end
|
15
|
+
|
16
|
+
def prepare
|
17
|
+
suspend_az_rebalance
|
18
|
+
decrease_min_size
|
19
|
+
end
|
20
|
+
|
21
|
+
def restore
|
22
|
+
restore_az_rebalance
|
23
|
+
restore_min_size
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def set_original_values
|
29
|
+
@original_min_size = asg.min_size
|
30
|
+
@original_suspended_processes = asg.suspended_processes
|
31
|
+
end
|
32
|
+
|
33
|
+
def suspend_az_rebalance
|
34
|
+
return unless adjust_az_rebalance?
|
35
|
+
|
36
|
+
asg.suspend_processes([AZREBALANCE])
|
37
|
+
end
|
38
|
+
|
39
|
+
def restore_az_rebalance
|
40
|
+
return unless adjust_az_rebalance?
|
41
|
+
|
42
|
+
asg.resume_processes([AZREBALANCE])
|
43
|
+
end
|
44
|
+
|
45
|
+
def decrease_min_size
|
46
|
+
return unless adjust_min_size?
|
47
|
+
|
48
|
+
new_min_size = @original_min_size - ASG_MIN_SIZE_DELTA
|
49
|
+
|
50
|
+
asg.update_min_size(new_min_size)
|
51
|
+
end
|
52
|
+
|
53
|
+
def restore_min_size
|
54
|
+
return unless adjust_min_size?
|
55
|
+
|
56
|
+
asg.update_min_size(original_min_size)
|
57
|
+
end
|
58
|
+
|
59
|
+
def adjust_min_size?
|
60
|
+
@original_min_size > 0
|
61
|
+
end
|
62
|
+
|
63
|
+
def adjust_az_rebalance?
|
64
|
+
!original_suspended_processes.include?(AZREBALANCE)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Asg
|
2
|
+
module Rebooter
|
3
|
+
module Configuration
|
4
|
+
class TargetGroup
|
5
|
+
DEREGISTRATION_DELAY_DURING_REBOOT_PROCEDURE = '60'
|
6
|
+
|
7
|
+
DEREGISTRATION_CONFIG = [
|
8
|
+
key: 'deregistration_delay.timeout_seconds',
|
9
|
+
value: DEREGISTRATION_DELAY_DURING_REBOOT_PROCEDURE
|
10
|
+
]
|
11
|
+
|
12
|
+
attr_reader :target_group, :original_attributes
|
13
|
+
|
14
|
+
def initialize(target_group)
|
15
|
+
@target_group = target_group
|
16
|
+
|
17
|
+
@original_attributes = target_group.attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
def prepare
|
21
|
+
target_group.update_attributes(DEREGISTRATION_CONFIG)
|
22
|
+
|
23
|
+
# Validate?
|
24
|
+
end
|
25
|
+
|
26
|
+
def restore
|
27
|
+
target_group.update_attributes(original_attributes)
|
28
|
+
|
29
|
+
# Validate?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Asg
|
2
|
+
module Rebooter
|
3
|
+
class InstanceRebooter
|
4
|
+
REBOOT_TIMEOUT = 60
|
5
|
+
|
6
|
+
attr_reader :asg, :asg_config, :target_group_config
|
7
|
+
|
8
|
+
def initialize(asg)
|
9
|
+
@asg = asg
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute
|
13
|
+
puts "[start] Starting reboot sequence for: #{asg.name}"
|
14
|
+
|
15
|
+
begin
|
16
|
+
prepare_configuration
|
17
|
+
|
18
|
+
instances.each do |instance|
|
19
|
+
puts "#{'*' * 4} #{instance.instance_id} #{'*' * 4}"
|
20
|
+
puts "#{instance.instance_id} [enter_standby]"
|
21
|
+
instance.enter_standby
|
22
|
+
|
23
|
+
puts "#{instance.instance_id} [reboot]"
|
24
|
+
instance.reboot
|
25
|
+
|
26
|
+
sleep REBOOT_TIMEOUT # TODO: Ensure instance is rebooted: how to do this?
|
27
|
+
|
28
|
+
puts "#{instance.instance_id} [exit_standby]"
|
29
|
+
instance.exit_standby
|
30
|
+
|
31
|
+
puts "#{instance.instance_id} [wait_for_healthy]"
|
32
|
+
target_group.wait_for_healthy(instance)
|
33
|
+
|
34
|
+
puts "\n"
|
35
|
+
end
|
36
|
+
ensure
|
37
|
+
restore_configuration
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def instances
|
44
|
+
asg.instances
|
45
|
+
end
|
46
|
+
|
47
|
+
def target_group
|
48
|
+
asg.target_groups.first
|
49
|
+
end
|
50
|
+
|
51
|
+
def prepare_configuration
|
52
|
+
@asg_config = Asg::Rebooter::Configuration::AutoScalingGroup.new(asg)
|
53
|
+
|
54
|
+
@target_group_config = asg.target_groups.map do |target_group|
|
55
|
+
Asg::Rebooter::Configuration::TargetGroup.new(target_group)
|
56
|
+
end
|
57
|
+
|
58
|
+
asg_config.prepare
|
59
|
+
target_group_config.each(&:prepare)
|
60
|
+
end
|
61
|
+
|
62
|
+
def restore_configuration
|
63
|
+
asg_config.restore
|
64
|
+
target_group_config.each(&:restore)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Asg
|
2
|
+
module Rebooter
|
3
|
+
module Wrappers
|
4
|
+
class AutoScalingGroupNameRequiredError < StandardError; end
|
5
|
+
|
6
|
+
class AutoScalingGroup
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
raise AutoScalingGroupNameRequiredError unless name
|
11
|
+
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def suspended_processes
|
16
|
+
asg.suspended_processes
|
17
|
+
end
|
18
|
+
|
19
|
+
def suspend_processes(processes)
|
20
|
+
asg.suspend_processes(scaling_processes: processes)
|
21
|
+
end
|
22
|
+
|
23
|
+
def resume_processes(processes)
|
24
|
+
asg.resume_processes(scaling_processes: processes)
|
25
|
+
end
|
26
|
+
|
27
|
+
def min_size
|
28
|
+
asg.min_size
|
29
|
+
end
|
30
|
+
|
31
|
+
def update_min_size(new_min_size)
|
32
|
+
asg.update(min_size: new_min_size)
|
33
|
+
end
|
34
|
+
|
35
|
+
def target_groups
|
36
|
+
asg.target_group_arns.map do |arn|
|
37
|
+
Asg::Rebooter::Wrappers::TargetGroup.new(arn)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def instances
|
42
|
+
asg.instances.map do |instance|
|
43
|
+
Asg::Rebooter::Wrappers::Instance.new(instance)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def reboot
|
48
|
+
Asg::Rebooter::InstanceRebooter.new(self).execute
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def asg
|
54
|
+
Aws::AutoScaling::AutoScalingGroup.new(name)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# Example response from AWS for Aws::AutoScaling::Instance:
|
2
|
+
#
|
3
|
+
# #<Aws::AutoScaling::Instance:0x00007fc8a2781438
|
4
|
+
# @client=#<Aws::AutoScaling::Client>,
|
5
|
+
# @data=
|
6
|
+
# #<struct Aws::AutoScaling::Types::Instance
|
7
|
+
# instance_id="i-007b9fa2f7df584f7",
|
8
|
+
# instance_type="t3.medium",
|
9
|
+
# availability_zone="us-west-2b",
|
10
|
+
# lifecycle_state="InService",
|
11
|
+
# health_status="Healthy",
|
12
|
+
# launch_configuration_name=
|
13
|
+
# "h1-testserver-lc-asg-rebooter-20191219124104839000000001",
|
14
|
+
# launch_template=nil,
|
15
|
+
# protected_from_scale_in=false,
|
16
|
+
# weighted_capacity=nil>,
|
17
|
+
# @group_name="h1-testserver-asg-asg-rebooter",
|
18
|
+
# @id="i-007b9fa2f7df584f7">
|
19
|
+
|
20
|
+
module Asg
|
21
|
+
module Rebooter
|
22
|
+
module Wrappers
|
23
|
+
class Instance
|
24
|
+
LIFECYCLE_STANDBY = 'Standby'
|
25
|
+
LIFECYCLE_IN_SERVICE = 'InService'
|
26
|
+
LIFECYCLE_CHANGE_POLL_TIMEOUT = 4
|
27
|
+
|
28
|
+
HEALTH_STATUS_HEALTHY = 'HEALTHY'
|
29
|
+
|
30
|
+
SLEEP_AFTER_REBOOT = 30
|
31
|
+
SLEEP_AFTER_STANDBY = 10
|
32
|
+
|
33
|
+
POLL_TIMEOUT = 4
|
34
|
+
|
35
|
+
attr_reader :instance
|
36
|
+
|
37
|
+
def initialize(instance)
|
38
|
+
#
|
39
|
+
# @instance is of type Aws::AutoScaling::Instance
|
40
|
+
#
|
41
|
+
# #ec2_instance is of type Aws::EC2::Instance
|
42
|
+
#
|
43
|
+
@instance = instance
|
44
|
+
end
|
45
|
+
|
46
|
+
def method_missing(m, *args, &block)
|
47
|
+
instance.send(m, *args, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def enter_standby
|
51
|
+
# raise when in invalid state for action
|
52
|
+
|
53
|
+
instance.enter_standby(should_decrement_desired_capacity: true)
|
54
|
+
|
55
|
+
ensure_lifecycle_state(LIFECYCLE_STANDBY)
|
56
|
+
end
|
57
|
+
|
58
|
+
def exit_standby
|
59
|
+
# raise if in invalid state for action
|
60
|
+
|
61
|
+
instance.exit_standby
|
62
|
+
|
63
|
+
ensure_lifecycle_state(LIFECYCLE_IN_SERVICE)
|
64
|
+
end
|
65
|
+
|
66
|
+
def ensure_lifecycle_state(state)
|
67
|
+
loop do
|
68
|
+
instance.reload
|
69
|
+
|
70
|
+
break if instance.lifecycle_state == state
|
71
|
+
|
72
|
+
sleep LIFECYCLE_CHANGE_POLL_TIMEOUT
|
73
|
+
end
|
74
|
+
|
75
|
+
true
|
76
|
+
end
|
77
|
+
|
78
|
+
def standby?
|
79
|
+
instance.reload
|
80
|
+
|
81
|
+
instance.lifecycle_state == LIFECYCLE_STANDBY
|
82
|
+
end
|
83
|
+
|
84
|
+
def health_status
|
85
|
+
instance.reload.health_status
|
86
|
+
end
|
87
|
+
|
88
|
+
def healthy?
|
89
|
+
health_status == HEALTH_STATUS_HEALTHY
|
90
|
+
end
|
91
|
+
|
92
|
+
def wait_for_healthy
|
93
|
+
#
|
94
|
+
# TODO: Should check health state on the TargetGroup to ensure the instance is ready for work
|
95
|
+
#
|
96
|
+
loop do
|
97
|
+
break if healthy?
|
98
|
+
|
99
|
+
sleep(POLL_TIMEOUT)
|
100
|
+
end
|
101
|
+
|
102
|
+
true
|
103
|
+
end
|
104
|
+
|
105
|
+
def reboot
|
106
|
+
ec2_instance.reboot
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def ec2_instance
|
112
|
+
Aws::EC2::Instance.new(instance.instance_id)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Asg
|
2
|
+
module Rebooter
|
3
|
+
module Wrappers
|
4
|
+
class TargetGroupArnRequiredError < StandardError; end
|
5
|
+
|
6
|
+
class TargetGroup
|
7
|
+
TARGET_GROUP_INSTANCE_HEALTHY = 'healthy'
|
8
|
+
POLL_TIMEOUT = 4
|
9
|
+
|
10
|
+
attr_reader :arn
|
11
|
+
|
12
|
+
def initialize(arn)
|
13
|
+
raise TargetGroupArnRequiredError unless arn
|
14
|
+
|
15
|
+
@arn = arn
|
16
|
+
end
|
17
|
+
|
18
|
+
def attributes
|
19
|
+
target_group_attributes = client.describe_target_group_attributes(target_group_arn: arn)
|
20
|
+
target_group_attributes.attributes.map(&:to_hash)
|
21
|
+
end
|
22
|
+
|
23
|
+
def restore_attributes
|
24
|
+
update_attributes(original_attributes)
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_attributes(attributes_to_update)
|
28
|
+
client.modify_target_group_attributes \
|
29
|
+
target_group_arn: arn,
|
30
|
+
attributes: attributes_to_update
|
31
|
+
end
|
32
|
+
|
33
|
+
def target_health_descriptions
|
34
|
+
client.describe_target_health(target_group_arn: arn).target_health_descriptions
|
35
|
+
end
|
36
|
+
|
37
|
+
def health_for_instance(instance)
|
38
|
+
#
|
39
|
+
# TODO: Write specs and error handling
|
40
|
+
#
|
41
|
+
health_description_for_instance = target_health_descriptions.keep_if do |target_health_description|
|
42
|
+
target_health_description.target.id == instance.id
|
43
|
+
end
|
44
|
+
|
45
|
+
if health_description_for_instance.any?
|
46
|
+
health_description_for_instance.first.target_health.state
|
47
|
+
else
|
48
|
+
'unknown'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def wait_for_healthy(instance)
|
53
|
+
loop do
|
54
|
+
healthy = health_for_instance(instance) == TARGET_GROUP_INSTANCE_HEALTHY
|
55
|
+
|
56
|
+
break if healthy
|
57
|
+
|
58
|
+
sleep(POLL_TIMEOUT)
|
59
|
+
end
|
60
|
+
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def client
|
67
|
+
@client ||= Aws::ElasticLoadBalancingV2::Client.new
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asg-rebooter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom de Vries
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-autoscaling
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk-ec2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: aws-sdk-elasticloadbalancingv2
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.17'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.17'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: See summary.
|
98
|
+
email:
|
99
|
+
- tom@hackerone.com
|
100
|
+
executables:
|
101
|
+
- asg-rebooter
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- asg-rebooter.gemspec
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- exe/asg-rebooter
|
118
|
+
- lib/asg/rebooter.rb
|
119
|
+
- lib/asg/rebooter/configuration/auto_scaling_group.rb
|
120
|
+
- lib/asg/rebooter/configuration/target_group.rb
|
121
|
+
- lib/asg/rebooter/instance_rebooter.rb
|
122
|
+
- lib/asg/rebooter/validate_credentials.rb
|
123
|
+
- lib/asg/rebooter/version.rb
|
124
|
+
- lib/asg/rebooter/wrappers/auto_scaling_group.rb
|
125
|
+
- lib/asg/rebooter/wrappers/instance.rb
|
126
|
+
- lib/asg/rebooter/wrappers/target_group.rb
|
127
|
+
homepage: https://github.com/tomdev/asg-rebooter
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata:
|
131
|
+
allowed_push_host: https://rubygems.org
|
132
|
+
homepage_uri: https://github.com/tomdev/asg-rebooter
|
133
|
+
source_code_uri: https://github.com/tomdev/asg-rebooter
|
134
|
+
changelog_uri: https://github.com/tomdev/asg-rebooter
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubygems_version: 3.0.3
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Gracefully restart EC2 instances within an Auto Scaling Group.
|
154
|
+
test_files: []
|