Class: DPL::Provider::S3

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/s3.rb

Instance Attribute Summary

Attributes inherited from DPL::Provider

#context, #options

Instance Method Summary collapse

Methods inherited from DPL::Provider

apt_get, #cleanup, #commit_msg, context, #create_key, #default_text_charset, #default_text_charset?, deprecated, #detect_encoding?, #encoding_for, #error, experimental, #initialize, #log, new, npm_g, #option, pip, requires, #run, #setup_git_credentials, #setup_git_ssh, #sha, shell, #uncleanup, #user_agent, #warn

Constructor Details

This class inherits a constructor from DPL::Provider

Instance Method Details

#access_key_idObject



21
22
23
# File 'lib/dpl/provider/s3.rb', line 21

def access_key_id
  options[:access_key_id] || context.env['AWS_ACCESS_KEY_ID'] || raise(Error, "missing access_key_id")
end

#apiObject



9
10
11
# File 'lib/dpl/provider/s3.rb', line 9

def api
  @api ||= ::Aws::S3::Resource.new(s3_options)
end

#check_appObject



17
18
19
# File 'lib/dpl/provider/s3.rb', line 17

def check_app
  log 'Warning: The endpoint option is no longer used and can be removed.' if options[:endpoint]
end

#check_authObject



36
37
38
# File 'lib/dpl/provider/s3.rb', line 36

def check_auth
  log "Logging in with Access Key: #{access_key_id[-4..-1].rjust(20, '*')}"
end

#deployObject



74
75
76
77
78
79
80
81
82
# File 'lib/dpl/provider/s3.rb', line 74

def deploy
  super
rescue ::Aws::S3::Errors::InvalidAccessKeyId
  raise Error, "Invalid S3 Access Key Id, Stopping Deploy"
rescue ::Aws::S3::Errors::ChecksumError
  raise Error, "Aws Secret Key does not match Access Key Id, Stopping Deploy"
rescue ::Aws::S3::Errors::AccessDenied
  raise Error, "Oops, It looks like you tried to write to a bucket that isn't yours or doesn't exist yet. Please create the bucket before trying to write to it."
end

#needs_key?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/dpl/provider/s3.rb', line 13

def needs_key?
  false
end

#push_appObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dpl/provider/s3.rb', line 44

def push_app
  glob_args = ["**/*"]
  glob_args << File::FNM_DOTMATCH if options[:dot_match]
  Dir.chdir(options.fetch(:local_dir, Dir.pwd)) do
    Dir.glob(*glob_args) do |filename|
      opts                          = content_data_for(filename)
      opts[:cache_control]          = get_option_value_by_filename(options[:cache_control], filename) if options[:cache_control]
      opts[:acl]                    = options[:acl].gsub(/_/, '-') if options[:acl]
      opts[:expires]                = get_option_value_by_filename(options[:expires], filename) if options[:expires]
      opts[:storage_class]          = options[:storage_class] if options[:storage_class]
      opts[:server_side_encryption] = "AES256" if options[:server_side_encryption]
      unless File.directory?(filename)
        log "uploading #{filename.inspect} with #{opts.inspect}"
        result = api.bucket(option(:bucket)).object(upload_path(filename)).upload_file(filename, opts)
        warn "error while uploading #{filename.inspect}" unless result
      end
    end
  end

  if suffix = options[:index_document_suffix]
    api.bucket(option(:bucket)).website.put(
      website_configuration: {
        index_document: {
          suffix: suffix
        }
      }
    )
  end
end

#s3_optionsObject



29
30
31
32
33
34
# File 'lib/dpl/provider/s3.rb', line 29

def s3_options
  {
    region:      options[:region] || 'us-east-1',
    credentials: ::Aws::Credentials.new(access_key_id, secret_access_key)
  }
end

#secret_access_keyObject



25
26
27
# File 'lib/dpl/provider/s3.rb', line 25

def secret_access_key
  options[:secret_access_key] || context.env['AWS_SECRET_ACCESS_KEY'] || raise(Error, "missing secret_access_key")
end

#upload_path(filename) ⇒ Object



40
41
42
# File 'lib/dpl/provider/s3.rb', line 40

def upload_path(filename)
  [options[:upload_dir], filename].compact.join("/")
end