From 1ac372ac0f79545cedc9d3670745495c12a58cf4 Mon Sep 17 00:00:00 2001 From: Dennis Hoer Date: Thu, 25 Sep 2014 11:51:20 -0700 Subject: [PATCH] Replace ruby-wmi with win32ole --- README.md | 11 +++---- TLDR.md | 29 +++++++++++++++++-- providers/default.rb | 17 ++++++++--- resources/default.rb | 4 +-- spec/unit/install_service_spec.rb | 14 +++++---- spec/unit/remove_service_spec.rb | 8 +++-- .../nssm_test/recipes/install_service.rb | 7 ++--- 7 files changed, 60 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index a6b129a..38acaf2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Read [TLDR](https://github.com/dhoer/chef-nssm/blob/master/TLDR.md) for more det ### Platform -Should work under Windows 2000 or later. +- Windows Server 2012 R2 ### Cookbooks @@ -17,18 +17,15 @@ Should work under Windows 2000 or later. ## Usage -Simply add `recipe[nssm]` to a run list. +Add `recipe[nssm]` to a run list. ### Examples To install a Windows service: nssm 'service name' do - app 'java' - args [ - '-jar', - "'C:\\path to\\my-executable.jar'" - ] + program 'C:\\Windows\\System32\\java.exe' + args '-jar C:/path/to/my-executable.jar' action :install end diff --git a/TLDR.md b/TLDR.md index b16ab9e..7f0f2d8 100644 --- a/TLDR.md +++ b/TLDR.md @@ -2,6 +2,27 @@ ## Usage +Advanced usage of NSSM. + +### Examples + +When dealing with an argument that contain spaces, add triple double quotes `"""` around it: + + nssm 'service name' do + program 'C:\\Windows\\System32\\java.exe' + args '-jar """C:/path/with spaces to/my-executable.jar"""' + action :install + end + +When dealing with an arguments that require interpolation and handling of spaces, then encapsulate the entire args using `%Q{}` notation and use `"""` around the arguments containing spaces: + + my_path_with_spaces = 'C:/path/with spaces to/my-executable.jar + nssm 'service name' do + program 'C:\\Windows\\System32\\java.exe' + args %Q{-jar """#{my_path_with_spaces}"""} + action :install + end + ### Attributes - `node['nssm']['src']` - This can either be a URI or a local path to nssm zip. @@ -17,8 +38,8 @@ #### Attribute Parameters - :servicename: Name attribute. The name of the Windows service. -- :app: The program to be run as a service. -- :args: Array of arguments for the program. Optional +- :program: The program to be run as a service. +- :args: String of arguments for the program. Optional - :start: Start service after installing. Default: true ## ChefSpec Matchers @@ -28,7 +49,9 @@ The NSSM cookbook includes custom [ChefSpec](https://github.com/sethvargo/chefsp Example Matcher Usage expect(chef_run).to install_nssm_service('service name').with( - app: 'java') + :program 'C:\\Windows\\System32\\java.exe' + :args '-jar C:/path/to/my-executable.jar' + ) NSSM Cookbook Matchers diff --git a/providers/default.rb b/providers/default.rb index 8fc33bf..2258f83 100644 --- a/providers/default.rb +++ b/providers/default.rb @@ -1,13 +1,22 @@ +require 'win32ole' if RUBY_PLATFORM =~ /mswin|mingw32|windows/ + +def execute_wmi_query(wmi_query) + wmi = ::WIN32OLE.connect('winmgmts://') + result = wmi.ExecQuery(wmi_query) + return nil unless result.each.count > 0 + result +end + def service_installed?(servicename) - !(WMI::Win32_Service.find(:first, conditions: { name: servicename }).nil?) + !(execute_wmi_query("select * from Win32_Service where name = '#{servicename}'").nil?) end action :install do service_installed = service_installed?(new_resource.servicename) - batch "Install service #{new_resource.servicename}" do + batch "Install #{new_resource.servicename} service" do code <<-EOH - nssm install '#{new_resource.servicename}' '#{new_resource.app}' #{new_resource.args.join(' ')} + nssm install "#{new_resource.servicename}" #{new_resource.program} #{new_resource.args} EOH not_if { service_installed } end @@ -27,7 +36,7 @@ def service_installed?(servicename) batch "Remove service #{new_resource.servicename}" do code <<-EOH - nssm remove '#{new_resource.servicename}' confirm + nssm remove "#{new_resource.servicename}" confirm EOH only_if { service_installed } end diff --git a/resources/default.rb b/resources/default.rb index feee6ca..dfe9b2b 100644 --- a/resources/default.rb +++ b/resources/default.rb @@ -2,6 +2,6 @@ default_action :install attribute :servicename, name_attribute: true -attribute :app, kind_of: String, required: true -attribute :args, kind_of: Array, default: [] +attribute :program, kind_of: String, required: true +attribute :args, kind_of: String attribute :start, kind_of: [TrueClass, FalseClass], default: true diff --git a/spec/unit/install_service_spec.rb b/spec/unit/install_service_spec.rb index 0450825..9d1b31f 100644 --- a/spec/unit/install_service_spec.rb +++ b/spec/unit/install_service_spec.rb @@ -6,20 +6,22 @@ let(:fake_class) { Class.new } before do - stub_const('WMI::Win32_Service', fake_class) - allow(fake_class).to receive(:find) { nil } + stub_const('::WIN32OLE', fake_class) + obj = double + allow(obj).to receive(:ExecQuery) { [] } + allow(fake_class).to receive(:connect) { obj } end it 'calls nssm install resource' do expect(chef_run).to install_nssm_service('service name').with( - app: 'java', - args: ['-jar', "'C:\\path to\\my-executable.jar'"] + program: 'C:\\Windows\\System32\\java.exe', + args: '-jar C:/path/to/my-executable.jar' ) end it 'executes batch command to install service' do - expect(chef_run).to run_batch('Install service service name').with( - code: /nssm install 'service name' 'java' -jar 'C:\\path to\\my-executable.jar'/ + expect(chef_run).to run_batch('Install service name service').with( + code: %r{nssm install "service name" C:\\Windows\\System32\\java.exe -jar C:/path/to/my-executable.jar} ) end diff --git a/spec/unit/remove_service_spec.rb b/spec/unit/remove_service_spec.rb index e4b719d..36c2111 100644 --- a/spec/unit/remove_service_spec.rb +++ b/spec/unit/remove_service_spec.rb @@ -6,8 +6,10 @@ let(:fake_class) { Class.new } before do - stub_const('WMI::Win32_Service', fake_class) - allow(fake_class).to receive(:find) { 'ignore' } + stub_const('::WIN32OLE', fake_class) + obj = double + allow(obj).to receive(:ExecQuery) { [''] } + allow(fake_class).to receive(:connect) { obj } end it 'calls nssm remove resource' do @@ -16,7 +18,7 @@ it 'executes batch command to remove service' do expect(chef_run).to run_batch('Remove service service name').with( - code: /nssm remove 'service name' confirm/ + code: /nssm remove "service name" confirm/ ) end end diff --git a/test/fixtures/cookbooks/nssm_test/recipes/install_service.rb b/test/fixtures/cookbooks/nssm_test/recipes/install_service.rb index 3b00f7f..811d764 100644 --- a/test/fixtures/cookbooks/nssm_test/recipes/install_service.rb +++ b/test/fixtures/cookbooks/nssm_test/recipes/install_service.rb @@ -1,8 +1,5 @@ nssm 'service name' do - app 'java' - args [ - '-jar', - "'C:\\path to\\my-executable.jar'" - ] + program 'C:\\Windows\\System32\\java.exe' + args '-jar C:/path/to/my-executable.jar' action :install end