diff options
author | John Keiser <john@johnkeiser.com> | 2015-12-10 20:22:46 -0800 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-12-10 20:22:46 -0800 |
commit | 9d9bfdef864d7693e5fe087dc5d458e6a9e3f51d (patch) | |
tree | b86908e9ffdd811c2c99eb06bf9c8c15ba2c722b | |
parent | 0388ad29f61e9816202b8b4ce5629511f48986e5 (diff) | |
download | chef-jk/service-properties.tar.gz |
Use properties for servicejk/service-properties
-rw-r--r-- | lib/chef/property/capability_hash.rb | 23 | ||||
-rw-r--r-- | lib/chef/resource/macosx_service.rb | 1 | ||||
-rw-r--r-- | lib/chef/resource/service.rb | 159 | ||||
-rw-r--r-- | lib/chef/resource/windows_service.rb | 42 |
4 files changed, 47 insertions, 178 deletions
diff --git a/lib/chef/property/capability_hash.rb b/lib/chef/property/capability_hash.rb new file mode 100644 index 0000000000..9da69ae68c --- /dev/null +++ b/lib/chef/property/capability_hash.rb @@ -0,0 +1,23 @@ +require 'chef/property' + +class Chef + class Property + class CapabilityHash < Chef::Property + # - After supports(:a, :b); supports(:c), the result is supports == { a: true, b: true, c: true } + # - After supports({ x: 1, y: 2 }), supports == { x: 1, y: 2 } + # - After supports({}) or supports, supports remains unchanged. There is no way to nil out supports. + # The additivity and the fact that supports({}) does a get both seem wrong, but we preserve them for now. Chef 13 likely. + def call(resource, value) + if value.is_a?(Array) + result = get(resource) + value.each { |arg| result[arg] = true } + result + elsif value == NOT_PASSED || !value.any? + get(resource) + else + set(resource, value) + end + end + end + end +end diff --git a/lib/chef/resource/macosx_service.rb b/lib/chef/resource/macosx_service.rb index f1ed4051cb..cd8d36c4b7 100644 --- a/lib/chef/resource/macosx_service.rb +++ b/lib/chef/resource/macosx_service.rb @@ -21,6 +21,7 @@ require 'chef/resource/service' class Chef class Resource class MacosxService < Chef::Resource::Service + resource_name :macosx_service provides :macosx_service, os: "darwin" provides :service, os: "darwin" diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index 6d1b81f9cb..5e9bd97f9c 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -18,128 +18,42 @@ # require 'chef/resource' +require 'chef/property/capability_hash' class Chef class Resource class Service < Chef::Resource - identity_attr :service_name + resource_name :service + 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 - @run_levels = nil - @supports = { :restart => nil, :reload => nil, :status => nil } - end - - def service_name(arg=nil) - set_or_return( - :service_name, - arg, - :kind_of => [ String ] - ) - end - + 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 }, desired_state: false # 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, desired_state: false # 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, desired_state: false # 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, desired_state: false # 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, desired_state: false + # command to call to reload service + property :reload_command, String, desired_state: false # 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, desired_state: false # 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 @@ -150,47 +64,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 ], desired_state: false # 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 - - def run_levels(arg=nil) - set_or_return( - :run_levels, - arg, - :kind_of => [ Array ] ) - end - - def supports(args={}) - if args.is_a? Array - args.each { |arg| @supports[arg] = true } - elsif args.any? - @supports = args - else - @supports - end - end + property :timeout, Integer, desired_state: false + property :parameters, Hash, desired_state: false + property :run_levels, Array, desired_state: false + property :supports, Property::CapabilityHash, default: { restart: nil, reload: nil, status: nil }, desired_state: false end end diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb index a77690652e..9959436de8 100644 --- a/lib/chef/resource/windows_service.rb +++ b/lib/chef/resource/windows_service.rb @@ -21,51 +21,17 @@ require 'chef/resource/service' class Chef class Resource class WindowsService < Chef::Resource::Service - + resource_name :windows_service # Until #1773 is resolved, you need to manually specify the windows_service resource # to use action :configure_startup and attribute startup_type - provides :windows_service, os: "windows" provides :service, os: "windows" 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 + property :startup_type, [ :automatic, :manual, :disabled ], default: :automatic, desired_state: false + property :run_as_user, String, default: "" + property :run_as_password, String, default: "" end end end |