Skip to content

Commit

Permalink
Merge pull request #263 from visioncritical/nssm-param-reset
Browse files Browse the repository at this point in the history
Added ability to remove nssm parameters
  • Loading branch information
johnbellone committed Feb 3, 2016
2 parents 19b551b + aac210a commit 257a086
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
default['consul']['config']['bag_item'] = 'consul'

default['consul']['config']['path'] = join_path config_prefix_path, 'consul.json'
default['consul']['config']['data_dir'] = join_path data_prefix_path, 'data'
default['consul']['config']['data_dir'] = data_prefix_path
default['consul']['config']['ca_file'] = join_path config_prefix_path, 'ssl', 'CA', 'ca.crt'
default['consul']['config']['cert_file'] = join_path config_prefix_path, 'ssl', 'certs', 'consul.crt'
default['consul']['config']['key_file'] = join_path config_prefix_path, 'ssl', 'private', 'consul.key'
Expand All @@ -33,6 +33,8 @@
default['consul']['diplomat_version'] = nil

default['consul']['service']['config_dir'] = join_path config_prefix_path, 'conf.d'

default['consul']['service']['install_path'] = windows? ? config_prefix_path : '/srv'
default['consul']['service']['install_method'] = 'binary'
default['consul']['service']['binary_url'] = "https://releases.hashicorp.com/consul/%{version}/%{filename}.zip" # rubocop:disable Style/StringLiterals

Expand Down
3 changes: 1 addition & 2 deletions libraries/consul_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConsulService < Chef::Resource

# @!attribute install_path
# @return [String]
attribute(:install_path, kind_of: String, default: lazy { windows? ? config_prefix_path : '/srv' })
attribute(:install_path, kind_of: String, default: lazy { node['consul']['service']['install_path'] })

# @!attribute config_file
# @return [String]
Expand Down Expand Up @@ -81,7 +81,6 @@ class ConsulService < Chef::Provider
include ConsulCookbook::Helpers

def action_enable
new_resource.notifies(:reload, new_resource, :delayed)
notifying_block do
case new_resource.install_method
when 'package'
Expand Down
27 changes: 20 additions & 7 deletions libraries/consul_service_windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ def action_enable
Chef::Application.fatal!('The Consul Service provider for Windows only supports the binary install_method at this time')
end

%W{#{new_resource.data_dir}
#{new_resource.config_dir}
#{::File.dirname(new_resource.nssm_params['AppStdout'])}
#{::File.dirname(new_resource.nssm_params['AppStderr'])}}.uniq.each do |dirname|
directories = %W{#{new_resource.data_dir}
#{new_resource.config_dir}
#{::File.dirname(new_resource.nssm_params['AppStdout'])}
#{::File.dirname(new_resource.nssm_params['AppStderr'])}}.uniq.compact

# ::File.dirname '' == '.'
directories.delete_if { |i| i.eql? '.' }.each do |dirname|
directory dirname do
recursive true
# owner new_resource.user
Expand All @@ -46,17 +49,20 @@ def action_enable
nssm 'consul' do
action :install
program join_path(new_resource.install_path, 'consul.exe')
params new_resource.nssm_params
# Don't try and set empty parameters
params new_resource.nssm_params.select { |_k, v| v != '' }
args command(new_resource.config_file, new_resource.config_dir)
not_if { nssm_service_installed? }
end

if nssm_service_installed?
# The nssm resource does not check param values after they've been set
mismatch_params = check_nssm_params
unless mismatch_params.empty?
mismatch_params.each do |k, v|
action = v.eql?('') ? "reset consul #{k}" : "set consul #{k} #{v}"
batch "Set nssm parameter - #{k}" do
code "#{nssm_exe} set consul #{k} #{v}"
code "#{nssm_exe} #{action}"
notifies :run, 'batch[Trigger consul restart]', :delayed
end
end
Expand All @@ -67,7 +73,7 @@ def action_enable
end
# Check if the service is running, but don't bother if we're already
# changing some nssm parameters
unless nssm_service_running? && mismatch_params.empty?
unless nssm_service_status?(%w{SERVICE_RUNNING}) && mismatch_params.empty?
batch 'Trigger consul restart' do
action :run
code "#{nssm_exe} restart consul"
Expand All @@ -85,6 +91,13 @@ def action_restart

def action_disable
notifying_block do
# nssm resource doesn't stop the service before it removes it
batch 'Stop consul' do
action :run
code "#{nssm_exe} stop consul"
only_if { nssm_service_installed? && nssm_service_status?(%w{SERVICE_RUNNING SERVICE_PAUSED}) }
end

nssm 'consul' do
action :remove
end
Expand Down
4 changes: 2 additions & 2 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def nssm_service_installed?
exit_code == 0 ? true : false
end

def nssm_service_running?
shell_out!(%{"#{nssm_exe}" status consul}, returns: [0]).stdout.delete("\0").strip.eql? 'SERVICE_RUNNING'
def nssm_service_status?(expected_status)
expected_status.include? shell_out!(%{"#{nssm_exe}" status consul}, returns: [0]).stdout.delete("\0").strip
end

# Returns a hash of mismatched params
Expand Down
2 changes: 1 addition & 1 deletion test/spec/libraries/consul_service_linux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
recipe 'consul::default'

it { expect(chef_run).to create_directory('/etc/consul/conf.d') }
it { is_expected.to create_directory('/var/lib/consul/data') }
it { is_expected.to create_directory('/var/lib/consul') }
end
end
2 changes: 1 addition & 1 deletion test/spec/libraries/consul_service_windows_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
recipe 'consul::default'

it { expect(chef_run).to create_directory('C:\Program Files\consul\conf.d') }
it { is_expected.to create_directory('C:\Program Files\consul\data') }
it { is_expected.to create_directory('C:\Program Files\consul') }
it { expect(chef_run).to install_nssm('consul').with(
program: 'C:\Program Files\consul\consul.exe',
args: 'agent -config-file="""C:\Program Files\consul\consul.json""" -config-dir="""C:\Program Files\consul\conf.d"""'
Expand Down

0 comments on commit 257a086

Please sign in to comment.