summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb
blob: 3523d85a4ec14b27ad3fc8372f2d98738030fc7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/chef_fs/file_system/base_fs_dir'
require 'chef/chef_fs/file_system/chef_repository_file_system_entry'
require 'chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir'
require 'chef/chef_fs/file_system/chef_repository_file_system_data_bags_dir'
require 'chef/chef_fs/file_system/multiplexed_dir'
require 'chef/chef_fs/data_handler/client_data_handler'
require 'chef/chef_fs/data_handler/environment_data_handler'
require 'chef/chef_fs/data_handler/node_data_handler'
require 'chef/chef_fs/data_handler/role_data_handler'
require 'chef/chef_fs/data_handler/user_data_handler'
require 'chef/chef_fs/data_handler/group_data_handler'
require 'chef/chef_fs/data_handler/container_data_handler'
require 'chef/chef_fs/data_handler/acl_data_handler'

class Chef
  module ChefFS
    module FileSystem
      class ChefRepositoryFileSystemRootDir < BaseFSDir
        def initialize(child_paths)
          super("", nil)
          @child_paths = child_paths
        end

        attr_reader :child_paths

        def children
          @children ||= child_paths.keys.sort.map { |name| make_child_entry(name) }.select { |child| !child.nil? }
        end

        def can_have_child?(name, is_dir)
          child_paths.has_key?(name) && is_dir
        end

        def create_child(name, file_contents = nil)
          child_paths[name].each do |path|
            Dir.mkdir(path)
          end
          make_child_entry(name)
        end

        def json_class
          nil
        end

        # Used to print out the filesystem
        def fs_description
          repo_path = File.dirname(child_paths['cookbooks'][0])
          result = "repository at #{repo_path}\n"
          if Chef::Config[: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| File.dirname(path) != repo_path }
              result << "  #{name} at #{paths.join(', ')}\n"
            end
          end
          result
        end

        private

        def make_child_entry(name)
          paths = child_paths[name].select do |path|
            File.exists?(path)
          end
          if paths.size == 0
            return nil
          end
          if name == 'cookbooks'
            dirs = paths.map { |path| ChefRepositoryFileSystemCookbooksDir.new(name, self, path) }
          elsif name == 'data_bags'
            dirs = paths.map { |path| ChefRepositoryFileSystemDataBagsDir.new(name, self, path) }
          else
            data_handler = case name
              when 'clients'
                Chef::ChefFS::DataHandler::ClientDataHandler.new
              when 'environments'
                Chef::ChefFS::DataHandler::EnvironmentDataHandler.new
              when 'nodes'
                Chef::ChefFS::DataHandler::NodeDataHandler.new
              when 'roles'
                Chef::ChefFS::DataHandler::RoleDataHandler.new
              when 'users'
                Chef::ChefFS::DataHandler::UserDataHandler.new
              when 'groups'
                Chef::ChefFS::DataHandler::GroupDataHandler.new
              when 'containers'
                Chef::ChefFS::DataHandler::ContainerDataHandler.new
              when 'acls'
                Chef::ChefFS::DataHandler::AclDataHandler.new
              else
                raise "Unknown top level path #{name}"
              end
            dirs = paths.map { |path| ChefRepositoryFileSystemEntry.new(name, self, path, data_handler) }
          end
          MultiplexedDir.new(dirs)
        end
      end
    end
  end
end