Class: EY::BucketMinder

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BucketMinder

Returns a new instance of BucketMinder.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bucket_minder.rb', line 13

def initialize(opts={})
  AWS::S3::Base.establish_connection!(
      :access_key_id     => opts[:aws_secret_id],
      :secret_access_key => opts[:aws_secret_key]
    )
  @type = opts[:type]
  @env  = opts[:env]
  opts[:extension] ||= "tgz"
  @keep = opts[:keep]
  @bucket = "#{@env}-#{@type}-#{Digest::SHA1.hexdigest(opts[:aws_secret_id])[0..6]}"
  @name = "#{Time.now.strftime("%Y-%m-%dT%H:%M:%S").gsub(/:/, '-')}.#{@type}.#{opts[:extension]}"
  begin
    AWS::S3::Bucket.create @bucket
  rescue AWS::S3::ResponseError
  end  
end

Instance Method Details

#cleanupObject



54
55
56
57
58
59
# File 'lib/bucket_minder.rb', line 54

def cleanup
  list[0...-(@keep)].each{|o| 
    puts "deleting: #{o.key}"  
    o.delete
  }
end

#clear_bucketObject



66
67
68
69
70
71
# File 'lib/bucket_minder.rb', line 66

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/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

#get_currentObject



61
62
63
64
# File 'lib/bucket_minder.rb', line 61

def get_current
  name = download(list.size - 1)
  File.expand_path(name)
end

#list(printer = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/bucket_minder.rb', line 79

def list(printer = false)
  objects = AWS::S3::Bucket.objects(@bucket).sort
  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



73
74
75
76
77
# File 'lib/bucket_minder.rb', line 73

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/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