Module: Chef::ChefFS::FileSystem

Defined in:
lib/chef/chef_fs/file_system.rb,
lib/chef/chef_fs/file_system/nodes_dir.rb,
lib/chef/chef_fs/file_system/base_fs_dir.rb,
lib/chef/chef_fs/file_system/cookbook_dir.rb,
lib/chef/chef_fs/file_system/data_bag_dir.rb,
lib/chef/chef_fs/file_system/cookbook_file.rb,
lib/chef/chef_fs/file_system/cookbooks_dir.rb,
lib/chef/chef_fs/file_system/data_bag_item.rb,
lib/chef/chef_fs/file_system/data_bags_dir.rb,
lib/chef/chef_fs/file_system/rest_list_dir.rb,
lib/chef/chef_fs/file_system/base_fs_object.rb,
lib/chef/chef_fs/file_system/cookbook_subdir.rb,
lib/chef/chef_fs/file_system/not_found_error.rb,
lib/chef/chef_fs/file_system/rest_list_entry.rb,
lib/chef/chef_fs/file_system/file_system_entry.rb,
lib/chef/chef_fs/file_system/file_system_error.rb,
lib/chef/chef_fs/file_system/chef_server_root_dir.rb,
lib/chef/chef_fs/file_system/file_system_root_dir.rb,
lib/chef/chef_fs/file_system/nonexistent_fs_object.rb,
lib/chef/chef_fs/file_system/must_delete_recursively_error.rb,
lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb,
lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb

Defined Under Namespace

Classes: BaseFSDir, BaseFSObject, ChefRepositoryFileSystemEntry, ChefRepositoryFileSystemRootDir, ChefServerRootDir, CookbookDir, CookbookFile, CookbookSubdir, CookbooksDir, DataBagDir, DataBagItem, DataBagsDir, FileSystemEntry, FileSystemError, FileSystemRootDir, MustDeleteRecursivelyError, NodesDir, NonexistentFSObject, NotFoundError, RestListDir, RestListEntry

Class Method Summary collapse

Class Method Details

.child_pairs(a, b) ⇒ Object

Get entries for children of either a or b, with matching pairs matched up.

Returns

An array of child pairs.

[ [ a_child, b_child ], ... ]

If a child is only in a or only in b, the other child entry will be retrieved by name (and will most likely be a “nonexistent child”).

Example

Chef::ChefFS::FileSystem.child_pairs(a, b).length


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef/chef_fs/file_system.rb', line 177

def self.child_pairs(a, b)
  # If both are directories, recurse into them and diff the children instead of returning ourselves.
  result = []
  a_children_names = Set.new
  a.children.each do |a_child|
    a_children_names << a_child.name
    result << [ a_child, b.child(a_child.name) ]
  end

  # Check b for children that aren't in a
  b.children.each do |b_child|
    if !a_children_names.include?(b_child.name)
      result << [ a.child(b_child.name), b_child ]
    end
  end
  result
end

.compare(a, b) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/chef/chef_fs/file_system.rb', line 195

def self.compare(a, b)
  are_same, a_value, b_value = a.compare_to(b)
  if are_same.nil?
    are_same, b_value, a_value = b.compare_to(a)
  end
  if are_same.nil?
    begin
      a_value = a.read if a_value.nil?
    rescue Chef::ChefFS::FileSystem::NotFoundError
      a_value = :none
    end
    begin
      b_value = b.read if b_value.nil?
    rescue Chef::ChefFS::FileSystem::NotFoundError
      b_value = :none
    end
    are_same = (a_value == b_value)
  end
  [ are_same, a_value, b_value ]
end

.copy_to(pattern, src_root, dest_root, recurse_depth, options) ⇒ Object

Copy everything matching the given pattern from src to dest.

After this method completes, everything in dest matching the given pattern will look identical to src.

