mamiya 0.0.1.alpha10 → 0.0.1.alpha11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mamiya/storages/s3.rb +9 -5
- data/lib/mamiya/storages/s3_proxy.rb +93 -0
- data/lib/mamiya/version.rb +1 -1
- data/spec/storages/s3_proxy_spec.rb +139 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72fe2e5902f669e48c73a5b62ed1aec2f4b55e2e
|
4
|
+
data.tar.gz: 1135c8729f2c67847a9a736f447d2bf77e386e44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ace24e5310616ea9bc045d9a7dc74498bec6caef2425c0f29d6641488e036976a12d6766b1e3c33f67700a7a0b8035f0b0842315430418d684633cdab7224a56
|
7
|
+
data.tar.gz: ddad6f309a018a1db82f0f04e2db9478bcfefb04adc1592331d117f0d64c4373c8d7541e64d95378156333b18f170c94537fc7a39b6ae26e9f1340b40fe67d3f
|
data/lib/mamiya/storages/s3.rb
CHANGED
@@ -95,11 +95,15 @@ module Mamiya
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def self.initiate_s3_with_config(config) # :nodoc:
|
98
|
-
s3_config
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
98
|
+
Aws::S3.new(s3_config(config))
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.s3_config(base) # :nodoc:
|
102
|
+
base.dup.tap do |c|
|
103
|
+
c.delete(:bucket)
|
104
|
+
c.delete(:application)
|
105
|
+
c.delete(:type)
|
106
|
+
end
|
103
107
|
end
|
104
108
|
|
105
109
|
private
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'mamiya/package'
|
2
|
+
require 'mamiya/storages/s3'
|
3
|
+
|
4
|
+
module Mamiya
|
5
|
+
module Storages
|
6
|
+
# Because there's no S3 endpoint in Amazon VPC, fetching from instances
|
7
|
+
# with no public IP may consume your NAT instances' bandwidth. This
|
8
|
+
# basically uses Amazon S3 but use s3_proxy.gem for fetching from specific
|
9
|
+
# host to avoid heavy bandwidth load.
|
10
|
+
#
|
11
|
+
# Note: s3_proxy is a simple Rack app that proxies HTTP GET requests to
|
12
|
+
# Amazon S3 GetObject. You can use it with puma + nginx proxy_cache
|
13
|
+
# to avoid heavy bandwidth load.
|
14
|
+
class S3Proxy < Mamiya::Storages::S3
|
15
|
+
def fetch(package_name, dir)
|
16
|
+
package_key, meta_key = package_and_meta_key_for(package_name)
|
17
|
+
|
18
|
+
package_path = File.join(dir, File.basename(package_key))
|
19
|
+
meta_path = File.join(dir, File.basename(meta_key))
|
20
|
+
|
21
|
+
if File.exists?(package_path) || File.exists?(meta_path)
|
22
|
+
raise AlreadyFetched
|
23
|
+
end
|
24
|
+
|
25
|
+
open(package_path, 'wb+') do |io|
|
26
|
+
proxy_get(package_key, io)
|
27
|
+
end
|
28
|
+
open(meta_path, 'wb+') do |io|
|
29
|
+
proxy_get(meta_key, io)
|
30
|
+
end
|
31
|
+
|
32
|
+
return Mamiya::Package.new(package_path)
|
33
|
+
rescue Exception => e
|
34
|
+
File.unlink package_path if package_path && File.exists?(package_path)
|
35
|
+
File.unlink meta_path if meta_path && File.exists?(meta_path)
|
36
|
+
|
37
|
+
raise e
|
38
|
+
end
|
39
|
+
|
40
|
+
def meta(package_name)
|
41
|
+
_, meta_key = package_and_meta_key_for(package_name)
|
42
|
+
JSON.parse(proxy_get(meta_key, nil, &:body))
|
43
|
+
rescue NotFound
|
44
|
+
return nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.s3_config(base) # :nodoc:
|
48
|
+
superclass.s3_config(base).tap do |c|
|
49
|
+
c.delete(:proxy_host)
|
50
|
+
c.delete(:proxy_ssl_verify_none)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def proxy_host_uri
|
57
|
+
@proxy_host_uri ||= URI.parse(@config[:proxy_host])
|
58
|
+
end
|
59
|
+
|
60
|
+
def connect_proxy(&block)
|
61
|
+
Net::HTTP.new(proxy_host_uri.host, proxy_host_uri.port).tap do |http|
|
62
|
+
http.use_ssl = (proxy_host_uri.scheme == 'https')
|
63
|
+
if @config[:proxy_ssl_verify_none] == 'none'
|
64
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
65
|
+
end
|
66
|
+
|
67
|
+
break http.start(&block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def proxy_get(key, target)
|
72
|
+
connect_proxy do |http|
|
73
|
+
response = http.get("#{proxy_host_uri.path}/#{@config[:bucket]}/#{key}")
|
74
|
+
response.value
|
75
|
+
|
76
|
+
if block_given?
|
77
|
+
yield(response)
|
78
|
+
else
|
79
|
+
response.read_body do |chunk|
|
80
|
+
target.write chunk
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
rescue Net::HTTPServerException => e
|
85
|
+
if e.message.start_with?('404 ')
|
86
|
+
raise NotFound
|
87
|
+
else
|
88
|
+
raise e
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/mamiya/version.rb
CHANGED
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'aws-sdk-core'
|
3
|
+
require 'mamiya/package'
|
4
|
+
require 'mamiya/storages/abstract'
|
5
|
+
require 'mamiya/storages/s3_proxy'
|
6
|
+
require 'mamiya/storages/s3'
|
7
|
+
require 'tmpdir'
|
8
|
+
require 'fileutils'
|
9
|
+
require 'stringio'
|
10
|
+
|
11
|
+
describe Mamiya::Storages::S3Proxy do
|
12
|
+
let(:bucket) { 'testbucket' }
|
13
|
+
|
14
|
+
let(:config) do
|
15
|
+
{
|
16
|
+
application: 'myapp',
|
17
|
+
bucket: bucket,
|
18
|
+
foo: :bar,
|
19
|
+
access_key_id: 'AKI',
|
20
|
+
secret_access_key: 'secret',
|
21
|
+
region: 'ap-northeast-1',
|
22
|
+
proxy_host: 'http://my-proxy:8080/_',
|
23
|
+
}
|
24
|
+
end
|
25
|
+
subject(:storage) { described_class.new(config) }
|
26
|
+
|
27
|
+
let(:http) do
|
28
|
+
double('http').tap do |http|
|
29
|
+
allow(http).to receive(:use_ssl=).with(false)
|
30
|
+
allow(http).to receive(:start).and_yield(http)
|
31
|
+
|
32
|
+
allow(http).to receive(:get).with('/_/testbucket/myapp/test.tar.gz').and_return(
|
33
|
+
double('tarball response').tap do |resp|
|
34
|
+
allow(resp).to receive(:value).and_return(200)
|
35
|
+
allow(resp).to receive(:read_body).and_yield("{}\n")
|
36
|
+
end
|
37
|
+
)
|
38
|
+
|
39
|
+
allow(http).to receive(:get).with('/_/testbucket/myapp/test.json').and_return(
|
40
|
+
double('json response').tap do |resp|
|
41
|
+
allow(resp).to receive(:value).and_return(200)
|
42
|
+
allow(resp).to receive(:read_body).and_yield("{}\n")
|
43
|
+
end
|
44
|
+
)
|
45
|
+
|
46
|
+
allow(http).to receive(:get).with('/_/testbucket/myapp/not-found.tar.gz').and_return(
|
47
|
+
double('tarball 404 response').tap do |resp|
|
48
|
+
allow(resp).to receive(:value).and_raise(Net::HTTPServerException.new('404 "Not Found"',''))
|
49
|
+
end
|
50
|
+
)
|
51
|
+
|
52
|
+
allow(http).to receive(:get).with('/_/testbucket/myapp/not-found.json').and_return(
|
53
|
+
double('json 404 response').tap do |resp|
|
54
|
+
allow(resp).to receive(:value).and_raise(Net::HTTPServerException.new('404 "Not Found"',''))
|
55
|
+
end
|
56
|
+
)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
before do
|
61
|
+
expect(Aws::S3).not_to receive(:new)
|
62
|
+
allow(Net::HTTP).to receive(:new).with('my-proxy', 8080).and_return(http)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "inherits S3 storage" do
|
66
|
+
expect(described_class.ancestors).to include(Mamiya::Storages::S3)
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#fetch(package_name, dir)" do
|
70
|
+
let!(:tmpdir) { Dir.mktmpdir("mamiya-package-spec") }
|
71
|
+
after { FileUtils.remove_entry_secure tmpdir }
|
72
|
+
|
73
|
+
let(:tarball) { File.join(tmpdir, 'test.tar.gz') }
|
74
|
+
let(:metafile) { File.join(tmpdir, 'test.json') }
|
75
|
+
|
76
|
+
let(:package_name) { 'test' }
|
77
|
+
subject(:fetch) { storage.fetch(package_name, tmpdir) }
|
78
|
+
|
79
|
+
it "retrieves package from S3" do
|
80
|
+
expect(fetch).to be_a_kind_of(Mamiya::Package)
|
81
|
+
expect(File.realpath(fetch.path)).to eq File.realpath(tarball)
|
82
|
+
|
83
|
+
expect(File.read(tarball)).to eq "{}\n"
|
84
|
+
expect(File.read(metafile)).to eq "{}\n"
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
context "when not found" do
|
89
|
+
let(:package_name) { 'not-found' }
|
90
|
+
|
91
|
+
it "raises error" do
|
92
|
+
expect {
|
93
|
+
fetch
|
94
|
+
}.to raise_error(Mamiya::Storages::Abstract::NotFound)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when meta already exists" do
|
99
|
+
before do
|
100
|
+
File.write metafile, "\n"
|
101
|
+
end
|
102
|
+
|
103
|
+
it "raises error" do
|
104
|
+
expect {
|
105
|
+
fetch
|
106
|
+
}.to raise_error(Mamiya::Storages::Abstract::AlreadyFetched)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "when tarball already exists" do
|
111
|
+
before do
|
112
|
+
File.write tarball, "\n"
|
113
|
+
end
|
114
|
+
|
115
|
+
it "raises error" do
|
116
|
+
expect {
|
117
|
+
fetch
|
118
|
+
}.to raise_error(Mamiya::Storages::Abstract::AlreadyFetched)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
#describe "#meta(package_name)" do
|
124
|
+
# let(:package_name) { 'test' }
|
125
|
+
# subject(:meta) { storage.meta(package_name) }
|
126
|
+
|
127
|
+
# it "retrieves meta JSON from S3" do
|
128
|
+
# expect(meta).to eq({})
|
129
|
+
# end
|
130
|
+
|
131
|
+
# context "when not found" do
|
132
|
+
# let(:package_name) { 'not-found' }
|
133
|
+
|
134
|
+
# it "returns nil" do
|
135
|
+
# expect(meta).to be_nil
|
136
|
+
# end
|
137
|
+
# end
|
138
|
+
#end
|
139
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mamiya
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.alpha11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shota Fukumori (sora_h)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -198,6 +198,7 @@ files:
|
|
198
198
|
- lib/mamiya/storages/abstract.rb
|
199
199
|
- lib/mamiya/storages/mock.rb
|
200
200
|
- lib/mamiya/storages/s3.rb
|
201
|
+
- lib/mamiya/storages/s3_proxy.rb
|
201
202
|
- lib/mamiya/util/label_matcher.rb
|
202
203
|
- lib/mamiya/version.rb
|
203
204
|
- mamiya.gemspec
|
@@ -227,6 +228,7 @@ files:
|
|
227
228
|
- spec/steps/fetch_spec.rb
|
228
229
|
- spec/steps/push_spec.rb
|
229
230
|
- spec/storages/abstract_spec.rb
|
231
|
+
- spec/storages/s3_proxy_spec.rb
|
230
232
|
- spec/storages/s3_spec.rb
|
231
233
|
- spec/storages_spec.rb
|
232
234
|
- spec/support/dummy_serf.rb
|
@@ -281,6 +283,7 @@ test_files:
|
|
281
283
|
- spec/steps/fetch_spec.rb
|
282
284
|
- spec/steps/push_spec.rb
|
283
285
|
- spec/storages/abstract_spec.rb
|
286
|
+
- spec/storages/s3_proxy_spec.rb
|
284
287
|
- spec/storages/s3_spec.rb
|
285
288
|
- spec/storages_spec.rb
|
286
289
|
- spec/support/dummy_serf.rb
|