summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@loftninjas.org>2014-08-06 12:37:41 -0400
committerBryan McLellan <btm@loftninjas.org>2014-08-12 16:51:10 -0400
commitf7dc2d50db5e780c7c0148f8895d84a3fd0fdf3d (patch)
tree93d1144ba09a8973e5214607d02c78edd9e312c7
parente08741f4f713c98f23e6db37426961c8c0d01b8f (diff)
downloadchef-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.
-rw-r--r--lib/chef/provider/service/windows.rb48
-rw-r--r--lib/chef/resource/windows_service.rb50
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/provider/service/windows_spec.rb53
-rw-r--r--spec/unit/resource/windows_service_spec.rb41
5 files changed, 172 insertions, 21 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'
diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb
index 33d3cde8fd..cf39ad6d3a 100644
--- a/spec/unit/provider/service/windows_spec.rb
+++ b/spec/unit/provider/service/windows_spec.rb
@@ -24,7 +24,7 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
@node = Chef::Node.new
@events = Chef::EventDispatch::Dispatcher.new
@run_context = Chef::RunContext.new(@node, {}, @events)
- @new_resource = Chef::Resource::Service.new("chef")
+ @new_resource = Chef::Resource::WindowsService.new("chef")
@provider = Chef::Provider::Service::Windows.new(@new_resource, @run_context)
Object.send(:remove_const, 'Win32') if defined?(Win32)
Win32 = Module.new
@@ -192,8 +192,8 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
end
it "should pass custom timeout to the stop command if provided" do
- Win32::Service.stub!(:status).with(@new_resource.service_name).and_return(
- mock("StatusStruct", :current_state => "running"))
+ Win32::Service.stub(:status).with(@new_resource.service_name).and_return(
+ double("StatusStruct", :current_state => "running"))
@new_resource.timeout 1
Win32::Service.should_receive(:stop).with(@new_resource.service_name)
Timeout.timeout(2) do
@@ -265,12 +265,46 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
@new_resource.updated_by_last_action?.should be_false
end
- it "should do nothing if the service is enabled" do
- Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
- double("ConfigStruct", :start_type => "auto start"))
- Win32::Service.should_not_receive(:configure)
- @provider.enable_service
- @new_resource.updated_by_last_action?.should be_false
+ # [ Set-Service/Chef Startup Type, Win32-Service Startup type ]
+ [ [ :automatic, "auto start" ], [ :manual, "demand start" ] ].each do |type, win32|
+ context "startup_type is set to #{type}" do
+ it "changes the start type if it is not #{type}" do
+ Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
+ double("ConfigStruct", :start_type => win32))
+ Win32::Service.should_receive(:configure)
+ @provider.enable_service
+ @new_resource.updated_by_last_action?.should be_true
+ end
+ end
+ end
+ end
+
+ describe Chef::Provider::Service::Windows, "action_enable" do
+ before do
+ @current_resource = Chef::Resource::WindowsService.new("chef")
+ @provider.current_resource = @current_resource
+ @provider.current_resource.enabled(true)
+ @provider.stub(:should_update_startup_type?).and_return(false)
+ end
+
+ it "does nothing if the service is enabled and startup_type is correct" do
+ @provider.should_not_receive(:enable_service)
+ @provider.action_enable
+ @new_resource.enabled.should be_true
+ end
+
+ it "runs enable_service if the service is enabled but the startup_type is wrong" do
+ @provider.should_receive(:enable_service)
+ @provider.stub(:should_update_startup_type?).and_return(true)
+ @provider.action_enable
+ @new_resource.enabled.should be_true
+ end
+
+ it "enables the service if it is not enabled" do
+ @provider.current_resource.enabled(false)
+ @provider.should_receive(:enable_service)
+ @provider.action_enable
+ @new_resource.enabled.should be_true
end
end
@@ -302,6 +336,5 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
@provider.disable_service
@new_resource.updated_by_last_action?.should be_false
end
-
end
end
diff --git a/spec/unit/resource/windows_service_spec.rb b/spec/unit/resource/windows_service_spec.rb
new file mode 100644
index 0000000000..dcf69a3c4d
--- /dev/null
+++ b/spec/unit/resource/windows_service_spec.rb
@@ -0,0 +1,41 @@
+#
+# 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 'spec_helper'
+
+describe Chef::Resource::WindowsService, "initialize", :windows_only do
+
+ let(:resource) { Chef::Resource::WindowsService.new("BITS") }
+
+ it "returns a Chef::Resource::WindowsService" do
+ expect(resource).to be_a_kind_of(Chef::Resource::WindowsService)
+ end
+
+ it "sets the resource_name to :windows_service" do
+ expect(resource.resource_name).to eql(:windows_service)
+ end
+
+ it "sets the provider to Chef::Provider::Service::Windows" do
+ expect(resource.provider).to eql(Chef::Provider::Service::Windows)
+ end
+
+ it "supports setting startup_type" do
+ resource.startup_type(:manual)
+ expect(resource.startup_type).to eql(:manual)
+ end
+end