diff options
author | Bryan McLellan <btm@loftninjas.org> | 2014-08-06 12:37:41 -0400 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2014-08-12 16:51:10 -0400 |
commit | f7dc2d50db5e780c7c0148f8895d84a3fd0fdf3d (patch) | |
tree | 93d1144ba09a8973e5214607d02c78edd9e312c7 /lib | |
parent | e08741f4f713c98f23e6db37426961c8c0d01b8f (diff) | |
download | chef-f7dc2d50db5e780c7c0148f8895d84a3fd0fdf3d.tar.gz |
CHEF-5022: Differentiate between Windows service startup_types
A Windows service startup_type can be automatic, manual, or disabled.
This adds a startup_type attribute to specify automatic or manual, and defaults to automatic
because we currently have no functionality to set a service to manual.
This is important because if you stop a service that is automatic, Windows will change
the startup_type to manual.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider/service/windows.rb | 48 | ||||
-rw-r--r-- | lib/chef/resource/windows_service.rb | 50 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 |
3 files changed, 88 insertions, 11 deletions
diff --git a/lib/chef/provider/service/windows.rb b/lib/chef/provider/service/windows.rb index 2d478fa9fe..4eb9cbebbf 100644 --- a/lib/chef/provider/service/windows.rb +++ b/lib/chef/provider/service/windows.rb @@ -27,6 +27,7 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service #Win32::Service.get_start_type AUTO_START = 'auto start' + MANUAL = 'demand start' DISABLED = 'disabled' #Win32::Service.get_current_state @@ -45,11 +46,11 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service end def load_current_resource - @current_resource = Chef::Resource::Service.new(@new_resource.name) + @current_resource = Chef::Resource::WindowsService.new(@new_resource.name) @current_resource.service_name(@new_resource.service_name) @current_resource.running(current_state == RUNNING) Chef::Log.debug "#{@new_resource} running: #{@current_resource.running}" - @current_resource.enabled(start_type == AUTO_START) + @current_resource.enabled(start_type != DISABLED) Chef::Log.debug "#{@new_resource} enabled: #{@current_resource.enabled}" @current_resource end @@ -123,17 +124,34 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service end end + def action_enable + # Ensure the service is enabled using the specified startup_type + if (not @current_resource.enabled) || (@current_resource.enabled && should_update_startup_type?) + converge_by("enable service #{@new_resource}") do + enable_service + Chef::Log.info("#{@new_resource} enabled, startup_type: #{@new_resource.startup_type}") + end + else + Chef::Log.debug("#{@new_resource} already enabled - nothing to do") + end + load_new_resource_state + @new_resource.enabled(true) + end + def enable_service if Win32::Service.exists?(@new_resource.service_name) - if start_type == AUTO_START - Chef::Log.debug "#{@new_resource} already enabled - nothing to do" - else - Win32::Service.configure( - :service_name => @new_resource.service_name, - :start_type => Win32::Service::AUTO_START - ) - @new_resource.updated_by_last_action(true) + case @new_resource.startup_type + when :automatic + win32_service_startup_type = Win32::Service::AUTO_START + when :manual + win32_service_startup_type = Win32::Service::MANUAL end + + Win32::Service.configure( + :service_name => @new_resource.service_name, + :start_type => win32_service_startup_type + ) + @new_resource.updated_by_last_action(true) else Chef::Log.debug "#{@new_resource} does not exist - nothing to do" end @@ -141,7 +159,7 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service def disable_service if Win32::Service.exists?(@new_resource.service_name) - if start_type == AUTO_START + if start_type != DISABLED Win32::Service.configure( :service_name => @new_resource.service_name, :start_type => Win32::Service::DISABLED @@ -188,4 +206,12 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service worker.join end end + + def should_update_startup_type? + # We could check :disabled too, but we're dedicating action_disable to that + return true if @new_resource.startup_type == :automatic && start_type != AUTO_START + return true if @new_resource.startup_type == :manual && start_type != MANUAL + + false + end end diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb new file mode 100644 index 0000000000..3f207b5014 --- /dev/null +++ b/lib/chef/resource/windows_service.rb @@ -0,0 +1,50 @@ +# +# Author:: Bryan McLellan <btm@loftninjas.org> +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/resource/service' + +class Chef + class Resource + class WindowsService < Chef::Resource::Service + + provides :service, :on_platforms => ["windows"] + + identity_attr :service_name + + state_attrs :enabled, :running + + def initialize(name, run_context=nil) + super + @resource_name = :windows_service + @provider = Chef::Provider::Service::Windows + @startup_type = :automatic + 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 ] + ) + end + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index 01e8d63040..93ff682288 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -64,6 +64,7 @@ require 'chef/resource/ruby_block' require 'chef/resource/scm' require 'chef/resource/script' require 'chef/resource/service' +require 'chef/resource/windows_service' require 'chef/resource/subversion' require 'chef/resource/smartos_package' require 'chef/resource/template' |