Parent

Files

Class/Module Index [+]

Quicksearch

Chef::ChefFS::ChefFSDataStore

Attributes

chef_fs[R]

Public Class Methods

new(chef_fs) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 31
def initialize(chef_fs)
  @chef_fs = chef_fs
  @memory_store = ChefZero::DataStore::MemoryStore.new
end

Public Instance Methods

create(path, name, data, *options) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 56
def create(path, name, data, *options)
  if use_memory_store?(path)
    @memory_store.create(path, name, data, *options)

  elsif path[0] == 'cookbooks' && path.length == 2
    # Do nothing.  The entry gets created when the cookbook is created.

  else
    if !data.is_a?(String)
      raise "set only works with strings"
    end

    with_dir(path) do |parent|
      begin
        parent.create_child(chef_fs_filename(path + [name]), data)
      rescue Chef::ChefFS::FileSystem::AlreadyExistsError => e
        raise ChefZero::DataStore::DataAlreadyExistsError.new(to_zero_path(e.entry), e)
      end
    end
  end
end
create_dir(path, name, *options) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 42
def create_dir(path, name, *options)
  if use_memory_store?(path)
    @memory_store.create_dir(path, name, *options)
  else
    with_dir(path) do |parent|
      begin
        parent.create_child(chef_fs_filename(path + [name]), nil)
      rescue Chef::ChefFS::FileSystem::AlreadyExistsError => e
        raise ChefZero::DataStore::DataAlreadyExistsError.new(to_zero_path(e.entry), e)
      end
    end
  end
end
delete(path) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 154
def delete(path)
  if use_memory_store?(path)
    @memory_store.delete(path)
  else
    with_entry(path) do |entry|
      begin
        if path[0] == 'cookbooks' && path.length >= 3
          entry.delete(true)
        else
          entry.delete(false)
        end
      rescue Chef::ChefFS::FileSystem::NotFoundError => e
        raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
      end
    end
  end
end
delete_dir(path, *options) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 172
def delete_dir(path, *options)
  if use_memory_store?(path)
    @memory_store.delete_dir(path, *options)
  else
    with_entry(path) do |entry|
      begin
        entry.delete(options.include?(:recursive))
      rescue Chef::ChefFS::FileSystem::NotFoundError => e
        raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
      end
    end
  end
end
exists?(path) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 239
def exists?(path)
  if use_memory_store?(path)
    @memory_store.exists?(path)
  else
    path_always_exists?(path) || Chef::ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists?
  end
end
exists_dir?(path) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 247
def exists_dir?(path)
  if use_memory_store?(path)
    @memory_store.exists_dir?(path)
  elsif path[0] == 'cookbooks' && path.length == 2
    list([ path[0] ]).include?(path[1])
  else
    Chef::ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists?
  end
end
get(path, request=nil) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 78
def get(path, request=nil)
  if use_memory_store?(path)
    @memory_store.get(path)

  elsif path[0] == 'file_store' && path[1] == 'repo'
    entry = Chef::ChefFS::FileSystem.resolve_path(chef_fs, path[2..-1].join('/'))
    begin
      entry.read
    rescue Chef::ChefFS::FileSystem::NotFoundError => e
      raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
    end

  else
    with_entry(path) do |entry|
      if path[0] == 'cookbooks' && path.length == 3
        # get /cookbooks/NAME/version
        result = nil
        begin
          result = entry.chef_object.to_hash
        rescue Chef::ChefFS::FileSystem::NotFoundError => e
          raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
        end

        result.each_pair do |key, value|
          if value.is_a?(Array)
            value.each do |file|
              if file.is_a?(Hash) && file.has_key?('checksum')
                relative = ['file_store', 'repo', 'cookbooks']
                if Chef::Config.versioned_cookbooks
                  relative << "#{path[1]}-#{path[2]}"
                else
                  relative << path[1]
                end
                relative = relative + file[:path].split('/')
                file['url'] = ChefZero::RestBase::build_uri(request.base_uri, relative)
              end
            end
          end
        end
        JSON.pretty_generate(result)

      else
        begin
          entry.read
        rescue Chef::ChefFS::FileSystem::NotFoundError => e
          raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
        end
      end
    end
  end
end
list(path) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 186
def list(path)
  if use_memory_store?(path)
    @memory_store.list(path)

  elsif path[0] == 'cookbooks' && path.length == 1
    with_entry(path) do |entry|
      begin
        if Chef::Config.versioned_cookbooks
          # /cookbooks/name-version -> /cookbooks/name
          entry.children.map { |child| split_name_version(child.name)[0] }.uniq
        else
          entry.children.map { |child| child.name }
        end
      rescue Chef::ChefFS::FileSystem::NotFoundError
        # If the cookbooks dir doesn't exist, we have no cookbooks (not 404)
        []
      end
    end

  elsif path[0] == 'cookbooks' && path.length == 2
    if Chef::Config.versioned_cookbooks
      result = with_entry([ 'cookbooks' ]) do |entry|
        # list /cookbooks/name = filter /cookbooks/name-version down to name
        entry.children.map { |child| split_name_version(child.name) }.
                       select { |name, version| name == path[1] }.
                       map { |name, version| version }
      end
      if result.empty?
        raise ChefZero::DataStore::DataNotFoundError.new(path)
      end
      result
    else
      # list /cookbooks/name = <single version>
      version = get_single_cookbook_version(path)
      [version]
    end

  else
    with_entry(path) do |entry|
      begin
        entry.children.map { |c| zero_filename(c) }.sort
      rescue Chef::ChefFS::FileSystem::NotFoundError => e
        # /cookbooks, /data, etc. never return 404
        if path_always_exists?(path)
          []
        else
          raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
        end
      end
    end
  end
end
publish_description() click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 36
def publish_description
  "Reading and writing data to #{chef_fs.fs_description}"
end
set(path, data, *options) click to toggle source
# File lib/chef/chef_fs/chef_fs_data_store.rb, line 130
def set(path, data, *options)
  if use_memory_store?(path)
    @memory_store.set(path, data, *options)
  else
    if !data.is_a?(String)
      raise "set only works with strings: #{path} = #{data.inspect}"
    end

    # Write out the files!
    if path[0] == 'cookbooks' && path.length == 3
      write_cookbook(path, data, *options)
    else
      with_dir(path[0..-2]) do |parent|
        child = parent.child(chef_fs_filename(path))
        if child.exists?
          child.write(data)
        else
          parent.create_child(chef_fs_filename(path), data)
        end
      end
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.