defog 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -0
- data/lib/defog/fog_wrapper.rb +15 -7
- data/lib/defog/proxy.rb +14 -0
- data/lib/defog/version.rb +1 -1
- data/spec/file_spec.rb +18 -1
- data/spec/support/helpers.rb +9 -0
- metadata +15 -15
data/README.rdoc
CHANGED
@@ -43,6 +43,8 @@ object, which proxies files in a specific remote location, e.g.:
|
|
43
43
|
defog = Defog::Proxy.new(:provider => :Local,
|
44
44
|
:local_root => "/path/to/directory")
|
45
45
|
|
46
|
+
For complete options, see Defog::Proxy.new RDOC
|
47
|
+
|
46
48
|
=== Proxy a file
|
47
49
|
|
48
50
|
Open a proxy to a remote file by creating a <code>Defog::File</code> object:
|
data/lib/defog/fog_wrapper.rb
CHANGED
@@ -9,6 +9,7 @@ module Defog #:nodoc: all
|
|
9
9
|
attr_reader :location
|
10
10
|
attr_reader :fog_connection
|
11
11
|
attr_reader :fog_directory
|
12
|
+
attr_accessor :logger
|
12
13
|
|
13
14
|
def self.connect(opts={})
|
14
15
|
opts = opts.keyword_args(:provider => :required, :OTHERS => :optional)
|
@@ -24,6 +25,7 @@ module Defog #:nodoc: all
|
|
24
25
|
def get_file(key, path, encoding)
|
25
26
|
raise Error::NoCloudFile, "No such file in #{provider} #{location}: #{key}" unless fog_head(key)
|
26
27
|
return if path.exist? and Digest::MD5.hexdigest(path.read) == get_md5(key)
|
28
|
+
log :download, key, path
|
27
29
|
path.open("w#{encoding}") do |f|
|
28
30
|
f.write(fog_head(key).body)
|
29
31
|
end
|
@@ -31,13 +33,14 @@ module Defog #:nodoc: all
|
|
31
33
|
|
32
34
|
def put_file(key, path, encoding)
|
33
35
|
return if path.exist? and fog_head(key) and Digest::MD5.hexdigest(path.read) == get_md5(key)
|
36
|
+
log :upload, key, path
|
34
37
|
path.open("r#{encoding}") do |file|
|
35
38
|
fog_directory.files.create(:key => @prefix.to_s + key, :body => file)
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
39
42
|
def fog_head(key)
|
40
|
-
fog_directory.files.head(@prefix.to_s + key)
|
43
|
+
@heads[key] ||= fog_directory.files.head(@prefix.to_s + key)
|
41
44
|
end
|
42
45
|
|
43
46
|
def each
|
@@ -51,17 +54,22 @@ module Defog #:nodoc: all
|
|
51
54
|
private
|
52
55
|
|
53
56
|
def initialize(opts={})
|
54
|
-
opts
|
55
|
-
@prefix = opts.prefix
|
57
|
+
opts.replace(opts.keyword_args(:prefix => :optional, :logger => :optional, :OTHERS => :optional))
|
58
|
+
@prefix = opts.delete(:prefix)
|
59
|
+
@logger = opts.delete(:logger)
|
60
|
+
@heads = {}
|
56
61
|
end
|
57
62
|
|
63
|
+
def log(action, key, path)
|
64
|
+
@logger.info "Defog[#{provider}:#{location}] #{action.to_s.capitalize} #{@prefix}#{key} #{action==:download ? "=>" : "<="} #{path}" if @logger
|
65
|
+
end
|
58
66
|
|
59
67
|
class Local < FogWrapper
|
60
68
|
def provider ; :local ; end
|
61
69
|
|
62
70
|
def initialize(opts={})
|
63
|
-
opts
|
64
|
-
|
71
|
+
super(opts)
|
72
|
+
opts = opts.keyword_args(:local_root => :required)
|
65
73
|
@local_root = Pathname.new(opts.local_root)
|
66
74
|
@local_root.mkpath unless @local_root.exist?
|
67
75
|
@local_root = @local_root.realpath
|
@@ -89,8 +97,8 @@ module Defog #:nodoc: all
|
|
89
97
|
def provider ; :AWS ; end
|
90
98
|
|
91
99
|
def initialize(opts={})
|
92
|
-
opts
|
93
|
-
|
100
|
+
super(opts)
|
101
|
+
opts = opts.keyword_args(:aws_access_key_id => :required, :aws_secret_access_key => :required, :region => :optional, :bucket => :required)
|
94
102
|
@location = opts.delete(:bucket)
|
95
103
|
@fog_connection = (@@aws_connection_cache||={})[opts] ||= Fog::Storage.new(opts.merge(:provider => provider))
|
96
104
|
@fog_connection.directories.create :key => @location unless @fog_connection.directories.map(&:key).include? @location
|
data/lib/defog/proxy.rb
CHANGED
@@ -10,6 +10,7 @@ module Defog
|
|
10
10
|
attr_reader :persist
|
11
11
|
attr_reader :synchronize
|
12
12
|
attr_reader :max_cache_size
|
13
|
+
|
13
14
|
attr_reader :fog_wrapper # :nodoc:
|
14
15
|
|
15
16
|
# Opens a <code>Fog</code> cloud storage connection to map to a corresponding proxy
|
@@ -68,6 +69,11 @@ module Defog
|
|
68
69
|
# Defog::File#close). Note that this applies only to upload of changes to
|
69
70
|
# proxy files that are opened as writeable; the download of data to
|
70
71
|
# readable proxy files always happens synchronously.
|
72
|
+
#
|
73
|
+
# If you specify
|
74
|
+
# :logger => an-instance-of-Logger
|
75
|
+
# (or provide a logger via #logger=), Defog will log downloads and
|
76
|
+
# upload using Logger#info.
|
71
77
|
def initialize(opts={})
|
72
78
|
opts = opts.keyword_args(:provider => :required,
|
73
79
|
:proxy_root => :optional,
|
@@ -124,6 +130,14 @@ module Defog
|
|
124
130
|
@fog_wrapper.prefix
|
125
131
|
end
|
126
132
|
|
133
|
+
def logger
|
134
|
+
@fog_wrapper.logger
|
135
|
+
end
|
136
|
+
|
137
|
+
def logger=(log)
|
138
|
+
@fog_wrapper.logger= log
|
139
|
+
end
|
140
|
+
|
127
141
|
# Proxy a remote cloud file. Returns or yields a Defog::Handle object that
|
128
142
|
# represents the file.
|
129
143
|
#
|
data/lib/defog/version.rb
CHANGED
data/spec/file_spec.rb
CHANGED
@@ -4,6 +4,7 @@ shared_examples "get proxy" do
|
|
4
4
|
|
5
5
|
it "should create proxy if remote exists" do
|
6
6
|
create_remote("hello")
|
7
|
+
should_log /download/
|
7
8
|
file = @proxy.file(key, @mode)
|
8
9
|
File.exist?(file.path).should be_true
|
9
10
|
file.close
|
@@ -17,6 +18,7 @@ shared_examples "get proxy" do
|
|
17
18
|
create_remote("hello")
|
18
19
|
create_proxy("goodbye")
|
19
20
|
proxy_path.read.should == "goodbye"
|
21
|
+
should_log /download/
|
20
22
|
@proxy.file(key, @mode)
|
21
23
|
proxy_path.read.should == "hello"
|
22
24
|
end
|
@@ -26,6 +28,7 @@ shared_examples "get proxy" do
|
|
26
28
|
create_proxy("hello")
|
27
29
|
handle = @proxy.file(key)
|
28
30
|
handle.proxy_path.should_not_receive(:open).with(/^w/)
|
31
|
+
should_not_log /download/
|
29
32
|
handle.open(@mode)
|
30
33
|
end
|
31
34
|
end
|
@@ -81,6 +84,7 @@ shared_examples "append" do
|
|
81
84
|
create_remote("hello")
|
82
85
|
@proxy.file(key, @mode, :persist => true) do |file|
|
83
86
|
file.write "goodbye"
|
87
|
+
should_log /upload/
|
84
88
|
end
|
85
89
|
proxy_path.read.should == "hellogoodbye"
|
86
90
|
end
|
@@ -89,6 +93,7 @@ end
|
|
89
93
|
shared_examples "create" do
|
90
94
|
|
91
95
|
it "should create remote" do
|
96
|
+
should_log /upload/
|
92
97
|
file = @proxy.file(key, @mode)
|
93
98
|
create_proxy("upload me")
|
94
99
|
file.close
|
@@ -96,6 +101,7 @@ shared_examples "create" do
|
|
96
101
|
end
|
97
102
|
|
98
103
|
it "should not create remote if proxy is deleted" do
|
104
|
+
should_not_log /upload/
|
99
105
|
@proxy.file(key, @mode) do |file|
|
100
106
|
file.write("ignore me")
|
101
107
|
proxy_path.unlink
|
@@ -104,6 +110,7 @@ shared_examples "create" do
|
|
104
110
|
end
|
105
111
|
|
106
112
|
it "should not create remote if :synchronize => false" do
|
113
|
+
should_not_log /upload/
|
107
114
|
file = @proxy.file(key, @mode)
|
108
115
|
create_proxy("ignore me")
|
109
116
|
file.close(:synchronize => false)
|
@@ -111,6 +118,7 @@ shared_examples "create" do
|
|
111
118
|
end
|
112
119
|
|
113
120
|
it "should create remote asynchronously if :synchronize => async" do
|
121
|
+
should_log /upload/
|
114
122
|
file = @proxy.file(key, @mode)
|
115
123
|
create_proxy("upload me in thread")
|
116
124
|
Thread.should_receive(:new) { |&block|
|
@@ -130,6 +138,7 @@ shared_examples "update" do
|
|
130
138
|
remote_body.should == "overwrite me"
|
131
139
|
file = @proxy.file(key, @mode)
|
132
140
|
create_proxy("upload me")
|
141
|
+
should_log /upload/
|
133
142
|
file.close
|
134
143
|
remote_body.should == "upload me"
|
135
144
|
end
|
@@ -142,11 +151,13 @@ shared_examples "update" do
|
|
142
151
|
remote_body.should == "overwrite me"
|
143
152
|
block.call
|
144
153
|
}
|
154
|
+
should_log /upload/
|
145
155
|
file.close(:synchronize => :async)
|
146
156
|
remote_body.should == "upload me"
|
147
157
|
end
|
148
158
|
|
149
159
|
it "should not overwrite remote if proxy is deleted" do
|
160
|
+
should_not_log /upload/
|
150
161
|
create_remote("keep me")
|
151
162
|
@proxy.file(key, @mode) do |file|
|
152
163
|
file.write("ignore me")
|
@@ -156,6 +167,7 @@ shared_examples "update" do
|
|
156
167
|
end
|
157
168
|
|
158
169
|
it "should not overwrite remote if :synchronize => false" do
|
170
|
+
should_not_log /upload/
|
159
171
|
create_remote("keep me")
|
160
172
|
file = @proxy.file(key, @mode)
|
161
173
|
create_proxy("ignore me")
|
@@ -208,11 +220,16 @@ shared_examples "persistence" do
|
|
208
220
|
|
209
221
|
end
|
210
222
|
|
223
|
+
class MockLogger
|
224
|
+
def info(arg)
|
225
|
+
end
|
226
|
+
end
|
211
227
|
|
212
228
|
shared_examples "a proxy file" do |proxyargs|
|
213
229
|
|
214
230
|
before(:all) do
|
215
|
-
@proxy = Defog::Proxy.new(proxyargs)
|
231
|
+
@proxy = Defog::Proxy.new(proxyargs)
|
232
|
+
@proxy.logger = MockLogger.new
|
216
233
|
end
|
217
234
|
|
218
235
|
%W[r r+ w w+ a a+].each do |mode|
|
data/spec/support/helpers.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-05-01 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: fog
|
16
|
-
requirement: &
|
16
|
+
requirement: &70349591699120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70349591699120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: hash_keyword_args
|
27
|
-
requirement: &
|
27
|
+
requirement: &70349591698520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70349591698520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: fastandand
|
38
|
-
requirement: &
|
38
|
+
requirement: &70349591698100 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70349591698100
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &70349591697640 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70349591697640
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rspec
|
60
|
-
requirement: &
|
60
|
+
requirement: &70349591697160 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70349591697160
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement: &
|
71
|
+
requirement: &70349591696700 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70349591696700
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: simplecov-gem-adapter
|
82
|
-
requirement: &
|
82
|
+
requirement: &70349591696200 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70349591696200
|
91
91
|
description: Wrapper to fog gem, proxying access to cloud files as local files.
|
92
92
|
email:
|
93
93
|
- ronen@barzel.org
|