Class: ShootsDeploy::Bucket

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name) ⇒ Bucket

Returns a new instance of Bucket.



6
7
8
9
10
# File 'lib/shoots_deploy/bucket.rb', line 6

def initialize(bucket_name)
  @name = bucket_name
  s3 = AWS::S3.new
  @s3_bucket = s3.buckets[bucket_name]
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



4
5
6
# File 'lib/shoots_deploy/bucket.rb', line 4

def configuration
  @configuration
end

Class Method Details

.initialize_with_name(bucket_name) ⇒ Object



12
13
14
15
16
17
# File 'lib/shoots_deploy/bucket.rb', line 12

def self.initialize_with_name(bucket_name)
  puts "\nCreating bucket with name #{bucket_name}"
  s3 = AWS::S3.new
  s3.buckets.create(bucket_name)
  new(bucket_name)
end

Instance Method Details

#configure_policyObject



19
20
21
# File 'lib/shoots_deploy/bucket.rb', line 19

def configure_policy
  @s3_bucket.policy = AWS::S3::Policy.from_json("{\"Version\":\"2008-10-17\",\"Id\":\"d1b38dd800704a01924bef9a0b40f05f\",\"Statement\":[{\"Sid\":\"Allow Public Access to All Objects\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Resource\":[\"arn:aws:s3:::#{@name}/*\"],\"Action\":[\"s3:GetObject\"]}]}")
end

#configure_to_serve_websiteObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shoots_deploy/bucket.rb', line 23

def configure_to_serve_website
  print "\nWhat is your index document? eg. index.html or home.html: "
  index_document = gets.chomp
  index_document = index_document.empty? ? 'index.html' : index_document

  print "\nWhat is your error document? eg. 404.html or error.html: "
  error_document = gets.chomp
  error_document = error_document.empty? ? '404.html' : error_document

  @s3_bucket.configure_website do |cfg|
    cfg.index_document_suffix = index_document
    cfg.error_document_key = error_document
  end
end

#redirect_trafficObject

for secondary buckets



39
40
41
42
# File 'lib/shoots_deploy/bucket.rb', line 39

def redirect_traffic
  puts "\nRedirecting secondary bucket's traffic to #{configuration.root_domain}"
  @s3_bucket.website_configuration = AWS::S3::WebsiteConfiguration.new(redirect_all_requests_to: { host_name: configuration.root_domain })
end

#sync_site_with(folder_absolute_path) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/shoots_deploy/bucket.rb', line 62

def sync_site_with(folder_absolute_path)
  puts "\nDeleting old files..."
  @s3_bucket.clear!
  puts "\nUploading new files..."
  upload_files_from(folder_absolute_path)
  puts "\nSite updated!"
end

#upload_files_from(folder_absolute_path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shoots_deploy/bucket.rb', line 44

def upload_files_from(folder_absolute_path)
  puts "\nUploading all files in #{folder_absolute_path}"
  Dir.glob(folder_absolute_path + '/**/**') do |file|
    next if File.directory?(file)
    configuration_file_regex = "#{ShootsDeploy::Configuration::CONFIG_FILE}"
    configuration_file_regex = Regexp.new(configuration_file_regex)
    next if file.match(configuration_file_regex)

    folder_absolute_path_regex = folder_absolute_path + "/"
    file_relative_path = file.gsub(folder_absolute_path_regex, '')
    puts "Uploading #{file_relative_path}....."
    obj = @s3_bucket.objects[file_relative_path]
    obj.write(Pathname.new(file))
  end

  puts "\nFinish uploading!"
end