summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@loftninjas.org>2014-08-07 17:25:56 -0400
committerBryan McLellan <btm@loftninjas.org>2014-08-12 16:51:10 -0400
commit940479ea0bbd5c8f2feeb1293db63cd396a484aa (patch)
tree45ffe6eddbf52229995f48a4e63cefe8b0da4ff6
parentf7dc2d50db5e780c7c0148f8895d84a3fd0fdf3d (diff)
downloadchef-940479ea0bbd5c8f2feeb1293db63cd396a484aa.tar.gz
CHEF-5022: Add configure_startup action
enable and disable actions will continue to set the startup type to automatic/disabled If you want the startup type to be manual, you can set the startup_type attribute and use action configure_startup. It also supports automatic and disabled.
-rw-r--r--lib/chef/provider/service/windows.rb99
-rw-r--r--lib/chef/resource/windows_service.rb3
-rw-r--r--spec/unit/provider/service/windows_spec.rb83
-rw-r--r--spec/unit/resource/windows_service_spec.rb5
4 files changed, 117 insertions, 73 deletions
diff --git a/lib/chef/provider/service/windows.rb b/lib/chef/provider/service/windows.rb
index 4eb9cbebbf..ff80279038 100644
--- a/lib/chef/provider/service/windows.rb
+++ b/lib/chef/provider/service/windows.rb
@@ -124,12 +124,27 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service
end
end
+ def enable_service
+ if Win32::Service.exists?(@new_resource.service_name)
+ set_startup_type(:automatic)
+ else
+ Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
+ end
+ end
+
+ def disable_service
+ if Win32::Service.exists?(@new_resource.service_name)
+ set_startup_type(:disabled)
+ else
+ Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
+ 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?)
+ if start_type != AUTO_START
converge_by("enable service #{@new_resource}") do
enable_service
- Chef::Log.info("#{@new_resource} enabled, startup_type: #{@new_resource.startup_type}")
+ Chef::Log.info("#{@new_resource} enabled")
end
else
Chef::Log.debug("#{@new_resource} already enabled - nothing to do")
@@ -138,39 +153,49 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service
@new_resource.enabled(true)
end
- def enable_service
- if Win32::Service.exists?(@new_resource.service_name)
- case @new_resource.startup_type
- when :automatic
- win32_service_startup_type = Win32::Service::AUTO_START
- when :manual
- win32_service_startup_type = Win32::Service::MANUAL
+ def action_disable
+ if start_type != DISABLED
+ converge_by("disable service #{@new_resource}") do
+ disable_service
+ Chef::Log.info("#{@new_resource} disabled")
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"
+ Chef::Log.debug("#{@new_resource} already disabled - nothing to do")
end
+ load_new_resource_state
+ @new_resource.enabled(false)
end
- def disable_service
- if Win32::Service.exists?(@new_resource.service_name)
+ def action_configure_startup
+ case @new_resource.startup_type
+ when :automatic
+ if start_type != AUTO_START
+ converge_by("set service #{@new_resource} startup type to automatic") do
+ set_startup_type(:automatic)
+ end
+ else
+ Chef::Log.debug("#{@new_resource} startup_type already automatic - nothing to do")
+ end
+ when :manual
+ if start_type != MANUAL
+ converge_by("set service #{@new_resource} startup type to manual") do
+ set_startup_type(:manual)
+ end
+ else
+ Chef::Log.debug("#{@new_resource} startup_type already manual - nothing to do")
+ end
+ when :disabled
if start_type != DISABLED
- Win32::Service.configure(
- :service_name => @new_resource.service_name,
- :start_type => Win32::Service::DISABLED
- )
- @new_resource.updated_by_last_action(true)
+ converge_by("set service #{@new_resource} startup type to disabled") do
+ set_startup_type(:disabled)
+ end
else
- Chef::Log.debug "#{@new_resource} already disabled - nothing to do"
+ Chef::Log.debug("#{@new_resource} startup_type already disabled - nothing to do")
end
- else
- Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
end
+
+ # Avoid changing enabled from true/false for now
+ @new_resource.enabled(nil)
end
private
@@ -207,11 +232,21 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service
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
+ # Takes Win32::Service start_types
+ def set_startup_type(type)
+ # Set-Service Startup Type => Win32::Service Constant
+ allowed_types = { :automatic => Win32::Service::AUTO_START,
+ :manual => Win32::Service::DEMAND_START,
+ :disabled => Win32::Service::DISABLED }
+ unless allowed_types.keys.include?(type)
+ raise Chef::Exceptions::ConfigurationError, "#{@new_resource.name}: Startup type '#{type}' is not supported"
+ end
- false
+ Chef::Log.debug "#{@new_resource.name} setting start_type to #{type}"
+ Win32::Service.configure(
+ :service_name => @new_resource.service_name,
+ :start_type => allowed_types[type]
+ )
+ @new_resource.updated_by_last_action(true)
end
end
diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb
index 3f207b5014..42b256c4a7 100644
--- a/lib/chef/resource/windows_service.rb
+++ b/lib/chef/resource/windows_service.rb
@@ -32,6 +32,7 @@ class Chef
super
@resource_name = :windows_service
@provider = Chef::Provider::Service::Windows
+ @allowed_actions.push(:configure_startup)
@startup_type = :automatic
end
@@ -42,7 +43,7 @@ class Chef
set_or_return(
:startup_type,
arg,
- :equal_to => [ :automatic, :manual ]
+ :equal_to => [ :automatic, :manual, :disabled ]
)
end
end
diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb
index cf39ad6d3a..b303a35ff9 100644
--- a/spec/unit/provider/service/windows_spec.rb
+++ b/spec/unit/provider/service/windows_spec.rb
@@ -26,10 +26,12 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
@run_context = Chef::RunContext.new(@node, {}, @events)
@new_resource = Chef::Resource::WindowsService.new("chef")
@provider = Chef::Provider::Service::Windows.new(@new_resource, @run_context)
+ @provider.current_resource = Chef::Resource::WindowsService.new("current-chef")
Object.send(:remove_const, 'Win32') if defined?(Win32)
Win32 = Module.new
Win32::Service = Class.new
Win32::Service::AUTO_START = 0x00000002
+ Win32::Service::DEMAND_START = 0x00000003
Win32::Service::DISABLED = 0x00000004
Win32::Service.stub(:status).with(@new_resource.service_name).and_return(
double("StatusStruct", :current_state => "running"))
@@ -246,7 +248,6 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
end
describe Chef::Provider::Service::Windows, "enable_service" do
-
before(:each) do
Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
double("ConfigStruct", :start_type => "disabled"))
@@ -264,59 +265,48 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
@provider.enable_service
@new_resource.updated_by_last_action?.should be_false
end
-
- # [ 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
+ it "does 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"))
@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
+ it "enables the service if it is not set to automatic start" do
+ Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
+ double("ConfigStruct", :start_type => "disabled"))
@provider.should_receive(:enable_service)
- @provider.stub(:should_update_startup_type?).and_return(true)
@provider.action_enable
- @new_resource.enabled.should be_true
+ end
+ end
+
+ describe Chef::Provider::Service::Windows, "action_disable" do
+ it "does nothing if the service is disabled" do
+ Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
+ double("ConfigStruct", :start_type => "disabled"))
+ @provider.should_not_receive(:disable_service)
+ @provider.action_disable
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
+ it "disables the service if it is not set to disabled" do
+ Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
+ double("ConfigStruct", :start_type => "auto start"))
+ @provider.should_receive(:disable_service)
+ @provider.action_disable
end
end
describe Chef::Provider::Service::Windows, "disable_service" do
-
before(:each) do
Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
double("ConfigStruct", :start_type => "auto start"))
end
it "should disable service" do
- Win32::Service.should_receive(:configure).with(:service_name => @new_resource.service_name, :start_type => Win32::Service::DISABLED)
+ Win32::Service.should_receive(:configure)
@provider.disable_service
@new_resource.updated_by_last_action?.should be_true
end
@@ -327,14 +317,27 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do
@provider.disable_service
@new_resource.updated_by_last_action?.should be_false
end
+ end
- it "should do nothing if the service is disabled" do
- Win32::Service.stub(:config_info).with(@new_resource.service_name).and_return(
- double("ConfigStruct", :start_type => "disabled"))
- @provider.load_current_resource
- Win32::Service.should_not_receive(:configure)
- @provider.disable_service
- @new_resource.updated_by_last_action?.should be_false
+ # FIXME
+ describe Chef::Provider::Service::Windows, "action_configure_startup" do
+ { :automatic => "auto start", :manual => "demand start", :disabled => "disabled" }.each do |type,win32|
+ it "sets the startup type to #{type} if it is something else" do
+ @new_resource.startup_type(type)
+ @provider.stub(:start_type).and_return("fire")
+ @provider.should_receive(:set_startup_type).with(type)
+ @provider.action_configure_startup
+ end
+
+ it "leaves the startup type as #{type} if it is already set" do
+ @new_resource.startup_type(type)
+ @provider.stub(:start_type).and_return(win32)
+ @provider.should_not_receive(:set_startup_type).with(type)
+ @provider.action_configure_startup
+ end
end
end
+ describe Chef::Provider::Service::Windows, "set_start_type" do
+ #FIXME
+ end
end
diff --git a/spec/unit/resource/windows_service_spec.rb b/spec/unit/resource/windows_service_spec.rb
index dcf69a3c4d..c92c3be1b0 100644
--- a/spec/unit/resource/windows_service_spec.rb
+++ b/spec/unit/resource/windows_service_spec.rb
@@ -38,4 +38,9 @@ describe Chef::Resource::WindowsService, "initialize", :windows_only do
resource.startup_type(:manual)
expect(resource.startup_type).to eql(:manual)
end
+
+ it "allows the action to be 'configure_startup'" do
+ resource.action :configure_startup
+ resource.action.should == [:configure_startup]
+ end
end