diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-08-02 14:01:51 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-08-02 14:01:51 -0700 |
commit | 3c911290b661258e183269bcdfffbb71236dfc6c (patch) | |
tree | d3f3f904a32169bd94a0a1091f72e1dc09751dda | |
parent | 7828cef4ada2128c1c3f978a856e1797fa331b94 (diff) | |
download | chef-3c911290b661258e183269bcdfffbb71236dfc6c.tar.gz |
Add API to skip resource parsing in a cookbooklcg/skip-resource-loading
The cookbook itself can use this API from its own library segment in
order to skip loading some of its resources based on the chef-client
version.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/chef_class.rb | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/chef/chef_class.rb b/lib/chef/chef_class.rb index 95b03f1c5c..c14359fd55 100644 --- a/lib/chef/chef_class.rb +++ b/lib/chef/chef_class.rb @@ -1,6 +1,6 @@ # # Author:: Lamont Granquist (<lamont@chef.io>) -# Copyright:: Copyright 2015-2016, Chef Software, Inc. +# Copyright:: Copyright 2015-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -119,6 +119,23 @@ class Chef end # + # Useful to call from library files in order to skip loading resources based on the chef_version that is running. + # + # Chef.skip_resources_on_chef_version(cookbook_name: "test", chef_version: ">= 14.0", skip_resources: [ "stuff.rb" ]) + # + # @param cookbook_name [String] name of the cookbook to skip resources in (difficult for us to get this from introspection so caller has to provide it) + # @param chef_version [String] constraint of the form ">= 14.0.0" + # @param skip_resources [Array<String>] list of files in the :resources segment to skip parsing + # + def skip_resources_on_chef_version(cookbook_name:, chef_version:, skip_resources:) + if Gem::Requirement.new(chef_version).satisfied_by?(Gem::Version.new(Chef::VERSION)) + Chef.run_context.cookbook_collection[cookbook_name].files_for(:resources).select { |x| skip_resources.include? x["name"].split("/").last }.map { |x| x["full_path"] }.each do |x| + Chef::Resource::LWRPBase.send(:loaded_lwrps)[x] = true + end + end + end + + # # Dependency Injection API (Private not Public) # [ in the ruby sense these have to be public methods, but they are # *NOT* for public consumption ] |