s3sizecalc 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: 5989b8eeb58eafc76a897bef4b9564d71a0895d4
4
+ data.tar.gz: d89b75c832ecd11811a5b3c129c15b62e5e85448
5
+ SHA512:
6
+ metadata.gz: 08b53da0f51e52f2b76fd2ebc39da35e629a738da28d59b7c85ca6f2b86ede4c480234d3f634b6d39508411e5a60e60a8cd7181ee75ca2090db23e66ec1dc0b1
7
+ data.tar.gz: 3495ac2b34566e37202d32e1371669ab4ae05cb51b7d80d5c4ca40a6c5a36f56b643d00cfdebdbe86f0d6c7504e1fba752eaed8ed6321713a61bcdc6f392b951
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in S3SizeCalc.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # S3SizeCalc
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/s3sizecalc`. 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 's3sizecalc'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install s3sizecalc
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ # Bucket total
27
+ $ s3sizecalc bucket --bucket my-bucket
28
+
29
+ # Region total
30
+ $ s3sizecalc region --region ap-northeast-1
31
+
32
+ # Account total
33
+ $ s3sizecalc all
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec S3SizeCalc` to use the code located in this directory, ignoring other installed copies of this gem.
39
+
40
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## TODO
43
+
44
+ * Concurrent head_object requests
45
+ * Error handling, retries
46
+ * Write spec
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/y13i/s3sizecalc/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
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 "s3sizecalc"
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 "pry"
14
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/s3sizecalc ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "s3sizecalc"
4
+
5
+ S3SizeCalc::CLI.start
@@ -0,0 +1,67 @@
1
+ require "net/http"
2
+ require "timeout"
3
+ require "aws-sdk"
4
+ require "retryable"
5
+
6
+ module S3SizeCalc
7
+ class Calculator
8
+ def initialize(options = {})
9
+ @options = options
10
+ end
11
+
12
+ def bucket_total(bucket_name)
13
+ puts "Calculating size of bucket `#{bucket_name}`." if @options.verbose
14
+
15
+ s3.bucket(bucket_name).objects.inject 0 do |total, object|
16
+ size = object.size
17
+ puts "Size of object `#{object.key}` => #{size} bytes." if @options.very_verbose
18
+ total += size
19
+ end
20
+ end
21
+
22
+ def region_total(region = current_region)
23
+ @current_region = region
24
+
25
+ s3(region).buckets.inject 0 do |total, bucket|
26
+ if s3.client.get_bucket_location(bucket: bucket.name).location_constraint == region
27
+ total += bucket_total(bucket.name)
28
+ else
29
+ total
30
+ end
31
+ end
32
+ end
33
+
34
+ def account_total
35
+ regions = ec2.client.describe_regions[:regions].map {|region| region[:region_name]}
36
+
37
+ regions.inject 0 do |total, region|
38
+ total += region_total(region)
39
+ end
40
+ end
41
+
42
+ def current_region
43
+ @current_region || @options.region || own_region
44
+ end
45
+
46
+ private
47
+
48
+ def own_region
49
+ @own_region ||= begin
50
+ timeout 2 do
51
+ Net::HTTP.get("169.254.169.254", "latest/meta-data/placement/availability-zone").chop
52
+ end
53
+ rescue
54
+ abort "You must specify a region."
55
+ end
56
+ end
57
+
58
+ def s3(region = current_region)
59
+ name = :"@s3_#{region.gsub("-", "_")}"
60
+ instance_variable_get(name) or instance_variable_set(name, Aws::S3::Resource.new(region: (region)))
61
+ end
62
+
63
+ def ec2
64
+ @ec2 ||= Aws::EC2::Resource.new(region: (current_region))
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,57 @@
1
+ require "thor"
2
+ require "thor/aws"
3
+
4
+ module S3SizeCalc
5
+ class CLI < Thor
6
+ include Thor::Aws
7
+
8
+ class_option :humanize, type: :boolean, default: true, aliases: [:h], desc: "Output size with human friendly unit."
9
+ class_option :verbose, type: :boolean, default: true, aliases: [:v], desc: "Verbose output."
10
+ class_option :very_verbose, type: :boolean, default: false, desc: "Very verbose output."
11
+
12
+ desc :bucket, "Puts bucket total used size."
13
+ method_option :bucket, required: true, aliases: [:b], desc: "S3 bucket name."
14
+ def bucket
15
+ puts "Bucket (#{options.bucket}) total: #{format_size(calculator.bucket_total)}"
16
+ end
17
+
18
+ desc :region, "Puts region total used size."
19
+ def region
20
+ puts "Region (#{calculator.current_region}) total: #{format_size(calculator.region_total)}"
21
+ end
22
+
23
+ desc :all, "Puts account total used size."
24
+ def all
25
+ puts "Account total: #{format_size(calculator.account_total)}"
26
+ end
27
+
28
+ private
29
+
30
+ def calculator
31
+ @calculator ||= Calculator.new options
32
+ end
33
+
34
+ def format_size(bytes)
35
+ if options.humanize
36
+ case
37
+ when bytes > 2 ** 60
38
+ "#{sprintf "%.2f", (bytes.to_f / (2 ** 60))} EiB"
39
+ when bytes > 2 ** 50
40
+ "#{sprintf "%.2f", (bytes.to_f / (2 ** 50))} PiB"
41
+ when bytes > 2 ** 40
42
+ "#{sprintf "%.2f", (bytes.to_f / (2 ** 40))} TiB"
43
+ when bytes > 2 ** 30
44
+ "#{sprintf "%.2f", (bytes.to_f / (2 ** 30))} GiB"
45
+ when bytes > 2 ** 20
46
+ "#{sprintf "%.2f", (bytes.to_f / (2 ** 20))} MiB"
47
+ when bytes > 2 ** 10
48
+ "#{sprintf "%.2f", (bytes.to_f / (2 ** 10))} KiB"
49
+ else
50
+ "#{bytes} Bytes"
51
+ end
52
+ else
53
+ "#{bytes} Bytes"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module S3SizeCalc
2
+ VERSION = "0.0.1".freeze
3
+ end
data/lib/s3sizecalc.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "s3sizecalc/version"
2
+ require "s3sizecalc/calculator"
3
+ require "s3sizecalc/cli"
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3sizecalc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3sizecalc"
8
+ spec.version = S3SizeCalc::VERSION
9
+ spec.authors = ["y13i"]
10
+ spec.email = ["email@y13i.com"]
11
+
12
+ spec.summary = %(Calculate total size usage of Amazon S3.)
13
+ spec.description = %(Calculate bucket/region/account total size usage of Amazon S3.)
14
+ spec.homepage = "https://github.com/y13i/s3sizecalc"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+
26
+ spec.add_dependency "aws-sdk", "~> 2"
27
+ spec.add_dependency "thor"
28
+ spec.add_dependency "thor-aws"
29
+ spec.add_dependency "concurrent-ruby"
30
+ spec.add_dependency "retryable"
31
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3sizecalc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - y13i
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
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: thor-aws
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: concurrent-ruby
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: retryable
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Calculate bucket/region/account total size usage of Amazon S3.
140
+ email:
141
+ - email@y13i.com
142
+ executables:
143
+ - s3sizecalc
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - README.md
152
+ - Rakefile
153
+ - bin/console
154
+ - bin/setup
155
+ - exe/s3sizecalc
156
+ - lib/s3sizecalc.rb
157
+ - lib/s3sizecalc/calculator.rb
158
+ - lib/s3sizecalc/cli.rb
159
+ - lib/s3sizecalc/version.rb
160
+ - s3sizecalc.gemspec
161
+ homepage: https://github.com/y13i/s3sizecalc
162
+ licenses: []
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.5
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Calculate total size usage of Amazon S3.
184
+ test_files: []