Class: EY::Recipes::BucketMinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_recipes/bucket_minder.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BucketMinder

Returns a new instance of BucketMinder.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ey_recipes/bucket_minder.rb', line 5

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

#bucketObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/ey_recipes/bucket_minder.rb', line 19

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

#cleanupObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ey_recipes/bucket_minder.rb', line 54

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_bucketObject



70
71
72
73
74
75
# File 'lib/ey_recipes/bucket_minder.rb', line 70

def clear_bucket
  list.each do |o|
    puts "deleting: #{o.key}" 
    o.delete
  end  
end

#download(index, printer = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ey_recipes/bucket_minder.rb', line 42

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ey_recipes/bucket_minder.rb', line 83

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

#rollbackObject



77
78
79
80
81
# File 'lib/ey_recipes/bucket_minder.rb', line 77

def rollback
  o = list.last
  puts "rolling back: #{o.key}"  
  o.delete
end

#upload_object(file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ey_recipes/bucket_minder.rb', line 30

def upload_object(file)
  AWS::S3::S3Object.store(
     @name,
     open(file),
     bucket,
     :access => :private
  )
  FileUtils.rm file
  puts "successful upload: #{@name}"
  true
end