Class: EY::Recipes::BucketMinder
- Inherits:
-
Object
- Object
- EY::Recipes::BucketMinder
- Defined in:
- lib/ey_recipes/bucket_minder.rb
Instance Method Summary collapse
- #bucket ⇒ Object
- #cleanup ⇒ Object
- #clear_bucket ⇒ Object
- #download(index, printer = false) ⇒ Object
-
#initialize(opts = {}) ⇒ BucketMinder
constructor
A new instance of BucketMinder.
- #list(printer = false) ⇒ Object
- #rollback ⇒ Object
- #upload_object(file) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ BucketMinder
Returns a new instance of BucketMinder.
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/ey_recipes/bucket_minder.rb', line 3 def initialize(opts={}) AWS::S3::Base.establish_connection!( :access_key_id => opts[:aws_secret_id], :secret_access_key => opts[:aws_secret_key] ) @instance_id = opts[:instance_id] @type = opts[:type] @env = opts[:env] @opts = opts opts[:extension] ||= "tgz" @keep = opts[:keep] @name = "#{Time.now.strftime("%Y-%m-%dT%H:%M:%S").gsub(/:/, '-')}.#{@type}.#{opts[:extension]}" end |
Instance Method Details
#bucket ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ey_recipes/bucket_minder.rb', line 17 def bucket @bucket ||= begin buck = "#{@env}-#{@type}-#{@instance_id}-#{Digest::SHA1.hexdigest(@opts[:aws_secret_id])[0..6]}" begin AWS::S3::Bucket.create buck rescue AWS::S3::ResponseError end buck end end |
#cleanup ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ey_recipes/bucket_minder.rb', line 52 def cleanup begin list[0...-(@keep)].each do |o| puts "deleting: #{o.key}" o.delete end # S3's eventual consistency sometimes causes really weird # failures. # Since cleanup happens every time and will clean up all stale # objects, we can just ignore S3-interaction failures. It'll # work next time. rescue AWS::S3::S3Exception, AWS::S3::Error nil end end |
#clear_bucket ⇒ Object
68 69 70 71 72 73 |
# File 'lib/ey_recipes/bucket_minder.rb', line 68 def clear_bucket list.each do |o| puts "deleting: #{o.key}" o.delete end end |
#download(index, printer = false) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ey_recipes/bucket_minder.rb', line 40 def download(index, printer = false) obj = list[index.to_i] puts "downloading: #{obj}" if printer File.open(obj.key, 'wb') do |f| print "." if printer obj.value {|chunk| f.write chunk } end puts if printer puts "finished" if printer obj.key end |
#list(printer = false) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ey_recipes/bucket_minder.rb', line 81 def list(printer = false) objects = AWS::S3::Bucket.objects(bucket) object_last_modified = objects.map do |o| # NB: the last-modified value may be nil. [o, o.about['last-modified']] end.inject({}) do |acc, (obj, obj_last_modified)| acc.merge(obj => obj_last_modified) end # sort only the remaining ones objects = objects.find_all do |o| object_last_modified[o] end.sort do |a, b| DateTime.parse(object_last_modified[a]) <=> DateTime.parse(object_last_modified[a]) end puts "listing bucket #{bucket}" if printer && !objects.empty? if printer objects.each_with_index do |b,i| puts "#{i}:#{@env} #{b.key}" end end objects end |
#rollback ⇒ Object
75 76 77 78 79 |
# File 'lib/ey_recipes/bucket_minder.rb', line 75 def rollback o = list.last puts "rolling back: #{o.key}" o.delete end |
#upload_object(file) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ey_recipes/bucket_minder.rb', line 28 def upload_object(file) AWS::S3::S3Object.store( @name, open(file), bucket, :access => :private ) FileUtils.rm file puts "successful upload: #{@name}" true end |