diff options
Diffstat (limited to 'lib')
79 files changed, 600 insertions, 406 deletions
diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index 97f5088b5d..77646376ba 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -21,6 +21,7 @@ require 'chef/mixin/convert_to_class_name' require 'chef/exceptions' require 'chef/resource_builder' require 'chef/mixin/shell_out' +require 'chef/mixin/powershell_out' require 'chef/dsl/resources' require 'chef/dsl/definitions' @@ -33,6 +34,7 @@ class Chef module Recipe include Chef::Mixin::ShellOut + include Chef::Mixin::PowershellOut # method_missing must live for backcompat purposes until Chef 13. def method_missing(method_symbol, *args, &block) diff --git a/lib/chef/dsl/resources.rb b/lib/chef/dsl/resources.rb index 4072ff2c89..a181c3be33 100644 --- a/lib/chef/dsl/resources.rb +++ b/lib/chef/dsl/resources.rb @@ -10,12 +10,12 @@ class Chef def self.add_resource_dsl(dsl_name) begin module_eval(<<-EOM, __FILE__, __LINE__+1) - def #{dsl_name}(name, created_at=nil, &block) + def #{dsl_name}(name=nil, created_at=nil, &block) declare_resource(#{dsl_name.inspect}, name, created_at || caller[0], &block) end EOM rescue SyntaxError - define_method(dsl_name.to_sym) do |name, created_at=nil, &block| + define_method(dsl_name.to_sym) do |name=nil, created_at=nil, &block| declare_resource(dsl_name, name, created_at || caller[0], &block) end end diff --git a/lib/chef/knife/core/subcommand_loader.rb b/lib/chef/knife/core/subcommand_loader.rb index 1f59515f44..a8705c724f 100644 --- a/lib/chef/knife/core/subcommand_loader.rb +++ b/lib/chef/knife/core/subcommand_loader.rb @@ -23,7 +23,7 @@ class Chef class SubcommandLoader MATCHES_CHEF_GEM = %r{/chef-[\d]+\.[\d]+\.[\d]+}.freeze - MATCHES_THIS_CHEF_GEM = %r{/chef-#{Chef::VERSION}/}.freeze + MATCHES_THIS_CHEF_GEM = %r{/chef-#{Chef::VERSION}(-\w+)?(-\w+)?/}.freeze attr_reader :chef_config_dir attr_reader :env diff --git a/lib/chef/mixin/powershell_out.rb b/lib/chef/mixin/powershell_out.rb new file mode 100644 index 0000000000..e4f29c07c4 --- /dev/null +++ b/lib/chef/mixin/powershell_out.rb @@ -0,0 +1,98 @@ +#-- +# Copyright:: Copyright (c) 2015 Chef Software, 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/mixin/shell_out' +require 'chef/mixin/windows_architecture_helper' + +class Chef + module Mixin + module PowershellOut + include Chef::Mixin::ShellOut + include Chef::Mixin::WindowsArchitectureHelper + + # Run a command under powershell with the same API as shell_out. The + # options hash is extended to take an "architecture" flag which + # can be set to :i386 or :x86_64 to force the windows architecture. + # + # @param script [String] script to run + # @param options [Hash] options hash + # @return [Mixlib::Shellout] mixlib-shellout object + def powershell_out(*command_args) + script = command_args.first + options = command_args.last.is_a?(Hash) ? command_args.last : nil + + run_command_with_os_architecture(script, options) + end + + # Run a command under powershell with the same API as shell_out! + # (raises exceptions on errors) + # + # @param script [String] script to run + # @param options [Hash] options hash + # @return [Mixlib::Shellout] mixlib-shellout object + def powershell_out!(*command_args) + cmd = powershell_out(*command_args) + cmd.error! + cmd + end + + private + + # Helper function to run shell_out and wrap it with the correct + # flags to possibly disable WOW64 redirection (which we often need + # because chef-client runs as a 32-bit app on 64-bit windows). + # + # @param script [String] script to run + # @param options [Hash] options hash + # @return [Mixlib::Shellout] mixlib-shellout object + def run_command_with_os_architecture(script, options) + options ||= {} + options = options.dup + arch = options.delete(:architecture) + + with_os_architecture(nil, architecture: arch) do + shell_out( + build_powershell_command(script), + options + ) + end + end + + # Helper to build a powershell command around the script to run. + # + # @param script [String] script to run + # @retrurn [String] powershell command to execute + def build_powershell_command(script) + flags = [ + # Hides the copyright banner at startup. + "-NoLogo", + # Does not present an interactive prompt to the user. + "-NonInteractive", + # Does not load the Windows PowerShell profile. + "-NoProfile", + # always set the ExecutionPolicy flag + # see http://technet.microsoft.com/en-us/library/ee176961.aspx + "-ExecutionPolicy Unrestricted", + # Powershell will hang if STDIN is redirected + # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected + "-InputFormat None" + ] + + "powershell.exe #{flags.join(' ')} -Command \"#{script}\"" + end + end + end +end diff --git a/lib/chef/mixin/windows_architecture_helper.rb b/lib/chef/mixin/windows_architecture_helper.rb index a0ac34f627..c5f3e1bd79 100644 --- a/lib/chef/mixin/windows_architecture_helper.rb +++ b/lib/chef/mixin/windows_architecture_helper.rb @@ -42,7 +42,7 @@ class Chef is_i386_process_on_x86_64_windows? end - def with_os_architecture(node) + def with_os_architecture(node, architecture: nil) node ||= begin os_arch = ENV['PROCESSOR_ARCHITEW6432'] || ENV['PROCESSOR_ARCHITECTURE'] @@ -51,9 +51,12 @@ class Chef n[:kernel][:machine] = os_arch == 'AMD64' ? :x86_64 : :i386 end end + + architecture ||= node_windows_architecture(node) + wow64_redirection_state = nil - if wow64_architecture_override_required?(node, node_windows_architecture(node)) + if wow64_architecture_override_required?(node, architecture) wow64_redirection_state = disable_wow64_file_redirection(node) end diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb index 03b1c9ca1e..b3948eac21 100644 --- a/lib/chef/platform/query_helpers.rb +++ b/lib/chef/platform/query_helpers.rb @@ -39,6 +39,11 @@ class Chef is_server_2003 end + def supports_powershell_execution_bypass?(node) + node[:languages] && node[:languages][:powershell] && + node[:languages][:powershell][:version].to_i >= 3 + end + def supports_dsc?(node) node[:languages] && node[:languages][:powershell] && node[:languages][:powershell][:version].to_i >= 4 diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index 99d09d0507..42347a230e 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -22,6 +22,7 @@ require 'chef/mixin/convert_to_class_name' require 'chef/mixin/enforce_ownership_and_permissions' require 'chef/mixin/why_run' require 'chef/mixin/shell_out' +require 'chef/mixin/powershell_out' require 'chef/mixin/provides' require 'chef/platform/service_helpers' require 'chef/node_map' @@ -30,6 +31,7 @@ class Chef class Provider include Chef::Mixin::WhyRun include Chef::Mixin::ShellOut + include Chef::Mixin::PowershellOut extend Chef::Mixin::Provides # supports the given resource and action (late binding) diff --git a/lib/chef/provider/dsc_resource.rb b/lib/chef/provider/dsc_resource.rb index 2812c154c6..5fa84a21e9 100644 --- a/lib/chef/provider/dsc_resource.rb +++ b/lib/chef/provider/dsc_resource.rb @@ -121,7 +121,14 @@ class Chef # however Invoke-DscResource is not correctly writing to that # stream and instead just dumping to stdout @converge_description = result.stdout - result.return_value[0]["InDesiredState"] + + if result.return_value.is_a?(Array) + # WMF Feb 2015 Preview + result.return_value[0]["InDesiredState"] + else + # WMF April 2015 Preview + result.return_value["InDesiredState"] + end end def set_resource diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb index c53aa8934a..d120a64f0d 100644 --- a/lib/chef/provider/package/rubygems.rb +++ b/lib/chef/provider/package/rubygems.rb @@ -32,14 +32,7 @@ require 'rubygems/version' require 'rubygems/dependency' require 'rubygems/spec_fetcher' require 'rubygems/platform' - -# Compatibility note: Rubygems 2.0 removes rubygems/format in favor of -# rubygems/package. -begin - require 'rubygems/format' -rescue LoadError - require 'rubygems/package' -end +require 'rubygems/package' require 'rubygems/dependency_installer' require 'rubygems/uninstaller' require 'rubygems/specification' diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb index f9dcd6d80c..ed44dee6ae 100644 --- a/lib/chef/provider/powershell_script.rb +++ b/lib/chef/provider/powershell_script.rb @@ -24,71 +24,153 @@ class Chef provides :powershell_script, os: "windows" + def initialize (new_resource, run_context) + super(new_resource, run_context, '.ps1') + add_exit_status_wrapper + end + + def action_run + valid_syntax = validate_script_syntax! + super if valid_syntax + end + + def flags + # Must use -File rather than -Command to launch the script + # file created by the base class that contains the script + # code -- otherwise, powershell.exe does not propagate the + # error status of a failed Windows process that ran at the + # end of the script, it gets changed to '1'. + interpreter_flags = [default_interpreter_flags, '-File'].join(' ') + + if ! (@new_resource.flags.nil?) + interpreter_flags = [@new_resource.flags, interpreter_flags].join(' ') + end + + interpreter_flags + end + protected - EXIT_STATUS_EXCEPTION_HANDLER = "\ntrap [Exception] {write-error -exception ($_.Exception.Message);exit 1}".freeze - EXIT_STATUS_NORMALIZATION_SCRIPT = "\nif ($? -ne $true) { if ( $LASTEXITCODE ) {exit $LASTEXITCODE} else { exit 1 }}".freeze - EXIT_STATUS_RESET_SCRIPT = "\n$global:LASTEXITCODE=$null".freeze - # Process exit codes are strange with PowerShell. Unless you - # explicitly call exit in Powershell, the powershell.exe - # interpreter returns only 0 for success or 1 for failure. Since - # we'd like to get specific exit codes from executable tools run - # with Powershell, we do some work using the automatic variables - # $? and $LASTEXITCODE to return the process exit code of the - # last process run in the script if it is the last command - # executed, otherwise 0 or 1 based on whether $? is set to true - # (success, where we return 0) or false (where we return 1). - def normalize_script_exit_status( code ) - target_code = ( EXIT_STATUS_EXCEPTION_HANDLER + - EXIT_STATUS_RESET_SCRIPT + - "\n" + - code.to_s + - EXIT_STATUS_NORMALIZATION_SCRIPT ) - convert_boolean_return = @new_resource.convert_boolean_return - self.code = <<EOH -new-variable -name interpolatedexitcode -visibility private -value $#{convert_boolean_return} -new-variable -name chefscriptresult -visibility private -$chefscriptresult = { -#{target_code} -}.invokereturnasis() -if ($interpolatedexitcode -and $chefscriptresult.gettype().name -eq 'boolean') { exit [int32](!$chefscriptresult) } else { exit 0 } -EOH - Chef::Log.debug("powershell_script provider called with script code:\n\n#{code}\n") + # Process exit codes are strange with PowerShell and require + # special handling to cover common use cases. + def add_exit_status_wrapper + self.code = wrapper_script + Chef::Log.debug("powershell_script provider called with script code:\n\n#{@new_resource.code}\n") Chef::Log.debug("powershell_script provider will execute transformed code:\n\n#{self.code}\n") end - public + def validate_script_syntax! + interpreter_arguments = default_interpreter_flags.join(' ') + Tempfile.open(['chef_powershell_script-user-code', '.ps1']) do | user_script_file | + user_script_file.puts("{#{@new_resource.code}}") + user_script_file.close - def initialize (new_resource, run_context) - super(new_resource, run_context, '.ps1') - normalize_script_exit_status(new_resource.code) + validation_command = "\"#{interpreter}\" #{interpreter_arguments} -Command #{user_script_file.path}" + + # For consistency with other script resources, allow even syntax errors + # to be suppressed if the returns attribute would have suppressed it + # at converge. + valid_returns = [0] + specified_returns = @new_resource.returns.is_a?(Integer) ? + [@new_resource.returns] : + @new_resource.returns + valid_returns.concat([1]) if specified_returns.include?(1) + + result = shell_out!(validation_command, {returns: valid_returns}) + result.exitstatus == 0 + end end - def flags - default_flags = [ + def default_interpreter_flags + # 'Bypass' is preferable since it doesn't require user input confirmation + # for files such as PowerShell modules downloaded from the + # Internet. However, 'Bypass' is not supported prior to + # PowerShell 3.0, so the fallback is 'Unrestricted' + execution_policy = Chef::Platform.supports_powershell_execution_bypass?(run_context.node) ? 'Bypass' : 'Unrestricted' + + [ "-NoLogo", "-NonInteractive", "-NoProfile", - "-ExecutionPolicy Unrestricted", + "-ExecutionPolicy #{execution_policy}", # Powershell will hang if STDIN is redirected # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected - "-InputFormat None", - # Must use -File rather than -Command to launch the script - # file created by the base class that contains the script - # code -- otherwise, powershell.exe does not propagate the - # error status of a failed Windows process that ran at the - # end of the script, it gets changed to '1'. - "-File" + "-InputFormat None" ] + end - interpreter_flags = default_flags.join(' ') + # A wrapper script is used to launch user-supplied script while + # still obtaining useful process exit codes. Unless you + # explicitly call exit in Powershell, the powershell.exe + # interpreter returns only 0 for success or 1 for failure. Since + # we'd like to get specific exit codes from executable tools run + # with Powershell, we do some work using the automatic variables + # $? and $LASTEXITCODE to return the process exit code of the + # last process run in the script if it is the last command + # executed, otherwise 0 or 1 based on whether $? is set to true + # (success, where we return 0) or false (where we return 1). + def wrapper_script +<<-EOH +# Chef Client wrapper for powershell_script resources - if ! (@new_resource.flags.nil?) - interpreter_flags = [@new_resource.flags, interpreter_flags].join(' ') - end +# LASTEXITCODE can be uninitialized -- make it explictly 0 +# to avoid incorrect detection of failure (non-zero) codes +$global:LASTEXITCODE = 0 - interpreter_flags +# Catch any exceptions -- without this, exceptions will result +# In a zero return code instead of the desired non-zero code +# that indicates a failure +trap [Exception] {write-error ($_.Exception.Message);exit 1} + +# Variable state that should not be accessible to the user code +new-variable -name interpolatedexitcode -visibility private -value $#{@new_resource.convert_boolean_return} +new-variable -name chefscriptresult -visibility private + +# Initialize a variable we use to capture $? inside a block +$global:lastcmdlet = $null + +# Execute the user's code in a script block -- +$chefscriptresult = +{ + #{@new_resource.code} + + # This assignment doesn't affect the block's return value + $global:lastcmdlet = $? +}.invokereturnasis() + +# Assume failure status of 1 -- success cases +# will have to override this +$exitstatus = 1 + +# If convert_boolean_return is enabled, the block's return value +# gets precedence in determining our exit status +if ($interpolatedexitcode -and $chefscriptresult -ne $null -and $chefscriptresult.gettype().name -eq 'boolean') +{ + $exitstatus = [int32](!$chefscriptresult) +} +elseif ($lastcmdlet) +{ + # Otherwise, a successful cmdlet execution defines the status + $exitstatus = 0 +} +elseif ( $LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0 ) +{ + # If the cmdlet status is failed, allow the Win32 status + # in $LASTEXITCODE to define exit status. This handles the case + # where no cmdlets, only Win32 processes have run since $? + # will be set to $false whenever a Win32 process returns a non-zero + # status. + $exitstatus = $LASTEXITCODE +} + +# If this script is launched with -File, the process exit +# status of PowerShell.exe will be $exitstatus. If it was +# launched with -Command, it will be 0 if $exitstatus was 0, +# 1 (i.e. failed) otherwise. +exit $exitstatus +EOH end + end end end diff --git a/lib/chef/provider/service/macosx.rb b/lib/chef/provider/service/macosx.rb index 7cfe57a92a..7324822eff 100644 --- a/lib/chef/provider/service/macosx.rb +++ b/lib/chef/provider/service/macosx.rb @@ -28,8 +28,8 @@ class Chef class Service class Macosx < Chef::Provider::Service::Simple - provides :service, os: "darwin" provides :macosx_service, os: "darwin" + provides :service, os: "darwin" def self.gather_plist_dirs locations = %w{/Library/LaunchAgents diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 757cd442af..070793a7a2 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -34,10 +34,12 @@ require 'chef/platform' require 'chef/resource/resource_notification' require 'chef/provider_resolver' require 'chef/resource_resolver' +require 'set' require 'chef/mixin/deprecation' require 'chef/mixin/provides' require 'chef/mixin/shell_out' +require 'chef/mixin/powershell_out' class Chef class Resource @@ -54,6 +56,9 @@ class Chef # This lets user code do things like `not_if { shell_out!("command") }` include Chef::Mixin::ShellOut + include Chef::Mixin::PowershellOut + + NULL_ARG = Object.new # # The node the current Chef run is using. @@ -86,7 +91,6 @@ class Chef run_context.resource_collection.find(*args) end - # # Resource User Interface (for users) # @@ -105,8 +109,8 @@ class Chef @before = nil @params = Hash.new @provider = nil - @allowed_actions = [ :nothing ] - @action = :nothing + @allowed_actions = self.class.allowed_actions.to_a + @action = self.class.default_action @updated = false @updated_by_last_action = false @supports = {} @@ -167,19 +171,24 @@ class Chef # @param arg [Array[Symbol], Symbol] A list of actions (e.g. `:create`) # @return [Array[Symbol]] the list of actions. # + attr_accessor :action def action(arg=nil) if arg - action_list = arg.kind_of?(Array) ? arg : [ arg ] - action_list = action_list.collect { |a| a.to_sym } - action_list.each do |action| + if arg.is_a?(Array) + arg = arg.map { |a| a.to_sym } + else + arg = arg.to_sym + end + Array(arg).each do |action| validate( { action: action }, - { action: { kind_of: Symbol, equal_to: @allowed_actions } } + { action: { kind_of: Symbol, equal_to: allowed_actions } } ) end - @action = action_list + self.action = arg else - @action + # Pull the action from the class if it's not set + @action || self.class.default_action end end @@ -187,8 +196,7 @@ class Chef # Sets up a notification that will run a particular action on another resource # if and when *this* resource is updated by an action. # - # If the action does nothing--does not update this resource, the - # notification never triggers.) + # If the action does not update this resource, the notification never triggers. # # Only one resource may be specified per notification. # @@ -604,7 +612,7 @@ class Chef # def to_s - "#{@resource_name}[#{@name}]" + "#{resource_name}[#{name}]" end def to_text @@ -765,6 +773,12 @@ class Chef # have. # attr_accessor :allowed_actions + def allowed_actions(value=NULL_ARG) + if value != NULL_ARG + self.allowed_actions = value + end + @allowed_actions + end # # Whether or not this resource was updated during an action. If multiple @@ -823,23 +837,15 @@ class Chef end # - # The DSL name of this resource (e.g. `package` or `yum_package`) - # - # @return [String] The DSL name of this resource. - def self.dsl_name - Chef::Log.deprecation "Resource.dsl_name is deprecated and will be removed in Chef 11. Use resource.resource_name instead." - if name - name = self.name.split('::')[-1] - convert_to_snake_case(name) - end - end - + # The display name of this resource type, for printing purposes. # - # The name of this resource (e.g. `file`) + # Will be used to print out the resource in messages, e.g. resource_name[name] # - # @return [String] The name of this resource. + # @return [Symbol] The name of this resource type (e.g. `:execute`). # - attr_reader :resource_name + def resource_name + @resource_name || self.class.resource_name + end # # Sets a list of capabilities of the real resource. For example, `:remount` @@ -871,25 +877,142 @@ class Chef nil end - # - # The module where Chef should look for providers for this resource. - # The provider for `MyResource` will be looked up using - # `provider_base::MyResource`. Defaults to `Chef::Provider`. - # - # @param arg [Module] The module containing providers for this resource - # @return [Module] The module containing providers for this resource - # - # @example - # class MyResource < Chef::Resource - # provider_base Chef::Provider::Deploy - # # ...other stuff - # end - # - def self.provider_base(arg=nil) - @provider_base ||= arg - @provider_base ||= Chef::Provider - end + # Provider lookup and naming + class<<self + # + # The DSL name of this resource (e.g. `package` or `yum_package`) + # + # @return [String] The DSL name of this resource. + # + # @deprecated Use resource_name instead. + # + def dsl_name + Chef::Log.deprecation "Resource.dsl_name is deprecated and will be removed in Chef 13. Use resource_name instead." + if name + name = self.name.split('::')[-1] + convert_to_snake_case(name) + end + end + # + # The display name of this resource type, for printing purposes. + # + # This also automatically calls "provides" to provide DSL with the given + # name. + # + # @param value [Symbol] The desired name of this resource type (e.g. + # `execute`). + # + # @return [Symbol] The name of this resource type (e.g. `:execute`). + # + def resource_name(value=NULL_ARG) + if value != NULL_ARG + @resource_name = value.to_sym + provides self.resource_name + end + # Backcompat: set resource name for classes in Chef::Resource automatically + if !@resource_name && self.name + chef, resource, class_name, *extra = self.name.split('::') + if chef == 'Chef' && resource == 'Resource' && extra.size == 0 + @resource_name = convert_to_snake_case(self.name.split('::')[-1]) + end + end + @resource_name + end + alias :resource_name= :resource_name + + # + # Use the class name as the resource name. + # + # Munges the last part of the class name from camel case to snake case, + # and sets the resource_name to that: + # + # A::B::BlahDBlah -> blah_d_blah + # + def use_automatic_resource_name + automatic_name = convert_to_snake_case(self.name.split('::')[-1]) + resource_name automatic_name + end + + # + # The module where Chef should look for providers for this resource. + # The provider for `MyResource` will be looked up using + # `provider_base::MyResource`. Defaults to `Chef::Provider`. + # + # @param arg [Module] The module containing providers for this resource + # @return [Module] The module containing providers for this resource + # + # @example + # class MyResource < Chef::Resource + # provider_base Chef::Provider::Deploy + # # ...other stuff + # end + # + # @deprecated Use `provides` on the provider, or `provider` on the resource, instead. + # + def provider_base(arg=nil) + if arg + Chef::Log.deprecation("Resource.provider_base is deprecated and will be removed in Chef 13. Use provides on the provider, or provider on the resource, instead.") + end + @provider_base ||= arg || Chef::Provider + end + + # + # The list of allowed actions for the resource. + # + # @param actions [Array<Symbol>] The list of actions to add to allowed_actions. + # + # @return [Arrau<Symbol>] The list of actions, as symbols. + # + def allowed_actions(*actions) + @allowed_actions ||= + if superclass.respond_to?(:allowed_actions) + superclass.allowed_actions.dup + else + [ :nothing ] + end + @allowed_actions |= actions + end + def allowed_actions=(value) + @allowed_actions = value + end + + # + # The action that will be run if no other action is specified. + # + # Setting default_action will automatially add the action to + # allowed_actions, if it isn't already there. + # + # Defaults to :nothing. + # + # @param action_name [Symbol,Array<Symbol>] The default action (or series + # of actions) to use. + # + # @return [Symbol,Array<Symbol>] The default actions for the resource. + # + def default_action(action_name=NULL_ARG) + unless action_name.equal?(NULL_ARG) + if action_name.is_a?(Array) + @default_action = action_name.map { |arg| arg.to_sym } + else + @default_action = action_name.to_sym + end + + self.allowed_actions |= Array(@default_action) + end + + if @default_action + @default_action + elsif superclass.respond_to?(:default_action) + superclass.default_action + else + :nothing + end + end + def default_action=(action_name) + default_action action_name + end + end # # Internal Resource Interface (for Chef) @@ -1160,7 +1283,7 @@ class Chef def const_missing(class_name) if deprecated_constants[class_name.to_sym] - Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 12 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.") + Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 13 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.") deprecated_constants[class_name.to_sym] else raise NameError, "uninitialized constant Chef::Resource::#{class_name}" diff --git a/lib/chef/resource/apt_package.rb b/lib/chef/resource/apt_package.rb index f944825ac3..83bb6906d4 100644 --- a/lib/chef/resource/apt_package.rb +++ b/lib/chef/resource/apt_package.rb @@ -23,12 +23,11 @@ class Chef class Resource class AptPackage < Chef::Resource::Package - provides :apt_package + use_automatic_resource_name provides :package, os: "linux", platform_family: [ "debian" ] def initialize(name, run_context=nil) super - @resource_name = :apt_package @default_release = nil end diff --git a/lib/chef/resource/bash.rb b/lib/chef/resource/bash.rb index 366d8c7bd6..554d2de924 100644 --- a/lib/chef/resource/bash.rb +++ b/lib/chef/resource/bash.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Bash < Chef::Resource::Script - provides :bash + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :bash @interpreter = "bash" end diff --git a/lib/chef/resource/batch.rb b/lib/chef/resource/batch.rb index c091ec56b6..8a19e04174 100644 --- a/lib/chef/resource/batch.rb +++ b/lib/chef/resource/batch.rb @@ -22,10 +22,11 @@ class Chef class Resource class Batch < Chef::Resource::WindowsScript + use_automatic_resource_name provides :batch, os: "windows" def initialize(name, run_context=nil) - super(name, run_context, :batch, "cmd.exe") + super(name, run_context, nil, "cmd.exe") end end diff --git a/lib/chef/resource/bff_package.rb b/lib/chef/resource/bff_package.rb index d4139e7ffe..f31fe6a0d8 100644 --- a/lib/chef/resource/bff_package.rb +++ b/lib/chef/resource/bff_package.rb @@ -22,13 +22,7 @@ require 'chef/provider/package/aix' class Chef class Resource class BffPackage < Chef::Resource::Package - provides :bff_package - - def initialize(name, run_context=nil) - super - @resource_name = :bff_package - end - + use_automatic_resource_name end end end diff --git a/lib/chef/resource/breakpoint.rb b/lib/chef/resource/breakpoint.rb index 5a55858f71..0107b8968a 100644 --- a/lib/chef/resource/breakpoint.rb +++ b/lib/chef/resource/breakpoint.rb @@ -22,15 +22,14 @@ require 'chef/resource' class Chef class Resource class Breakpoint < Chef::Resource - provides :breakpoint + use_automatic_resource_name + + default_action :break def initialize(action="break", *args) - @name = caller.first - super(@name, *args) - @action = "break" - @allowed_actions << :break - @resource_name = :breakpoint + super(caller.first, *args) end + end end end diff --git a/lib/chef/resource/chef_gem.rb b/lib/chef/resource/chef_gem.rb index 59f575a524..e4e3ccfbab 100644 --- a/lib/chef/resource/chef_gem.rb +++ b/lib/chef/resource/chef_gem.rb @@ -23,11 +23,10 @@ class Chef class Resource class ChefGem < Chef::Resource::Package::GemPackage - provides :chef_gem + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :chef_gem @compile_time = Chef::Config[:chef_gem_compile_time] @gem_binary = RbConfig::CONFIG['bindir'] + "/gem" end diff --git a/lib/chef/resource/cookbook_file.rb b/lib/chef/resource/cookbook_file.rb index 7be353b648..08f4733497 100644 --- a/lib/chef/resource/cookbook_file.rb +++ b/lib/chef/resource/cookbook_file.rb @@ -27,13 +27,13 @@ class Chef class CookbookFile < Chef::Resource::File include Chef::Mixin::Securable - provides :cookbook_file + use_automatic_resource_name + + default_action :create def initialize(name, run_context=nil) super @provider = Chef::Provider::CookbookFile - @resource_name = :cookbook_file - @action = "create" @source = ::File.basename(name) @cookbook = nil end diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb index cb16506012..a399caebe9 100644 --- a/lib/chef/resource/cron.rb +++ b/lib/chef/resource/cron.rb @@ -27,13 +27,13 @@ class Chef state_attrs :minute, :hour, :day, :month, :weekday, :user - provides :cron + use_automatic_resource_name + + default_action :create + allowed_actions :create, :delete def initialize(name, run_context=nil) super - @resource_name = :cron - @action = :create - @allowed_actions.push(:create, :delete) @minute = "*" @hour = "*" @day = "*" diff --git a/lib/chef/resource/csh.rb b/lib/chef/resource/csh.rb index d37f1a8e0c..1c89f2aca6 100644 --- a/lib/chef/resource/csh.rb +++ b/lib/chef/resource/csh.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Csh < Chef::Resource::Script - provides :csh + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :csh @interpreter = "csh" end diff --git a/lib/chef/resource/deploy.rb b/lib/chef/resource/deploy.rb index 55e3547b25..f88133119a 100644 --- a/lib/chef/resource/deploy.rb +++ b/lib/chef/resource/deploy.rb @@ -50,17 +50,17 @@ class Chef # release directory. Callback files can contain chef code (resources, etc.) # class Deploy < Chef::Resource - provides :deploy - - provider_base Chef::Provider::Deploy + use_automatic_resource_name identity_attr :repository state_attrs :deploy_to, :revision + default_action :deploy + allowed_actions :force_deploy, :deploy, :rollback + def initialize(name, run_context=nil) super - @resource_name = :deploy @deploy_to = name @environment = nil @repository_cache = 'cached-copy' @@ -70,7 +70,6 @@ class Chef @symlink_before_migrate = {"config/database.yml" => "config/database.yml"} @symlinks = {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"} @revision = 'HEAD' - @action = :deploy @migrate = false @rollback_on_error = false @remote = "origin" @@ -78,7 +77,6 @@ class Chef @shallow_clone = false @scm_provider = Chef::Provider::Git @svn_force_export = false - @allowed_actions.push(:force_deploy, :deploy, :rollback) @additional_remotes = Hash[] @keep_releases = 5 @enable_checkout = true @@ -282,6 +280,12 @@ class Chef ) end + # This is to support "provider :revision" without deprecation warnings. + # Do NOT copy this. + def self.provider_base + Chef::Provider::Deploy + end + def svn_force_export(arg=nil) set_or_return( :svn_force_export, diff --git a/lib/chef/resource/deploy_revision.rb b/lib/chef/resource/deploy_revision.rb index e144ce2162..86a8631ba7 100644 --- a/lib/chef/resource/deploy_revision.rb +++ b/lib/chef/resource/deploy_revision.rb @@ -23,22 +23,14 @@ class Chef # deployment strategy (provider) class DeployRevision < Chef::Resource::Deploy - provides :deploy_revision + use_automatic_resource_name - def initialize(*args, &block) - super - @resource_name = :deploy_revision - end end class DeployBranch < Chef::Resource::DeployRevision - provides :deploy_branch + use_automatic_resource_name - def initialize(*args, &block) - super - @resource_name = :deploy_branch - end end end diff --git a/lib/chef/resource/directory.rb b/lib/chef/resource/directory.rb index 1ab7f0d16d..304317e07a 100644 --- a/lib/chef/resource/directory.rb +++ b/lib/chef/resource/directory.rb @@ -32,15 +32,15 @@ class Chef include Chef::Mixin::Securable - provides :directory + use_automatic_resource_name + + default_action :create + allowed_actions :create, :delete def initialize(name, run_context=nil) super - @resource_name = :directory @path = name - @action = :create @recursive = false - @allowed_actions.push(:create, :delete) end def recursive(arg=nil) diff --git a/lib/chef/resource/dpkg_package.rb b/lib/chef/resource/dpkg_package.rb index 35a47e8a82..e0b86947f1 100644 --- a/lib/chef/resource/dpkg_package.rb +++ b/lib/chef/resource/dpkg_package.rb @@ -23,13 +23,9 @@ class Chef class Resource class DpkgPackage < Chef::Resource::Package + use_automatic_resource_name provides :dpkg_package, os: "linux" - def initialize(name, run_context=nil) - super - @resource_name = :dpkg_package - end - end end end diff --git a/lib/chef/resource/dsc_resource.rb b/lib/chef/resource/dsc_resource.rb index 912b683434..7c822604e2 100644 --- a/lib/chef/resource/dsc_resource.rb +++ b/lib/chef/resource/dsc_resource.rb @@ -21,17 +21,17 @@ class Chef class Resource
class DscResource < Chef::Resource
+ use_automatic_resource_name
provides :dsc_resource, os: "windows"
include Chef::DSL::Powershell
+ default_action :run
+
def initialize(name, run_context)
super
@properties = {}
- @resource_name = :dsc_resource
@resource = nil
- @allowed_actions.push(:run)
- @action = :run
end
def resource(value=nil)
diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb index cf96ef6b7f..0ff7988d7d 100644 --- a/lib/chef/resource/dsc_script.rb +++ b/lib/chef/resource/dsc_script.rb @@ -22,13 +22,13 @@ class Chef class Resource class DscScript < Chef::Resource + use_automatic_resource_name provides :dsc_script, platform: "windows" + default_action :run + def initialize(name, run_context=nil) super - @allowed_actions.push(:run) - @action = :run - @resource_name = :dsc_script @imports = {} end diff --git a/lib/chef/resource/easy_install_package.rb b/lib/chef/resource/easy_install_package.rb index 5286e9a289..2483b0a8b7 100644 --- a/lib/chef/resource/easy_install_package.rb +++ b/lib/chef/resource/easy_install_package.rb @@ -22,12 +22,7 @@ class Chef class Resource class EasyInstallPackage < Chef::Resource::Package - provides :easy_install_package - - def initialize(name, run_context=nil) - super - @resource_name = :easy_install_package - end + use_automatic_resource_name def easy_install_binary(arg=nil) set_or_return( diff --git a/lib/chef/resource/env.rb b/lib/chef/resource/env.rb index 2072ae5d80..da7d48f062 100644 --- a/lib/chef/resource/env.rb +++ b/lib/chef/resource/env.rb @@ -25,16 +25,17 @@ class Chef state_attrs :value + use_automatic_resource_name provides :env, os: "windows" + default_action :create + allowed_actions :create, :delete, :modify + def initialize(name, run_context=nil) super - @resource_name = :env @key_name = name @value = nil - @action = :create @delim = nil - @allowed_actions.push(:create, :delete, :modify) end def key_name(arg=nil) diff --git a/lib/chef/resource/erl_call.rb b/lib/chef/resource/erl_call.rb index 75422c55a1..b025259b4c 100644 --- a/lib/chef/resource/erl_call.rb +++ b/lib/chef/resource/erl_call.rb @@ -23,24 +23,22 @@ require 'chef/provider/erl_call' class Chef class Resource class ErlCall < Chef::Resource - provides :erl_call + use_automatic_resource_name # erl_call : http://erlang.org/doc/man/erl_call.html identity_attr :code + default_action :run + def initialize(name, run_context=nil) super - @resource_name = :erl_call @code = "q()." # your erlang code goes here @cookie = nil # cookie of the erlang node @distributed = false # if you want to have a distributed erlang node @name_type = "sname" # type of erlang hostname name or sname @node_name = "chef@localhost" # the erlang node hostname - - @action = "run" - @allowed_actions.push(:run) end def code(arg=nil) diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb index 8fc97d748f..34ed6b1bd9 100644 --- a/lib/chef/resource/execute.rb +++ b/lib/chef/resource/execute.rb @@ -23,7 +23,7 @@ require 'chef/provider/execute' class Chef class Resource class Execute < Chef::Resource - provides :execute + use_automatic_resource_name identity_attr :command @@ -33,12 +33,12 @@ class Chef # Only execute resources (and subclasses) can be guard interpreters. attr_accessor :is_guard_interpreter + default_action :run + def initialize(name, run_context=nil) super - @resource_name = :execute @command = name @backup = 5 - @action = "run" @creates = nil @cwd = nil @environment = nil @@ -47,7 +47,6 @@ class Chef @returns = 0 @timeout = nil @user = nil - @allowed_actions.push(:run) @umask = nil @default_guard_interpreter = :execute @is_guard_interpreter = false diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb index b36fcc2135..51969f65a0 100644 --- a/lib/chef/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -47,15 +47,14 @@ class Chef # @returns [String] Checksum of the file we actually rendered attr_accessor :final_checksum - provides :file + use_automatic_resource_name + default_action :create + allowed_actions :create, :delete, :touch, :create_if_missing def initialize(name, run_context=nil) super - @resource_name = :file @path = name @backup = 5 - @action = "create" - @allowed_actions.push(:create, :delete, :touch, :create_if_missing) @atomic_update = Chef::Config[:file_atomic_update] @force_unlink = false @manage_symlink_source = nil diff --git a/lib/chef/resource/freebsd_package.rb b/lib/chef/resource/freebsd_package.rb index 9c8db506f8..f89e1813b9 100644 --- a/lib/chef/resource/freebsd_package.rb +++ b/lib/chef/resource/freebsd_package.rb @@ -29,13 +29,9 @@ class Chef class FreebsdPackage < Chef::Resource::Package include Chef::Mixin::ShellOut + use_automatic_resource_name provides :package, platform: "freebsd" - def initialize(name, run_context=nil) - super - @resource_name = :freebsd_package - end - def after_created assign_provider end diff --git a/lib/chef/resource/gem_package.rb b/lib/chef/resource/gem_package.rb index 0e838ca040..5bd3a89100 100644 --- a/lib/chef/resource/gem_package.rb +++ b/lib/chef/resource/gem_package.rb @@ -22,11 +22,10 @@ class Chef class Resource class GemPackage < Chef::Resource::Package - provides :gem_package + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :gem_package @clear_sources = false end diff --git a/lib/chef/resource/git.rb b/lib/chef/resource/git.rb index 7156873315..1229914766 100644 --- a/lib/chef/resource/git.rb +++ b/lib/chef/resource/git.rb @@ -22,11 +22,10 @@ class Chef class Resource class Git < Chef::Resource::Scm - provides :git + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :git @additional_remotes = Hash[] end diff --git a/lib/chef/resource/group.rb b/lib/chef/resource/group.rb index 9e8f1309b0..49cf92a282 100644 --- a/lib/chef/resource/group.rb +++ b/lib/chef/resource/group.rb @@ -25,19 +25,19 @@ class Chef state_attrs :members - provides :group + use_automatic_resource_name + + allowed_actions :create, :remove, :modify, :manage + default_action :create def initialize(name, run_context=nil) super - @resource_name = :group @group_name = name @gid = nil @members = [] @excluded_members = [] - @action = :create @append = false @non_unique = false - @allowed_actions.push(:create, :remove, :modify, :manage) end def group_name(arg=nil) diff --git a/lib/chef/resource/homebrew_package.rb b/lib/chef/resource/homebrew_package.rb index 73409b13ac..fe0bd89ced 100644 --- a/lib/chef/resource/homebrew_package.rb +++ b/lib/chef/resource/homebrew_package.rb @@ -25,12 +25,11 @@ class Chef class Resource class HomebrewPackage < Chef::Resource::Package - provides :homebrew_package + use_automatic_resource_name provides :package, os: "darwin" def initialize(name, run_context=nil) super - @resource_name = :homebrew_package @homebrew_user = nil end diff --git a/lib/chef/resource/http_request.rb b/lib/chef/resource/http_request.rb index 5986ebd4a0..192484f557 100644 --- a/lib/chef/resource/http_request.rb +++ b/lib/chef/resource/http_request.rb @@ -23,18 +23,18 @@ require 'chef/provider/http_request' class Chef class Resource class HttpRequest < Chef::Resource - provides :http_request + use_automatic_resource_name identity_attr :url + default_action :get + allowed_actions :get, :put, :post, :delete, :head, :options + def initialize(name, run_context=nil) super - @resource_name = :http_request @message = name @url = nil - @action = :get @headers = {} - @allowed_actions.push(:get, :put, :post, :delete, :head, :options) end def url(args=nil) diff --git a/lib/chef/resource/ifconfig.rb b/lib/chef/resource/ifconfig.rb index 60feba1704..47adc52425 100644 --- a/lib/chef/resource/ifconfig.rb +++ b/lib/chef/resource/ifconfig.rb @@ -22,18 +22,18 @@ require 'chef/resource' class Chef class Resource class Ifconfig < Chef::Resource - provides :ifconfig + use_automatic_resource_name identity_attr :device state_attrs :inet_addr, :mask + default_action :add + allowed_actions :add, :delete, :enable, :disable + def initialize(name, run_context=nil) super - @resource_name = :ifconfig @target = name - @action = :add - @allowed_actions.push(:add, :delete, :enable, :disable) @hwaddr = nil @mask = nil @inet_addr = nil diff --git a/lib/chef/resource/ips_package.rb b/lib/chef/resource/ips_package.rb index c0e699e31a..6b40fe138c 100644 --- a/lib/chef/resource/ips_package.rb +++ b/lib/chef/resource/ips_package.rb @@ -23,12 +23,13 @@ class Chef class Resource class IpsPackage < ::Chef::Resource::Package + use_automatic_resource_name provides :ips_package, os: "solaris2" + allowed_actions :install, :remove, :upgrade + def initialize(name, run_context = nil) super(name, run_context) - @resource_name = :ips_package - @allowed_actions.push(:install, :remove, :upgrade) @accept_license = false end diff --git a/lib/chef/resource/link.rb b/lib/chef/resource/link.rb index 30f8ec86d1..2fe4da718c 100644 --- a/lib/chef/resource/link.rb +++ b/lib/chef/resource/link.rb @@ -25,21 +25,21 @@ class Chef class Link < Chef::Resource include Chef::Mixin::Securable - provides :link + use_automatic_resource_name identity_attr :target_file state_attrs :to, :owner, :group + default_action :create + allowed_actions :create, :delete + def initialize(name, run_context=nil) verify_links_supported! super - @resource_name = :link @to = nil - @action = :create @link_type = :symbolic @target_file = name - @allowed_actions.push(:create, :delete) end def to(arg=nil) diff --git a/lib/chef/resource/log.rb b/lib/chef/resource/log.rb index 87be01aaa9..5c119df4d8 100644 --- a/lib/chef/resource/log.rb +++ b/lib/chef/resource/log.rb @@ -23,10 +23,12 @@ require 'chef/provider/log' class Chef class Resource class Log < Chef::Resource - provides :log + use_automatic_resource_name identity_attr :message + default_action :write + # Sends a string from a recipe to a log provider # # log "some string to log" do @@ -49,10 +51,7 @@ class Chef # node<Chef::Node>:: Node where resource will be used def initialize(name, run_context=nil) super - @resource_name = :log @level = :info - @action = :write - @allowed_actions.push(:write) @message = name end diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb index a42261cfc4..20d236a161 100644 --- a/lib/chef/resource/lwrp_base.rb +++ b/lib/chef/resource/lwrp_base.rb @@ -35,8 +35,6 @@ class Chef # so attributes, default action, etc. can be defined with pleasing syntax. class LWRPBase < Resource - NULL_ARG = Object.new - # Class methods class <<self @@ -55,12 +53,11 @@ class Chef # We load the class first to give it a chance to set its own name resource_class = Class.new(self) - resource_class.resource_name = resource_name + resource_class.resource_name resource_name.to_sym resource_class.run_context = run_context - resource_class.provides resource_name.to_sym resource_class.class_from_file(filename) - # Respect resource_name set inside the LWRP + # Make a useful string for the class (rather than <Class:312894723894>) resource_class.instance_eval do define_singleton_method(:to_s) do "LWRP resource #{resource_name} from cookbook #{cookbook_name}" @@ -77,16 +74,6 @@ class Chef resource_class end - def resource_name(arg = NULL_ARG) - if arg.equal?(NULL_ARG) - @resource_name - else - @resource_name = arg - end - end - - alias_method :resource_name=, :resource_name - # Define an attribute on this resource, including optional validation # parameters. def attribute(attr_name, validation_opts={}) @@ -95,42 +82,21 @@ class Chef end end - # Sets the default action - def default_action(action_name=NULL_ARG) - unless action_name.equal?(NULL_ARG) - @actions ||= [] - if action_name.is_a?(Array) - action = action_name.map { |arg| arg.to_sym } - @actions = actions | action - @default_action = action - else - action = action_name.to_sym - @actions.push(action) unless @actions.include?(action) - @default_action = [action] - end - end - - @default_action ||= from_superclass(:default_action) - end - # Adds +action_names+ to the list of valid actions for this resource. + # Does not include superclass's action list when appending. def actions(*action_names) - if action_names.empty? - defined?(@actions) ? @actions : from_superclass(:actions, []).dup + if !action_names.empty? && !@allowed_actions + self.allowed_actions = action_names else - # BC-compat way for checking if actions have already been defined - if defined?(@actions) - @actions.push(*action_names) - else - @actions = action_names - end + allowed_actions(*action_names) end end + alias :actions= :allowed_actions= # @deprecated def valid_actions(*args) - Chef::Log.warn("`valid_actions' is deprecated, please use actions `instead'!") - actions(*args) + Chef::Log.warn("`valid_actions' is deprecated, please use allowed_actions `instead'!") + allowed_actions(*args) end # Set the run context on the class. Used to provide access to the node @@ -162,23 +128,6 @@ class Chef superclass.respond_to?(m) ? superclass.send(m) : default end end - - private - - # Default initializer. Sets the default action and allowed actions. - def initialize(name, run_context=nil) - super(name, run_context) - - # Raise an exception if the resource_name was not defined - if self.class.resource_name.nil? - raise Chef::Exceptions::InvalidResourceSpecification, - "You must specify `resource_name'!" - end - - @resource_name = self.class.resource_name.to_sym - @action = self.class.default_action - allowed_actions.push(self.class.actions).flatten! - end end end end diff --git a/lib/chef/resource/macosx_service.rb b/lib/chef/resource/macosx_service.rb index 879ea99cf8..29da2e6309 100644 --- a/lib/chef/resource/macosx_service.rb +++ b/lib/chef/resource/macosx_service.rb @@ -22,8 +22,9 @@ class Chef class Resource class MacosxService < Chef::Resource::Service - provides :service, os: "darwin" + use_automatic_resource_name provides :macosx_service, os: "darwin" + provides :service, os: "darwin" identity_attr :service_name @@ -31,7 +32,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :macosx_service @plist = nil @session_type = nil end diff --git a/lib/chef/resource/macports_package.rb b/lib/chef/resource/macports_package.rb index 0d4e5dec65..3ccf831cf2 100644 --- a/lib/chef/resource/macports_package.rb +++ b/lib/chef/resource/macports_package.rb @@ -20,13 +20,8 @@ class Chef class Resource class MacportsPackage < Chef::Resource::Package - provides :macports_package + use_automatic_resource_name provides :package, os: "darwin" - - def initialize(name, run_context=nil) - super - @resource_name = :macports_package - end end end end diff --git a/lib/chef/resource/mdadm.rb b/lib/chef/resource/mdadm.rb index 971b6c51b4..2927cc8321 100644 --- a/lib/chef/resource/mdadm.rb +++ b/lib/chef/resource/mdadm.rb @@ -27,11 +27,13 @@ class Chef state_attrs :devices, :level, :chunk - provides :mdadm + use_automatic_resource_name + + default_action :create + allowed_actions :create, :assemble, :stop def initialize(name, run_context=nil) super - @resource_name = :mdadm @chunk = 16 @devices = [] @@ -40,9 +42,6 @@ class Chef @metadata = "0.90" @bitmap = nil @raid_device = name - - @action = :create - @allowed_actions.push(:create, :assemble, :stop) end def chunk(arg=nil) diff --git a/lib/chef/resource/mount.rb b/lib/chef/resource/mount.rb index 142dec87f7..ce6d2cc232 100644 --- a/lib/chef/resource/mount.rb +++ b/lib/chef/resource/mount.rb @@ -27,11 +27,13 @@ class Chef state_attrs :mount_point, :device_type, :fstype, :username, :password, :domain - provides :mount + use_automatic_resource_name + + default_action :mount + allowed_actions :mount, :umount, :remount, :enable, :disable def initialize(name, run_context=nil) super - @resource_name = :mount @mount_point = name @device = nil @device_type = :device @@ -42,9 +44,7 @@ class Chef @pass = 2 @mounted = false @enabled = false - @action = :mount @supports = { :remount => false } - @allowed_actions.push(:mount, :umount, :remount, :enable, :disable) @username = nil @password = nil @domain = nil diff --git a/lib/chef/resource/ohai.rb b/lib/chef/resource/ohai.rb index e2d12ce395..005b149442 100644 --- a/lib/chef/resource/ohai.rb +++ b/lib/chef/resource/ohai.rb @@ -20,18 +20,17 @@ class Chef class Resource class Ohai < Chef::Resource - provides :ohai + use_automatic_resource_name identity_attr :name state_attrs :plugin + default_action :reload + def initialize(name, run_context=nil) super - @resource_name = :ohai @name = name - @allowed_actions.push(:reload) - @action = :reload @plugin = nil end diff --git a/lib/chef/resource/openbsd_package.rb b/lib/chef/resource/openbsd_package.rb index 20a2523e3a..1071958cd2 100644 --- a/lib/chef/resource/openbsd_package.rb +++ b/lib/chef/resource/openbsd_package.rb @@ -28,13 +28,9 @@ class Chef class OpenbsdPackage < Chef::Resource::Package include Chef::Mixin::ShellOut + use_automatic_resource_name provides :package, os: "openbsd" - def initialize(name, run_context=nil) - super - @resource_name = :openbsd_package - end - def after_created assign_provider end @@ -48,4 +44,3 @@ class Chef end end end - diff --git a/lib/chef/resource/package.rb b/lib/chef/resource/package.rb index 5bea894a02..4148144816 100644 --- a/lib/chef/resource/package.rb +++ b/lib/chef/resource/package.rb @@ -22,20 +22,20 @@ require 'chef/resource' class Chef class Resource class Package < Chef::Resource - provides :package + use_automatic_resource_name identity_attr :package_name state_attrs :version, :options + default_action :install + allowed_actions :install, :upgrade, :remove, :purge, :reconfig + def initialize(name, run_context=nil) super - @action = :install - @allowed_actions.push(:install, :upgrade, :remove, :purge, :reconfig) @candidate_version = nil @options = nil @package_name = name - @resource_name = :package @response_file = nil @response_file_variables = Hash.new @source = nil diff --git a/lib/chef/resource/pacman_package.rb b/lib/chef/resource/pacman_package.rb index 4c45dd004f..222fb3c78e 100644 --- a/lib/chef/resource/pacman_package.rb +++ b/lib/chef/resource/pacman_package.rb @@ -22,13 +22,9 @@ class Chef class Resource class PacmanPackage < Chef::Resource::Package + use_automatic_resource_name provides :pacman_package, os: "linux" - def initialize(name, run_context=nil) - super - @resource_name = :pacman_package - end - end end end diff --git a/lib/chef/resource/paludis_package.rb b/lib/chef/resource/paludis_package.rb index 552c96857a..f0ddc5927a 100644 --- a/lib/chef/resource/paludis_package.rb +++ b/lib/chef/resource/paludis_package.rb @@ -23,12 +23,13 @@ class Chef class Resource class PaludisPackage < Chef::Resource::Package + use_automatic_resource_name provides :paludis_package, os: "linux" + allowed_actions :install, :remove, :upgrade + def initialize(name, run_context=nil) super(name, run_context) - @resource_name = :paludis_package - @allowed_actions.push(:install, :remove, :upgrade) @timeout = 3600 end end diff --git a/lib/chef/resource/perl.rb b/lib/chef/resource/perl.rb index cb741d145a..6870f487eb 100644 --- a/lib/chef/resource/perl.rb +++ b/lib/chef/resource/perl.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Perl < Chef::Resource::Script - provides :perl + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :perl @interpreter = "perl" end diff --git a/lib/chef/resource/portage_package.rb b/lib/chef/resource/portage_package.rb index b03b69796a..009a525d22 100644 --- a/lib/chef/resource/portage_package.rb +++ b/lib/chef/resource/portage_package.rb @@ -21,11 +21,10 @@ require 'chef/resource/package' class Chef class Resource class PortagePackage < Chef::Resource::Package - provides :portage_package + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :portage_package @provider = Chef::Provider::Package::Portage end diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb index 43aafe4df2..a3a24fce7f 100644 --- a/lib/chef/resource/powershell_script.rb +++ b/lib/chef/resource/powershell_script.rb @@ -21,10 +21,11 @@ class Chef class Resource class PowershellScript < Chef::Resource::WindowsScript + use_automatic_resource_name provides :powershell_script, os: "windows" def initialize(name, run_context=nil) - super(name, run_context, :powershell_script, "powershell.exe") + super(name, run_context, nil, "powershell.exe") @convert_boolean_return = false end diff --git a/lib/chef/resource/python.rb b/lib/chef/resource/python.rb index fffd4d75f6..5c120d7d27 100644 --- a/lib/chef/resource/python.rb +++ b/lib/chef/resource/python.rb @@ -21,11 +21,10 @@ require 'chef/provider/script' class Chef class Resource class Python < Chef::Resource::Script - provides :python + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :python @interpreter = "python" end diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb index 7cd53450ed..216367692f 100644 --- a/lib/chef/resource/reboot.rb +++ b/lib/chef/resource/reboot.rb @@ -24,13 +24,13 @@ require 'chef/resource' class Chef class Resource class Reboot < Chef::Resource - provides :reboot + use_automatic_resource_name + + allowed_actions :request_reboot, :reboot_now, :cancel def initialize(name, run_context=nil) super - @resource_name = :reboot @provider = Chef::Provider::Reboot - @allowed_actions.push(:request_reboot, :reboot_now, :cancel) @reason = "Reboot by Chef" @delay_mins = 0 diff --git a/lib/chef/resource/registry_key.rb b/lib/chef/resource/registry_key.rb index cc8d05dd53..9ee031b751 100644 --- a/lib/chef/resource/registry_key.rb +++ b/lib/chef/resource/registry_key.rb @@ -22,11 +22,14 @@ require 'chef/digester' class Chef class Resource class RegistryKey < Chef::Resource - provides :registry_key + use_automatic_resource_name identity_attr :key state_attrs :values + default_action :create + allowed_actions :create, :create_if_missing, :delete, :delete_key + # Some registry key data types may not be safely reported as json. # Example (CHEF-5323): # @@ -60,13 +63,10 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :registry_key - @action = :create @architecture = :machine @recursive = false @key = name @values, @unscrubbed_values = [], [] - @allowed_actions.push(:create, :create_if_missing, :delete, :delete_key) end def key(arg=nil) diff --git a/lib/chef/resource/remote_directory.rb b/lib/chef/resource/remote_directory.rb index d4108da47a..bb052d3bd4 100644 --- a/lib/chef/resource/remote_directory.rb +++ b/lib/chef/resource/remote_directory.rb @@ -26,19 +26,20 @@ class Chef class RemoteDirectory < Chef::Resource::Directory include Chef::Mixin::Securable - provides :remote_directory + use_automatic_resource_name identity_attr :path state_attrs :files_owner, :files_group, :files_mode + default_action :create + allowed_actions :create, :create_if_missing, :delete + def initialize(name, run_context=nil) super - @resource_name = :remote_directory @path = name @source = ::File.basename(name) @delete = false - @action = :create @recursive = true @purge = false @files_backup = 5 @@ -46,7 +47,6 @@ class Chef @files_group = nil @files_mode = 0644 unless Chef::Platform.windows? @overwrite = true - @allowed_actions.push(:create, :create_if_missing, :delete) @cookbook = nil end diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb index df9fb7ad76..99c21cae52 100644 --- a/lib/chef/resource/remote_file.rb +++ b/lib/chef/resource/remote_file.rb @@ -28,12 +28,10 @@ class Chef class RemoteFile < Chef::Resource::File include Chef::Mixin::Securable - provides :remote_file + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :remote_file - @action = "create" @source = [] @use_etag = true @use_last_modified = true diff --git a/lib/chef/resource/route.rb b/lib/chef/resource/route.rb index 8f9172060b..3df767a128 100644 --- a/lib/chef/resource/route.rb +++ b/lib/chef/resource/route.rb @@ -22,18 +22,18 @@ require 'chef/resource' class Chef class Resource class Route < Chef::Resource - provides :route + use_automatic_resource_name identity_attr :target state_attrs :netmask, :gateway + default_action :add + allowed_actions :add, :delete + def initialize(name, run_context=nil) super - @resource_name = :route @target = name - @action = [:add] - @allowed_actions.push(:add, :delete) @netmask = nil @gateway = nil @metric = nil diff --git a/lib/chef/resource/rpm_package.rb b/lib/chef/resource/rpm_package.rb index f00121dd69..67a6c156d8 100644 --- a/lib/chef/resource/rpm_package.rb +++ b/lib/chef/resource/rpm_package.rb @@ -23,11 +23,11 @@ class Chef class Resource class RpmPackage < Chef::Resource::Package + use_automatic_resource_name provides :rpm_package, os: [ "linux", "aix" ] def initialize(name, run_context=nil) super - @resource_name = :rpm_package @allow_downgrade = false end diff --git a/lib/chef/resource/ruby.rb b/lib/chef/resource/ruby.rb index 2b7644562a..759955da42 100644 --- a/lib/chef/resource/ruby.rb +++ b/lib/chef/resource/ruby.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Ruby < Chef::Resource::Script - provides :ruby + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :ruby @interpreter = "ruby" end diff --git a/lib/chef/resource/ruby_block.rb b/lib/chef/resource/ruby_block.rb index 07eec5599d..4ce7b2cee1 100644 --- a/lib/chef/resource/ruby_block.rb +++ b/lib/chef/resource/ruby_block.rb @@ -23,15 +23,14 @@ require 'chef/provider/ruby_block' class Chef class Resource class RubyBlock < Chef::Resource - provides :ruby_block + use_automatic_resource_name + default_action :run + allowed_actions :create, :run identity_attr :block_name def initialize(name, run_context=nil) super - @resource_name = :ruby_block - @action = "run" - @allowed_actions << :create << :run @block_name = name end diff --git a/lib/chef/resource/scm.rb b/lib/chef/resource/scm.rb index d41764d595..8379a3d8a9 100644 --- a/lib/chef/resource/scm.rb +++ b/lib/chef/resource/scm.rb @@ -22,24 +22,24 @@ require 'chef/resource' class Chef class Resource class Scm < Chef::Resource - provides :scm + use_automatic_resource_name identity_attr :destination state_attrs :revision + default_action :sync + allowed_actions :checkout, :export, :sync, :diff, :log + def initialize(name, run_context=nil) super @destination = name - @resource_name = :scm @enable_submodules = false @enable_checkout = true @revision = "HEAD" @remote = "origin" @ssh_wrapper = nil @depth = nil - @allowed_actions.push(:checkout, :export, :sync, :diff, :log) - @action = [:sync] @checkout_branch = "deploy" @environment = nil end diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb index e2fbb29d0f..f3d3ef01f4 100644 --- a/lib/chef/resource/script.rb +++ b/lib/chef/resource/script.rb @@ -23,14 +23,13 @@ require 'chef/provider/script' class Chef class Resource class Script < Chef::Resource::Execute - provides :script + use_automatic_resource_name # Chef-13: go back to using :name as the identity attr identity_attr :command def initialize(name, run_context=nil) super - @resource_name = :script # Chef-13: the command variable should be initialized to nil @command = name @code = nil diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index 47d2ab9e12..9995eccb3f 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -22,15 +22,17 @@ require 'chef/resource' class Chef class Resource class Service < Chef::Resource - provides :service + use_automatic_resource_name identity_attr :service_name state_attrs :enabled, :running + default_action :nothing + allowed_actions :enable, :disable, :start, :stop, :restart, :reload + def initialize(name, run_context=nil) super - @resource_name = :service @service_name = name @enabled = nil @running = nil @@ -44,9 +46,7 @@ class Chef @init_command = nil @priority = nil @timeout = nil - @action = "nothing" @supports = { :restart => false, :reload => false, :status => false } - @allowed_actions.push(:enable, :disable, :start, :stop, :restart, :reload) end def service_name(arg=nil) diff --git a/lib/chef/resource/smartos_package.rb b/lib/chef/resource/smartos_package.rb index 99b3b953b0..7460f0f687 100644 --- a/lib/chef/resource/smartos_package.rb +++ b/lib/chef/resource/smartos_package.rb @@ -23,15 +23,9 @@ class Chef class Resource class SmartosPackage < Chef::Resource::Package - provides :smartos_package + use_automatic_resource_name provides :package, os: "solaris2", platform_family: "smartos" - def initialize(name, run_context=nil) - super - @resource_name = :smartos_package - end - end end end - diff --git a/lib/chef/resource/solaris_package.rb b/lib/chef/resource/solaris_package.rb index 94be4693b6..545e783b75 100644 --- a/lib/chef/resource/solaris_package.rb +++ b/lib/chef/resource/solaris_package.rb @@ -24,20 +24,13 @@ class Chef class Resource class SolarisPackage < Chef::Resource::Package - provides :solaris_package + use_automatic_resource_name provides :package, os: "solaris2", platform_family: "nexentacore" provides :package, os: "solaris2", platform_family: "solaris2" do |node| # on >= Solaris 11 we default to IPS packages instead node[:platform_version].to_f <= 5.10 end - def initialize(name, run_context=nil) - super - @resource_name = :solaris_package - end - end end end - - diff --git a/lib/chef/resource/subversion.rb b/lib/chef/resource/subversion.rb index e5a2e9d1a5..e12d939ca6 100644 --- a/lib/chef/resource/subversion.rb +++ b/lib/chef/resource/subversion.rb @@ -22,14 +22,13 @@ require "chef/resource/scm" class Chef class Resource class Subversion < Chef::Resource::Scm - provides :subversion + use_automatic_resource_name + allowed_actions :force_export def initialize(name, run_context=nil) super @svn_arguments = '--no-auth-cache' @svn_info_args = '--no-auth-cache' - @resource_name = :subversion - allowed_actions << :force_export end # Override exception to strip password if any, so it won't appear in logs and different Chef notifications diff --git a/lib/chef/resource/template.rb b/lib/chef/resource/template.rb index 67a9e6a418..a8e38aa5fb 100644 --- a/lib/chef/resource/template.rb +++ b/lib/chef/resource/template.rb @@ -27,15 +27,13 @@ class Chef class Template < Chef::Resource::File include Chef::Mixin::Securable - provides :template + use_automatic_resource_name attr_reader :inline_helper_blocks attr_reader :inline_helper_modules def initialize(name, run_context=nil) super - @resource_name = :template - @action = "create" @source = "#{::File.basename(name)}.erb" @cookbook = nil @local = false diff --git a/lib/chef/resource/timestamped_deploy.rb b/lib/chef/resource/timestamped_deploy.rb index b2109db85c..15ac296f78 100644 --- a/lib/chef/resource/timestamped_deploy.rb +++ b/lib/chef/resource/timestamped_deploy.rb @@ -21,10 +21,7 @@ class Chef # Convenience class for using the deploy resource with the timestamped # deployment strategy (provider) class TimestampedDeploy < Chef::Resource::Deploy - provides :timestamped_deploy - def initialize(*args, &block) - super(*args, &block) - end + use_automatic_resource_name end end end diff --git a/lib/chef/resource/user.rb b/lib/chef/resource/user.rb index 7d2ec25596..66c183ad7e 100644 --- a/lib/chef/resource/user.rb +++ b/lib/chef/resource/user.rb @@ -26,11 +26,13 @@ class Chef state_attrs :uid, :gid, :home - provides :user + use_automatic_resource_name + + default_action :create + allowed_actions :create, :remove, :modify, :manage, :lock, :unlock def initialize(name, run_context=nil) super - @resource_name = :user @username = name @comment = nil @uid = nil @@ -42,14 +44,12 @@ class Chef @manage_home = false @force = false @non_unique = false - @action = :create @supports = { :manage_home => false, :non_unique => false } @iterations = 27855 @salt = nil - @allowed_actions.push(:create, :remove, :modify, :manage, :lock, :unlock) end def username(arg=nil) diff --git a/lib/chef/resource/whyrun_safe_ruby_block.rb b/lib/chef/resource/whyrun_safe_ruby_block.rb index f512dc67fc..0ade9c981f 100644 --- a/lib/chef/resource/whyrun_safe_ruby_block.rb +++ b/lib/chef/resource/whyrun_safe_ruby_block.rb @@ -19,12 +19,8 @@ class Chef class Resource class WhyrunSafeRubyBlock < Chef::Resource::RubyBlock - provides :whyrun_safe_ruby_block - def initialize(name, run_context=nil) - super - @resource_name = :whyrun_safe_ruby_block - end + use_automatic_resource_name end end diff --git a/lib/chef/resource/windows_package.rb b/lib/chef/resource/windows_package.rb index d4f8ae0603..bcf288e7b2 100644 --- a/lib/chef/resource/windows_package.rb +++ b/lib/chef/resource/windows_package.rb @@ -26,13 +26,14 @@ class Chef class WindowsPackage < Chef::Resource::Package include Chef::Mixin::Uris - provides :package, os: "windows" + use_automatic_resource_name provides :windows_package, os: "windows" + provides :package, os: "windows" + + allowed_actions :install, :remove def initialize(name, run_context=nil) super - @allowed_actions.push(:install, :remove) - @resource_name = :windows_package @source ||= source(@package_name) # Unique to this resource diff --git a/lib/chef/resource/windows_script.rb b/lib/chef/resource/windows_script.rb index 1af7a48fe4..48e2b535a8 100644 --- a/lib/chef/resource/windows_script.rb +++ b/lib/chef/resource/windows_script.rb @@ -31,8 +31,8 @@ class Chef def initialize(name, run_context, resource_name, interpreter_command) super(name, run_context) @interpreter = interpreter_command - @resource_name = resource_name - @default_guard_interpreter = resource_name + @resource_name = resource_name if resource_name + @default_guard_interpreter = self.resource_name end include Chef::Mixin::WindowsArchitectureHelper diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb index 8090adceb0..099042840c 100644 --- a/lib/chef/resource/windows_service.rb +++ b/lib/chef/resource/windows_service.rb @@ -25,8 +25,11 @@ class Chef # Until #1773 is resolved, you need to manually specify the windows_service resource # to use action :configure_startup and attribute startup_type - provides :service, os: "windows" + use_automatic_resource_name provides :windows_service, os: "windows" + provides :service, os: "windows" + + allowed_actions :configure_startup identity_attr :service_name @@ -34,8 +37,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :windows_service - @allowed_actions.push(:configure_startup) @startup_type = :automatic @run_as_user = "" @run_as_password = "" diff --git a/lib/chef/resource/yum_package.rb b/lib/chef/resource/yum_package.rb index d8be8c9748..ae9c840582 100644 --- a/lib/chef/resource/yum_package.rb +++ b/lib/chef/resource/yum_package.rb @@ -23,12 +23,11 @@ class Chef class Resource class YumPackage < Chef::Resource::Package - provides :yum_package + use_automatic_resource_name provides :package, os: "linux", platform_family: [ "rhel", "fedora" ] def initialize(name, run_context=nil) super - @resource_name = :yum_package @flush_cache = { :before => false, :after => false } @allow_downgrade = false end diff --git a/lib/chef/resource_builder.rb b/lib/chef/resource_builder.rb index bb0962d128..9e9f7047a4 100644 --- a/lib/chef/resource_builder.rb +++ b/lib/chef/resource_builder.rb @@ -18,6 +18,10 @@ # NOTE: this was extracted from the Recipe DSL mixin, relevant specs are in spec/unit/recipe_spec.rb +require 'chef/exceptions' +require 'chef/resource' +require 'chef/log' + class Chef class ResourceBuilder attr_reader :type @@ -46,6 +50,9 @@ class Chef raise ArgumentError, "You must supply a name when declaring a #{type} resource" if name.nil? @resource = resource_class.new(name, run_context) + if resource.resource_name.nil? + raise Chef::Exceptions::InvalidResourceSpecification, "#{resource}.resource_name is `nil`! Did you forget to put `provides :blah` or `resource_name :blah` in your resource class?" + end resource.source_line = created_at resource.declared_type = type |