aws_auditor 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: 0a8fb7ab9b3d04b41fb21c6640e2c4de2a469799
4
+ data.tar.gz: b2ab26ed6fc4707deb07ae35d3f149d7e0340c7a
5
+ SHA512:
6
+ metadata.gz: d8826b6b49c204399655aa81c10be4364ff898f52c9318434fe20165b943927d9afe0d93bdb773c0b6de9e4c366d688ebb9c3caac4ab982dbcd15a3cc9a69d6f
7
+ data.tar.gz: eae728c9c0221dbc66d7d7559c588f5dfe15e79346ce77b77ed00dddbd6125b4ff5b98cf84833a4eef8ce77d62440cc381a46b0e30e2c93aae2dc7d08f2128af
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ .aws.yml
17
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_auditor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Elliot Hursh
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,49 @@
1
+ # AwsAuditor
2
+
3
+ Audits your AWS accounts to find discrepancies between the number of running instances and purchased reserved instances.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'aws_auditor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install aws_auditor
20
+
21
+ ## Usage
22
+
23
+ Create a `.aws.yml` file in the root directory, with the following structure.
24
+
25
+ ```yaml
26
+ ---
27
+ account1:
28
+ access_key_id: 'ACCESS_KEY_ID'
29
+ secret_access_key: 'SECRET_ACCESS_KEY
30
+ account2:
31
+ access_key_id: 'ACCESS_KEY_ID'
32
+ secret_access_key: 'SECRET_ACCESS_KEY
33
+ ```
34
+
35
+ To find discrepancies between number of running instances and purchased instances, run:
36
+
37
+ $ aws_auditor audit account1
38
+
39
+ To list instances for all stacks in your account, run:
40
+
41
+ $ aws_auditor stack-audit account1
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/aws_auditor/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'aws_auditor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws_auditor"
8
+ spec.version = AwsAuditor::VERSION
9
+ spec.authors = ["Elliot Hursh"]
10
+ spec.email = ["elliothursh@gmail.com"]
11
+ spec.summary = %q{AWS configuration as code}
12
+ spec.description = %q{Helps with AWS configuration}
13
+ spec.homepage = "https://github.com/elliothursh/aws_auditor"
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_dependency 'aws-sdk', '~>1'
22
+ spec.add_dependency 'hashie', '~> 3.3'
23
+ spec.add_dependency 'gli', '~> 2.10'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
data/bin/aa ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'gli'
4
+ require_relative '../lib/aws_auditor'
5
+
6
+ include GLI::App
7
+
8
+ program_desc 'AWS Auditor'
9
+ version AwsAuditor::VERSION
10
+
11
+ wrap_help_text :verbatim
12
+
13
+ program_long_desc """
14
+ DOCUMENTATION
15
+ """
16
+
17
+ commands_from File.expand_path(File.dirname(__FILE__) + '/../lib/aws_auditor/commands')
18
+
19
+ exit run(ARGV)
data/bin/aws-auditor ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'gli'
4
+ require_relative '../lib/aws_auditor'
5
+
6
+ include GLI::App
7
+
8
+ program_desc 'AWS Auditor'
9
+ version AwsAuditor::VERSION
10
+
11
+ wrap_help_text :verbatim
12
+
13
+ program_long_desc """
14
+ DOCUMENTATION
15
+ """
16
+
17
+ commands_from File.expand_path(File.dirname(__FILE__) + '/../lib/aws_auditor/commands')
18
+
19
+ exit run(ARGV)
@@ -0,0 +1,46 @@
1
+ require 'aws-sdk'
2
+ require 'yaml'
3
+ require 'hashie'
4
+
5
+ module AwsAuditor
6
+ class AwsConfig < Hash
7
+ include Hashie::Extensions::IndifferentAccess
8
+ end
9
+
10
+ class AWSSDK
11
+ FILE_NAMES = %w[.aws.yml]
12
+
13
+ def self.configuration(environment)
14
+ @environment = environment
15
+ load_config
16
+ AWS.config({
17
+ :access_key_id => @config[:access_key_id],
18
+ :secret_access_key => @config[:secret_access_key],
19
+ :region => @config[:region]
20
+ })
21
+ end
22
+
23
+ def self.load_config
24
+ return @config if @config
25
+ @config = AwsConfig[YAML.load_file(config_path)]
26
+ @config = @config[@environment] if @environment
27
+ @config[:region] ||= 'us-east-1'
28
+ @config
29
+ end
30
+
31
+ def self.config_path
32
+ if filepath = FILE_NAMES.detect {|filename| File.exists?(filename)}
33
+ File.join(Dir.pwd, filepath)
34
+ else
35
+ old_dir = Dir.pwd
36
+ Dir.chdir('..')
37
+ if old_dir != Dir.pwd
38
+ config_path
39
+ else
40
+ puts "Could not find #{FILE_NAMES.join(' or ')}"
41
+ exit
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ desc 'Reviews Reserved Instances'
2
+ command 'audit' do |c|
3
+ c.action do |global_options, options, args|
4
+ require_relative '../scripts/audit'
5
+ AwsAuditor::Scripts::Audit.execute args.first
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ arg :environment
2
+ desc 'Reviews Stack Instances'
3
+ command 'stack-audit' do |c|
4
+ c.action do |global_options, options, args|
5
+ require_relative '../scripts/stack-audit'
6
+ AwsAuditor::Scripts::StackAudit.execute args.first
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ require_relative './aws'
2
+
3
+ module AwsAuditor
4
+ module AWSWrapper
5
+ attr_accessor :aws
6
+
7
+ def aws(environment)
8
+ @aws ||= AwsAuditor::AWSSDK.configuration(environment)
9
+ end
10
+ end
11
+
12
+ module EC2Wrapper
13
+ attr_accessor :ec2
14
+
15
+ def ec2
16
+ @ec2 ||= AWS::EC2.new()
17
+ end
18
+ end
19
+
20
+ module OpsWorksWrapper
21
+ attr_accessor :opsworks
22
+
23
+ def opsworks
24
+ @opsworks ||= AWS::OpsWorks.new.client
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,62 @@
1
+ module AwsAuditor
2
+ class Instance
3
+ extend EC2Wrapper
4
+
5
+ attr_accessor :id, :platform, :availability_zone, :instance_type, :count
6
+ def initialize(aws_instance, count=1)
7
+ @id = aws_instance.id
8
+ @platform = platform_helper(aws_instance)
9
+ @availability_zone = aws_instance.availability_zone
10
+ @instance_type = aws_instance.instance_type
11
+ @count = count
12
+ end
13
+
14
+ def to_s
15
+ "#{@platform} #{@availability_zone} #{@instance_type}"
16
+ end
17
+
18
+ def platform_helper(aws_instance)
19
+ if aws_instance.class.to_s == 'AWS::EC2::Instance'
20
+ if aws_instance.vpc?
21
+ return 'VPC'
22
+ elsif aws_instance.platform
23
+ if aws_instance.platform.downcase.include? 'windows'
24
+ return 'Windows'
25
+ else
26
+ return 'Linux'
27
+ end
28
+ else
29
+ return 'Linux'
30
+ end
31
+ elsif aws_instance.class.to_s == 'AWS::EC2::ReservedInstances'
32
+ if aws_instance.product_description.downcase.include? 'vpc'
33
+ return 'VPC'
34
+ elsif aws_instance.product_description.downcase.include? 'windows'
35
+ return 'Windows'
36
+ else
37
+ return 'Linux'
38
+ end
39
+ end
40
+ end
41
+
42
+ def self.get_instances
43
+ instances = ec2.instances
44
+ instances.map do |instance|
45
+ next unless instance.status.to_s == 'running'
46
+ Instance.new(instance)
47
+ end if instances
48
+ end
49
+
50
+ def self.get_reserved_instances
51
+ reserved_instances = ec2.reserved_instances
52
+ reserved_instances.map do |ri|
53
+ next unless ri.state == 'active'
54
+ Instance.new(ri, ri.instance_count)
55
+ end if reserved_instances
56
+ end
57
+
58
+ def self.instance_hash
59
+ Hash[get_instances.map {|instance| [instance.id, instance]}]
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,36 @@
1
+ module AwsAuditor
2
+ module Scripts
3
+ class Audit
4
+ extend AWSWrapper
5
+ extend EC2Wrapper
6
+
7
+ def self.execute(environment)
8
+ aws(environment)
9
+ compare.each do |key,value|
10
+ puts "#{key}: #{value}"
11
+ end
12
+ end
13
+
14
+ def self.create_instance_hash(instance_type)
15
+ instance_hash = Hash.new()
16
+ instance_type.each do |instance|
17
+ next if instance.nil?
18
+ instance_hash[instance.to_s] = instance_hash.has_key?(instance.to_s) ? instance_hash[instance.to_s] + instance.count : instance.count
19
+ end
20
+ instance_hash
21
+ end
22
+
23
+ def self.compare
24
+ differences = Hash.new()
25
+ instances = create_instance_hash(Instance.get_instances)
26
+ ris = create_instance_hash(Instance.get_reserved_instances)
27
+ instances.keys.concat(ris.keys).uniq.each do |key|
28
+ instance_count = instances.has_key?(key) ? instances[key] : 0
29
+ ris_count = ris.has_key?(key) ? ris[key] : 0
30
+ differences[key] = ris_count - instance_count
31
+ end
32
+ differences
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ module AwsAuditor
2
+ module Scripts
3
+ class StackAudit
4
+ extend AWSWrapper
5
+ extend EC2Wrapper
6
+ extend OpsWorksWrapper
7
+
8
+ def self.execute(environment)
9
+ aws(environment)
10
+ get_stacks
11
+ end
12
+
13
+ def self.get_stacks
14
+ stacks = opsworks.describe_stacks
15
+ stacks.data[:stacks].map do |stack|
16
+ stck = Stack.new(stack)
17
+ stck.pretty_print
18
+ stck
19
+ end if stacks
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,37 @@
1
+ module AwsAuditor
2
+ class Stack
3
+ extend OpsWorksWrapper
4
+ extend EC2Wrapper
5
+
6
+ attr_accessor :id, :name, :instances
7
+ def initialize(aws_stack)
8
+ @id = aws_stack[:stack_id]
9
+ @name = aws_stack[:name]
10
+ @instances = get_instances.compact
11
+ end
12
+
13
+ def get_instances
14
+ instances = self.class.opsworks.describe_instances({stack_id: id})[:instances]
15
+ instances.map do |instance|
16
+ next unless instance[:status].to_s == 'online'
17
+ all_instances[instance[:ec2_instance_id]].to_s
18
+ end
19
+ end
20
+
21
+ def pretty_print
22
+ puts "----------------------------------"
23
+ puts "#{@name}"
24
+ puts "----------------------------------"
25
+ instances.each do |instance|
26
+ puts instance.to_s
27
+ end
28
+ puts "\n"
29
+ end
30
+
31
+ def all_instances
32
+ @all_instances ||= Instance.instance_hash
33
+ end
34
+
35
+ end
36
+ end
37
+
@@ -0,0 +1,3 @@
1
+ module AwsAuditor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'aws_auditor/version'
2
+ require_relative 'aws_auditor/convenience_wrappers'
3
+ require_relative 'aws_auditor/instance'
4
+ require_relative 'aws_auditor/stack'
5
+
6
+ module AwsAuditor
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_auditor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Elliot Hursh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-27 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: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.10'
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.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
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
+ description: Helps with AWS configuration
84
+ email:
85
+ - elliothursh@gmail.com
86
+ executables:
87
+ - aa
88
+ - aws-auditor
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - aws_auditor.gemspec
98
+ - bin/aa
99
+ - bin/aws-auditor
100
+ - lib/aws_auditor.rb
101
+ - lib/aws_auditor/aws.rb
102
+ - lib/aws_auditor/commands/audit.rb
103
+ - lib/aws_auditor/commands/stack-audit.rb
104
+ - lib/aws_auditor/convenience_wrappers.rb
105
+ - lib/aws_auditor/instance.rb
106
+ - lib/aws_auditor/scripts/audit.rb
107
+ - lib/aws_auditor/scripts/stack-audit.rb
108
+ - lib/aws_auditor/stack.rb
109
+ - lib/aws_auditor/version.rb
110
+ homepage: https://github.com/elliothursh/aws_auditor
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: AWS configuration as code
134
+ test_files: []