Class: Chef::ChefFS::ChefFSDataStore
- Defined in:
- lib/chef/chef_fs/chef_fs_data_store.rb
Overview
Translation layer between chef-zero’s DataStore (a place where it expects files to be stored) and ChefFS (the user’s repository directory layout).
chef-zero expects the data store to store files its way–for example, it expects get(“nodes/blah”) to return the JSON text for the blah node, and it expects get(“cookbooks/blah/1.0.0”) to return the JSON definition of the blah cookbook version 1.0.0.
The repository is defined the way the user wants their layout. These two things are very similar in layout (for example, nodes are stored under the nodes/ directory and their filename is the name of the node).
However, there are a few differences that make this more than just a raw file store:
-
Cookbooks are stored much differently.
- chef-zero places JSON text with the checksums for the cookbook at
/cookbooks/NAME/VERSION, and expects the JSON to contain URLs to the
actual files, which are stored elsewhere.
- The repository contains an actual directory with just the cookbook
files and a metadata.rb containing a version #. There is no JSON to
be found.
- Further, if versioned_cookbooks is false, that directory is named
/cookbooks/NAME and only one version exists. If versioned_cookbooks
is true, the directory is named /cookbooks/NAME-VERSION.
- Therefore, ChefFSDataStore calculates the cookbook JSON by looking at
the files in the cookbook and checksumming them, and reading metadata.rb
for the version and dependency information.
- ChefFSDataStore also modifies the cookbook file URLs so that they point
to /file_store/repo/<filename> (the path to the actual file under the
repository root). For example, /file_store/repo/apache2/metadata.rb or
/file_store/repo/cookbooks/apache2/recipes/default.rb).
-
Sandboxes don’t exist in the repository.
- ChefFSDataStore lets cookbooks be uploaded into a temporary memory
storage, and when the cookbook is committed, copies the files onto the
disk in the correct place (/cookbooks/apache2/recipes/default.rb).
-
Data bags:
- The Chef server expects data bags in /data/BAG/ITEM
- The repository stores data bags in /data_bags/BAG/ITEM
-
JSON filenames are generally NAME.json in the repository (e.g. /nodes/foo.json).
Instance Attribute Summary collapse
-
#chef_fs ⇒ Object
readonly
Returns the value of attribute chef_fs.
Instance Method Summary collapse
- #create(path, name, data, *options) ⇒ Object
- #create_dir(path, name, *options) ⇒ Object
- #delete(path) ⇒ Object
- #delete_dir(path, *options) ⇒ Object
- #exists?(path) ⇒ Boolean
- #exists_dir?(path) ⇒ Boolean
- #get(path, request = nil) ⇒ Object
-
#initialize(chef_fs) ⇒ ChefFSDataStore
constructor
Create a new ChefFSDataStore.
- #list(path) ⇒ Object
- #publish_description ⇒ Object
- #set(path, data, *options) ⇒ Object
Constructor Details
#initialize(chef_fs) ⇒ ChefFSDataStore
Create a new ChefFSDataStore
Arguments
- chef_fs
-
A
ChefFS::FileSystem
object representing the repository root. Generally will be aChefFS::FileSystem::ChefRepositoryFileSystemRoot
object, created fromChefFS::Config.local_fs
.
85 86 87 88 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 85 def initialize(chef_fs) @chef_fs = chef_fs @memory_store = ChefZero::DataStore::MemoryStore.new end |
Instance Attribute Details
#chef_fs ⇒ Object (readonly)
Returns the value of attribute chef_fs.
94 95 96 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 94 def chef_fs @chef_fs end |
Instance Method Details
#create(path, name, data, *options) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 110 def create(path, name, data, *) if use_memory_store?(path) @memory_store.create(path, name, data, *) 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
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 96 def create_dir(path, name, *) if use_memory_store?(path) @memory_store.create_dir(path, name, *) 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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 208 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
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 226 def delete_dir(path, *) if use_memory_store?(path) @memory_store.delete_dir(path, *) else with_entry(path) do |entry| begin entry.delete(.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
293 294 295 296 297 298 299 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 293 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
301 302 303 304 305 306 307 308 309 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 301 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 132 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_fs.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 Chef::JSONCompat.to_json_pretty(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
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 240 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_fs.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_fs.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 ⇒ Object
90 91 92 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 90 def publish_description "Reading and writing data to #{chef_fs.fs_description}" end |
#set(path, data, *options) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 184 def set(path, data, *) if use_memory_store?(path) @memory_store.set(path, data, *) 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, *) 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 |