Class: Chef::ChefFS::ChefFSDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/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.



31
32
33
34
# 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

Instance Attribute Details

#chef_fsObject (readonly)

Returns the value of attribute chef_fs.



40
41
42
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 40

def chef_fs
  @chef_fs
end

Instance Method Details

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# 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) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
# 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) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
253
254
255
# 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) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# 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_descriptionObject



36
37
38
# 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) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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