diff options
author | John Keiser <john@johnkeiser.com> | 2015-06-05 23:03:51 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-30 17:08:49 -0600 |
commit | 704a5eee444153595a7db2c2f6f08210e8b5de67 (patch) | |
tree | 5fd5c8538053f8261de032e04f3da3b54ffa9c7b | |
parent | 480d18f26f17c289889ed65d3f8a50e5b336053f (diff) | |
download | chef-jk/property-all-the-things.tar.gz |
Property all the thingsjk/property-all-the-things
54 files changed, 493 insertions, 2626 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 7965068037..66f0d75bc5 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -40,6 +40,7 @@ require 'chef/mixin/deprecation' require 'chef/mixin/provides' require 'chef/mixin/shell_out' require 'chef/mixin/powershell_out' +require 'chef/resource/treat_property_defaults_as_initial_values' class Chef class Resource diff --git a/lib/chef/resource/apt_package.rb b/lib/chef/resource/apt_package.rb index ca119b50c4..bef228f149 100644 --- a/lib/chef/resource/apt_package.rb +++ b/lib/chef/resource/apt_package.rb @@ -22,22 +22,9 @@ require 'chef/provider/package/apt' class Chef class Resource class AptPackage < Chef::Resource::Package - provides :package, os: "linux", platform_family: [ "debian" ] - def initialize(name, run_context=nil) - super - @default_release = nil - end - - def default_release(arg=nil) - set_or_return( - :default_release, - arg, - :kind_of => [ String ] - ) - end - + property :default_release, String end end end diff --git a/lib/chef/resource/bash.rb b/lib/chef/resource/bash.rb index 025687e879..d94f372bad 100644 --- a/lib/chef/resource/bash.rb +++ b/lib/chef/resource/bash.rb @@ -22,12 +22,6 @@ require 'chef/provider/script' class Chef class Resource class Bash < Chef::Resource::Script - - def initialize(name, run_context=nil) - super - @interpreter = "bash" - end - end end end diff --git a/lib/chef/resource/chef_gem.rb b/lib/chef/resource/chef_gem.rb index 0c2fdfa819..cc4e645d54 100644 --- a/lib/chef/resource/chef_gem.rb +++ b/lib/chef/resource/chef_gem.rb @@ -22,28 +22,18 @@ require 'chef/resource/gem_package' class Chef class Resource class ChefGem < Chef::Resource::Package::GemPackage - - def initialize(name, run_context=nil) - super - @compile_time = Chef::Config[:chef_gem_compile_time] - @gem_binary = RbConfig::CONFIG['bindir'] + "/gem" - end + property :compile_time, [ true, false ], default: lazy { Chef::Config[:chef_gem_compile_time] } + property :gem_binary, String, default: lazy { RbConfig::CONFIG['bindir'] + "/gem" } # The chef_gem resources is for installing gems to the current gem environment only for use by Chef cookbooks. def gem_binary(arg=nil) if arg raise ArgumentError, "The chef_gem resource is restricted to the current gem environment, use gem_package to install to other environments." end - - @gem_binary + super end - - def compile_time(arg=nil) - set_or_return( - :compile_time, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) + def gem_binary=(arg) + gem_binary(arg) end def after_created diff --git a/lib/chef/resource/cookbook_file.rb b/lib/chef/resource/cookbook_file.rb index 42f16e6db6..f6d4423efe 100644 --- a/lib/chef/resource/cookbook_file.rb +++ b/lib/chef/resource/cookbook_file.rb @@ -29,21 +29,8 @@ class Chef default_action :create - def initialize(name, run_context=nil) - super - @provider = Chef::Provider::CookbookFile - @source = ::File.basename(name) - @cookbook = nil - end - - def source(source_filename=nil) - set_or_return(:source, source_filename, :kind_of => [ String, Array ]) - end - - def cookbook(cookbook_name=nil) - set_or_return(:cookbook, cookbook_name, :kind_of => String) - end - + property :source, [ String, Array ], default: lazy { ::File.basename(name) } + property :cookbook, String end end end diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb index 93cf41bc37..15919abbf6 100644 --- a/lib/chef/resource/cron.rb +++ b/lib/chef/resource/cron.rb @@ -30,97 +30,42 @@ class Chef default_action :create allowed_actions :create, :delete - def initialize(name, run_context=nil) - super - @minute = "*" - @hour = "*" - @day = "*" - @month = "*" - @weekday = "*" - @command = nil - @user = "root" - @mailto = nil - @path = nil - @shell = nil - @home = nil - @time = nil - @environment = {} - end - - def minute(arg=nil) - if arg.is_a?(Integer) - converted_arg = arg.to_s - else - converted_arg = arg - end + # TODO these can be DRY'd a *lot*. Consider the type [ 0..59, '*' ] + property :minute, String, default: '*', coerce: proc do |arg| + arg = arg.to_s if arg.is_a?(Integer) begin if integerize(arg) > 59 then raise RangeError end rescue ArgumentError end - set_or_return( - :minute, - converted_arg, - :kind_of => String - ) + arg end - - def hour(arg=nil) - if arg.is_a?(Integer) - converted_arg = arg.to_s - else - converted_arg = arg - end + property :hour, String, default: '*', coerce: proc do |arg| + arg = arg.to_s if arg.is_a?(Integer) begin if integerize(arg) > 23 then raise RangeError end rescue ArgumentError end - set_or_return( - :hour, - converted_arg, - :kind_of => String - ) + arg end - - def day(arg=nil) - if arg.is_a?(Integer) - converted_arg = arg.to_s - else - converted_arg = arg - end + property :day, String, default: '*', coerce: proc do |arg| + arg = arg.to_s if arg.is_a?(Integer) begin if integerize(arg) > 31 then raise RangeError end rescue ArgumentError end - set_or_return( - :day, - converted_arg, - :kind_of => String - ) + arg end - - def month(arg=nil) - if arg.is_a?(Integer) - converted_arg = arg.to_s - else - converted_arg = arg - end + property :month, String, default: '*', coerce: proc do |arg| + arg = arg.to_s if arg.is_a?(Integer) begin if integerize(arg) > 12 then raise RangeError end rescue ArgumentError end - set_or_return( - :month, - converted_arg, - :kind_of => String - ) + arg end - - def weekday(arg=nil) - if arg.is_a?(Integer) - converted_arg = arg.to_s - else - converted_arg = arg - end + # Consider the type [ 0..7, Provider::Cron::WEEKDAY_SYMBOLS, '*' ] + property :weekday, String, default: '*', coerce: proc do |arg| + arg = arg.to_s if arg.is_a?(Integer) begin error_message = "You provided '#{arg}' as a weekday, acceptable values are " error_message << Provider::Cron::WEEKDAY_SYMBOLS.map {|sym| ":#{sym.to_s}"}.join(', ') @@ -132,76 +77,16 @@ class Chef end rescue ArgumentError end - set_or_return( - :weekday, - converted_arg, - :kind_of => [String, Symbol] - ) - end - - def time(arg=nil) - set_or_return( - :time, - arg, - :equal_to => Chef::Provider::Cron::SPECIAL_TIME_VALUES - ) - end - - def mailto(arg=nil) - set_or_return( - :mailto, - arg, - :kind_of => String - ) - end - - def path(arg=nil) - set_or_return( - :path, - arg, - :kind_of => String - ) - end - - def home(arg=nil) - set_or_return( - :home, - arg, - :kind_of => String - ) - end - - def shell(arg=nil) - set_or_return( - :shell, - arg, - :kind_of => String - ) - end - - def command(arg=nil) - set_or_return( - :command, - arg, - :kind_of => String - ) - end - - def user(arg=nil) - set_or_return( - :user, - arg, - :kind_of => String - ) - end - - def environment(arg=nil) - set_or_return( - :environment, - arg, - :kind_of => Hash - ) + arg end + property :command, String + property :user, String, default: 'root' + property :mailto, String + property :path, String + property :shell, String + property :home, String + property :time, Chef::Provider::Cron::SPECIAL_TIME_VALUES + property :environment, Hash, default: lazy { {} } private diff --git a/lib/chef/resource/csh.rb b/lib/chef/resource/csh.rb index d5e9c910b1..747506a60e 100644 --- a/lib/chef/resource/csh.rb +++ b/lib/chef/resource/csh.rb @@ -22,12 +22,6 @@ require 'chef/provider/script' class Chef class Resource class Csh < Chef::Resource::Script - - def initialize(name, run_context=nil) - super - @interpreter = "csh" - end - end end end diff --git a/lib/chef/resource/deploy.rb b/lib/chef/resource/deploy.rb index 3e5255bced..b90656b632 100644 --- a/lib/chef/resource/deploy.rb +++ b/lib/chef/resource/deploy.rb @@ -36,6 +36,7 @@ # end require "chef/resource/scm" +require "chef/provider/git" class Chef class Resource @@ -58,273 +59,58 @@ class Chef default_action :deploy allowed_actions :force_deploy, :deploy, :rollback - def initialize(name, run_context=nil) - super - @deploy_to = name - @environment = nil - @repository_cache = 'cached-copy' - @copy_exclude = [] - @purge_before_symlink = %w{log tmp/pids public/system} - @create_dirs_before_symlink = %w{tmp public config} - @symlink_before_migrate = {"config/database.yml" => "config/database.yml"} - @symlinks = {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"} - @revision = 'HEAD' - @migrate = false - @rollback_on_error = false - @remote = "origin" - @enable_submodules = false - @shallow_clone = false - @scm_provider = Chef::Provider::Git - @svn_force_export = false - @additional_remotes = Hash[] - @keep_releases = 5 - @enable_checkout = true - @checkout_branch = "deploy" - end - - # where the checked out/cloned code goes - def destination - @destination ||= shared_path + "/#{@repository_cache}" - end - - # where shared stuff goes, i.e., logs, tmp, etc. goes here - def shared_path - @shared_path ||= @deploy_to + "/shared" - end - - # where the deployed version of your code goes - def current_path - @current_path ||= @deploy_to + "/current" - end - - def depth - @shallow_clone ? "5" : nil - end # note: deploy_to is your application "meta-root." - def deploy_to(arg=nil) - set_or_return( - :deploy_to, - arg, - :kind_of => [ String ] - ) - end - - def repo(arg=nil) - set_or_return( - :repo, - arg, - :kind_of => [ String ] - ) - end + property :deploy_to, String, name_property: true + property :repo, String alias :repository :repo - - def remote(arg=nil) - set_or_return( - :remote, - arg, - :kind_of => [ String ] - ) - end - - def role(arg=nil) - set_or_return( - :role, - arg, - :kind_of => [ String ] - ) - end - - def restart_command(arg=nil, &block) - arg ||= block - set_or_return( - :restart_command, - arg, - :kind_of => [ String, Proc ] - ) - end + property :remote, String, default: "origin" + property :role, String + property :restart_command, [ String, Proc ], coerce: proc { |arg=nil, &block| arg || &block} alias :restart :restart_command - def migrate(arg=nil) - set_or_return( - :migrate, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def migration_command(arg=nil) - set_or_return( - :migration_command, - arg, - :kind_of => [ String ] - ) - end + property :migrate, [ true, false ], default: false + property :migration_command, String - def rollback_on_error(arg=nil) - set_or_return( - :rollback_on_error, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end + property :rollback_on_error, [ true, false ], default: false - def user(arg=nil) - set_or_return( - :user, - arg, - :kind_of => [ String ] - ) - end + property :user, String + property :group, String - def group(arg=nil) - set_or_return( - :group, - arg, - :kind_of => [ String ] - ) - end - - def enable_submodules(arg=nil) - set_or_return( - :enable_submodules, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def shallow_clone(arg=nil) - set_or_return( - :shallow_clone, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def repository_cache(arg=nil) - set_or_return( - :repository_cache, - arg, - :kind_of => [ String ] - ) - end - - def copy_exclude(arg=nil) - set_or_return( - :copy_exclude, - arg, - :kind_of => [ String ] - ) - end + property :enable_submodules, [ true, false ], default: false + property :shallow_clone, [ true, false ], default: false + property :repository_cache, String, default: 'cached-copy' + property :copy_exclude, String, default: lazy { [] } + property :revision, String, default: 'HEAD' - def revision(arg=nil) - set_or_return( - :revision, - arg, - :kind_of => [ String ] - ) - end - alias :branch :revision - - def git_ssh_wrapper(arg=nil) - set_or_return( - :git_ssh_wrapper, - arg, - :kind_of => [ String ] - ) + property :scm_provider, Class, default: Chef::Provider::Git, coerce: proc do |arg| + arg = lookup_provider_constant(arg) if arg.kind_of?(String) || arg.kind_of?(Symbol) + arg end + property :git_ssh_wrapper, String alias :ssh_wrapper :git_ssh_wrapper + property :svn_username, String + property :svn_password, String + property :svn_arguments, String + property :svn_info_args, String + property :svn_force_export, [ true, false ], default: false - def svn_username(arg=nil) - set_or_return( - :svn_username, - arg, - :kind_of => [ String ] - ) - end - - def svn_password(arg=nil) - set_or_return( - :svn_password, - arg, - :kind_of => [ String ] - ) - end - - def svn_arguments(arg=nil) - set_or_return( - :svn_arguments, - arg, - :kind_of => [ String ] - ) - end - - def svn_info_args(arg=nil) - set_or_return( - :svn_arguments, - arg, - :kind_of => [ String ]) - end - - def scm_provider(arg=nil) - klass = if arg.kind_of?(String) || arg.kind_of?(Symbol) - lookup_provider_constant(arg) - else - arg - end - set_or_return( - :scm_provider, - klass, - :kind_of => [ Class ] - ) - 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, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def environment(arg=nil) + property :environment, Hash, coerce: proc do |arg| if arg.is_a?(String) Chef::Log.debug "Setting RAILS_ENV, RACK_ENV, and MERB_ENV to `#{arg}'" Chef::Log.warn "[DEPRECATED] please modify your deploy recipe or attributes to set the environment using a hash" arg = {"RAILS_ENV"=>arg,"MERB_ENV"=>arg,"RACK_ENV"=>arg} end - set_or_return( - :environment, - arg, - :kind_of => [ Hash ] - ) + arg end - # The number of old release directories to keep around after cleanup - def keep_releases(arg=nil) - [set_or_return( - :keep_releases, - arg, - :kind_of => [ Integer ]), 1].max - end + property :keep_releases, Integer, default: 5, coerce: { |v| v.is_a?(Integer) ? [ v, 1 ].max } # An array of paths, relative to your app's root, to be purged from a # SCM clone/checkout before symlinking. Use this to get rid of files and # directories you want to be shared between releases. # Default: ["log", "tmp/pids", "public/system"] - def purge_before_symlink(arg=nil) - set_or_return( - :purge_before_symlink, - arg, - :kind_of => Array - ) - end + property :purge_before_symlink, Array, default: lazy { %w{log tmp/pids public/system} } # An array of paths, relative to your app's root, where you expect dirs to # exist before symlinking. This runs after #purge_before_symlink, so you @@ -334,27 +120,7 @@ class Chef # then specify tmp here so that the tmp directory will exist when you # symlink the pids directory in to the current release. # Default: ["tmp", "public", "config"] - def create_dirs_before_symlink(arg=nil) - set_or_return( - :create_dirs_before_symlink, - arg, - :kind_of => Array - ) - end - - # A Hash of shared/dir/path => release/dir/path. This attribute determines - # which files and dirs in the shared directory get symlinked to the current - # release directory, and where they go. If you have a directory - # $shared/pids that you would like to symlink as $current_release/tmp/pids - # you specify it as "pids" => "tmp/pids" - # Default {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"} - def symlinks(arg=nil) - set_or_return( - :symlinks, - arg, - :kind_of => Hash - ) - end + property :create_dirs_before_symlink, Array default: %w{tmp public config} # A Hash of shared/dir/path => release/dir/path. This attribute determines # which files in the shared directory get symlinked to the current release @@ -363,73 +129,59 @@ class Chef # For a rails/merb app, this is used to link in a known good database.yml # (with the production db password) before running migrate. # Default {"config/database.yml" => "config/database.yml"} - def symlink_before_migrate(arg=nil) - set_or_return( - :symlink_before_migrate, - arg, - :kind_of => Hash - ) - end + property :symlink_before_migrate, Hash, default: {"config/database.yml" => "config/database.yml"} - # Callback fires before migration is run. - def before_migrate(arg=nil, &block) - arg ||= block - set_or_return(:before_migrate, arg, :kind_of => [Proc, String]) - end + # A Hash of shared/dir/path => release/dir/path. This attribute determines + # which files and dirs in the shared directory get symlinked to the current + # release directory, and where they go. If you have a directory + # $shared/pids that you would like to symlink as $current_release/tmp/pids + # you specify it as "pids" => "tmp/pids" + # Default {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"} + property :symlinks, Hash, default: {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"} - # Callback fires before symlinking - def before_symlink(arg=nil, &block) - arg ||= block - set_or_return(:before_symlink, arg, :kind_of => [Proc, String]) - end + property :before_migrate, [ Proc String ], coerce: proc { |arg=nil, &block| arg || block } + property :before_symlink, [ Proc String ], coerce: proc { |arg=nil, &block| arg || block } + property :before_restart, [ Proc String ], coerce: proc { |arg=nil, &block| arg || block } + property :after_restart, [ Proc String ], coerce: proc { |arg=nil, &block| arg || block } - # Callback fires before restart - def before_restart(arg=nil, &block) - arg ||= block - set_or_return(:before_restart, arg, :kind_of => [Proc, String]) - end + property :shallow_clone, default: false + + property :additional_remotes, kind_of: Hash, default: lazy { Hash[] } - # Callback fires after restart - def after_restart(arg=nil, &block) - arg ||= block - set_or_return(:after_restart, arg, :kind_of => [Proc, String]) + property :enable_checkout, [true, false], default: true + property :checkout_branch, String, default: "deploy" + + # FIXME The Deploy resource may be passed to an SCM provider as its + # resource. The SCM provider knows that SCM resources can specify a + # timeout for SCM operations. The deploy resource must therefore support + # a timeout method, but the timeout it describes is for SCM operations, + # not the overall deployment. This is potentially confusing. + property :timeout, Integer + + # where the checked out/cloned code goes + def destination + @destination ||= "#{shared_path}/#{repository_cache}" end - def additional_remotes(arg=nil) - set_or_return( - :additional_remotes, - arg, - :kind_of => Hash - ) + # where shared stuff goes, i.e., logs, tmp, etc. goes here + def shared_path + @shared_path ||= "#{deploy_to}/shared" end - def enable_checkout(arg=nil) - set_or_return( - :enable_checkout, - arg, - :kind_of => [TrueClass, FalseClass] - ) + # where the deployed version of your code goes + def current_path + @current_path ||= "#{deploy_to}/current" end - def checkout_branch(arg=nil) - set_or_return( - :checkout_branch, - arg, - :kind_of => String - ) + def depth + shallow_clone ? "5" : nil end - # FIXME The Deploy resource may be passed to an SCM provider as its - # resource. The SCM provider knows that SCM resources can specify a - # timeout for SCM operations. The deploy resource must therefore support - # a timeout method, but the timeout it describes is for SCM operations, - # not the overall deployment. This is potentially confusing. - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => Integer - ) + + # This is to support "provider :revision" without deprecation warnings. + # Do NOT copy this. + def self.provider_base + Chef::Provider::Deploy end end diff --git a/lib/chef/resource/directory.rb b/lib/chef/resource/directory.rb index 9cac2ce243..7779132bf4 100644 --- a/lib/chef/resource/directory.rb +++ b/lib/chef/resource/directory.rb @@ -35,27 +35,8 @@ class Chef default_action :create allowed_actions :create, :delete - def initialize(name, run_context=nil) - super - @path = name - @recursive = false - end - - def recursive(arg=nil) - set_or_return( - :recursive, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def path(arg=nil) - set_or_return( - :path, - arg, - :kind_of => String - ) - end + property :path, String, name_property: true + property :recursive, [ true, false ], default: false end end diff --git a/lib/chef/resource/dsc_resource.rb b/lib/chef/resource/dsc_resource.rb index 5db00f49ca..041f199181 100644 --- a/lib/chef/resource/dsc_resource.rb +++ b/lib/chef/resource/dsc_resource.rb @@ -27,27 +27,9 @@ class Chef default_action :run
- def initialize(name, run_context)
- super
- @properties = {}
- @resource = nil
- end
-
- def resource(value=nil)
- if value
- @resource = value
- else
- @resource
- end
- end
-
- def module_name(value=nil)
- if value
- @module_name = value
- else
- @module_name
- end
- end
+ property :resource
+ property :module_name
+ property :properties, default: lazy { {} }
def property(property_name, value=nil)
if not property_name.is_a?(Symbol)
@@ -55,13 +37,14 @@ class Chef end
if value.nil?
- value_of(@properties[property_name])
+ value_of(properties[property_name])
else
- @properties[property_name] = value
+ properties[property_name] = value
end
end
def properties
+ @properties ||= {}
@properties.reduce({}) do |memo, (k, v)|
memo[k] = value_of(v)
memo
diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb index 2877f61eb4..3390ae2c02 100644 --- a/lib/chef/resource/dsc_script.rb +++ b/lib/chef/resource/dsc_script.rb @@ -26,113 +26,40 @@ class Chef default_action :run - def initialize(name, run_context=nil) - super - @imports = {} - end - - def code(arg=nil) - if arg && command - raise ArgumentError, "Only one of 'code' and 'command' attributes may be specified" - end - if arg && configuration_name - raise ArgumentError, "The 'code' and 'command' attributes may not be used together" - end - set_or_return( - :code, - arg, - :kind_of => [ String ] - ) - end - - def configuration_name(arg=nil) - if arg && code - raise ArgumentError, "Attribute `configuration_name` may not be set if `code` is set" - end - set_or_return( - :configuration_name, - arg, - :kind_of => [ String ] - ) - end - - def command(arg=nil) - if arg && code - raise ArgumentError, "The 'code' and 'command' attributes may not be used together" - end - set_or_return( - :command, - arg, - :kind_of => [ String ] - ) - end - - def configuration_data(arg=nil) - if arg && configuration_data_script - raise ArgumentError, "The 'configuration_data' and 'configuration_data_script' attributes may not be used together" - end - set_or_return( - :configuration_data, - arg, - :kind_of => [ String ] - ) - end + property :code, String, callbacks: { + "Only one of 'code' and 'command' properties may be specified" => proc { |arg| !command }, + "Only one of 'code' and 'configuration_name' properties may be specified" => proc { |arg| !configuration_name }, + } + property :configuration_name, String, callbacks: { + "Only one of 'code' and 'configuration_name' properties may be specified" => proc { |arg| !code }, + } + property :command, String, callbacks: { + "Only one of 'code' and 'command' properties may be specified" => proc { |arg| !code }, + } + property :configuration_data, String, callbacks: { + "Only one of 'configuration_data' and 'configuration_data_script' properties may be specified" => proc { |arg| !configuration_data_script }, + } + property :configuration_data_script, String, callbacks: { + "Only one of 'configuration_data' and 'configuration_data_script' properties may be specified" => proc { |arg| !configuration_data }, + } - def configuration_data_script(arg=nil) - if arg && configuration_data - raise ArgumentError, "The 'configuration_data' and 'configuration_data_script' attributes may not be used together" - end - set_or_return( - :configuration_data_script, - arg, - :kind_of => [ String ] - ) - end + property :flags, Hash + property :cwd, String + property :environment, Hash + property :timeout, Integer + property :imports, Hash, lazy { {} } def imports(module_name=nil, *args) if module_name - @imports[module_name] ||= [] if args.length == 0 - @imports[module_name] << '*' + imports[module_name] << '*' else - @imports[module_name].push(*args) + imports[module_name].push(*args) end else - @imports + super end end - - def flags(arg=nil) - set_or_return( - :flags, - arg, - :kind_of => [ Hash ] - ) - end - - def cwd(arg=nil) - set_or_return( - :cwd, - arg, - :kind_of => [ String ] - ) - end - - def environment(arg=nil) - set_or_return( - :environment, - arg, - :kind_of => [ Hash ] - ) - end - - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => [ Integer ] - ) - end end end end diff --git a/lib/chef/resource/easy_install_package.rb b/lib/chef/resource/easy_install_package.rb index df4cee1ab3..aed96b926b 100644 --- a/lib/chef/resource/easy_install_package.rb +++ b/lib/chef/resource/easy_install_package.rb @@ -21,31 +21,9 @@ require 'chef/resource/package' class Chef class Resource class EasyInstallPackage < Chef::Resource::Package - - def easy_install_binary(arg=nil) - set_or_return( - :easy_install_binary, - arg, - :kind_of => [ String ] - ) - end - - def python_binary(arg=nil) - set_or_return( - :python_install_binary, - arg, - :kind_of => [ String ] - ) - end - - def module_name(arg=nil) - set_or_return( - :module_name, - arg, - :kind_of => [ String ] - ) - end - + property :easy_install_binary, String + property :python_binary, String + property :module_name, String end end end diff --git a/lib/chef/resource/env.rb b/lib/chef/resource/env.rb index 025bfc72b7..5fff6e01c1 100644 --- a/lib/chef/resource/env.rb +++ b/lib/chef/resource/env.rb @@ -20,46 +20,17 @@ class Chef class Resource class Env < Chef::Resource - - identity_attr :key_name - - state_attrs :value - provides :env, os: "windows" default_action :create allowed_actions :create, :delete, :modify - def initialize(name, run_context=nil) - super - @key_name = name - @value = nil - @delim = nil - end - - def key_name(arg=nil) - set_or_return( - :key_name, - arg, - :kind_of => [ String ] - ) - end - - def value(arg=nil) - set_or_return( - :value, - arg, - :kind_of => [ String ] - ) - end + identity_attr :key_name + state_attrs :value - def delim(arg=nil) - set_or_return( - :delim, - arg, - :kind_of => [ String ] - ) - end + property :key_name, String, name_property: true + property :value, String + property :delim, String end end end diff --git a/lib/chef/resource/erl_call.rb b/lib/chef/resource/erl_call.rb index 1976c54c45..d4bce2385e 100644 --- a/lib/chef/resource/erl_call.rb +++ b/lib/chef/resource/erl_call.rb @@ -26,59 +26,19 @@ class Chef # erl_call : http://erlang.org/doc/man/erl_call.html - identity_attr :code - default_action :run + identity_attr :code - def initialize(name, run_context=nil) - super - - @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 - end - - def code(arg=nil) - set_or_return( - :code, - arg, - :kind_of => [ String ] - ) - end - - def cookie(arg=nil) - set_or_return( - :cookie, - arg, - :kind_of => [ String ] - ) - end - - def distributed(arg=nil) - set_or_return( - :distributed, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def name_type(arg=nil) - set_or_return( - :name_type, - arg, - :kind_of => [ String ] - ) - end - - def node_name(arg=nil) - set_or_return( - :node_name, - arg, - :kind_of => [ String ] - ) - end + # your erlang code goes here + property :code, String, default: "q()." + # cookie of the erlang node + property :cookie, String + # if you want to have a distributed erlang node + property :distributed, [ true, false ], default: false + # type of erlang hostname name or sname + property :name_type, String, "sname" + # the erlang node hostname + property :node_name, String, "chef@localhost" end end diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb index ec669a75d3..d201a00f8d 100644 --- a/lib/chef/resource/execute.rb +++ b/lib/chef/resource/execute.rb @@ -24,8 +24,6 @@ class Chef class Resource class Execute < Chef::Resource - identity_attr :command - # The ResourceGuardInterpreter wraps a resource's guards in another resource. That inner resource # needs to behave differently during (for example) why_run mode, so we flag it here. For why_run mode # we still want to execute the guard resource even if we are not executing the wrapping resource. @@ -36,129 +34,39 @@ class Chef def initialize(name, run_context=nil) super - @command = name @backup = 5 - @creates = nil - @cwd = nil - @environment = nil - @group = nil - @path = nil - @returns = 0 - @timeout = nil @user = nil - @umask = nil @default_guard_interpreter = :execute @is_guard_interpreter = false end - def umask(arg=nil) - set_or_return( - :umask, - arg, - :kind_of => [ String, Integer ] - ) - end - - def command(arg=nil) - set_or_return( - :command, - arg, - :kind_of => [ String, Array ] - ) - end - - def creates(arg=nil) - set_or_return( - :creates, - arg, - :kind_of => [ String ] - ) - end - - def cwd(arg=nil) - set_or_return( - :cwd, - arg, - :kind_of => [ String ] - ) - end - - def environment(arg=nil) - set_or_return( - :environment, - arg, - :kind_of => [ Hash ] - ) - end + identity_attr :command + property :command, [ String, Array ], name_property: true + property :creates, String + property :cwd, String + property :environment, Hash + property :group, [ String, Integer ] + property :path, Array + property :returns, [ Integer, Array ], default: 0 + property :timeout, [ Integer, Float ] + property :umask, [ String, Integer ] + property :user, [ String, Integer ] alias :env :environment - def group(arg=nil) - set_or_return( - :group, - arg, - :kind_of => [ String, Integer ] - ) - end - - def path(arg=nil) + def path(*args, &block) Chef::Log.warn "'path' attribute of 'execute' is not used by any provider in Chef 11 and Chef 12. Use 'environment' attribute to configure 'PATH'. This attribute will be removed in Chef 13." - - set_or_return( - :path, - arg, - :kind_of => [ Array ] - ) - end - - def returns(arg=nil) - set_or_return( - :returns, - arg, - :kind_of => [ Integer, Array ] - ) - end - - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => [ Integer, Float ] - ) - end - - def user(arg=nil) - set_or_return( - :user, - arg, - :kind_of => [ String, Integer ] - ) + super end - def self.set_guard_inherited_attributes(*inherited_attributes) - @class_inherited_attributes = inherited_attributes + define_class_properties do + property :guard_inherited_attributes, Array, default: [ :cwd, :environment, :group, :user, :umask ] end - def self.guard_inherited_attributes(*inherited_attributes) - # Similar to patterns elsewhere, return attributes from this - # class and superclasses as a form of inheritance - ancestor_attributes = [] - - if superclass.respond_to?(:guard_inherited_attributes) - ancestor_attributes = superclass.guard_inherited_attributes - end - - ancestor_attributes.concat(@class_inherited_attributes ? @class_inherited_attributes : []).uniq + def self.set_guard_inherited_attributes(*attributes) + guard_inherited_attributes attributes end - set_guard_inherited_attributes( - :cwd, - :environment, - :group, - :user, - :umask - ) - end end end diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb index d278652cc3..3501230ad7 100644 --- a/lib/chef/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -50,80 +50,15 @@ class Chef default_action :create allowed_actions :create, :delete, :touch, :create_if_missing - def initialize(name, run_context=nil) - super - @path = name - @backup = 5 - @atomic_update = Chef::Config[:file_atomic_update] - @force_unlink = false - @manage_symlink_source = nil - @diff = nil - @verifications = [] - end - - def content(arg=nil) - set_or_return( - :content, - arg, - :kind_of => String - ) - end - - def backup(arg=nil) - set_or_return( - :backup, - arg, - :kind_of => [ Integer, FalseClass ] - ) - end + property :path, String, name_property: true - def checksum(arg=nil) - set_or_return( - :checksum, - arg, - :regex => /^[a-zA-Z0-9]{64}$/ - ) - end - - def path(arg=nil) - set_or_return( - :path, - arg, - :kind_of => String - ) - end - - def diff(arg=nil) - set_or_return( - :diff, - arg, - :kind_of => String - ) - end - - def atomic_update(arg=nil) - set_or_return( - :atomic_update, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def force_unlink(arg=nil) - set_or_return( - :force_unlink, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def manage_symlink_source(arg=nil) - set_or_return( - :manage_symlink_source, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end + property :backup, [ Integer, false ], default: 5 + property :checksum, /^[a-zA-Z0-9]{64}$/ + property :content, String + property :diff, String + property :atomic_update, [ true, false ], default: lazy { Chef::Config[:file_atomic_update] } + property :force_unlink, [ true, false ], default: false + property :manage_symlink_source, [ true, false ] def verify(command=nil, opts={}, &block) if ! (command.nil? || [String, Symbol].include?(command.class)) @@ -131,9 +66,9 @@ class Chef end if command || block_given? - @verifications << Verification.new(self, command, opts, &block) + verifications << Verification.new(self, command, opts, &block) else - @verifications + verifications end end @@ -145,6 +80,12 @@ class Chef end state_attrs end + + private + + def verifications + @verifications ||= [] + end end end end diff --git a/lib/chef/resource/gem_package.rb b/lib/chef/resource/gem_package.rb index b981797876..77962a3c3b 100644 --- a/lib/chef/resource/gem_package.rb +++ b/lib/chef/resource/gem_package.rb @@ -21,36 +21,16 @@ require 'chef/resource/package' class Chef class Resource class GemPackage < Chef::Resource::Package - - def initialize(name, run_context=nil) - super - @clear_sources = false - end - - def source(arg=nil) - set_or_return(:source, arg, :kind_of => [ String, Array ]) - end - - def clear_sources(arg=nil) - set_or_return(:clear_sources, arg, :kind_of => [ TrueClass, FalseClass ]) - end - - # Sets a custom gem_binary to run for gem commands. - def gem_binary(gem_cmd=nil) - set_or_return(:gem_binary,gem_cmd,:kind_of => [ String ]) - end - + property :source, [ String, Array ] + property :clear_sources, [ true, false ], default: false + property :gem_binary, String ## # Options for the gem install, either a Hash or a String. When a hash is # given, the options are passed to Gem::DependencyInstaller.new, and the # gem will be installed via the gems API. When a String is given, the gem # will be installed by shelling out to the gem command. Using a Hash of # options with an explicit gem_binary will result in undefined behavior. - def options(opts=nil) - set_or_return(:options,opts,:kind_of => [String,Hash]) - end - - + property :options, [ String, Hash ] end end end diff --git a/lib/chef/resource/git.rb b/lib/chef/resource/git.rb index 393a0689fe..c4e7cb2b2c 100644 --- a/lib/chef/resource/git.rb +++ b/lib/chef/resource/git.rb @@ -21,19 +21,7 @@ require "chef/resource/scm" class Chef class Resource class Git < Chef::Resource::Scm - - def initialize(name, run_context=nil) - super - @additional_remotes = Hash[] - end - - def additional_remotes(arg=nil) - set_or_return( - :additional_remotes, - arg, - :kind_of => Hash - ) - end + property :additional_remotes, Hash, default: lazy { Hash[] } alias :branch :revision alias :reference :revision diff --git a/lib/chef/resource/group.rb b/lib/chef/resource/group.rb index 2e80f32fea..3f5f7c56af 100644 --- a/lib/chef/resource/group.rb +++ b/lib/chef/resource/group.rb @@ -20,84 +20,22 @@ class Chef class Resource class Group < Chef::Resource - identity_attr :group_name - state_attrs :members allowed_actions :create, :remove, :modify, :manage default_action :create - def initialize(name, run_context=nil) - super - @group_name = name - @gid = nil - @members = [] - @excluded_members = [] - @append = false - @non_unique = false - end - - def group_name(arg=nil) - set_or_return( - :group_name, - arg, - :kind_of => [ String ] - ) - end - - def gid(arg=nil) - set_or_return( - :gid, - arg, - :kind_of => [ String, Integer ] - ) - end - - def members(arg=nil) - converted_members = arg.is_a?(String) ? [].push(arg) : arg - set_or_return( - :members, - converted_members, - :kind_of => [ Array ] - ) - end - + identity_attr :group_name + state_attrs :members + property :group_name, String, name_property: true + property :gid, [ String, Integer ] + property :members, Array, default: lazy { [] }, coerce: { |v| Array(v) } alias_method :users, :members - - def excluded_members(arg=nil) - converted_members = arg.is_a?(String) ? [].push(arg) : arg - set_or_return( - :excluded_members, - converted_members, - :kind_of => [ Array ] - ) - end - - - def append(arg=nil) - set_or_return( - :append, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def system(arg=nil) - set_or_return( - :system, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def non_unique(arg=nil) - set_or_return( - :non_unique, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end + property :excluded_members, Array, default: lazy { [] }, coerce: { |v| Array(v) } + property :append, [ true, false ], default: false + property :system, [ true, false ] + property :non_unique, [ true, false ], default: false end end end diff --git a/lib/chef/resource/homebrew_package.rb b/lib/chef/resource/homebrew_package.rb index 048ba6b3aa..244a86ed1f 100644 --- a/lib/chef/resource/homebrew_package.rb +++ b/lib/chef/resource/homebrew_package.rb @@ -27,19 +27,7 @@ class Chef provides :package, os: "darwin" - def initialize(name, run_context=nil) - super - @homebrew_user = nil - end - - def homebrew_user(arg=nil) - set_or_return( - :homebrew_user, - arg, - :kind_of => [ String, Integer ] - ) - end - + property :homebrew_user, [ String, Integer ] end end end diff --git a/lib/chef/resource/http_request.rb b/lib/chef/resource/http_request.rb index f9f056325a..e87cb8f29e 100644 --- a/lib/chef/resource/http_request.rb +++ b/lib/chef/resource/http_request.rb @@ -24,43 +24,13 @@ class Chef class Resource class HttpRequest < Chef::Resource - identity_attr :url - default_action :get allowed_actions :get, :put, :post, :delete, :head, :options - def initialize(name, run_context=nil) - super - @message = name - @url = nil - @headers = {} - end - - def url(args=nil) - set_or_return( - :url, - args, - :kind_of => String - ) - end - - def message(args=nil, &block) - args = block if block_given? - set_or_return( - :message, - args, - :kind_of => Object - ) - end - - def headers(args=nil) - set_or_return( - :headers, - args, - :kind_of => Hash - ) - end - + identity_attr :url + property :message, name_property: true, coerce: proc { |v=nil, &block| v || block } + property :url, String + property :headers, Hash, default: lazy { {} } end end end diff --git a/lib/chef/resource/ifconfig.rb b/lib/chef/resource/ifconfig.rb index 527eb0e515..6e52daeb6c 100644 --- a/lib/chef/resource/ifconfig.rb +++ b/lib/chef/resource/ifconfig.rb @@ -23,125 +23,23 @@ class Chef class Resource class Ifconfig < Chef::Resource - identity_attr :device - - state_attrs :inet_addr, :mask - default_action :add allowed_actions :add, :delete, :enable, :disable - def initialize(name, run_context=nil) - super - @target = name - @hwaddr = nil - @mask = nil - @inet_addr = nil - @bcast = nil - @mtu = nil - @metric = nil - @device = nil - @onboot = nil - @network = nil - @bootproto = nil - @onparent = nil - end - - def target(arg=nil) - set_or_return( - :target, - arg, - :kind_of => String - ) - end - - def device(arg=nil) - set_or_return( - :device, - arg, - :kind_of => String - ) - end - - def hwaddr(arg=nil) - set_or_return( - :hwaddr, - arg, - :kind_of => String - ) - end - - def inet_addr(arg=nil) - set_or_return( - :inet_addr, - arg, - :kind_of => String - ) - end - - def bcast(arg=nil) - set_or_return( - :bcast, - arg, - :kind_of => String - ) - end - - def mask(arg=nil) - set_or_return( - :mask, - arg, - :kind_of => String - ) - end - - def mtu(arg=nil) - set_or_return( - :mtu, - arg, - :kind_of => String - ) - end - - def metric(arg=nil) - set_or_return( - :metric, - arg, - :kind_of => String - ) - end - - def onboot(arg=nil) - set_or_return( - :onboot, - arg, - :kind_of => String - ) - end - - def network(arg=nil) - set_or_return( - :network, - arg, - :kind_of => String - ) - end - - def bootproto(arg=nil) - set_or_return( - :bootproto, - arg, - :kind_of => String - ) - end - - def onparent(arg=nil) - set_or_return( - :onparent, - arg, - :kind_of => String - ) - end + identity_attr :device + state_attrs :inet_addr, :mask + property :target, String, name_property: true + property :device, String + property :hwaddr, String + property :inet_addr, String + property :bcast, String + property :mask, String + property :mtu, String + property :metric, String + property :onboot, String + property :network, String + property :bootproto, String + property :onparent, String end - end end diff --git a/lib/chef/resource/ips_package.rb b/lib/chef/resource/ips_package.rb index 2bf8e1dba8..4d94749289 100644 --- a/lib/chef/resource/ips_package.rb +++ b/lib/chef/resource/ips_package.rb @@ -28,18 +28,7 @@ class Chef allowed_actions :install, :remove, :upgrade - def initialize(name, run_context = nil) - super(name, run_context) - @accept_license = false - end - - def accept_license(arg=nil) - set_or_return( - :purge, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end + property :accept_license, [ true, false ], default: false end end end diff --git a/lib/chef/resource/link.rb b/lib/chef/resource/link.rb index f932383cc1..d46bed4ab1 100644 --- a/lib/chef/resource/link.rb +++ b/lib/chef/resource/link.rb @@ -26,7 +26,6 @@ class Chef include Chef::Mixin::Securable identity_attr :target_file - state_attrs :to, :owner, :group default_action :create @@ -35,51 +34,15 @@ class Chef def initialize(name, run_context=nil) verify_links_supported! super - @to = nil - @link_type = :symbolic - @target_file = name - end - - def to(arg=nil) - set_or_return( - :to, - arg, - :kind_of => String - ) - end - - def target_file(arg=nil) - set_or_return( - :target_file, - arg, - :kind_of => String - ) - end - - def link_type(arg=nil) - real_arg = arg.kind_of?(String) ? arg.to_sym : arg - set_or_return( - :link_type, - real_arg, - :equal_to => [ :symbolic, :hard ] - ) - end - - def group(arg=nil) - set_or_return( - :group, - arg, - :regex => Chef::Config[:group_valid_regex] - ) end - def owner(arg=nil) - set_or_return( - :owner, - arg, - :regex => Chef::Config[:user_valid_regex] - ) - end + identity_attr :target_file + state_attrs :to, :owner, :group + property :target_file, String, name_property: true + property :to, String + property :link_type, [ :symbolic, :hard ], default: :symbolic + property :group, Chef::Config[:group_valid_regex] + property :owner, Chef::Config[:user_valid_regex] # make link quack like a file (XXX: not for public consumption) def path diff --git a/lib/chef/resource/log.rb b/lib/chef/resource/log.rb index 9adffb26bb..bac43ce787 100644 --- a/lib/chef/resource/log.rb +++ b/lib/chef/resource/log.rb @@ -24,8 +24,6 @@ class Chef class Resource class Log < Chef::Resource - identity_attr :message - default_action :write # Sends a string from a recipe to a log provider @@ -42,35 +40,10 @@ class Chef # log "a debug string" { level :debug } # - # Initialize log resource with a name as the string to log - # - # === Parameters - # name<String>:: Message to log - # collection<Array>:: Collection of included recipes - # node<Chef::Node>:: Node where resource will be used - def initialize(name, run_context=nil) - super - @level = :info - @message = name - end - - def message(arg=nil) - set_or_return( - :message, - arg, - :kind_of => String - ) - end - + identity_attr :message + property :message, name_property: true # <Symbol> Log level, one of :debug, :info, :warn, :error or :fatal - def level(arg=nil) - set_or_return( - :level, - arg, - :equal_to => [ :debug, :info, :warn, :error, :fatal ] - ) - end - + property :level, [ :debug, :info, :warn, :error, :fatal ], default: :info end end end diff --git a/lib/chef/resource/macosx_service.rb b/lib/chef/resource/macosx_service.rb index f1ed4051cb..afb4461f1b 100644 --- a/lib/chef/resource/macosx_service.rb +++ b/lib/chef/resource/macosx_service.rb @@ -26,33 +26,11 @@ class Chef provides :service, os: "darwin" identity_attr :service_name - state_attrs :enabled, :running - - def initialize(name, run_context=nil) - super - @plist = nil - @session_type = nil - end - # This will enable user to pass a plist in the case # that the filename and label for the service dont match - def plist(arg=nil) - set_or_return( - :plist, - arg, - :kind_of => String - ) - end - - def session_type(arg=nil) - set_or_return( - :session_type, - arg, - :kind_of => String - ) - end - + property :plist, String + property :session_type, String end end end diff --git a/lib/chef/resource/mdadm.rb b/lib/chef/resource/mdadm.rb index b789fab155..98d6c2c94a 100644 --- a/lib/chef/resource/mdadm.rb +++ b/lib/chef/resource/mdadm.rb @@ -22,82 +22,21 @@ require 'chef/resource' class Chef class Resource class Mdadm < Chef::Resource - identity_attr :raid_device - state_attrs :devices, :level, :chunk default_action :create allowed_actions :create, :assemble, :stop - def initialize(name, run_context=nil) - super - - @chunk = 16 - @devices = [] - @exists = false - @level = 1 - @metadata = "0.90" - @bitmap = nil - @raid_device = name - end - - def chunk(arg=nil) - set_or_return( - :chunk, - arg, - :kind_of => [ Integer ] - ) - end - - def devices(arg=nil) - set_or_return( - :devices, - arg, - :kind_of => [ Array ] - ) - end - - def exists(arg=nil) - set_or_return( - :exists, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def level(arg=nil) - set_or_return( - :level, - arg, - :kind_of => [ Integer ] - ) - end - - def metadata(arg=nil) - set_or_return( - :metadata, - arg, - :kind_of => [ String ] - ) - end - - def bitmap(arg=nil) - set_or_return( - :bitmap, - arg, - :kind_of => [ String ] - ) - end - - def raid_device(arg=nil) - set_or_return( - :raid_device, - arg, - :kind_of => [ String ] - ) - end - + identity_attr :raid_device + state_attrs :devices, :level, :chunk + property :raid_device, String, name_property: true + property :chunk, Integer, default: 16 + property :devices, Array, default: lazy { [] } + property :exists, [ true, false ], default: false + property :level, Integer, default: 1 + property :metadata, String, default: "0.90" + property :bitmap, String end end diff --git a/lib/chef/resource/mount.rb b/lib/chef/resource/mount.rb index a5da0ba329..d970acdce7 100644 --- a/lib/chef/resource/mount.rb +++ b/lib/chef/resource/mount.rb @@ -22,9 +22,7 @@ require 'chef/resource' class Chef class Resource class Mount < Chef::Resource - identity_attr :device - state_attrs :mount_point, :device_type, :fstype, :username, :password, :domain default_action :mount @@ -32,114 +30,25 @@ class Chef def initialize(name, run_context=nil) super - @mount_point = name - @device = nil - @device_type = :device - @fsck_device = '-' - @fstype = "auto" - @options = ["defaults"] - @dump = 0 - @pass = 2 - @mounted = false - @enabled = false @supports = { :remount => false } - @username = nil - @password = nil - @domain = nil - end - - def mount_point(arg=nil) - set_or_return( - :mount_point, - arg, - :kind_of => [ String ] - ) - end - - def device(arg=nil) - set_or_return( - :device, - arg, - :kind_of => [ String ] - ) - end - - def device_type(arg=nil) - real_arg = arg.kind_of?(String) ? arg.to_sym : arg - valid_devices = if RUBY_PLATFORM =~ /solaris/i - [ :device ] - else - [ :device, :label, :uuid ] - end - set_or_return( - :device_type, - real_arg, - :equal_to => valid_devices - ) - end - - def fsck_device(arg=nil) - set_or_return( - :fsck_device, - arg, - :kind_of => [ String ] - ) - end - - def fstype(arg=nil) - set_or_return( - :fstype, - arg, - :kind_of => [ String ] - ) - end - - def options(arg=nil) - ret = set_or_return( - :options, - arg, - :kind_of => [ Array, String ] - ) - - if ret.is_a? String - ret.gsub(/,/, ' ').split(/ /) - else - ret - end - end - - def dump(arg=nil) - set_or_return( - :dump, - arg, - :kind_of => [ Integer, FalseClass ] - ) - end - - def pass(arg=nil) - set_or_return( - :pass, - arg, - :kind_of => [ Integer, FalseClass ] - ) - end - - def mounted(arg=nil) - set_or_return( - :mounted, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def enabled(arg=nil) - set_or_return( - :enabled, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) end + identity_attr :device + state_attrs :mount_point, :device_type, :fstype, :username, :password, :domain + property :mount_point, String, name_property: true + property :device, String + property :device_type, is: (RUBY_PLATFORM =~ /solaris/i ? :device : [ :device, :label, :uid ]), + coerce: { |v| v.is_a?(String) ? v.to_sym : v }, + default: :device + property :fsck_device, String, default: '-' + property :fstype, String, default: "auto" + property :options, [ Array ], default: lazy { [ 'defaults' ] }, coerce: { |v| v.is_a?(String) ? v.gsub(/,/, ' ').split(/ /) : v } + property :dump, [ Integer, false ], default: 0 + property :pass, [ Integer, false ], default: 2 + property :mounted, [ true, false ], default: false + property :enabled, [ true, false ], default: false + # TODO "{}" is treated as a get, and this doesn't support that ... +# property :supports, Hash, default: { remount: false }, coerce: { |v| v.is_a?(Array) ? supports.merge(v.map { |v| [ v, true ] }) : v } def supports(args={}) if args.is_a? Array args.each { |arg| @supports[arg] = true } @@ -149,30 +58,9 @@ class Chef @supports end end - - def username(arg=nil) - set_or_return( - :username, - arg, - :kind_of => [ String ] - ) - end - - def password(arg=nil) - set_or_return( - :password, - arg, - :kind_of => [ String ] - ) - end - - def domain(arg=nil) - set_or_return( - :domain, - arg, - :kind_of => [ String ] - ) - end + property :username, String + property :password, String + property :domain, String private @@ -181,7 +69,6 @@ class Chef def clear_fstype @fstype = nil end - end end end diff --git a/lib/chef/resource/ohai.rb b/lib/chef/resource/ohai.rb index 9425e55c0c..1047ce6a2f 100644 --- a/lib/chef/resource/ohai.rb +++ b/lib/chef/resource/ohai.rb @@ -21,33 +21,11 @@ class Chef class Resource class Ohai < Chef::Resource - identity_attr :name - - state_attrs :plugin - default_action :reload - def initialize(name, run_context=nil) - super - @name = name - @plugin = nil - end - - def plugin(arg=nil) - set_or_return( - :plugin, - arg, - :kind_of => [ String ] - ) - end - - def name(arg=nil) - set_or_return( - :name, - arg, - :kind_of => [ String ] - ) - end + identity_attr :name + state_attrs :plugin + property :plugin, String end end end diff --git a/lib/chef/resource/package.rb b/lib/chef/resource/package.rb index 5be1c34b89..cd6f38f523 100644 --- a/lib/chef/resource/package.rb +++ b/lib/chef/resource/package.rb @@ -23,80 +23,20 @@ class Chef class Resource class Package < Chef::Resource 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 - @candidate_version = nil - @options = nil - @package_name = name - @response_file = nil - @response_file_variables = Hash.new - @source = nil - @version = nil - @timeout = nil - end - - def package_name(arg=nil) - set_or_return( - :package_name, - arg, - :kind_of => [ String, Array ] - ) - end - - def version(arg=nil) - set_or_return( - :version, - arg, - :kind_of => [ String, Array ] - ) - end - - def response_file(arg=nil) - set_or_return( - :response_file, - arg, - :kind_of => [ String ] - ) - end - - def response_file_variables(arg=nil) - set_or_return( - :response_file_variables, - arg, - :kind_of => [ Hash ] - ) - end - - def source(arg=nil) - set_or_return( - :source, - arg, - :kind_of => [ String ] - ) - end - - def options(arg=nil) - set_or_return( - :options, - arg, - :kind_of => [ String ] - ) - end - - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => [String, Integer] - ) - end - + identity_attr :package_name + state_attrs :version, :options + property :package_name, [ String, Array ], name_property: true + property :version, [ String, Array ] + property :response_file, String + property :response_file_variables, Hash, default: lazy { {} } + property :source, String + property :options, String + property :timeout, [ String, Integer ] end end end diff --git a/lib/chef/resource/paludis_package.rb b/lib/chef/resource/paludis_package.rb index 56c47bc141..051de4514d 100644 --- a/lib/chef/resource/paludis_package.rb +++ b/lib/chef/resource/paludis_package.rb @@ -25,11 +25,8 @@ class Chef provides :paludis_package, os: "linux" allowed_actions :install, :remove, :upgrade - - def initialize(name, run_context=nil) - super(name, run_context) - @timeout = 3600 - end + # Override from parent, to change the default + property :timeout, [ String, Integer ], default: 3600 end end end diff --git a/lib/chef/resource/perl.rb b/lib/chef/resource/perl.rb index 773eba6571..a22cff69fa 100644 --- a/lib/chef/resource/perl.rb +++ b/lib/chef/resource/perl.rb @@ -22,11 +22,6 @@ require 'chef/provider/script' class Chef class Resource class Perl < Chef::Resource::Script - def initialize(name, run_context=nil) - super - @interpreter = "perl" - end - end end end diff --git a/lib/chef/resource/portage_package.rb b/lib/chef/resource/portage_package.rb index 1af48702fa..7d873343c6 100644 --- a/lib/chef/resource/portage_package.rb +++ b/lib/chef/resource/portage_package.rb @@ -21,11 +21,7 @@ require 'chef/resource/package' class Chef class Resource class PortagePackage < Chef::Resource::Package - def initialize(name, run_context=nil) - super - @provider = Chef::Provider::Package::Portage - end - + use_automatic_resource_name end end end diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb index 7d432883e4..0a4fa61b12 100644 --- a/lib/chef/resource/powershell_script.rb +++ b/lib/chef/resource/powershell_script.rb @@ -24,16 +24,9 @@ class Chef def initialize(name, run_context=nil) super(name, run_context, nil, "powershell.exe") - @convert_boolean_return = false end - def convert_boolean_return(arg=nil) - set_or_return( - :convert_boolean_return, - arg, - :kind_of => [ FalseClass, TrueClass ] - ) - end + property :convert_boolean_return, [ true, false ], default: false # Allow callers evaluating guards to request default # attribute values. This is needed to allow diff --git a/lib/chef/resource/python.rb b/lib/chef/resource/python.rb index 432ee46b85..dabfddf9dc 100644 --- a/lib/chef/resource/python.rb +++ b/lib/chef/resource/python.rb @@ -21,11 +21,6 @@ require 'chef/provider/script' class Chef class Resource class Python < Chef::Resource::Script - def initialize(name, run_context=nil) - super - @interpreter = "python" - end - end end end diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb index 401f2f338f..f4a6d493f5 100644 --- a/lib/chef/resource/reboot.rb +++ b/lib/chef/resource/reboot.rb @@ -26,23 +26,8 @@ class Chef class Reboot < Chef::Resource allowed_actions :request_reboot, :reboot_now, :cancel - def initialize(name, run_context=nil) - super - @provider = Chef::Provider::Reboot - - @reason = "Reboot by Chef" - @delay_mins = 0 - - # no default action. - end - - def reason(arg=nil) - set_or_return(:reason, arg, :kind_of => String) - end - - def delay_mins(arg=nil) - set_or_return(:delay_mins, arg, :kind_of => Fixnum) - end + property :reason, String, default: "Reboot by Chef" + property :delay_mins, Fixnum, default: 0 end end end diff --git a/lib/chef/resource/registry_key.rb b/lib/chef/resource/registry_key.rb index 4ed0d4a4e0..fe83b292e4 100644 --- a/lib/chef/resource/registry_key.rb +++ b/lib/chef/resource/registry_key.rb @@ -57,65 +57,45 @@ class Chef # may want to extend the state_attrs API with the ability to rename POST'd attrs. # # See lib/chef/resource_reporter.rb for more information. - attr_reader :unscrubbed_values - - def initialize(name, run_context=nil) - super - @architecture = :machine - @recursive = false - @key = name - @values, @unscrubbed_values = [], [] - end - - def key(arg=nil) - set_or_return( - :key, - arg, - :kind_of => String - ) + def unscrubbed_values + @unscrubbed_values ||= [] end - def values(arg=nil) - if not arg.nil? - if arg.is_a?(Hash) - @values = [ arg ] - elsif arg.is_a?(Array) - @values = arg - else - raise ArgumentError, "Bad type for RegistryKey resource, use Hash or Array" - end + identity_attr :key + state_attrs :values + property :key, String, name_property: true + property :values, Array, lazy: { [] }, coerce: proc do |arg| + case arg + when Hash + arg = [ arg ] + when Array + else + raise ArgumentError, "Bad type for RegistryKey resource, use Hash or Array" + end - @values.each do |v| - raise ArgumentError, "Missing name key in RegistryKey values hash" unless v.has_key?(:name) - raise ArgumentError, "Missing type key in RegistryKey values hash" unless v.has_key?(:type) - raise ArgumentError, "Missing data key in RegistryKey values hash" unless v.has_key?(:data) - v.each_key do |key| - raise ArgumentError, "Bad key #{key} in RegistryKey values hash" unless [:name,:type,:data].include?(key) - end - raise ArgumentError, "Type of name => #{v[:name]} should be string" unless v[:name].is_a?(String) - raise Argument Error "Type of type => #{v[:name]} should be symbol" unless v[:type].is_a?(Symbol) + arg.each do |v| + raise ArgumentError, "Missing name key in RegistryKey values hash" unless v.has_key?(:name) + raise ArgumentError, "Missing type key in RegistryKey values hash" unless v.has_key?(:type) + raise ArgumentError, "Missing data key in RegistryKey values hash" unless v.has_key?(:data) + v.each_key do |key| + raise ArgumentError, "Bad key #{key} in RegistryKey values hash" unless [:name,:type,:data].include?(key) end - @unscrubbed_values = @values - elsif self.instance_variable_defined?(:@values) - scrub_values(@values) + raise ArgumentError, "Type of name => #{v[:name]} should be string" unless v[:name].is_a?(String) + raise Argument Error "Type of type => #{v[:name]} should be symbol" unless v[:type].is_a?(Symbol) end end - - def recursive(arg=nil) - set_or_return( - :recursive, - arg, - :kind_of => [TrueClass, FalseClass] - ) - end - - def architecture(arg=nil) - set_or_return( - :architecture, - arg, - :kind_of => Symbol - ) + def values(arg=NOT_PASSED) + result = super + if !arg.nil? && arg != NOT_PASSED + @unscrubbed_values = result + elsif property_is_set?(:values) + scrub_values(result) + else + result + end end + property :recursive, [ true, false ], default: false + property :architecture, Symbol, default: :machine private diff --git a/lib/chef/resource/remote_directory.rb b/lib/chef/resource/remote_directory.rb index b731f7b201..2aebd00759 100644 --- a/lib/chef/resource/remote_directory.rb +++ b/lib/chef/resource/remote_directory.rb @@ -26,98 +26,30 @@ class Chef class RemoteDirectory < Chef::Resource::Directory include Chef::Mixin::Securable - 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 - @path = name - @source = ::File.basename(name) - @delete = false - @recursive = true - @purge = false - @files_backup = 5 - @files_owner = nil - @files_group = nil - @files_mode = 0644 unless Chef::Platform.windows? - @overwrite = true - @cookbook = nil + identity_attr :path + state_attrs :files_owner, :files_group, :files_mode + property :source, String, default: lazy { ::File.basename(path) } + property :files_backup, [ Integer, false ], default: 5 + property :purge, [ true, false ], default: false + property :recursive, [ true, false ], default: true + property :delete, [ true, false ], default: false + property :overwrite, [ true, false ], default: true + property :files_group, Chef::Config[:group_valid_regex] + property :files_owner, Chef::Config[:user_valid_regex] + if Chef::Platform.windows? + property :files_mode, /^\d{3,4}$/ + else + property :files_mode, /^\d{3,4}$/, default: 0644 end + property :cookbook, String if Chef::Platform.windows? # create a second instance of the 'rights' attribute rights_attribute(:files_rights) end - - - def source(args=nil) - set_or_return( - :source, - args, - :kind_of => String - ) - end - - def files_backup(arg=nil) - set_or_return( - :files_backup, - arg, - :kind_of => [ Integer, FalseClass ] - ) - end - - def purge(arg=nil) - set_or_return( - :purge, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def files_group(arg=nil) - set_or_return( - :files_group, - arg, - :regex => Chef::Config[:group_valid_regex] - ) - end - - def files_mode(arg=nil) - set_or_return( - :files_mode, - arg, - :regex => /^\d{3,4}$/ - ) - end - - def files_owner(arg=nil) - set_or_return( - :files_owner, - arg, - :regex => Chef::Config[:user_valid_regex] - ) - end - - def overwrite(arg=nil) - set_or_return( - :overwrite, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def cookbook(args=nil) - set_or_return( - :cookbook, - args, - :kind_of => String - ) - end - end end end diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb index b7a553cbe8..b5d84757ab 100644 --- a/lib/chef/resource/remote_file.rb +++ b/lib/chef/resource/remote_file.rb @@ -28,16 +28,6 @@ class Chef class RemoteFile < Chef::Resource::File include Chef::Mixin::Securable - def initialize(name, run_context=nil) - super - @source = [] - @use_etag = true - @use_last_modified = true - @ftp_active_mode = false - @headers = {} - @provider = Chef::Provider::RemoteFile - end - # source can take any of the following as arguments # - A single string argument # - Multiple string arguments @@ -46,40 +36,24 @@ class Chef # or array of strings # All strings must be parsable as URIs. # source returns an array of strings. - def source(*args) - arg = parse_source_args(args) - ret = set_or_return(:source, - arg, - { :callbacks => { - :validate_source => method(:validate_source) - }}) - if ret.is_a? String - Array(ret) - else - ret - end - end + property :source, Array, callbacks: { validate_source: proc { validate_source } }, + default: lazy { [] }, + coerce: proc { |*args| parse_source_args(args) } + + property :checksum, String + property :use_etag, [ true, false ], default: true + alias :use_etags :use_etag + property :use_last_modified, [ true, false ], default: true + property :ftp_active_mode, [ true, false ], default: false + property :headers, Hash, default: lazy { {} } def parse_source_args(args) - if args.empty? - nil - elsif args[0].is_a?(Chef::DelayedEvaluator) && args.count == 1 - args[0] - elsif args.any? {|a| a.is_a?(Chef::DelayedEvaluator)} && args.count > 1 + args = Array(args).flatten + if args.any? {|a| a.is_a?(Chef::DelayedEvaluator)} raise Exceptions::InvalidRemoteFileURI, "Only 1 source argument allowed when using a lazy evaluator" - else - Array(args).flatten end end - def checksum(args=nil) - set_or_return( - :checksum, - args, - :kind_of => String - ) - end - # Disable or enable ETag and Last Modified conditional GET. Equivalent to # use_etag(true_or_false) # use_last_modified(true_or_false) @@ -88,40 +62,6 @@ class Chef use_last_modified(true_or_false) end - def use_etag(args=nil) - set_or_return( - :use_etag, - args, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - alias :use_etags :use_etag - - def use_last_modified(args=nil) - set_or_return( - :use_last_modified, - args, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def ftp_active_mode(args=nil) - set_or_return( - :ftp_active_mode, - args, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def headers(args=nil) - set_or_return( - :headers, - args, - :kind_of => Hash - ) - end - private include Chef::Mixin::Uris diff --git a/lib/chef/resource/route.rb b/lib/chef/resource/route.rb index 3ba8f6215b..b319003416 100644 --- a/lib/chef/resource/route.rb +++ b/lib/chef/resource/route.rb @@ -23,115 +23,25 @@ class Chef class Resource class Route < Chef::Resource identity_attr :target - state_attrs :netmask, :gateway default_action :add allowed_actions :add, :delete - def initialize(name, run_context=nil) - super - @target = name - @netmask = nil - @gateway = nil - @metric = nil - @device = nil - @route_type = :host - @networking = nil - @networking_ipv6 = nil - @hostname = nil - @domainname = nil - @domain = nil - end - - def networking(arg=nil) - set_or_return( - :networking, - arg, - :kind_of => String - ) - end - - def networking_ipv6(arg=nil) - set_or_return( - :networking_ipv6, - arg, - :kind_of => String - ) - end - - def hostname(arg=nil) - set_or_return( - :hostname, - arg, - :kind_of => String - ) - end - - def domainname(arg=nil) - set_or_return( - :domainname, - arg, - :kind_of => String - ) - end - - def domain(arg=nil) - set_or_return( - :domain, - arg, - :kind_of => String - ) - end - - def target(arg=nil) - set_or_return( - :target, - arg, - :kind_of => String - ) - end - - def netmask(arg=nil) - set_or_return( - :netmask, - arg, - :kind_of => String - ) - end - - def gateway(arg=nil) - set_or_return( - :gateway, - arg, - :kind_of => String - ) - end - - def metric(arg=nil) - set_or_return( - :metric, - arg, - :kind_of => Integer - ) - end - - def device(arg=nil) - set_or_return( - :device, - arg, - :kind_of => String - ) - end - - def route_type(arg=nil) - real_arg = arg.kind_of?(String) ? arg.to_sym : arg - set_or_return( - :route_type, - real_arg, - :equal_to => [ :host, :net ] - ) - end + identity_attr :target + state_attrs :netmask, :gateway + property :target, String, name_property: true + property :networking, String + property :networking_ipv6, String + property :hostname, String + property :domainname, String + property :domain, String + property :target, String + property :netmask, String + property :gateway, String + property :metric, Integer + property :device, String + property :route_type, [ :host, :net ], default: :net, coerce: proc { |v| arg.kind_of?(String) ? arg.to_sym : arg } end end end diff --git a/lib/chef/resource/rpm_package.rb b/lib/chef/resource/rpm_package.rb index b8b5144a42..14fb8b4687 100644 --- a/lib/chef/resource/rpm_package.rb +++ b/lib/chef/resource/rpm_package.rb @@ -29,14 +29,7 @@ class Chef @allow_downgrade = false end - def allow_downgrade(arg=nil) - set_or_return( - :allow_downgrade, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - + property :allow_downgrade, [ true, false ], default: false end end end diff --git a/lib/chef/resource/ruby.rb b/lib/chef/resource/ruby.rb index 3c3909043d..de1257679d 100644 --- a/lib/chef/resource/ruby.rb +++ b/lib/chef/resource/ruby.rb @@ -22,10 +22,6 @@ require 'chef/provider/script' class Chef class Resource class Ruby < Chef::Resource::Script - def initialize(name, run_context=nil) - super - @interpreter = "ruby" - end end end end diff --git a/lib/chef/resource/ruby_block.rb b/lib/chef/resource/ruby_block.rb index ae8e4cb7cd..751476f436 100644 --- a/lib/chef/resource/ruby_block.rb +++ b/lib/chef/resource/ruby_block.rb @@ -27,27 +27,8 @@ class Chef allowed_actions :create, :run identity_attr :block_name - - def initialize(name, run_context=nil) - super - @block_name = name - end - - def block(&block) - if block_given? and block - @block = block - else - @block - end - end - - def block_name(arg=nil) - set_or_return( - :block_name, - arg, - :kind_of => String - ) - end + property :block_name, String, name_property: true + property :block, Proc, coerce: proc { |v=nil, &block| v || block } end end end diff --git a/lib/chef/resource/scm.rb b/lib/chef/resource/scm.rb index 85028c266b..0d9f50df9d 100644 --- a/lib/chef/resource/scm.rb +++ b/lib/chef/resource/scm.rb @@ -22,164 +22,27 @@ require 'chef/resource' class Chef class Resource class Scm < Chef::Resource - 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 - @enable_submodules = false - @enable_checkout = true - @revision = "HEAD" - @remote = "origin" - @ssh_wrapper = nil - @depth = nil - @checkout_branch = "deploy" - @environment = nil - end - - def destination(arg=nil) - set_or_return( - :destination, - arg, - :kind_of => String - ) - end - - def repository(arg=nil) - set_or_return( - :repository, - arg, - :kind_of => String - ) - end - - def revision(arg=nil) - set_or_return( - :revision, - arg, - :kind_of => String - ) - end - - def user(arg=nil) - set_or_return( - :user, - arg, - :kind_of => [String, Integer] - ) - end - - def group(arg=nil) - set_or_return( - :group, - arg, - :kind_of => [String, Integer] - ) - end - - def svn_username(arg=nil) - set_or_return( - :svn_username, - arg, - :kind_of => String - ) - end - - def svn_password(arg=nil) - set_or_return( - :svn_password, - arg, - :kind_of => String - ) - end - - def svn_arguments(arg=nil) - @svn_arguments, arg = nil, nil if arg == false - set_or_return( - :svn_arguments, - arg, - :kind_of => String - ) - end - - def svn_info_args(arg=nil) - @svn_info_args, arg = nil, nil if arg == false - set_or_return( - :svn_info_args, - arg, - :kind_of => String) - end - - # Capistrano and git-deploy use ``shallow clone'' - def depth(arg=nil) - set_or_return( - :depth, - arg, - :kind_of => Integer - ) - end - - def enable_submodules(arg=nil) - set_or_return( - :enable_submodules, - arg, - :kind_of => [TrueClass, FalseClass] - ) - end - - def enable_checkout(arg=nil) - set_or_return( - :enable_checkout, - arg, - :kind_of => [TrueClass, FalseClass] - ) - end - - def remote(arg=nil) - set_or_return( - :remote, - arg, - :kind_of => String - ) - end - - def ssh_wrapper(arg=nil) - set_or_return( - :ssh_wrapper, - arg, - :kind_of => String - ) - end - - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => Integer - ) - end - - def checkout_branch(arg=nil) - set_or_return( - :checkout_branch, - arg, - :kind_of => String - ) - end - - def environment(arg=nil) - set_or_return( - :environment, - arg, - :kind_of => [ Hash ] - ) - end - + identity_attr :destination + state_attrs :revision + property :destination, String, name_property: true + property :repository, String + property :revision, String, default: "HEAD" + property :user, [ String, Integer ] + property :group, [ String, Integer ] + property :svn_username, String + property :svn_password, String + property :svn_arguments, [ String, nil ], coerce: proc { |arg| arg == false ? nil : arg } + property :svn_info_args, [ String, nil ], coerce: proc { |arg| arg == false ? nil : arg } + property :depth, Integer + property :enable_checkout, [ true, false ] + property :remote, String, default: "origin" + property :ssh_wrapper, String + property :timeout, Integer + property :checkout_branch, String, default: "deploy" + property :environment, Hash alias :env :environment end end diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb index 30bed367cb..cb7cdf9a0f 100644 --- a/lib/chef/resource/script.rb +++ b/lib/chef/resource/script.rb @@ -24,51 +24,24 @@ class Chef class Resource class Script < Chef::Resource::Execute # Chef-13: go back to using :name as the identity attr - identity_attr :command + # identity_attr :name (or extend from Chef::Resource) + # property :command, String (no name_property) - def initialize(name, run_context=nil) - super - # Chef-13: the command variable should be initialized to nil - @command = name - @code = nil - @interpreter = nil - @flags = nil - @default_guard_interpreter = :default - end + property :code, String + property :interpreter, String, default: lazy { resource_name } + property :flags, String - def command(arg=nil) - unless arg.nil? + def command(arg=NOT_PASSED) + unless arg.nil? || arg == NOT_PASSED # Chef-13: change this to raise if the user is trying to set a value here Chef::Log.warn "Specifying command attribute on a script resource is a coding error, use the 'code' attribute, or the execute resource" Chef::Log.warn "This attribute is deprecated and must be fixed or this code will fail on Chef-13" end super end - - def code(arg=nil) - set_or_return( - :code, - arg, - :kind_of => [ String ] - ) - end - - def interpreter(arg=nil) - set_or_return( - :interpreter, - arg, - :kind_of => [ String ] - ) - end - - def flags(arg=nil) - set_or_return( - :flags, - arg, - :kind_of => [ String ] - ) + def command=(arg) + command(arg) end - end end end diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index aa59b543be..5f5d40ab3a 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -22,123 +22,33 @@ require 'chef/resource' class Chef class Resource class Service < Chef::Resource - 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 - @service_name = name - @enabled = nil - @running = nil - @parameters = nil - @pattern = service_name - @start_command = nil - @stop_command = nil - @status_command = nil - @restart_command = nil - @reload_command = nil - @init_command = nil - @priority = nil - @timeout = nil - @supports = { :restart => false, :reload => false, :status => false } - end - - def service_name(arg=nil) - set_or_return( - :service_name, - arg, - :kind_of => [ String ] - ) - end - + identity_attr :service_name + state_attrs :enabled, :running + property :service_name, String, name_property: true # regex for match against ps -ef when !supports[:has_status] && status == nil - def pattern(arg=nil) - set_or_return( - :pattern, - arg, - :kind_of => [ String ] - ) - end - + property :pattern, String, default: lazy { service_name } # command to call to start service - def start_command(arg=nil) - set_or_return( - :start_command, - arg, - :kind_of => [ String ] - ) - end - + property :start_command, String # command to call to stop service - def stop_command(arg=nil) - set_or_return( - :stop_command, - arg, - :kind_of => [ String ] - ) - end - + property :stop_command, String # command to call to get status of service - def status_command(arg=nil) - set_or_return( - :status_command, - arg, - :kind_of => [ String ] - ) - end - + property :status_command, String # command to call to restart service - def restart_command(arg=nil) - set_or_return( - :restart_command, - arg, - :kind_of => [ String ] - ) - end - - def reload_command(arg=nil) - set_or_return( - :reload_command, - arg, - :kind_of => [ String ] - ) - end - + property :restart_command, String + property :reload_command, String # The path to the init script associated with the service. On many # distributions this is '/etc/init.d/SERVICE_NAME' by default. In # non-standard configurations setting this value will save having to # specify overrides for the start_command, stop_command and # restart_command attributes. - def init_command(arg=nil) - set_or_return( - :init_command, - arg, - :kind_of => [ String ] - ) - end - + property :init_command, String # if the service is enabled or not - def enabled(arg=nil) - set_or_return( - :enabled, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - + property :enabled, [ true, false ] # if the service is running or not - def running(arg=nil) - set_or_return( - :running, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - + property :running, [ true, false ] # Priority arguments can have two forms: # # - a simple number, in which the default start runlevels get @@ -149,31 +59,12 @@ class Chef # runlevel 2, stopped in 3 with priority 55 and no symlinks or # similar for other runlevels # - def priority(arg=nil) - set_or_return( - :priority, - arg, - :kind_of => [ Integer, String, Hash ] - ) - end - + property :priority, [ Integer, String, Hash ] # timeout only applies to the windows service manager - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => Integer - ) - end - - def parameters(arg=nil) - set_or_return( - :parameters, - arg, - :kind_of => [ Hash ] - ) - end - + property :timeout, Integer + property :parameters, Hash + # TODO "{}" is treated as a get, and this doesn't support that ... +# property :supports, Hash, default: lazy { { :restart => false, :reload => false, :status => false } }, coerce: { |v| v.is_a?(Array) ? supports.merge(v.map { |v| [ v, true ] }) : v } def supports(args={}) if args.is_a? Array args.each { |arg| @supports[arg] = true } diff --git a/lib/chef/resource/subversion.rb b/lib/chef/resource/subversion.rb index ae6a37caa2..fee00acc26 100644 --- a/lib/chef/resource/subversion.rb +++ b/lib/chef/resource/subversion.rb @@ -24,11 +24,9 @@ class Chef class Subversion < Chef::Resource::Scm allowed_actions :force_export - def initialize(name, run_context=nil) - super - @svn_arguments = '--no-auth-cache' - @svn_info_args = '--no-auth-cache' - end + # From parent + property :svn_arguments, [ String, nil ], coerce: proc { |arg| arg == false ? nil : arg }, default: '--no-auth-cache' + property :svn_info_args, [ String, nil ], coerce: proc { |arg| arg == false ? nil : arg }, default: '--no-auth-cache' # Override exception to strip password if any, so it won't appear in logs and different Chef notifications def custom_exception_message(e) diff --git a/lib/chef/resource/template.rb b/lib/chef/resource/template.rb index 5a7f7efd6f..ae47fe2f68 100644 --- a/lib/chef/resource/template.rb +++ b/lib/chef/resource/template.rb @@ -32,46 +32,15 @@ class Chef def initialize(name, run_context=nil) super - @source = "#{::File.basename(name)}.erb" - @cookbook = nil - @local = false - @variables = Hash.new @inline_helper_blocks = {} @inline_helper_modules = [] @helper_modules = [] end - def source(file=nil) - set_or_return( - :source, - file, - :kind_of => [ String, Array ] - ) - end - - def variables(args=nil) - set_or_return( - :variables, - args, - :kind_of => [ Hash ] - ) - end - - def cookbook(args=nil) - set_or_return( - :cookbook, - args, - :kind_of => [ String ] - ) - end - - def local(args=nil) - set_or_return( - :local, - args, - :kind_of => [ TrueClass, FalseClass ] - ) - end + property :source, [ String, Array ], default: lazy { "#{::File.basename(path)}.erb" } + property :variables, Hash, default: lazy { Hash.new } + property :cookbook, String + property :local, [ true, false ], default: false # Declares a helper method to be defined in the template context when # rendering. diff --git a/lib/chef/resource/treat_property_defaults_as_initial_values.rb b/lib/chef/resource/treat_property_defaults_as_initial_values.rb new file mode 100644 index 0000000000..5920cdc844 --- /dev/null +++ b/lib/chef/resource/treat_property_defaults_as_initial_values.rb @@ -0,0 +1,52 @@ +# +# Author:: John Keiser (<jkeiser@chef.io>) +# 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. +# + +class Chef + class Resource + module TreatPropertyDefaultsAsInitialValues + def self.included(other) + other.extend(ClassMethods) + end + + def initialize(*args, &block) + super + self.class.properties_with_defaults.each do |name| + send(name) + end + end + + module ClassMethods + def property(name, *args, **options, &block) + super + if options.has_key?(:default) + @properties_with_defaults ||= {} + @properties_with_defaults[name.to_sym] = options[:default] + end + end + + def properties_with_defaults + if superclass.respond_to?(:properties_with_defaults) + superclass.properties_with_defaults.merge(@properties_with_defaults || {}) + else + @properties_with_defaults || {} + end + end + end + end + end +end diff --git a/lib/chef/resource/user.rb b/lib/chef/resource/user.rb index b85b648c92..f99b6cb62f 100644 --- a/lib/chef/resource/user.rb +++ b/lib/chef/resource/user.rb @@ -21,140 +21,33 @@ require 'chef/resource' class Chef class Resource class User < Chef::Resource - identity_attr :username - - state_attrs :uid, :gid, :home - default_action :create allowed_actions :create, :remove, :modify, :manage, :lock, :unlock def initialize(name, run_context=nil) super - @username = name - @comment = nil - @uid = nil - @gid = nil - @home = nil - @shell = nil - @password = nil - @system = false - @manage_home = false - @force = false - @non_unique = false @supports = { :manage_home => false, :non_unique => false } - @iterations = 27855 - @salt = nil - end - - def username(arg=nil) - set_or_return( - :username, - arg, - :kind_of => [ String ] - ) - end - - def comment(arg=nil) - set_or_return( - :comment, - arg, - :kind_of => [ String ] - ) - end - - def uid(arg=nil) - set_or_return( - :uid, - arg, - :kind_of => [ String, Integer ] - ) - end - - def gid(arg=nil) - set_or_return( - :gid, - arg, - :kind_of => [ String, Integer ] - ) end + identity_attr :username + state_attrs :uid, :gid, :home + property :username, String, name_property: true + property :comment, String + property :uid, [ String, Integer ] + property :gid, [ String, Integer ] alias_method :group, :gid - - def home(arg=nil) - set_or_return( - :home, - arg, - :kind_of => [ String ] - ) - end - - def shell(arg=nil) - set_or_return( - :shell, - arg, - :kind_of => [ String ] - ) - end - - def password(arg=nil) - set_or_return( - :password, - arg, - :kind_of => [ String ] - ) - end - - def salt(arg=nil) - set_or_return( - :salt, - arg, - :kind_of => [ String ] - ) - end - - def iterations(arg=nil) - set_or_return( - :iterations, - arg, - :kind_of => [ Integer ] - ) - end - - def system(arg=nil) - set_or_return( - :system, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def manage_home(arg=nil) - set_or_return( - :manage_home, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def force(arg=nil) - set_or_return( - :force, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - - def non_unique(arg=nil) - set_or_return( - :non_unique, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - + property :home, String + property :shell, String + property :password, String + property :salt, String + property :iterations, Integer, default: 27855 + property :system, [ true, false ], default: false + property :manage_home, [ true, false ], default: false + property :force, [ true, false ], default: false + property :non_unique, [ true, false ], default: false end end end diff --git a/lib/chef/resource/windows_package.rb b/lib/chef/resource/windows_package.rb index a76765cc36..b6e6915745 100644 --- a/lib/chef/resource/windows_package.rb +++ b/lib/chef/resource/windows_package.rb @@ -31,70 +31,11 @@ class Chef allowed_actions :install, :remove - def initialize(name, run_context=nil) - super - @source ||= source(@package_name) - - # Unique to this resource - @installer_type = nil - @timeout = 600 - # In the past we accepted return code 127 for an unknown reason and 42 because of a bug - @returns = [ 0 ] - end - - def installer_type(arg=nil) - set_or_return( - :installer_type, - arg, - :kind_of => [ Symbol ] - ) - end - - def timeout(arg=nil) - set_or_return( - :timeout, - arg, - :kind_of => [ String, Integer ] - ) - end - - def returns(arg=nil) - set_or_return( - :returns, - arg, - :kind_of => [ String, Integer, Array ] - ) - end - - def source(arg=nil) - if arg == nil && self.instance_variable_defined?(:@source) == true - @source - else - raise ArgumentError, "Bad type for WindowsPackage resource, use a String" unless arg.is_a?(String) - if uri_scheme?(arg) - @source = arg - else - @source = Chef::Util::PathHelper.canonical_path(arg, false) - end - end - end - - def checksum(arg=nil) - set_or_return( - :checksum, - arg, - :kind_of => [ String ] - ) - end - - def remote_file_attributes(arg=nil) - set_or_return( - :remote_file_attributes, - arg, - :kind_of => [ Hash ] - ) - end - + property :installer_type, Symbol + property :timeout, [ String, Integer ], default: 600 + property :returns, [ String, Integer, Array ], default: lazy { [ 0 ] } + property :checksum, String + property :remote_file_attributes, Hash end end end diff --git a/lib/chef/resource/windows_script.rb b/lib/chef/resource/windows_script.rb index 48e2b535a8..66eb08863f 100644 --- a/lib/chef/resource/windows_script.rb +++ b/lib/chef/resource/windows_script.rb @@ -22,9 +22,11 @@ require 'chef/mixin/windows_architecture_helper' class Chef class Resource class WindowsScript < Chef::Resource::Script - # This is an abstract resource meant to be subclasses; thus no 'provides' + guard_inherited_attributes :architecture - set_guard_inherited_attributes(:architecture) + property :architecture, [ :x86_64, :i386 ], callbacks: { + "is not compatible with this computer's architecture" => proc { |a| assert_architecture_compatible!(a); } + } protected @@ -37,19 +39,6 @@ class Chef include Chef::Mixin::WindowsArchitectureHelper - public - - def architecture(arg=nil) - assert_architecture_compatible!(arg) if ! arg.nil? - result = set_or_return( - :architecture, - arg, - :kind_of => Symbol - ) - end - - protected - def assert_architecture_compatible!(desired_architecture) if ! node_supports_windows_architecture?(node, desired_architecture) raise Chef::Exceptions::Win32ArchitectureIncorrect, diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb index a77690652e..1db60c60aa 100644 --- a/lib/chef/resource/windows_service.rb +++ b/lib/chef/resource/windows_service.rb @@ -31,41 +31,12 @@ class Chef allowed_actions :configure_startup identity_attr :service_name - state_attrs :enabled, :running - - def initialize(name, run_context=nil) - super - @startup_type = :automatic - @run_as_user = "" - @run_as_password = "" - end - - def startup_type(arg=nil) - # Set-Service arguments are automatic and manual - # Win32::Service returns 'auto start' or 'demand start' respectively, which the provider currently uses - set_or_return( - :startup_type, - arg, - :equal_to => [ :automatic, :manual, :disabled ] - ) - end - - def run_as_user(arg=nil) - set_or_return( - :run_as_user, - arg, - :kind_of => [ String ] - ) - end - - def run_as_password(arg=nil) - set_or_return( - :run_as_password, - arg, - :kind_of => [ String ] - ) - end + # Set-Service arguments are automatic and manual + # Win32::Service returns 'auto start' or 'demand start' respectively, which the provider currently uses + property :startup_type, [ :automatic, :manual, :disabled ], default: :automatic + property :run_as_user, String, default: "" + property :run_as_password, String, default: "" end end end diff --git a/lib/chef/resource/yum_package.rb b/lib/chef/resource/yum_package.rb index 4d54f6051f..d6e982c47c 100644 --- a/lib/chef/resource/yum_package.rb +++ b/lib/chef/resource/yum_package.rb @@ -26,19 +26,14 @@ class Chef def initialize(name, run_context=nil) super - @flush_cache = { :before => false, :after => false } @allow_downgrade = false end # Install a specific arch - def arch(arg=nil) - set_or_return( - :arch, - arg, - :kind_of => [ String, Array ] - ) - end - + property :arch, [ String, Array ] + property :allow_downgrade, [ true, false ], default: false + # TODO "{}" is treated as a get, and this doesn't support that ... +# property :flush_cache, Hash, default: lazy { { :before => false, :after => false } }, coerce: { |v| v.is_a?(Array) ? supports.merge(v.map { |v| [ v, true ] }) : v } def flush_cache(args={}) if args.is_a? Array args.each { |arg| @flush_cache[arg] = true } @@ -48,15 +43,6 @@ class Chef @flush_cache end end - - def allow_downgrade(arg=nil) - set_or_return( - :allow_downgrade, - arg, - :kind_of => [ TrueClass, FalseClass ] - ) - end - end end end |