1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[local] rough first cut of local storage

This commit is contained in:
geemus 2010-05-01 22:10:11 -07:00
parent c02e9df787
commit 7ac536e873
11 changed files with 344 additions and 1 deletions

View file

@ -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'

View file

@ -24,7 +24,9 @@ require 'fog/deprecation'
require 'fog/model'
require 'fog/parser'
require 'fog/ssh'
require 'fog/aws'
require 'fog/local'
require 'fog/rackspace'
require 'fog/slicehost'
require 'fog/terremark'

View file

@ -5,7 +5,7 @@ module Fog
def services
services = []
[::AWS, ::Rackspace, ::Slicehost, ::Terremark].each do |service|
[::AWS, ::Local, ::Rackspace, ::Slicehost, ::Terremark].each do |service|
if service.initialized?
services << service
end
@ -13,6 +13,16 @@ module Fog
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|

View file

@ -27,6 +27,7 @@ module Fog
:#{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

72
lib/fog/local.rb Normal file
View file

@ -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
lib/fog/local/bin.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,5 @@
require 'fog/model'
require 'fog/rackspace/models/files/files'
module Fog
module Rackspace