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


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

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


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

def child_paths
  @child_paths
end

#root_pathsObject (readonly)

Returns the value of attribute root_paths


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

def root_paths
  @root_paths
end

#versioned_cookbooksObject (readonly)

Returns the value of attribute versioned_cookbooks


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

def versioned_cookbooks
  @versioned_cookbooks
end

#write_pretty_jsonObject

Returns the value of attribute write_pretty_json


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

def write_pretty_json
  @write_pretty_json
end

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)

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

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


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

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


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 107

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, 0700)
        if Chef::Platform.windows?
          all_mask = Chef::ReservedNames::Win32::API::Security::GENERIC_ALL
          administrators = Chef::ReservedNames::Win32::Security::SID.Administrators
          owner = Chef::ReservedNames::Win32::Security::SID.default_security_object_owner
          dacl = Chef::ReservedNames::Win32::Security::ACL.create([
            Chef::ReservedNames::Win32::Security::ACE.access_allowed(owner, all_mask),
            Chef::ReservedNames::Win32::Security::ACE.access_allowed(administrators, all_mask),
          ])
          so = Chef::ReservedNames::Win32::Security::SecurableObject.new(path)
          so.owner = owner
          so.set_dacl(dacl, false)
        end
      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


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 140

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


135
136
137
# File 'lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb', line 135

def json_class
  nil
end