Class: Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemRootDir

Inherits:
BaseFSDir show all
Defined in:
lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb

Overview

Represents the root of a local Chef repository, with directories for nodes, cookbooks, roles, etc. under it.

Constant Summary collapse

CHILDREN =
%w{org.json invitations.json members.json}

Instance Attribute Summary collapse

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from BaseFSDir

#dir?, #empty?

Methods inherited from BaseFSObject

#chef_object, #child, #compare_to, #delete, #dir?, #exists?, #path_for_printing, #read, #root, #write

Constructor Details

#initialize(child_paths, root_paths = [], chef_config = Chef::Config) ⇒ ChefRepositoryFileSystemRootDir

Create a new Chef Repository File System root.

Parameters

child_paths

A hash of child paths, e.g.:

"nodes" => [ '/var/nodes', '/home/jkeiser/nodes' ],
"roles" => [ '/var/roles' ],
...
root_paths

An array of paths representing the top level, where org.json, members.json, and invites.json will be stored.

chef_config
  • a hash of options that looks suspiciously like the ones

stored in Chef::Config, containing at least these keys:
:versioned_cookbooks:: whether to include versions in cookbook names


73
74
75
76
77
78
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 73

def initialize(child_paths, root_paths = [], chef_config = Chef::Config)
  super("", nil)
  @child_paths = child_paths
  @root_paths = root_paths
  @versioned_cookbooks = chef_config[:versioned_cookbooks]
end

Instance Attribute Details

#child_pathsObject (readonly)

Returns the value of attribute child_paths.



83
84
85
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 83

def child_paths
  @child_paths
end

#root_pathsObject (readonly)

Returns the value of attribute root_paths.



82
83
84
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 82

def root_paths
  @root_paths
end

#versioned_cookbooksObject (readonly)

Returns the value of attribute versioned_cookbooks.



84
85
86
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 84

def versioned_cookbooks
  @versioned_cookbooks
end

#write_pretty_jsonObject

Returns the value of attribute write_pretty_json.



80
81
82
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 80

def write_pretty_json
  @write_pretty_json
end

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 96

def can_have_child?(name, is_dir)
  if is_dir
    child_paths.has_key?(name)
  elsif root_dir
    CHILDREN.include?(name)
  else
    false
  end
end

#childrenObject



88
89
90
91
92
93
94
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 88

def children
  @children ||= begin
                  result = child_paths.keys.sort.map { |name| make_child_entry(name) }
                  result += CHILDREN.map { |name| make_child_entry(name) }
                  result.select { |c| c && c.exists? }.sort_by { |c| c.name }
                end
end

#create_child(name, file_contents = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 106

def create_child(name, file_contents = nil)
  if file_contents
    child = root_dir.create_child(name, file_contents)
  else
    child_paths[name].each do |path|
      begin
        Dir.mkdir(path)
      rescue Errno::EEXIST
      end
    end
    child = make_child_entry(name)
  end
  @children = nil
  child
end

#fs_descriptionObject

Used to print out a human-readable file system description



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 127

def fs_description
  repo_paths = root_paths || [ File.dirname(child_paths["cookbooks"][0]) ]
  result = "repository at #{repo_paths.join(', ')}\n"
  if versioned_cookbooks
    result << "  Multiple versions per cookbook\n"
  else
    result << "  One version per cookbook\n"
  end
  child_paths.each_pair do |name, paths|
    if paths.any? { |path| !repo_paths.include?(File.dirname(path)) }
      result << "  #{name} at #{paths.join(', ')}\n"
    end
  end
  result
end

#json_classObject



122
123
124
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 122

def json_class
  nil
end