cfn-status 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9955f7649ab904d7ecc3625a47db9d22badcf19cb1eec2d66a58781bfb8a3d1a
4
+ data.tar.gz: de4d99a18f7f1b0591647192b6c1832209c9eee7272f5fd855b189de7bc05a6a
5
+ SHA512:
6
+ metadata.gz: bcf0f0b4a676cdd04869c17a4023ab7025e26dd11b3784f5189e7a63ef16d65a87c6ded748002b1955efc9905d7d95153f9a9085b0972f1d4c51adc76a6c4014
7
+ data.tar.gz: 2cb014ef87a89ac99a63baedba39b62c5ac4f3d711a3488b3070d0a740c4aa90fbd1e84176581f0463c348ee380f98074ffa7bf68154c768bf6bb05b88888da3
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,7 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
+
6
+ ## [0.1.0]
7
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cfn-status.gemspec
4
+ gemspec
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cfn-status (0.1.0)
5
+ aws-sdk-cloudformation
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ aws-eventstream (1.0.3)
11
+ aws-partitions (1.170.0)
12
+ aws-sdk-cloudformation (1.22.0)
13
+ aws-sdk-core (~> 3, >= 3.53.0)
14
+ aws-sigv4 (~> 1.1)
15
+ aws-sdk-core (3.54.1)
16
+ aws-eventstream (~> 1.0, >= 1.0.2)
17
+ aws-partitions (~> 1.0)
18
+ aws-sigv4 (~> 1.1)
19
+ jmespath (~> 1.0)
20
+ aws-sigv4 (1.1.0)
21
+ aws-eventstream (~> 1.0, >= 1.0.2)
22
+ diff-lcs (1.3)
23
+ jmespath (1.4.0)
24
+ rake (10.5.0)
25
+ rspec (3.8.0)
26
+ rspec-core (~> 3.8.0)
27
+ rspec-expectations (~> 3.8.0)
28
+ rspec-mocks (~> 3.8.0)
29
+ rspec-core (3.8.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-expectations (3.8.3)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-mocks (3.8.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-support (3.8.0)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ bundler (~> 2.0)
44
+ cfn-status!
45
+ rake (~> 10.0)
46
+ rspec (~> 3.0)
47
+
48
+ BUNDLED WITH
49
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Tung Nguyen
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.
@@ -0,0 +1,56 @@
1
+ # Cfn Status
2
+
3
+ Helper library provides status of CloudFormation stack.
4
+
5
+ ## Usage
6
+
7
+ Add this line to your gem's gemspec:
8
+
9
+ ```ruby
10
+ gem.add_development_dependency "cfn-status"
11
+ ```
12
+
13
+ Require it to your library:
14
+
15
+ ```ruby
16
+ require "cfn/status"
17
+ ```
18
+
19
+ Use like so:
20
+
21
+ ```ruby
22
+ status = Cfn::Status.new(stack_name)
23
+ status.run # prints out stack events
24
+ ```
25
+
26
+ The `status.run` will:
27
+
28
+ * print out the most recent stack events and return right away if the stack is in a completed state.
29
+ * print out the most recent stack events and poll for more events until the stack in a completed state.
30
+
31
+ To find out whether the most recent completed state of the stack was a success or a fail, you can use `status.success?`.
32
+
33
+ ```ruby
34
+ status.success?
35
+ ```
36
+
37
+ If you need to just wait for the stack to complete, you can also use `status.wait`.
38
+
39
+ ```ruby
40
+ status.wait
41
+ status.success?
42
+ ```
43
+
44
+ ## Development
45
+
46
+ 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.
47
+
48
+ 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).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cfn-status.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cfn/status"
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__)
@@ -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
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cfn/status/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cfn-status"
8
+ spec.version = Cfn::Status::VERSION
9
+ spec.authors = ["Tung Nguyen"]
10
+ spec.email = ["tongueroo@gmail.com"]
11
+
12
+ spec.summary = "CloudFormation status library"
13
+ spec.homepage = "https://github.com/tongueroo/cfn-status"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "aws-sdk-cloudformation"
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,51 @@
1
+ require "aws-sdk-cloudformation"
2
+
3
+ module Cfn
4
+ module AwsService
5
+ def cfn
6
+ @cfn ||= Aws::CloudFormation::Client.new
7
+ end
8
+
9
+ def stack_exists?(stack_name)
10
+ return true if ENV['TEST']
11
+ return false if @options[:noop]
12
+
13
+ exist = nil
14
+ begin
15
+ # When the stack does not exist an exception is raised. Example:
16
+ # Aws::CloudFormation::Errors::ValidationError: Stack with id blah does not exist
17
+ cfn.describe_stacks(stack_name: stack_name)
18
+ exist = true
19
+ rescue Aws::CloudFormation::Errors::ValidationError => e
20
+ if e.message =~ /does not exist/
21
+ exist = false
22
+ elsif e.message.include?("'stackName' failed to satisfy constraint")
23
+ # Example of e.message when describe_stack with invalid stack name
24
+ # "1 validation error detected: Value 'instance_and_route53' at 'stackName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]*"
25
+ puts "Invalid stack name: #{stack_name}"
26
+ puts "Full error message: #{e.message}"
27
+ exit 1
28
+ else
29
+ raise # re-raise exception because unsure what other errors can happen
30
+ end
31
+ end
32
+ exist
33
+ end
34
+
35
+ def find_stack(stack_name)
36
+ resp = cfn.describe_stacks(stack_name: stack_name)
37
+ resp.stacks.first
38
+ rescue Aws::CloudFormation::Errors::ValidationError => e
39
+ # example: Stack with id demo-web does not exist
40
+ if e.message =~ /Stack with/ && e.message =~ /does not exist/
41
+ nil
42
+ else
43
+ raise
44
+ end
45
+ end
46
+
47
+ def rollback_complete?(stack)
48
+ stack.stack_status == 'ROLLBACK_COMPLETE'
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,219 @@
1
+ require "cfn/status/version"
2
+
3
+ module Cfn
4
+ autoload :AwsService, "cfn/aws_service"
5
+
6
+ class Status
7
+ class Error < StandardError; end
8
+
9
+ include AwsService
10
+
11
+ attr_reader :events
12
+ def initialize(stack_name, options={})
13
+ @stack_name = stack_name
14
+ @options = options
15
+ reset
16
+ end
17
+
18
+ def run
19
+ unless stack_exists?(@stack_name)
20
+ puts "The stack #{@stack_name.color(:green)} does not exist."
21
+ return true
22
+ end
23
+
24
+ resp = cfn.describe_stacks(stack_name: @stack_name)
25
+ stack = resp.stacks.first
26
+
27
+ puts "The current status for the stack #{@stack_name.color(:green)} is #{stack.stack_status.color(:green)}"
28
+ if stack.stack_status =~ /_IN_PROGRESS$/
29
+ puts "Stack events (tailing):"
30
+ # tail all events until done
31
+ @hide_time_took = true
32
+ wait
33
+ else
34
+ puts "Stack events:"
35
+ # show the last events that was user initiated
36
+ refresh_events
37
+ show_events(true)
38
+ end
39
+ success?
40
+ end
41
+
42
+ def reset
43
+ @events = [] # constantly replaced with recent events
44
+ @last_shown_event_id = nil
45
+ @stack_deletion_completed = nil
46
+ end
47
+
48
+ # check for /(_COMPLETE|_FAILED)$/ status
49
+ def wait
50
+ puts "Waiting for stack to complete"
51
+ start_time = Time.now
52
+
53
+ refresh_events
54
+ until completed || @stack_deletion_completed
55
+ show_events
56
+ end
57
+ show_events(true) # show the final event
58
+
59
+ if @stack_deletion_completed
60
+ puts "Stack #{@stack_name} deleted."
61
+ return
62
+ end
63
+
64
+ if last_event_status =~ /_FAILED/
65
+ puts "Stack failed: #{last_event_status}".color(:red)
66
+ puts "Stack reason #{@events[0]["resource_status_reason"]}".color(:red)
67
+ elsif last_event_status =~ /_ROLLBACK_/
68
+ puts "Stack rolled back: #{last_event_status}".color(:red)
69
+ else # success
70
+ puts "Stack success status: #{last_event_status}".color(:green)
71
+ end
72
+
73
+ # Never gets here when deleting a stack because the describe stack returns nothing
74
+ # once the stack is deleted. Gets here for stack create and update though.
75
+ return if @hide_time_took # set in run
76
+ took = Time.now - start_time
77
+ puts "Time took for stack deployment: #{pretty_time(took).color(:green)}."
78
+ success?
79
+ end
80
+
81
+ def completed
82
+ last_event_status =~ /(_COMPLETE|_FAILED)$/ &&
83
+ @events[0]["logical_resource_id"] == @stack_name &&
84
+ @events[0]["resource_type"] == "AWS::CloudFormation::Stack"
85
+ end
86
+
87
+ def last_event_status
88
+ @events[0]["resource_status"]
89
+ end
90
+
91
+ # Only shows new events
92
+ def show_events(final=false)
93
+ if @last_shown_event_id.nil?
94
+ i = find_index(:start, final)
95
+ print_events(i)
96
+ else
97
+ i = find_index(:last_shown, final)
98
+ # puts "last_shown index #{i}"
99
+ print_events(i-1) unless i == 0
100
+ end
101
+
102
+ return if final
103
+ sleep 5 unless ENV['TEST']
104
+ refresh_events
105
+ end
106
+
107
+ def print_events(i)
108
+ @events[0..i].reverse.each do |e|
109
+ print_event(e)
110
+ end
111
+ @last_shown_event_id = @events[0]["event_id"]
112
+ # puts "@last_shown_event_id #{@last_shown_event_id.inspect}"
113
+ end
114
+
115
+ def print_event(e)
116
+ message = [
117
+ event_time(e["timestamp"]),
118
+ e["resource_status"],
119
+ e["resource_type"],
120
+ e["logical_resource_id"],
121
+ e["resource_status_reason"]
122
+ ].join(" ")
123
+ message = message.color(:red) if e["resource_status"] =~ /_FAILED/
124
+ puts message
125
+ end
126
+
127
+ # https://stackoverflow.com/questions/18000432/rails-12-hour-am-pm-range-for-a-day
128
+ def event_time(timestamp)
129
+ Time.parse(timestamp.to_s).localtime.strftime("%I:%M:%S%p")
130
+ end
131
+
132
+ # refreshes the loaded events in memory
133
+ def refresh_events
134
+ resp = cfn.describe_stack_events(stack_name: @stack_name)
135
+ @events = resp["stack_events"]
136
+ rescue Aws::CloudFormation::Errors::ValidationError => e
137
+ if e.message =~ /Stack .* does not exis/
138
+ @stack_deletion_completed = true
139
+ else
140
+ raise
141
+ end
142
+ end
143
+
144
+ def find_index(name, final=false)
145
+ send("#{name}_index", final)
146
+ end
147
+
148
+ def start_index(final=false)
149
+ index = @events.find_index do |event|
150
+ event["resource_type"] == "AWS::CloudFormation::Stack" &&
151
+ event["resource_status_reason"] == "User Initiated"
152
+ end
153
+ # Instead of paginating until until we find the first "User Initiated" "AWS::CloudFormation::Stack" event
154
+ # we'll use the max.
155
+ index ? index : @events.size - 1
156
+ end
157
+
158
+ def last_shown_index(_)
159
+ @events.find_index do |event|
160
+ event["event_id"] == @last_shown_event_id
161
+ end
162
+ end
163
+
164
+ def success?
165
+ resource_status = @events[0]["resource_status"]
166
+ %w[CREATE_COMPLETE UPDATE_COMPLETE].include?(resource_status)
167
+ end
168
+
169
+ def update_rollback?
170
+ @events[0]["resource_status"] == "UPDATE_ROLLBACK_COMPLETE"
171
+ end
172
+
173
+ def find_update_failed_event
174
+ i = @events.find_index do |event|
175
+ event["resource_type"] == "AWS::CloudFormation::Stack" &&
176
+ event["resource_status_reason"] == "User Initiated"
177
+ end
178
+
179
+ @events[0..i].reverse.find do |e|
180
+ e["resource_status"] == "UPDATE_FAILED"
181
+ end
182
+ end
183
+
184
+ def rollback_error_message
185
+ return unless update_rollback?
186
+
187
+ event = find_update_failed_event
188
+ return unless event
189
+
190
+ reason = event["resource_status_reason"]
191
+ messages_map.each do |pattern, message|
192
+ if reason =~ pattern
193
+ return message
194
+ end
195
+ end
196
+
197
+ reason # default message is original reason if not found in messages map
198
+ end
199
+
200
+ def messages_map
201
+ {
202
+ /CloudFormation cannot update a stack when a custom-named resource requires replacing/ => "A workaround is to run ufo again with STATIC_NAME=0 and to switch to dynamic names for resources. Then run ufo again with STATIC_NAME=1 to get back to statically name resources. Note, there are caveats with the workaround.",
203
+ /cannot be associated with more than one load balancer/ => "There's was an issue updating the stack. Target groups can only be associated with one load balancer at a time. The workaround for this is to use UFO_FORCE_TARGET_GROUP=1 and run the command again. This will force the recreation of the target group resource.",
204
+ /SetSubnets is not supported for load balancers of type/ => "Changing subnets for Network Load Balancers is currently not supported. You can try workarouding this with UFO_FORCE_ELB=1 and run the command again. This will force the recreation of the elb resource."
205
+ }
206
+ end
207
+
208
+ # http://stackoverflow.com/questions/4175733/convert-duration-to-hoursminutesseconds-or-similar-in-rails-3-or-ruby
209
+ def pretty_time(total_seconds)
210
+ minutes = (total_seconds / 60) % 60
211
+ seconds = total_seconds % 60
212
+ if total_seconds < 60
213
+ "#{seconds.to_i}s"
214
+ else
215
+ "#{minutes.to_i}m #{seconds.to_i}s"
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,5 @@
1
+ module Cfn
2
+ class Status
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cfn-status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-cloudformation
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - tongueroo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - cfn-status.gemspec
88
+ - lib/cfn/aws_service.rb
89
+ - lib/cfn/status.rb
90
+ - lib/cfn/status/version.rb
91
+ homepage: https://github.com/tongueroo/cfn-status
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.0.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: CloudFormation status library
114
+ test_files: []