Skip to content

Commit

Permalink
[local] rough first cut of local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed May 2, 2010
1 parent c02e9df commit 7ac536e
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin/fog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ unless Fog.credentials
end

require 'fog/aws/bin'
require 'fog/local/bin'
require 'fog/rackspace/bin'
require 'fog/slicehost/bin'
require 'fog/terremark/bin'
Expand Down
2 changes: 2 additions & 0 deletions lib/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
require 'fog/model'
require 'fog/parser'
require 'fog/ssh'

require 'fog/aws'
require 'fog/local'
require 'fog/rackspace'
require 'fog/slicehost'
require 'fog/terremark'
Expand Down
12 changes: 11 additions & 1 deletion lib/fog/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ class << self

def services
services = []
[::AWS, ::Rackspace, ::Slicehost, ::Terremark].each do |service|
[::AWS, ::Local, ::Rackspace, ::Slicehost, ::Terremark].each do |service|
if service.initialized?
services << service
end
end
services
end

def directories
directories = {}
services.each do |service|
if service.respond_to?(:directories)
directories[service] = service.directories
end
end
directories
end

def flavors
flavors = {}
services.each do |service|
Expand Down
1 change: 1 addition & 0 deletions lib/fog/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def credentials
:#{credential}:
:aws_access_key_id: INTENTIONALLY_LEFT_BLANK
:aws_secret_access_key: INTENTIONALLY_LEFT_BLANK
:local_root: INTENTIONALLY_LEFT_BLANK
:rackspace_api_key: INTENTIONALLY_LEFT_BLANK
:rackspace_username: INTENTIONALLY_LEFT_BLANK
:slicehost_password: INTENTIONALLY_LEFT_BLANK
Expand Down
72 changes: 72 additions & 0 deletions lib/fog/local.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module Fog
module Local

def self.new(options={})

unless @required
require 'fog/local/models/directories'
require 'fog/local/models/directory'
require 'fog/local/models/file'
require 'fog/local/models/files'
@required = true
end

unless options[:local_root]
raise ArgumentError.new('local_root is required to access local')
end
if Fog.mocking?
Fog::Local::Mock.new(options)
else
Fog::Local::Real.new(options)
end
end

def self.reset_data(keys=Mock.data.keys)
Mock.reset_data(keys)
end

class Mock

def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end

def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end

def initialize(options={})
@local_root = ::File.expand_path(options[:local_root])
@data = self.class.data[@local_root]
end

def local_root
@local_root
end

def path(partial)
partial
end
end

class Real

def initialize(options={})
@local_root = ::File.expand_path(options[:local_root])
end

def local_root
@local_root
end

def path_to(partial)
::File.join(@local_root, partial)
end
end

end
end
34 changes: 34 additions & 0 deletions lib/fog/local/bin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Local
class << self
if Fog.credentials[:local_root]

def initialized?
true
end

def [](service)
@@connections ||= Hash.new do |hash, key|
credentials = Fog.credentials.reject do |k,v|
![:local_root].include?(k)
end
hash[key] = case key
when :files
Fog::Local.new(credentials)
end
end
@@connections[service]
end

def directories
self[:files].directories
end

else

def initialized?
false
end

end
end
end
43 changes: 43 additions & 0 deletions lib/fog/local/models/directories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'fog/collection'
require 'fog/local/models/directory'

module Fog
module Local

class Real
def directories
Fog::Local::Directories.new(:connection => self)
end
end

class Mock
def directories
Fog::Local::Directories.new(:connection => self)
end
end

class Directories < Fog::Collection

model Fog::Local::Directory

def all
data = Dir.entries(connection.local_root).select do |entry|
entry[0...1] != '.' && ::File.directory?(connection.path_to(entry))
end.map do |entry|
{:name => entry}
end
load(data)
end

def get(name)
if ::File.directory?(connection.path_to(name))
new(:name => name)
else
nil
end
end

end

end
end
47 changes: 47 additions & 0 deletions lib/fog/local/models/directory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'fog/model'
# require 'fog/local/models/files'

module Fog
module Local

class Directory < Fog::Model

identity :name

def destroy
requires :name

if ::File.directory?(path)
Dir.rmdir(path)
true
else
false
end
end

def files
@files ||= begin
Fog::Local::Files.new(
:directory => self,
:connection => connection
)
end
end

def save
requires :name

Dir.mkdir(path)
true
end

private

def path
connection.path_to(name)
end

end

end
end
58 changes: 58 additions & 0 deletions lib/fog/local/models/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'fog/model'

module Fog
module Local

class File < Fog::Model

identity :key, 'Key'

attr_accessor :body
attribute :content_length, 'Content-Length'
# attribute :content_type, 'Content-Type'
attribute :last_modified, 'Last-Modified'

def body
@body ||= if last_modified
collection.get(identity).body
else
''
end
end

def directory
@directory
end

def destroy
requires :directory, :key
::File.delete(path)
true
end

def save(options = {})
requires :body, :directory, :key
file = ::File.new(path, 'w')
file.write(body)
file.close
merge_attributes(
:content_length => ::File.size(path),
:last_modified => ::File.mtime(path)
)
true
end

private

def directory=(new_directory)
@directory = new_directory
end

def path
connection.path_to(::File.join(directory.name, key))
end

end

end
end
74 changes: 74 additions & 0 deletions lib/fog/local/models/files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'fog/collection'
require 'fog/local/models/file'

module Fog
module Local

class Files < Fog::Collection

model Fog::Local::File

def all
if directory.collection.get(directory.name)
data = Dir.entries(connection.path_to(directory.name)).select do |key|
key[0...1] != '.' && !::File.directory?(connection.path_to(key))
end.map do |key|
path = file_path(key)
{
:content_length => ::File.size(path),
:key => key,
:last_modified => ::File.mtime(path)
}
end
load(data)
else
nil
end
end

def directory
@directory
end

def get(key, &block)
path = file_path(key)
if ::File.exists?(path)
data = {
:content_length => ::File.size(path),
:key => key,
:last_modified => ::File.mtime(path)
}
if block_given?
file = ::File.open(path)
while (chunk = file.read(Excon::CHUNK_SIZE)) && yield(chunk); end
file.close
new(data)
else
body = nil
::File.open(path) do |file|
body = file.read
end
new(data.merge!(:body => body))
end
else
nil
end
end

def new(attributes = {})
super({ :directory => directory }.merge!(attributes))
end

private

def directory=(new_directory)
@directory = new_directory
end

def file_path(key)
connection.path_to(::File.join(directory.name, key))
end

end
end
end
1 change: 1 addition & 0 deletions lib/fog/rackspace/models/files/directory.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'fog/model'
require 'fog/rackspace/models/files/files'

module Fog
module Rackspace
Expand Down

0 comments on commit 7ac536e

Please sign in to comment.