Class: ChefApply::RecipeLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_apply/recipe_lookup.rb

Overview

When users are trying to converge a local recipe on a remote target, there is a very specific (but expansive) set of things they can specify. This class encapsulates that logic for testing purposes. We either return a path to a recipe or we raise an error.

Defined Under Namespace

Classes: CookbookNotFound, InvalidCookbook, NoDefaultRecipe, RecipeNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook_repo_paths) ⇒ RecipeLookup

Returns a new instance of RecipeLookup.



31
32
33
# File 'lib/chef_apply/recipe_lookup.rb', line 31

def initialize(cookbook_repo_paths)
  @cookbook_repo_paths = cookbook_repo_paths
end

Instance Attribute Details

#cookbook_repo_pathsObject (readonly)

Returns the value of attribute cookbook_repo_paths.



30
31
32
# File 'lib/chef_apply/recipe_lookup.rb', line 30

def cookbook_repo_paths
  @cookbook_repo_paths
end

Instance Method Details

#find_recipe(cookbook, recipe_name = nil) ⇒ Object

Find the specified recipe or default recipe if none is specified. Raise an error if recipe cannot be found.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef_apply/recipe_lookup.rb', line 80

def find_recipe(cookbook, recipe_name = nil)
  recipes = cookbook.recipe_filenames_by_name
  if recipe_name.nil?
    default_recipe = recipes["default"]
    raise NoDefaultRecipe.new(cookbook.root_dir, cookbook.name) if default_recipe.nil?
    default_recipe
  else
    recipe = recipes[recipe_name]
    raise RecipeNotFound.new(cookbook.root_dir, recipe_name, recipes.keys, cookbook.name) if recipe.nil?
    recipe
  end
end

#load_cookbook(path_or_name) ⇒ Object

Given a cookbook path or name, try to load that cookbook. Either return a cookbook object or raise an error.



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
# File 'lib/chef_apply/recipe_lookup.rb', line 43

def load_cookbook(path_or_name)
  require "chef/exceptions"
  if File.directory?(path_or_name)
    cookbook_path = path_or_name
    # First, is there a cookbook in the specified dir that matches?
    require "chef/cookbook/cookbook_version_loader"
    begin
      v = Chef::Cookbook::CookbookVersionLoader.new(cookbook_path)
      v.load!
      cookbook = v.cookbook_version
    rescue Chef::Exceptions::CookbookNotFoundInRepo
      raise InvalidCookbook.new(cookbook_path)
    end
  else
    cookbook_name = path_or_name
    # Second, is there a cookbook in their local repository that matches?
    require "chef/cookbook_loader"
    cb_loader = Chef::CookbookLoader.new(cookbook_repo_paths)
    cb_loader.load_cookbooks_without_shadow_warning

    begin
      cookbook = cb_loader[cookbook_name]
    rescue Chef::Exceptions::CookbookNotFoundInRepo
      cookbook_repo_paths.each do |repo_path|
        cookbook_path = File.join(repo_path, cookbook_name)
        if File.directory?(cookbook_path)
          raise InvalidCookbook.new(cookbook_path)
        end
      end
      raise CookbookNotFound.new(cookbook_name, cookbook_repo_paths)
    end
  end
  cookbook
end

#split(recipe_specifier) ⇒ Object

The recipe specifier is provided by the customer as either a path OR a cookbook and optional recipe name.



37
38
39
# File 'lib/chef_apply/recipe_lookup.rb', line 37

def split(recipe_specifier)
  recipe_specifier.split("::")
end