diff options
author | danielsdeleo <dan@opscode.com> | 2012-12-18 12:24:49 -0800 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2012-12-19 15:39:53 -0800 |
commit | 3927c163d868af1ee6d0865a7048b86b179c898d (patch) | |
tree | 27b9863bfdd91044b8cfc9067e30adc7d0f278bb | |
parent | 8acaa8fe1c52e34579bb78e2cf84a9fba44535ce (diff) | |
download | chef-3927c163d868af1ee6d0865a7048b86b179c898d.tar.gz |
Extract provider LWRP code to LWRPBase
-rw-r--r-- | lib/chef/provider.rb | 52 | ||||
-rw-r--r-- | lib/chef/provider/lwrp_base.rb | 75 | ||||
-rw-r--r-- | lib/chef/providers.rb | 2 | ||||
-rw-r--r-- | lib/chef/run_context/cookbook_compiler.rb | 4 | ||||
-rw-r--r-- | spec/unit/lwrp_spec.rb | 8 |
5 files changed, 83 insertions, 58 deletions
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index a7f60c0383..3fe057bd9b 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -172,57 +172,5 @@ class Chef end end - public - - class << self - include Chef::Mixin::ConvertToClassName - - def build_from_file(cookbook_name, filename, run_context) - pname = filename_to_qualified_string(cookbook_name, filename) - - # Add log entry if we override an existing light-weight provider. - class_name = convert_to_class_name(pname) - overriding = Chef::Provider.const_defined?(class_name) - Chef::Log.info("#{class_name} light-weight provider already initialized -- overriding!") if overriding - - new_provider_class = Class.new self do |cls| - - include Chef::DSL::Recipe - - # These were previously provided by Chef::Mixin::RecipeDefinitionDSLCore. - # They are not included by its replacment, Chef::DSL::Recipe, but - # they may be used in existing LWRPs. - include Chef::DSL::PlatformIntrospection - include Chef::DSL::DataQuery - - def load_current_resource - # silence Chef::Exceptions::Override exception - end - - class << cls - include Chef::Mixin::FromFile - - # setup DSL's shortcut methods - def action(name, &block) - define_method("action_#{name.to_s}") do - instance_eval(&block) - end - end - end - - # load provider definition from file - cls.class_from_file(filename) - end - - # register new class as a Chef::Provider - pname = filename_to_qualified_string(cookbook_name, filename) - class_name = convert_to_class_name(pname) - Chef::Provider.const_set(class_name, new_provider_class) - Chef::Log.debug("Loaded contents of #{filename} into a provider named #{pname} defined in Chef::Provider::#{class_name}") - - new_provider_class - end - end - end end diff --git a/lib/chef/provider/lwrp_base.rb b/lib/chef/provider/lwrp_base.rb new file mode 100644 index 0000000000..54ba23e728 --- /dev/null +++ b/lib/chef/provider/lwrp_base.rb @@ -0,0 +1,75 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Christopher Walters (<cw@opscode.com>) +# Copyright:: Copyright (c) 2008, 2009 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/provider' + +class Chef + class Provider + + # == Chef::Provider::LWRPBase + # Base class from which LWRP providers inherit. + class LWRPBase < Provider + + extend Chef::Mixin::ConvertToClassName + extend Chef::Mixin::FromFile + + include Chef::DSL::Recipe + + # These were previously provided by Chef::Mixin::RecipeDefinitionDSLCore. + # They are not included by its replacment, Chef::DSL::Recipe, but + # they may be used in existing LWRPs. + include Chef::DSL::PlatformIntrospection + include Chef::DSL::DataQuery + + def self.build_from_file(cookbook_name, filename, run_context) + provider_name = filename_to_qualified_string(cookbook_name, filename) + + # Add log entry if we override an existing light-weight provider. + class_name = convert_to_class_name(provider_name) + + if Chef::Provider.const_defined?(class_name) + Chef::Log.info("#{class_name} light-weight provider already initialized -- overriding!") + end + + provider_class = Class.new(self) + provider_class.class_from_file(filename) + + class_name = convert_to_class_name(provider_name) + Chef::Provider.const_set(class_name, provider_class) + Chef::Log.debug("Loaded contents of #{filename} into a provider named #{provider_name} defined in Chef::Provider::#{class_name}") + + provider_class + end + + # DSL for defining a provider's actions. + def self.action(name, &block) + define_method("action_#{name}") do + instance_eval(&block) + end + end + + # no-op `load_current_resource`. Allows simple LWRP providers to work + # without defining this method explicitly (silences + # Chef::Exceptions::Override exception) + def load_current_resource + end + + end + end +end diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 36281abf6a..568e06e253 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -98,3 +98,5 @@ require 'chef/provider/mount/windows' require 'chef/provider/deploy/revision' require 'chef/provider/deploy/timestamped' + +require "chef/provider/lwrp_base" diff --git a/lib/chef/run_context/cookbook_compiler.rb b/lib/chef/run_context/cookbook_compiler.rb index 67c28e4556..784458fdce 100644 --- a/lib/chef/run_context/cookbook_compiler.rb +++ b/lib/chef/run_context/cookbook_compiler.rb @@ -19,7 +19,7 @@ require 'chef/log' require 'chef/recipe' require 'chef/resource' -require 'chef/provider' +require 'chef/provider/lwrp_base' require 'chef/resource_definition_list' class Chef @@ -196,7 +196,7 @@ class Chef def load_lwrp_provider(cookbook_name, filename) Chef::Log.debug("Loading cookbook #{cookbook_name}'s providers from #{filename}") - Chef::Provider.build_from_file(cookbook_name, filename, self) + Chef::Provider::LWRPBase.build_from_file(cookbook_name, filename, self) @events.lwrp_file_loaded(filename) rescue Exception => e @events.lwrp_file_load_failed(filename, e) diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index da2278e547..a399ee0521 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -36,12 +36,12 @@ describe "override logging" do it "should log if attempting to load provider of same name" do Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp", "providers", "*"))].each do |file| - Chef::Provider.build_from_file("lwrp", file, nil) + Chef::Provider::LWRPBase.build_from_file("lwrp", file, nil) end Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp_override", "providers", "*"))].each do |file| Chef::Log.should_receive(:info).with(/overriding/) - Chef::Provider.build_from_file("lwrp", file, nil) + Chef::Provider::LWRPBase.build_from_file("lwrp", file, nil) end end @@ -130,11 +130,11 @@ describe "LWRP" do end Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp", "providers", "*"))].each do |file| - Chef::Provider.build_from_file("lwrp", file, @run_context) + Chef::Provider::LWRPBase.build_from_file("lwrp", file, @run_context) end Dir[File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "lwrp_override", "providers", "*"))].each do |file| - Chef::Provider.build_from_file("lwrp", file, @run_context) + Chef::Provider::LWRPBase.build_from_file("lwrp", file, @run_context) end end |