fog-external 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,4 +2,3 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- examples/ernie.pid
data/examples/ernie.conf CHANGED
@@ -1,4 +1,4 @@
1
1
  [{module, fog},
2
2
  {type, external},
3
- {command, "ruby examples/fog.rb"},
3
+ {command, "sh examples/handler.sh"},
4
4
  {count, 1}].
data/examples/example.rb CHANGED
@@ -1,11 +1,13 @@
1
+ # WARNING: Running this file will access /tmp/ernie.pid, /tmp/ernie.log and /tmp/fog-bertrpc-handler.
2
+ # Make sure you are not using any of these paths.
1
3
  require 'rubygems'
2
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
5
  require 'fog/external/storage'
4
- require "bertrpc"
6
+ require 'fog/external/backend/bertrpc'
5
7
 
6
- ROOT = '/tmp/fog-external-example-root'
7
8
  Dir.chdir File.join(File.dirname(__FILE__), '..')
8
9
 
10
+ ROOT = '/tmp/fog-bertrpc-handler'
9
11
  Dir.mkdir(ROOT) unless File.exists?(ROOT)
10
12
 
11
13
  def run(cmd)
@@ -14,23 +16,50 @@ def run(cmd)
14
16
  end
15
17
 
16
18
  puts "Starting ernie on localhost:8000"
17
- run "ernie -d -c examples/ernie.conf -P examples/ernie.pid -a /tmp/ernie.log"
19
+ run "ernie -d -c examples/ernie.conf -P /tmp/ernie.pid -a /tmp/ernie.log"
18
20
 
19
21
  puts "Ernie running."
20
22
 
23
+ begin
24
+
21
25
  storage = Fog::Storage.new({
22
26
  :provider => 'External',
23
- :delegate => BERTRPC::Service.new('localhost', 8000).call.fog
27
+ :delegate => Fog::External::Backend::Bertrpc.new('localhost', 8000)
24
28
  })
25
29
 
26
30
  puts "Known directories: "
27
31
  puts storage.directories.all.inspect
28
32
 
29
- puts "Creating directory mydir/ ..."
30
- puts storage.directories.create(key: 'mydir').save
33
+ print "Creating directory mydir/ ..."
34
+ dir = storage.directories.new(key: 'mydir')
35
+ puts dir.save
36
+
37
+ puts "Known directories: "
38
+ puts storage.directories.all.inspect
39
+
40
+ print "Creating mydir/test.txt with content 'ohai' ..."
41
+ f = dir.files.new(key: 'test.txt')
42
+ f.body = 'ohai'
43
+ if f.save
44
+ puts "done."
45
+ else
46
+ puts "error."
47
+ end
48
+
49
+ print "Contents of mydir/test.txt: "
50
+ f = storage.directories.get('/').files.get('mydir/test.txt')
51
+ puts f.body
52
+
53
+ puts "Destroying mydir/test.txt"
54
+ f.destroy
55
+
56
+ puts "Destroying mydir/"
57
+ dir.destroy
31
58
 
32
59
  puts "Known directories: "
33
60
  puts storage.directories.all.inspect
34
61
 
35
- puts "Stopping ernie on localhost:8000"
36
- run "kill `cat examples/ernie.pid`"
62
+ ensure
63
+ puts "Stopping ernie on localhost:8000"
64
+ run "kill `cat /tmp/ernie.pid`"
65
+ end
@@ -0,0 +1,2 @@
1
+ #!sh
2
+ exec ruby -Ilib -rfog/external/backend/local -e 'Fog::External::Backend::Local.act_as_ernie_handler!'
data/fog-external.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fog-external"
6
- s.version = "0.0.1"
6
+ s.version = "0.0.2"
7
7
  s.authors = ["Jonas Schneider"]
8
8
  s.email = ["mail@jonasschneider.com"]
9
9
  s.homepage = ""
