Skip to content

Commit

Permalink
Merge pull request #27 from axelrtgs/master
Browse files Browse the repository at this point in the history
Convert default recipe to custom resource with idempotence and make service parameters match exactly
  • Loading branch information
dhoer authored Aug 2, 2017
2 parents cf2ed96 + 9403ebe commit bb3cf1a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 34 deletions.
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
default['nssm']['sha256'] = '0bbe25025b69ebd8ab263ec4b443513d28a0d072e5fdd9b5cdb327359a27f96e'

default['nssm']['install_location'] = ENV['WINDIR']
default['nssm']['install_nssm'] = true
24 changes: 3 additions & 21 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
# frozen_string_literal: true

if platform?('windows')
src = node['nssm']['src']
basename = src.slice(src.rindex('/') + 1, src.rindex('.') - src.rindex('/') - 1)
system = node['kernel']['machine'] == 'x86_64' ? 'win64' : 'win32'
system_file = "#{Chef::Config[:file_cache_path]}/#{basename}/#{system}/nssm.exe"

windows_zipfile 'download nssm' do
path Chef::Config[:file_cache_path]
source src
overwrite true
checksum node['nssm']['sha256']
action :unzip
notifies :create, 'remote_file[install nssm]', :immediately
end

remote_file 'install nssm' do
path ::NSSM.binary_path node
source "file:///#{system_file}"
end
else
Chef::Log.warn('NSSM can only be installed on Windows platforms!')
nssm_install 'Install NSSM' do
source node['nssm']['src']
sha256 node['nssm']['sha256']
end
34 changes: 34 additions & 0 deletions resources/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
provides :nssm_install, platform: 'windows'

property :source, identity: true, name_attribute: true
property :sha256, kind_of: String, required: true

default_action :install

action :install do
src = new_resource.source
basename = src.slice(src.rindex('/') + 1, src.rindex('.') - src.rindex('/') - 1)
system = node['kernel']['machine'] == 'x86_64' ? 'win64' : 'win32'
system_file = "#{Chef::Config[:file_cache_path]}/#{basename}/#{system}/nssm.exe"

windows_zipfile 'download nssm' do
path Chef::Config[:file_cache_path]
source src
overwrite true
checksum new_resource.sha256
action :unzip
notifies :create, 'remote_file[install nssm]', :immediately
not_if { Digest::SHA256.file("#{Chef::Config[:file_cache_path]}/#{basename}.zip").hexdigest == new_resource.sha256 } if ::File.exist?("#{Chef::Config[:file_cache_path]}/#{basename}.zip")
end

remote_file 'install nssm' do
path ::NSSM.binary_path node
source "file:///#{system_file}"
end
end

action_class do
def whyrun_supported?
true
end
end
16 changes: 16 additions & 0 deletions resources/install_noop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
provides :nssm_install

property :source, identity: true, name_attribute: true
property :sha256, kind_of: String, required: true

default_action :install

action :install do
::Chef::Log.warn('NSSM service can only be installed on Windows platforms!')
end

action_class do
def whyrun_supported?
true
end
end
29 changes: 18 additions & 11 deletions resources/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
end

action :install do
install_nssm
nssm_install 'Install NSSM' do
source node['nssm']['src']
sha256 node['nssm']['sha256']
only_if { node['nssm']['install_nssm'] }
end

# Declare the service for start notification
service new_resource.servicename
Expand All @@ -39,13 +43,24 @@
only_if { current_resource && current_resource.nssm_binary != new_resource.nssm_binary }
end

params = new_resource.parameters.merge(Application: new_resource.program, AppParameters: new_resource.args)
params.map do |key, value|
params = new_resource.parameters.merge(Application: new_resource.program)
params = params.merge(AppParameters: new_resource.args) unless new_resource.args.nil?
params.each do |key, value|
execute "Set parameter #{key} to #{value}" do
command ::NSSM.command(new_resource.nssm_binary, :set, new_resource.servicename, key, value)
not_if { current_resource && current_resource.parameters[key] == ::NSSM.prepare_parameter(value) }
end
end

# Some NSSM parameters have no meaningful default, list them here to prevent errors on reset command
params_no_default = ' Application AppDirectory DisplayName ObjectName Start Type '

current_resource.parameters.each do |key, _value|
execute "Reset parameter #{key} to default" do
command ::NSSM.command(new_resource.nssm_binary, :reset, new_resource.servicename, key)
not_if { params.key?(key.to_sym) || params_no_default.include?(key) }
end
end unless current_resource.nil?
end

action :install_if_missing do
Expand Down Expand Up @@ -81,12 +96,4 @@
def whyrun_supported?
true
end

# TODO: Move this into a dedicated resource
def install_nssm
return if run_context.loaded_recipe? 'nssm::default'
recipe_eval do
run_context.include_recipe 'nssm::default'
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

CACHE = Chef::Config[:file_cache_path]
VERSION = '2.24-94-g9c88bc1'.freeze
SHA256 = '0bbe25025b69ebd8ab263ec4b443513d28a0d072e5fdd9b5cdb327359a27f96e'.freeze

def stub_win32_service_class
return if defined?(::Win32::Service) && ::Win32::Service.is_a?(::RSpec::Mocks::Double)
Expand Down
11 changes: 9 additions & 2 deletions spec/unit/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
context 'windows' do
let(:chef_run) do
ChefSpec::SoloRunner.new(
file_cache_path: CACHE, platform: 'windows', version: '2008R2'
file_cache_path: CACHE, platform: 'windows', version: '2008R2', step_into: ['nssm_install']
) do
ENV['WINDIR'] = 'C:\tmp'
end.converge(described_recipe)
end

it 'calls nssm_install resource' do
expect(chef_run).to install_nssm_install('Install NSSM').with(
source: "https://nssm.cc/ci/nssm-#{VERSION}.zip",
sha256: SHA256
)
end

it 'download nssm' do
expect(chef_run).to unzip_windows_zipfile('download nssm').with(
path: CACHE,
Expand All @@ -21,7 +28,7 @@

it 'install nssm' do
expect(chef_run).to create_remote_file('install nssm').with(
path: 'C:\tmp\nssm-2.24-94-g9c88bc1.exe',
path: "C:\\tmp\\nssm-#{VERSION}.exe",
source: "file:///#{CACHE}/nssm-2.24-94-g9c88bc1/win64/nssm.exe"
)
end
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/install_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@
it 'installs selenium' do
expect(chef_run).to create_remote_file(::File.join(cache_dir, 'selenium-server-standalone-2.53.0.jar'))
end

it 'calls nssm install resource' do
expect(chef_run).to install_nssm('service name')
end
end
end
end

0 comments on commit bb3cf1a

Please sign in to comment.