aws-inventory 0.2.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 (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +661 -0
  6. data/Guardfile +19 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +65 -0
  9. data/Rakefile +6 -0
  10. data/exe/aws-inventory +14 -0
  11. data/inventory.gemspec +37 -0
  12. data/lib/inventory.rb +29 -0
  13. data/lib/inventory/acm.rb +15 -0
  14. data/lib/inventory/aws_services.rb +53 -0
  15. data/lib/inventory/base.rb +74 -0
  16. data/lib/inventory/cfn.rb +50 -0
  17. data/lib/inventory/cli.rb +86 -0
  18. data/lib/inventory/cloudwatch.rb +38 -0
  19. data/lib/inventory/command.rb +25 -0
  20. data/lib/inventory/eb.rb +19 -0
  21. data/lib/inventory/ec2.rb +86 -0
  22. data/lib/inventory/ecs.rb +10 -0
  23. data/lib/inventory/ecs/cluster.rb +20 -0
  24. data/lib/inventory/ecs/service.rb +33 -0
  25. data/lib/inventory/elb.rb +71 -0
  26. data/lib/inventory/help.rb +9 -0
  27. data/lib/inventory/help/acm.md +1 -0
  28. data/lib/inventory/help/cfn.md +1 -0
  29. data/lib/inventory/help/cw.md +1 -0
  30. data/lib/inventory/help/eb.md +1 -0
  31. data/lib/inventory/help/ec2.md +1 -0
  32. data/lib/inventory/help/ecs.md +1 -0
  33. data/lib/inventory/help/elb.md +1 -0
  34. data/lib/inventory/help/iam.md +15 -0
  35. data/lib/inventory/help/keypair.md +1 -0
  36. data/lib/inventory/help/rds.md +5 -0
  37. data/lib/inventory/help/sg.md +1 -0
  38. data/lib/inventory/help/vpc.md +1 -0
  39. data/lib/inventory/iam.rb +13 -0
  40. data/lib/inventory/iam/group.rb +22 -0
  41. data/lib/inventory/iam/shared.rb +62 -0
  42. data/lib/inventory/iam/summary.rb +16 -0
  43. data/lib/inventory/iam/user.rb +21 -0
  44. data/lib/inventory/keypair.rb +25 -0
  45. data/lib/inventory/presenter.rb +20 -0
  46. data/lib/inventory/presenters/base.rb +5 -0
  47. data/lib/inventory/presenters/tab.rb +7 -0
  48. data/lib/inventory/presenters/table.rb +10 -0
  49. data/lib/inventory/rds.rb +11 -0
  50. data/lib/inventory/rds/port.rb +53 -0
  51. data/lib/inventory/rds/shared.rb +43 -0
  52. data/lib/inventory/rds/summary.rb +23 -0
  53. data/lib/inventory/route53.rb +29 -0
  54. data/lib/inventory/security_group.rb +11 -0
  55. data/lib/inventory/security_group/open.rb +76 -0
  56. data/lib/inventory/security_group/shared.rb +14 -0
  57. data/lib/inventory/security_group/summary.rb +17 -0
  58. data/lib/inventory/shared.rb +16 -0
  59. data/lib/inventory/version.rb +3 -0
  60. data/lib/inventory/vpc.rb +55 -0
  61. data/spec/lib/cli_spec.rb +31 -0
  62. data/spec/lib/inventory/base_spec.rb +7 -0
  63. data/spec/lib/inventory/security_group/open_spec.rb +31 -0
  64. data/spec/spec_helper.rb +24 -0
  65. metadata +308 -0
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ guard "bundler", cmd: "bundle" do
2
+ watch("Gemfile")
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard :rspec, cmd: "bundle exec rspec" do
7
+ require "guard/rspec/dsl"
8
+ dsl = Guard::RSpec::Dsl.new(self)
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tung Nguyen
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,65 @@
1
+ # AWS Inventory
2
+
3
+ AWS Inventory tool. Useful to get summarized information on AWS account. The tool by default produces a reoprt that can easily be read from a terminal, but it can also be used produce a tab separated output report that can be pasted into a spreadsheet and then copied to a report. This is controlled via a `AWS_INVENTORY_FORMAT` env variable and covered in the Format Options section.
4
+
5
+ ## Usage
6
+
7
+ ```sh
8
+ aws-inventory acm # report acm inventory
9
+ aws-inventory cfn # report cfn inventory
10
+ aws-inventory cw # report cloudwatch inventory
11
+ aws-inventory eb # report eb inventory
12
+ aws-inventory ec2 # report ec2 inventory
13
+ aws-inventory ecs # report ecs inventory
14
+ aws-inventory elb # report elb inventory
15
+ aws-inventory iam # report iam inventory
16
+ aws-inventory keypair # report keypair inventory
17
+ aws-inventory rds # report rds inventory
18
+ aws-inventory route53 # report route53 inventory
19
+ aws-inventory sg # report security group inventory
20
+ aws-inventory vpc # report vpc inventory
21
+ ```
22
+
23
+ ## Example
24
+
25
+ What the output looks something like:
26
+
27
+ ```sh
28
+ $ aws-inventory ec2
29
+ +-------+-------------+---------------+----------+-----------------+
30
+ | Name | Instance Id | Instance Type | Platform | Security Groups |
31
+ +-------+-------------+---------------+----------+-----------------+
32
+ | name1 | i-123 | m3.medium | linux | sg-123 |
33
+ | name2 | i-456 | t2.small | linux | sg-456 |
34
+ +-------+-------------+---------------+----------+-----------------+
35
+ $
36
+ ```
37
+
38
+ If you want to copy this to an spreadsheet, you can use the tab format. Here's an example with `pbcopy`:
39
+
40
+ ```sh
41
+ export AWS_INVENTORY_FORMAT=tab
42
+ aws-inventory ec2 | pbcopy
43
+ ```
44
+
45
+ ### To Check Every Region for EC2 Instances
46
+
47
+ ```sh
48
+ GREEN='\033[0;32m'
49
+ NC='\033[0m' # No Color
50
+ for i in $(aws ec2 describe-regions | jq -r '.Regions[].RegionName') ; do
51
+ echo -e "$GREEN$i$NC"
52
+ AWS_REGION=$i inventory ec2
53
+ done
54
+ ```
55
+
56
+ ### Format Option
57
+
58
+ There are 2 supported formats: tab and table. The default is table. To switch between formats use the AWS_INVENTORY_FORMAT environment variable.
59
+
60
+ ```sh
61
+ export AWS_INVENTORY_FORMAT=tab
62
+ aws-inventory ec2
63
+ export AWS_INVENTORY_FORMAT=table
64
+ aws-inventory ec2
65
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
6
+ RSpec::Core::RakeTask.new
data/exe/aws-inventory ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Trap ^C
4
+ Signal.trap("INT") {
5
+ puts "\nCtrl-C detected. Exiting..."
6
+ sleep 1
7
+ exit
8
+ }
9
+
10
+ $:.unshift(File.expand_path("../../lib", __FILE__))
11
+ require "inventory"
12
+ require "inventory/cli"
13
+
14
+ Inventory::CLI.start(ARGV)
data/inventory.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "inventory/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws-inventory"
8
+ spec.version = Inventory::VERSION
9
+ spec.authors = ["Tung Nguyen"]
10
+ spec.email = ["tongueroo@gmail.com"]
11
+ spec.description = %q{Tool to inventory AWS account}
12
+ spec.summary = %q{Tool to inventory AWS account}
13
+ spec.homepage = "https://github.com/tongueroo/aws-inventory"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "thor"
23
+ spec.add_dependency "hashie"
24
+ spec.add_dependency "colorize"
25
+ spec.add_dependency "aws-sdk"
26
+ spec.add_dependency "activesupport"
27
+ spec.add_dependency "text-table"
28
+ spec.add_dependency "facets"
29
+ spec.add_dependency "actionview"
30
+
31
+ spec.add_development_dependency "bundler"
32
+ spec.add_development_dependency "byebug"
33
+ spec.add_development_dependency "rake"
34
+ spec.add_development_dependency "guard"
35
+ spec.add_development_dependency "guard-bundler"
36
+ spec.add_development_dependency "guard-rspec"
37
+ end
data/lib/inventory.rb ADDED
@@ -0,0 +1,29 @@
1
+ $:.unshift(File.expand_path("../", __FILE__))
2
+ require "inventory/version"
3
+ require "text-table"
4
+ require "active_support/all"
5
+ require "pp"
6
+ require "byebug"
7
+
8
+ module Inventory
9
+ autoload :Base, "inventory/base"
10
+ autoload :Help, "inventory/help"
11
+ autoload :Command, "inventory/command"
12
+ autoload :CLI, "inventory/cli"
13
+ autoload :AwsServices, "inventory/aws_services"
14
+ autoload :Shared, "inventory/shared"
15
+ autoload :Presenter, "inventory/presenter"
16
+ autoload :Cfn, "inventory/cfn"
17
+ autoload :Ec2, "inventory/ec2"
18
+ autoload :Vpc, "inventory/vpc"
19
+ autoload :SecurityGroup, "inventory/security_group"
20
+ autoload :Rds, "inventory/rds"
21
+ autoload :Route53, "inventory/route53"
22
+ autoload :Acm, "inventory/acm"
23
+ autoload :Elb, "inventory/elb"
24
+ autoload :Eb, "inventory/eb"
25
+ autoload :Ecs, "inventory/ecs"
26
+ autoload :Keypair, "inventory/keypair"
27
+ autoload :Iam, "inventory/iam"
28
+ autoload :Cloudwatch, "inventory/cloudwatch"
29
+ end
@@ -0,0 +1,15 @@
1
+ class Inventory::Acm < Inventory::Base
2
+ def header
3
+ ["Domain", "Cert Arn"]
4
+ end
5
+
6
+ def data
7
+ certificate_summary_list.map do |cert|
8
+ [cert.domain_name, cert.certificate_arn]
9
+ end
10
+ end
11
+
12
+ def certificate_summary_list
13
+ @summary ||= acm.list_certificates.certificate_summary_list
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ require 'aws-sdk'
2
+
3
+ module Inventory::AwsServices
4
+ include Inventory::Shared
5
+
6
+ def cfn
7
+ @cfn ||= Aws::CloudFormation::Client.new
8
+ end
9
+
10
+ def ec2
11
+ @ec2 ||= Aws::EC2::Client.new
12
+ end
13
+
14
+ def pricing
15
+ @pricing ||= Aws::Pricing::Client.new
16
+ end
17
+
18
+ def rds
19
+ @rds ||= Aws::RDS::Client.new
20
+ end
21
+
22
+ def route53
23
+ @route53 ||= Aws::Route53::Client.new
24
+ end
25
+
26
+ def acm
27
+ @acm ||= Aws::ACM::Client.new
28
+ end
29
+
30
+ def elbv1
31
+ @elbv1 ||= Aws::ElasticLoadBalancing::Client.new
32
+ end
33
+
34
+ def elbv2
35
+ @elbv2 ||= Aws::ElasticLoadBalancingV2::Client.new
36
+ end
37
+
38
+ def eb
39
+ @eb ||= Aws::ElasticBeanstalk::Client.new
40
+ end
41
+
42
+ def ecs
43
+ @ecs ||= Aws::ECS::Client.new
44
+ end
45
+
46
+ def iam
47
+ @iam ||= Aws::IAM::Client.new
48
+ end
49
+
50
+ def cw
51
+ @cw ||= Aws::CloudWatch::Client.new
52
+ end
53
+ end
@@ -0,0 +1,74 @@
1
+ # Child class must implement this interface.
2
+ # Methods:
3
+ # header - header row to display. Array of strings.
4
+ # data - data to display under header. 2D Array.
5
+ # Each item in the array represents a row of data.
6
+ class Inventory::Base
7
+ include Inventory::AwsServices
8
+
9
+ def initialize(options)
10
+ @options = options
11
+ end
12
+
13
+ def report
14
+ return if test_mode
15
+
16
+ results = sort(data)
17
+ results.unshift(header) if header
18
+ presenter = Inventory::Presenter.new(results)
19
+ presenter.display
20
+ end
21
+
22
+ def sort(data)
23
+ data.sort_by {|a| a[0]}
24
+ end
25
+
26
+ def test_mode
27
+ if ENV['TEST']
28
+ puts "Testing #{self.class} report" # specs tests against this
29
+ true
30
+ end
31
+ end
32
+
33
+ def show(report_type)
34
+ ["all", report_type.to_s].include?(@options[:report_type])
35
+ end
36
+
37
+ class << self
38
+ # Track all command subclasses.
39
+ def subclasses
40
+ @subclasses ||= []
41
+ end
42
+
43
+ def inherited(base)
44
+ super
45
+
46
+ if base.name
47
+ self.subclasses << base
48
+ end
49
+ end
50
+
51
+ # Thought this might be useful for
52
+ # specs. Eager load all classes so then we can loop thorugh the
53
+ # methods and run specs on any new cli commands.
54
+ # The rspec code turn out a too ugly to follow though. Leaving this
55
+ # around in case eager_laod is useful for other purposes
56
+ def eager_load!
57
+ path = File.expand_path("../", __FILE__)
58
+
59
+ Dir.glob("#{path}/**/*.rb").select do |path|
60
+ next if !File.file?(path) or path =~ /version/
61
+
62
+ class_name = path
63
+ .sub('.rb','')
64
+ .sub(%r{.*/inventory}, 'inventory')
65
+ .camelize
66
+ # special rules
67
+ class_name.sub!("Cli", "CLI")
68
+
69
+ class_name.constantize # use constantize instead of require
70
+ # so we dont have to worry about require order.
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,50 @@
1
+ class Inventory::Cfn < Inventory::Base
2
+ ALL_STATUSES = %w[
3
+ REVIEW_IN_PROGRESS
4
+ CREATE_FAILED
5
+ UPDATE_ROLLBACK_FAILED
6
+ UPDATE_ROLLBACK_IN_PROGRESS
7
+ CREATE_IN_PROGRESS
8
+ UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS
9
+ ROLLBACK_IN_PROGRESS
10
+ DELETE_COMPLETE
11
+ UPDATE_COMPLETE
12
+ UPDATE_IN_PROGRESS
13
+ DELETE_FAILED
14
+ DELETE_IN_PROGRESS
15
+ ROLLBACK_COMPLETE
16
+ ROLLBACK_FAILED
17
+ UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
18
+ CREATE_COMPLETE
19
+ UPDATE_ROLLBACK_COMPLETE
20
+ ]
21
+ ACTIVE_STATUSES = ALL_STATUSES - %w[DELETE_COMPLETE]
22
+
23
+ def header
24
+ ["Stack Name", "Description"]
25
+ end
26
+
27
+ def data
28
+ stack_summaries.map do |summary|
29
+ [summary.stack_name, summary.template_description]
30
+ end
31
+ end
32
+
33
+ def stack_summaries
34
+ @stack_summaries ||= cfn.list_stacks(stack_status_filter: ACTIVE_STATUSES).stack_summaries
35
+ end
36
+
37
+ # unused right now but leaving around to later figure out how to integrate
38
+ def text_table
39
+ stack_summaries.each do |summary|
40
+ table.rows << [summary.stack_name, summary.template_description]
41
+ end
42
+
43
+ table = Text::Table.new
44
+ table.head = %w[Name Description]
45
+ stack_summaries.each do |summary|
46
+ table.rows << [summary.stack_name, summary.template_description]
47
+ end
48
+ puts table
49
+ end
50
+ end
@@ -0,0 +1,86 @@
1
+ module Inventory
2
+ class CLI < Command
3
+ class_option :verbose, type: :boolean
4
+
5
+ desc "cfn", "report cfn inventory"
6
+ long_desc Help.text(:cfn)
7
+ def cfn
8
+ Cfn.new(options).report
9
+ end
10
+
11
+ desc "ec2", "report ec2 inventory"
12
+ long_desc Help.text(:ec2)
13
+ def ec2
14
+ Ec2.new(options).report
15
+ end
16
+
17
+ desc "vpc", "report vpc inventory"
18
+ long_desc Help.text(:vpc)
19
+ def vpc
20
+ Vpc.new(options).report
21
+ end
22
+
23
+ desc "sg", "report security group inventory"
24
+ long_desc Help.text(:sg)
25
+ option :report_type, default: "open", desc: "all, summary, or open"
26
+ def sg
27
+ SecurityGroup.new(options).report
28
+ end
29
+
30
+ desc "rds", "report rds inventory"
31
+ long_desc Help.text(:rds)
32
+ option :report_type, default: "summary", desc: "all, summary, or port"
33
+ def rds
34
+ Rds.new(options).report
35
+ end
36
+
37
+ desc "route53", "report route53 inventory"
38
+ long_desc Help.text(:route53)
39
+ def route53
40
+ Route53.new(options).report
41
+ end
42
+
43
+ desc "acm", "report acm inventory"
44
+ long_desc Help.text(:acm)
45
+ def acm
46
+ Acm.new(options).report
47
+ end
48
+
49
+ desc "elb", "report elb inventory"
50
+ long_desc Help.text(:elb)
51
+ def elb
52
+ Elb.new(options).report
53
+ end
54
+
55
+ desc "eb", "report eb inventory"
56
+ long_desc Help.text(:eb)
57
+ def eb
58
+ Eb.new(options).report
59
+ end
60
+
61
+ desc "ecs", "report ecs inventory"
62
+ long_desc Help.text(:ecs)
63
+ def ecs
64
+ Ecs.new(options).report
65
+ end
66
+
67
+ desc "keypair", "report keypair inventory"
68
+ long_desc Help.text(:keypair)
69
+ def keypair
70
+ Keypair.new(options).report
71
+ end
72
+
73
+ desc "iam", "report iam inventory"
74
+ long_desc Help.text(:iam)
75
+ option :report_type, default: "groups", desc: "all, groups, users, or summary"
76
+ def iam
77
+ Iam.new(options).report
78
+ end
79
+
80
+ desc "cw", "report cloudwatch inventory"
81
+ long_desc Help.text(:cw)
82
+ def cw
83
+ Cloudwatch.new(options).report
84
+ end
85
+ end
86
+ end