@@ -0,0 +1,36 @@
1
+ require "bertrpc"
2
+
3
+ module Fog
4
+ module External
5
+ module Backend
6
+
7
+ class Bertrpc
8
+ def initialize(host, port)
9
+ @service = BERTRPC::Service.new(host, port)
10
+ @module = @service.call.fog
11
+ end
12
+
13
+ METHODS = %w(create_directory list_directories get_directory destroy_directory list_files head_file get_file destroy_file save_file)
14
+
15
+ METHODS.each do |m|
16
+ define_method m do |*args|
17
+ @module.send m, *args
18
+ end
19
+ end
20
+
21
+ def get_file(key)
22
+ res = @module.get_file(key)
23
+ res[:body] = Base64.decode64(res[:body])
24
+ res
25
+ end
26
+
27
+ def save_file(key, body)
28
+ body = Base64.encode64(body)
29
+ @module.save_file(key, body)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,137 @@
1
+ require "fileutils"
2
+
3
+ module Fog
4
+ module External
5
+ module Backend
6
+
7
+ class Local
8
+ def self.act_as_ernie_handler!(root = '/tmp/fog-bertrpc-handler')
9
+ require 'ernie'
10
+ instance = self.new(root)
11
+
12
+ mod = Module.new do
13
+ instance.methods.each do |m|
14
+ define_method m do |*args|
15
+ instance.send m, *args
16
+ end
17
+ end
18
+ end
19
+
20
+ Ernie.expose(:fog, mod)
21
+ end
22
+
23
+ def initialize(root)
24
+ @root = root
25
+ end
26
+
27
+ def create_directory(key)
28
+ if ::File.directory?(path_to(key))
29
+ false
30
+ else
31
+ Dir.mkdir path_to(key)
32
+ end
33
+ end
34
+
35
+ def list_directories
36
+ data = Dir.entries(@root).select do |entry|
37
+ entry[0...1] != '.' && ::File.directory?(path_to(entry))
38
+ end.map do |entry|
39
+ {:key => entry}
40
+ end
41
+ end
42
+
43
+ def get_directory(key)
44
+ if ::File.directory?(path_to(key))
45
+ { key: key }
46
+ else
47
+ nil
48
+ end
49
+ end
50
+
51
+ def destroy_directory(key)
52
+ if ::File.directory?(path_to(key))
53
+ Dir.rmdir(path_to(key))
54
+ true
55
+ else
56
+ false
57
+ end
58
+ end
59
+
60
+
61
+ def list_files(dir_key)
62
+ data = nil
63
+ Dir.chdir(path_to(dir_key)) do
64
+ data = Dir.glob('**/*').reject do |file|
65
+ ::File.directory?(file)
66
+ end.map do |key|
67
+ path = path_to(key)
68
+ {
69
+ :content_length => ::File.size(path),
70
+ :key => key,
71
+ :last_modified => ::File.mtime(path)
72
+ }
73
+ end
74
+ end
75
+ data
76
+ end
77
+
78
+ def head_file(key)
79
+ path = path_to(key)
80
+ if ::File.exists?(path)
81
+ {
82
+ :content_length => ::File.size(path),
83
+ :key => key,
84
+ :last_modified => ::File.mtime(path)
85
+ }
86
+ else
87
+ nil
88
+ end
89
+ end
90
+
91
+ def get_file(key)
92
+ path = path_to(key)
93
+ if ::File.exists?(path)
94
+ {
95
+ :content_length => ::File.size(path),
96
+ :key => key,
97
+ :last_modified => ::File.mtime(path),
98
+ :body => ::File.read(path)
99
+ }
100
+ else
101
+ nil
102
+ end
103
+ end
104
+
105
+ def destroy_file(key)
106
+ path = path_to(key)
107
+
108
+ if ::File.exists?(path)
109
+ ::File.delete(path)
110
+ true
111
+ else
112
+ false
113
+ end
114
+ end
115
+
116
+ def save_file(key, body)
117
+ path = path_to(key)
118
+
119
+ ::FileUtils.mkdir_p(File.dirname(path))
120
+
121
+ file = ::File.new(path, 'wb')
122
+ file.write(body)
123
+ file.close
124
+
125
+ ::File.mtime(path)
126
+ end
127
+
128
+ private
129
+
130
+ def path_to(entry)
131
+ ::File.join(@root, entry)
132
+ end
133
+ end
134
+
135
+ end
136
+ end
137
+ end
@@ -1,4 +1,5 @@
1
1
  require 'fog/core/model'
2
+ require "base64"
2
3
 
3
4
  module Fog
4
5
  module Storage
@@ -55,8 +56,8 @@ module Fog
55
56
 
56
57
  def save
57
58
  requires :body, :directory, :key
