dpl-s3 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 62afe207b4842b89b8c36a6980cd814b9c44d62f
4
+ data.tar.gz: 4c4f4e9aa6fab6ab46e9e4b457478a3995f01d23
5
+ SHA512:
6
+ metadata.gz: 27d1514f863c902b6560adf4849d488aaf3a8bb40f084bba06d60443a00a5a64b866b5382903e4170eddff70ba4ad4bf05596f8ca965b9de8bebdeeb146de3ab
7
+ data.tar.gz: 9bb51cee2e50bdad4f292f75dcfb5473291230c79eebcb24d8aea93a93459ccfaa06079676d4541b192546cee4c5fae5f6d4160402e7caa5eebb5be016b112c9
@@ -0,0 +1,3 @@
1
+ require './gemspec_helper'
2
+
3
+ gemspec_for 's3', [['aws-sdk', '~> 2.0'], ['mime-types', '~> 2.0']]
@@ -0,0 +1,122 @@
1
+ require 'json'
2
+ require 'aws-sdk'
3
+ require 'mime-types'
4
+
5
+ module DPL
6
+ class Provider
7
+ class S3 < Provider
8
+ def api
9
+ @api ||= ::Aws::S3::Resource.new(s3_options)
10
+ end
11
+
12
+ def needs_key?
13
+ false
14
+ end
15
+
16
+ def check_app
17
+ log 'Warning: The endpoint option is no longer used and can be removed.' if options[:endpoint]
18
+ end
19
+
20
+ def access_key_id
21
+ options[:access_key_id] || context.env['AWS_ACCESS_KEY_ID'] || raise(Error, "missing access_key_id")
22
+ end
23
+
24
+ def secret_access_key
25
+ options[:secret_access_key] || context.env['AWS_SECRET_ACCESS_KEY'] || raise(Error, "missing secret_access_key")
26
+ end
27
+
28
+ def s3_options
29
+ {
30
+ region: options[:region] || 'us-east-1',
31
+ credentials: ::Aws::Credentials.new(access_key_id, secret_access_key)
32
+ }
33
+ end
34
+
35
+ def check_auth
36
+ log "Logging in with Access Key: #{access_key_id[-4..-1].rjust(20, '*')}"
37
+ end
38
+
39
+ def upload_path(filename)
40
+ [options[:upload_dir], filename].compact.join("/")
41
+ end
42
+
43
+ def push_app
44
+ glob_args = ["**/*"]
45
+ glob_args << File::FNM_DOTMATCH if options[:dot_match]
46
+ Dir.chdir(options.fetch(:local_dir, Dir.pwd)) do
47
+ Dir.glob(*glob_args) do |filename|
48
+ opts = content_data_for(filename)
49
+ opts[:cache_control] = get_option_value_by_filename(options[:cache_control], filename) if options[:cache_control]
50
+ opts[:acl] = options[:acl].gsub(/_/, '-') if options[:acl]
51
+ opts[:expires] = get_option_value_by_filename(options[:expires], filename) if options[:expires]
52
+ opts[:storage_class] = options[:storage_class] if options[:storage_class]
53
+ opts[:server_side_encryption] = "AES256" if options[:server_side_encryption]
54
+ unless File.directory?(filename)
55
+ log "uploading #{filename.inspect} with #{opts.inspect}"
56
+ result = api.bucket(option(:bucket)).object(upload_path(filename)).upload_file(filename, opts)
57
+ warn "error while uploading #{filename.inspect}" unless result
58
+ end
59
+ end
60
+ end
61
+
62
+ if suffix = options[:index_document_suffix]
63
+ api.bucket(option(:bucket)).website.put(
64
+ website_configuration: {
65
+ index_document: {
66
+ suffix: suffix
67
+ }
68
+ }
69
+ )
70
+ end
71
+ end
72
+
73
+ def deploy
74
+ super
75
+ rescue ::Aws::S3::Errors::InvalidAccessKeyId
76
+ raise Error, "Invalid S3 Access Key Id, Stopping Deploy"
77
+ rescue ::Aws::S3::Errors::ChecksumError
78
+ raise Error, "Aws Secret Key does not match Access Key Id, Stopping Deploy"
79
+ rescue ::Aws::S3::Errors::AccessDenied
80
+ 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."
81
+ end
82
+
83
+ private
84
+ def content_data_for(path)
85
+ content_data = {}
86
+ content_type = MIME::Types.type_for(path).first
87
+ content_data[:content_type] = content_type.to_s
88
+
89
+ encoding = encoding_for(path)
90
+ if detect_encoding?
91
+ content_data[:content_encoding] = encoding if encoding
92
+ end
93
+
94
+ if encoding == 'text' && default_text_charset?
95
+ content_data[:content_type] = "#{content_data[:content_type]}; charset=#{default_text_charset}"
96
+ end
97
+
98
+ return content_data
99
+ end
100
+
101
+ def get_option_value_by_filename(option_values, filename)
102
+ return option_values if !option_values.kind_of?(Array)
103
+ preferred_value = nil
104
+ hashes = option_values.select {|value| value.kind_of?(Hash) }
105
+ hashes.each do |hash|
106
+ hash.each do |value, patterns|
107
+ unless patterns.kind_of?(Array)
108
+ patterns = [patterns]
109
+ end
110
+ patterns.each do |pattern|
111
+ if File.fnmatch?(pattern, filename)
112
+ preferred_value = value
113
+ end
114
+ end
115
+ end
116
+ end
117
+ preferred_value = option_values.select {|value| value.kind_of?(String) }.last if preferred_value.nil?
118
+ return preferred_value
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,174 @@
1
+ require 'spec_helper'
2
+ require 'dpl/provider/s3'
3
+
4
+ describe DPL::Provider::S3 do
5
+
6
+ subject :provider do
7
+ described_class.new(DummyContext.new, :access_key_id => 'qwertyuiopasdfghjklz', :secret_access_key => 'qwertyuiopasdfghjklzqwertyuiopasdfghjklz', :bucket => 'my-bucket')
8
+ end
9
+
10
+ describe '#s3_options' do
11
+ context 'without region' do
12
+ example do
13
+ options = provider.s3_options
14
+ expect(options[:region]).to eq('us-east-1')
15
+ end
16
+ end
17
+
18
+ context 'with region' do
19
+ example do
20
+ region = 'us-west-1'
21
+ provider.options.update(:region => region)
22
+ options = provider.s3_options
23
+ expect(options[:region]).to eq(region)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ describe DPL::Provider::S3 do
30
+
31
+ access_key_id = 'qwertyuiopasdfghjklz'
32
+ secret_access_key = 'qwertyuiopasdfghjklzqwertyuiopasdfghjklz'
33
+ region = 'us-east-1'
34
+ bucket = 'my-bucket'
35
+
36
+ client_options = {
37
+ stub_responses: true,
38
+ region: region,
39
+ credentials: Aws::Credentials.new(access_key_id, secret_access_key)
40
+ }
41
+
42
+ subject :provider do
43
+ described_class.new(DummyContext.new, {
44
+ access_key_id: access_key_id,
45
+ secret_access_key: secret_access_key,
46
+ bucket: bucket
47
+ })
48
+ end
49
+
50
+ before :each do
51
+ provider.stub(:s3_options).and_return(client_options)
52
+ end
53
+
54
+ describe "#check_auth" do
55
+ example do
56
+ expect(provider).to receive(:log).with("Logging in with Access Key: ****************jklz")
57
+ provider.check_auth
58
+ end
59
+ end
60
+
61
+ describe "#upload_path" do
62
+ example "Without :upload_dir"do
63
+ filename = "testfile.file"
64
+
65
+ expect(provider.upload_path(filename)).to eq("testfile.file")
66
+ end
67
+
68
+ example "With :upload_dir" do
69
+ provider.options.update(:upload_dir => 'BUILD3')
70
+ filename = "testfile.file"
71
+
72
+ expect(provider.upload_path(filename)).to eq("BUILD3/testfile.file")
73
+ end
74
+ end
75
+
76
+ describe "#needs_key?" do
77
+ example do
78
+ expect(provider.needs_key?).to eq(false)
79
+ end
80
+ end
81
+
82
+ describe "#push_app" do
83
+ example "Without local_dir" do
84
+ expect(Dir).to receive(:chdir).with(Dir.pwd)
85
+ provider.push_app
86
+ end
87
+
88
+ example "With local_dir" do
89
+ provider.options.update(:local_dir => 'BUILD')
90
+
91
+ expect(Dir).to receive(:chdir).with('BUILD')
92
+ provider.push_app
93
+ end
94
+
95
+ example "Sends MIME type" do
96
+ expect(Dir).to receive(:glob).and_yield(__FILE__)
97
+ expect_any_instance_of(Aws::S3::Object).to receive(:upload_file).with(anything(), hash_including(:content_type => 'application/x-ruby'))
98
+ provider.push_app
99
+ end
100
+
101
+ example "Sets Cache and Expiration" do
102
+ provider.options.update(:cache_control => "max-age=99999999", :expires => "2012-12-21 00:00:00 -0000")
103
+ expect(Dir).to receive(:glob).and_yield(__FILE__)
104
+ expect_any_instance_of(Aws::S3::Object).to receive(:upload_file).with(anything(), hash_including(:cache_control => 'max-age=99999999', :expires => '2012-12-21 00:00:00 -0000'))
105
+ provider.push_app
106
+ end
107
+
108
+ example "Sets different Cache and Expiration" do
109
+ option_list = []
110
+ provider.options.update(:cache_control => ["max-age=99999999", "no-cache" => ["foo.html", "bar.txt"], "max-age=9999" => "*.txt"], :expires => ["2012-12-21 00:00:00 -0000", "1970-01-01 00:00:00 -0000" => "*.html"])
111
+ expect(Dir).to receive(:glob).and_yield("foo.html").and_yield("bar.txt").and_yield("baz.js")
112
+ allow_any_instance_of(Aws::S3::Object).to receive(:upload_file) do |obj, _data, options|
113
+ option_list << { key: obj.key, options: options }
114
+ end
115
+ provider.push_app
116
+ expect(option_list).to match_array([
117
+ { key: "foo.html", options: hash_including(:cache_control => "no-cache", :expires => "1970-01-01 00:00:00 -0000") },
118
+ { key: "bar.txt", options: hash_including(:cache_control => "max-age=9999", :expires => "2012-12-21 00:00:00 -0000") },
119
+ { key: "baz.js", options: hash_including(:cache_control => "max-age=99999999", :expires => "2012-12-21 00:00:00 -0000") },
120
+ ])
121
+ end
122
+
123
+ example "Sets ACL" do
124
+ provider.options.update(:acl => "public_read")
125
+ expect(Dir).to receive(:glob).and_yield(__FILE__)
126
+ expect_any_instance_of(Aws::S3::Object).to receive(:upload_file).with(anything(), hash_including(:acl => "public-read"))
127
+ provider.push_app
128
+ end
129
+
130
+ example "Sets Storage Class" do
131
+ provider.options.update(:storage_class => "STANDARD_AI")
132
+ expect(Dir).to receive(:glob).and_yield(__FILE__)
133
+ expect_any_instance_of(Aws::S3::Object).to receive(:upload_file).with(anything(), hash_including(:storage_class => "STANDARD_AI"))
134
+ provider.push_app
135
+ end
136
+
137
+ example "Sets SSE" do
138
+ provider.options.update(:server_side_encryption => true)
139
+ expect(Dir).to receive(:glob).and_yield(__FILE__)
140
+ expect_any_instance_of(Aws::S3::Object).to receive(:upload_file).with(anything(), hash_including(:server_side_encryption => "AES256"))
141
+ provider.push_app
142
+ end
143
+
144
+ example "Sets Website Index Document" do
145
+ provider.options.update(:index_document_suffix => "test/index.html")
146
+ expect(Dir).to receive(:glob).and_yield(__FILE__)
147
+ expect_any_instance_of(Aws::S3::BucketWebsite).to receive(:put).with(:website_configuration => { :index_document => { :suffix => "test/index.html" } })
148
+ provider.push_app
149
+ end
150
+
151
+ example "when detect_encoding is set" do
152
+ path = 'foo.js'
153
+ provider.options.update(:detect_encoding => true)
154
+ expect(Dir).to receive(:glob).and_yield(path)
155
+ expect(provider).to receive(:`).at_least(1).times.with("file '#{path}'").and_return('gzip compressed')
156
+ expect_any_instance_of(Aws::S3::Object).to receive(:upload_file).with(anything(), hash_including(:content_encoding => 'gzip'))
157
+ provider.push_app
158
+ end
159
+
160
+ example "when dot_match is set" do
161
+ provider.options.update(:dot_match => true)
162
+ expect(Dir).to receive(:glob).with("**/*", File::FNM_DOTMATCH)
163
+ provider.push_app
164
+ end
165
+ end
166
+
167
+ describe "#check_app" do
168
+ example "With Endpoint" do
169
+ provider.options.update(:endpoint => 's3test.com.s3-website-us-west-2.amazonaws.com')
170
+ expect(provider).to receive(:log).with('Warning: The endpoint option is no longer used and can be removed.')
171
+ provider.check_app
172
+ end
173
+ end
174
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dpl-s3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Haase
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dpl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mime-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: json_pure
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: tins
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: coveralls
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: highline
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: deploy tool abstraction for clients
154
+ email: konstantin.mailinglists@googlemail.com
155
+ executables: []
156
+ extensions: []
157
+ extra_rdoc_files: []
158
+ files:
159
+ - dpl-s3.gemspec
160
+ - lib/dpl/provider/s3.rb
161
+ - spec/provider/s3_spec.rb
162
+ homepage: https://github.com/travis-ci/dpl
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '2.2'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.6.13
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: deploy tool
186
+ test_files:
187
+ - spec/provider/s3_spec.rb