summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb
blob: ded390fa8d1725a37b705dac69089a2eb0d9bbfa (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
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2013 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/chef_repository_file_system_cookbook_entry'
require 'chef/chef_fs/file_system/cookbook_dir'
require 'chef/chef_fs/file_system/not_found_error'
require 'chef/cookbook/chefignore'
require 'chef/cookbook/cookbook_version_loader'

class Chef
  module ChefFS
    module FileSystem
      class ChefRepositoryFileSystemCookbookDir < ChefRepositoryFileSystemCookbookEntry
        def initialize(name, parent, file_path = nil)
          super(name, parent, file_path)
        end

        def chef_object
          begin
            loader = Chef::Cookbook::CookbookVersionLoader.new(file_path, parent.chefignore)
            # We need the canonical cookbook name if we are using versioned cookbooks, but we don't
            # want to spend a lot of time adding code to the main Chef libraries
            if root.versioned_cookbooks
              canonical_name = canonical_cookbook_name(File.basename(file_path))
              raise "When versioned_cookbooks mode is on, cookbook #{file_path} must match format <cookbook_name>-x.y.z"  unless canonical_name

              # KLUDGE: We shouldn't have to use instance_variable_set
              loader.instance_variable_set(:@cookbook_name, canonical_name)
            end

            loader.load_cookbooks
            cb = loader.cookbook_version
            if !cb
              Chef::Log.error("Cookbook #{file_path} empty.")
              raise "Cookbook #{file_path} empty."
            end
            cb
          rescue => e
            Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{e}")
            Chef::Log.error(e.backtrace.join("\n"))
            raise
          end
        end

        def children
          super.select { |entry| !(entry.dir? && entry.children.size == 0 ) }
        end

        def can_have_child?(name, is_dir)
          if is_dir
            # Only the given directories will be uploaded.
            return CookbookDir::COOKBOOK_SEGMENT_INFO.keys.include?(name.to_sym) && name != 'root_files'
          elsif name == Chef::Cookbook::CookbookVersionLoader::UPLOADED_COOKBOOK_VERSION_FILE
            return false
          end
          super(name, is_dir)
        end

        # Exposed as a class method so that it can be used elsewhere
        def self.canonical_cookbook_name(entry_name)
          name_match = Chef::ChefFS::FileSystem::CookbookDir::VALID_VERSIONED_COOKBOOK_NAME.match(entry_name)
          return nil if name_match.nil?
          return name_match[1]
        end

        def canonical_cookbook_name(entry_name)
          self.class.canonical_cookbook_name(entry_name)
        end

        def uploaded_cookbook_version_path
          File.join(file_path, Chef::Cookbook::CookbookVersionLoader::UPLOADED_COOKBOOK_VERSION_FILE)
        end

        def can_upload?
          File.exists?(uploaded_cookbook_version_path) || children.size > 0
        end

        protected

        def make_child_entry(child_name)
          segment_info = CookbookDir::COOKBOOK_SEGMENT_INFO[child_name.to_sym] || {}
          ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, segment_info[:ruby_only], segment_info[:recursive])
        end
      end
    end
  end
end