Class: ChefFS::ChefFSDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_fs/chef_fs_data_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_fs) ⇒ ChefFSDataStore

Returns a new instance of ChefFSDataStore.



29
30
31
32
# File 'lib/chef_fs/chef_fs_data_store.rb', line 29

def initialize(chef_fs)
  @chef_fs = chef_fs
  @memory_store = ChefZero::DataStore::MemoryStore.new
end

Instance Attribute Details

#chef_fsObject (readonly)

Returns the value of attribute chef_fs.



38
39
40
# File 'lib/chef_fs/chef_fs_data_store.rb', line 38

def chef_fs
  @chef_fs
end

Instance Method Details

#create(path, name, data, *options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef_fs/chef_fs_data_store.rb', line 50

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|
      parent.create_child(chef_fs_filename(path + [name]), data)
    end
  end
end

#create_dir(path, name, *options) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/chef_fs/chef_fs_data_store.rb', line 40

def create_dir(path, name, *options)
  if use_memory_store?(path)
    @memory_store.create_dir(path, name, *options)
  else
    with_dir(path) do |parent|
      parent.create_child(chef_fs_filename(path + [name]), nil)
    end
  end
end

#delete(path) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef_fs/chef_fs_data_store.rb', line 129

def delete(path)
  if use_memory_store?(path)
    @memory_store.delete(path)
  else
    with_entry(path) do |entry|
      if path[0] == 'cookbooks' && path.length >= 3
        entry.delete(true)
      else
        entry.delete
      end
    end
  end
end

#delete_dir(path, *options) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/chef_fs/chef_fs_data_store.rb', line 143

def delete_dir(path, *options)
  if use_memory_store?(path)
    @memory_store.delete_dir(path, *options)
  else
    with_entry(path) do |entry|
      entry.delete(options.include?(:recursive))
    end
  end
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
203
204
205
206
# File 'lib/chef_fs/chef_fs_data_store.rb', line 200

def exists?(path)
  if use_memory_store?(path)
    @memory_store.exists?(path)
  else
    path_always_exists?(path) || ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists?
  end
end

#exists_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
# File 'lib/chef_fs/chef_fs_data_store.rb', line 208

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
    ChefFS::FileSystem.resolve_path(chef_fs, to_chef_fs_path(path)).exists?
  end
end

#get(path, request = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef_fs/chef_fs_data_store.rb', line 68

def get(path, request=nil)
  if use_memory_store?(path)
    @memory_store.get(path)

  elsif path[0] == 'file_store' && path[1] == 'repo'
    entry = ChefFS::FileSystem.resolve_path(chef_fs, path[2..-1].join('/'))
    begin
      entry.read
    rescue 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 = entry.chef_object.to_hash
        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
        entry.read
      end
    end
  end
end

#list(path) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/chef_fs/chef_fs_data_store.rb', line 153

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 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
      # 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 }.to_a
    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 ChefFS::FileSystem::NotFoundError
        # /cookbooks, /data, etc. never return 404
        if path_always_exists?(path)
          []
        else
          raise
        end
      end
    end
  end
end

#publish_descriptionObject



34
35
36
# File 'lib/chef_fs/chef_fs_data_store.rb', line 34

def publish_description
  "Reading and writing data to #{chef_fs.fs_description}"
end

#set(path, data, *options) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chef_fs/chef_fs_data_store.rb', line 110

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|
        parent.create_child(chef_fs_filename(path), data)
      end
    end
  end
end