58
-
59
- if res = connection.remote.save_file(full_key, body)
59
+ body_string = body.respond_to?(:read) ? body.read : body
60
+ if res = connection.remote.save_file(full_key, body_string)
60
61
  merge_attributes(
61
62
  :content_length => Fog::Storage.get_body_size(body),
62
63
  :last_modified => res
@@ -28,8 +28,9 @@ module Fog
28
28
 
29
29
  def head(id) # hackish!
30
30
  requires :directory
31
- real_key = file_key(id)
32
- if data = connection.remote.list_files(directory.key).detect{|c|c[:key] == real_key}
31
+
32
+ data = connection.remote.head_file(file_key(id))
33
+ if data
33
34
  new(data)
34
35
  else
35
36
  nil
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper'))
2
+ require 'fog/external/backend/bertrpc'
3
+
4
+ describe "Fog::External::Backend::Bertrpc" do
5
+ let(:backend) { Fog::External::Backend::Bertrpc.new 'localhost', 8000 }
6
+
7
+ %w(create_directory list_directories get_directory destroy_directory list_files head_file get_file destroy_file save_file).each do |m|
8
+ it "responds to #{m}" do
9
+ backend.should respond_to(m)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper'))
2
+ require 'fog/external/backend/local'
3
+
4
+ describe "Fog::External::Backend::Local" do
5
+ let(:backend) { Fog::External::Backend::Local.new '/tmp' }
6
+
7
+ %w(create_directory list_directories get_directory destroy_directory list_files head_file get_file destroy_file save_file).each do |m|
8
+ it "responds to #{m}" do
9
+ backend.should respond_to(m)
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fog-external
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &76776280 !ruby/object:Gem::Requirement
16
+ requirement: &72799370 !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: :development
23
23
  prerelease: false
24
- version_requirements: *76776280
24
+ version_requirements: *72799370
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &76776070 !ruby/object:Gem::Requirement
27
+ requirement: &72799160 !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: :development
34
34
  prerelease: false
35
- version_requirements: *76776070
35
+ version_requirements: *72799160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: fog
38
- requirement: &76775860 !ruby/object:Gem::Requirement
38
+ requirement: &72798950 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *76775860
46
+ version_requirements: *72798950
47
47
  description: Use a custom backend (such as BERTRPC) with Fog:Storage.
48
48
  email:
49
49
  - mail@jonasschneider.com
@@ -59,15 +59,19 @@ files:
59
59
  - Watchrfile
60
60
  - examples/ernie.conf
61
61
  - examples/example.rb
62
- - examples/fog.rb
62
+ - examples/handler.sh
63
63
  - fog-external.gemspec
64
64
  - lib/fog-external.rb
65
+ - lib/fog/external/backend/bertrpc.rb
66
+ - lib/fog/external/backend/local.rb
65
67
  - lib/fog/external/models/storage/directories.rb
66
68
  - lib/fog/external/models/storage/directory.rb
67
69
  - lib/fog/external/models/storage/file.rb
68
70
  - lib/fog/external/models/storage/files.rb
69
71
  - lib/fog/external/storage.rb
70
72
  - spec/boot.rb
73
+ - spec/fog/external/backend/bertrpc_spec.rb
74
+ - spec/fog/external/backend/local_spec.rb
71
75
  - spec/fog/external/storage_spec.rb
72
76
  - spec/service_mock.rb
73
77
  - spec/spec_helper.rb
@@ -97,6 +101,8 @@ specification_version: 3
97
101
  summary: Fog::Storage with a consistent API
98
102
  test_files:
99
103
  - spec/boot.rb
104
+ - spec/fog/external/backend/bertrpc_spec.rb
105
+ - spec/fog/external/backend/local_spec.rb
100
106
  - spec/fog/external/storage_spec.rb
101
107
  - spec/service_mock.rb
102
108
  - spec/spec_helper.rb
data/examples/fog.rb DELETED
@@ -1,102 +0,0 @@
1
- require 'ernie'
2
- ROOT = '/tmp/fog-external-example-root'
3
- # ported from Fog's Local storage
4
-
5
- module Fog
6
- def create_directory(key)
7
- if ::File.directory?(path_to(key))
8
- false
9
- else
10
- Dir.mkdir path_to(key)
11
- end
12
- end
13
-
14
- def list_directories
15
- data = Dir.entries(ROOT).select do |entry|
16
- entry[0...1] != '.' && ::File.directory?(path_to(entry))
17
- end.map do |entry|
18
- {:key => entry}
19
- end
20
- end
21
-
22
- def get_directory(key)
23
- if ::File.directory?(path_to(key))
24
- { key: key }
25
- else
26
- nil
27
- end
28
- end
29
-
30
- def destroy_directory(key)
31
- if ::File.directory?(path_to(key))
32
- Dir.rmdir(path_to(key))
33
- true
34
- else
35
- false
36
- end
37
- end
38
-
39
-
40
- def list_files(dir_key)
41
- Dir.chdir(path_to(dir_key)) do
42
- data = Dir.glob('**/*').reject do |file|
43
- ::File.directory?(file)
44
- end.map do |key|
45
- path = file_path(key)
46
- {
47
- :content_length => ::File.size(path),
48
- :key => key,
49
- :last_modified => ::File.mtime(path)
50
- }
51
- end
52
- end
53
- data
54
- end
55
-
56
- def get_file(key)
57
- path = path_to(key)
58
- if ::File.exists?(path)
59
- {
60
- :content_length => ::File.size(path),
61
- :key => key,
62
- :last_modified => ::File.mtime(path),
63
- :body => ::File.read(path)
64
- }
65
- else
66
- nil
67
- end
68
- end
69
-
70
- def destroy_file(key)
71
- path = path_to(key)
72
-
73
- if ::File.exists?(path)
74
- ::File.delete(path)
75
- true
76
- else
77
- false
78
- end
79
- end
80
-
81
- def save_file(key, body)
82
- file = ::File.new(path, 'wb')
83
- if body.is_a?(String)
84
- file.write(body)
85
- else
86
- file.write(body.read)
87
- end
88
- file.close
89
-
90
- ::File.mtime(path)
91
- rescue
92
- false
93
- end
94
-
95
- private
96
-
97
- def path_to(entry)
98
- ::File.join(ROOT, entry)
99
- end
100
- end
101
-
102
- Ernie.expose(:fog, Fog)