Class: Vacation::S3

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, key, bucket) ⇒ S3

Returns a new instance of S3.



8
9
10
11
12
13
14
15
16
17
# File 'lib/vacation/s3.rb', line 8

def initialize(id, key, bucket)
  @access_key_id = id
  @secret_access_key = key
  @bucket_name = bucket
  @storage = Fog::Storage.new(
    :provider => 'AWS',
    :aws_access_key_id => access_key_id,
    :aws_secret_access_key => secret_access_key
  )
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



6
7
8
# File 'lib/vacation/s3.rb', line 6

def access_key_id
  @access_key_id
end

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



6
7
8
# File 'lib/vacation/s3.rb', line 6

def bucket_name
  @bucket_name
end

#secret_access_keyObject (readonly)

Returns the value of attribute secret_access_key.



6
7
8
# File 'lib/vacation/s3.rb', line 6

def secret_access_key
  @secret_access_key
end

#storageObject (readonly)

Returns the value of attribute storage.



6
7
8
# File 'lib/vacation/s3.rb', line 6

def storage
  @storage
end

Instance Method Details

#backup_bucketObject



23
24
25
26
# File 'lib/vacation/s3.rb', line 23

def backup_bucket
  @_backup_bucket ||= storage.directories.create(:key => "#{bucket_name}-vacation-backup",
                                                  :privacy => 'private')
end

#bucketObject



19
20
21
# File 'lib/vacation/s3.rb', line 19

def bucket
  @_bucket ||= storage.directories.create(:key => bucket_name, :privacy => 'public-read')
end

#deploy_to_bucket(path, options = { :backup => true }) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vacation/s3.rb', line 60

def deploy_to_bucket(path, options = { :backup => true })
  if options[:backup] && options[:backup] == true
    Dir.mktmpdir do |temp_dir|
      backup_dir = File.expand_path(Time.now.strftime("%Y-%m-%d@%H:%M:%S(%Z)"), temp_dir)
      FileUtils.mkdir backup_dir
      
      download_from_bucket backup_dir
      upload_to_bucket temp_dir, true
    end
  end
  
  upload_to_bucket path
  
  storage.put_bucket_website bucket_name, 'index.html', :key => '404.html'
end

#download_from_bucket(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vacation/s3.rb', line 33

def download_from_bucket(path)
  return unless storage.directories.get(bucket_name)
  FileUtils.mkdir_p(path)
  
  bucket.files.each do |file|
    location = File.join(path, file.key)
    FileUtils.mkdir_p(File.dirname(location))
    File.open(location, 'w') do |local|
      local.write(file.body)
    end
  end
end

#strip_bucketObject



28
29
30
31
# File 'lib/vacation/s3.rb', line 28

def strip_bucket
  bucket.files.reload.each { |file| file.destroy }
  bucket.files.reload
end

#upload_to_bucket(path, is_backup = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vacation/s3.rb', line 46

def upload_to_bucket(path, is_backup = false)
  return unless File.exists? path

  strip_bucket unless is_backup
  target = is_backup ? backup_bucket : bucket
  
  Dir[File.join(path, '**', '*')].each do |file|
    relative_path = file[path.length + 1..-1]
    target.files.create(:key => relative_path, :body => File.read(file)) if File.file?(file)
  end
  
  target.files.reload
end