Attributes

  • pattern - Chef::ChefFS::FilePattern to match children under

  • src_root - the root from which things will be copied

  • dest_root - the root to which things will be copied

  • recurse_depth - the maximum depth to copy things. nil means infinite depth. 0 means no recursion.

  • options - hash of options:

    • purge - if true, items in dest that are not in src

    will be deleted from dest. If false, these items will be left alone.

    • force - if true, matching files are always copied from src to dest. If false, they will only be copied if actually different (which will take time to determine).

    • dry_run - if true, action will not actually be taken; things will be printed out instead.

Examples

Chef::ChefFS::FileSystem.copy_to(FilePattern.new('/cookbooks'),
  chef_fs, local_fs, nil, true) do |message|
  puts message
end


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/chef_fs/file_system.rb', line 116

def self.copy_to(pattern, src_root, dest_root, recurse_depth, options)
  found_result = false
  list_pairs(pattern, src_root, dest_root) do |src, dest|
    found_result = true
    new_dest_parent = get_or_create_parent(dest, options)
    copy_entries(src, dest, new_dest_parent, recurse_depth, options)
  end
  if !found_result && pattern.exact_path
    puts "#{pattern}: No such file or directory on remote or local"
  end
end

.list(entry, pattern, &block) ⇒ Object

Yields a list of all things under (and including) this entry that match the given pattern.

Attributes

  • entry - Entry to start listing under

  • pattern - Chef::ChefFS::FilePattern to match children under



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/chef_fs/file_system.rb', line 32

def self.list(entry, pattern, &block)
  # Include self in results if it matches
  if pattern.match?(entry.path)
    block.call(entry)
  end

  if entry.dir? && pattern.could_match_children?(entry.path)
    # If it's possible that our children could match, descend in and add matches.
    exact_child_name = pattern.exact_child_name_under(entry.path)

    # If we've got an exact name, don't bother listing children; just grab the
    # child with the given name.
    if exact_child_name
      exact_child = entry.child(exact_child_name)
      if exact_child
        list(exact_child, pattern, &block)
      end

    # Otherwise, go through all children and find any matches
    else
      entry.children.each do |child|
        list(child, pattern, &block)
      end
    end
  end
end

.list_pairs(pattern, a_root, b_root) ⇒ Object

Yield entries for children that are in either a_root or b_root, with matching pairs matched up.

Yields

Yields matching entries in pairs:

[ a_entry, b_entry ]

Example

Chef::ChefFS::FileSystem.list_pairs(FilePattern.new('**x.txt', a_root, b_root)) do |a, b|
  ...
end


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/chef_fs/file_system.rb', line 143

def self.list_pairs(pattern, a_root, b_root)
  # Make sure everything on the server is also on the filesystem, and diff
  found_paths = Set.new
  Chef::ChefFS::FileSystem.list(a_root, pattern) do |a|
    found_paths << a.path
    b = Chef::ChefFS::FileSystem.resolve_path(b_root, a.path)
    yield [ a, b ]
  end

  # Check the outer regex pattern to see if it matches anything on the
  # filesystem that isn't on the server
  Chef::ChefFS::FileSystem.list(b_root, pattern) do |b|
    if !found_paths.include?(b.path)
      a = Chef::ChefFS::FileSystem.resolve_path(a_root, b.path)
      yield [ a, b ]
    end
  end
end

.resolve_path(entry, path) ⇒ Object

Resolve the given path against the entry, returning the entry at the end of the path.

Attributes

  • entry - the entry to start looking under. Relative paths will be resolved from here.

  • path - the path to resolve. If it starts with /, the path will be resolved starting from entry.root.

Examples

Chef::ChefFS::FileSystem.resolve_path(root_path, 'cookbooks/java/recipes/default.rb')


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/chef_fs/file_system.rb', line 73

def self.resolve_path(entry, path)
  return entry if path.length == 0
  return resolve_path(entry.root, path) if path[0,1] == "/" && entry.root != entry
  if path[0,1] == "/"
    path = path[1,path.length-1]
  end

  result = entry
  Chef::ChefFS::PathUtils::split(path).each do |part|
    result = result.child(part)
  end
  result
end