@@ -31,10 +31,13 @@ def action_enable
31
31
Chef ::Application . fatal! ( 'The Consul Service provider for Windows only supports the binary install_method at this time' )
32
32
end
33
33
34
- %W{ #{ new_resource . data_dir }
35
- #{ new_resource . config_dir }
36
- #{ ::File . dirname ( new_resource . nssm_params [ 'AppStdout' ] ) }
37
- #{ ::File . dirname ( new_resource . nssm_params [ 'AppStderr' ] ) } } . uniq . each do |dirname |
34
+ directories = %W{ #{ new_resource . data_dir }
35
+ #{ new_resource . config_dir }
36
+ #{ ::File . dirname ( new_resource . nssm_params [ 'AppStdout' ] ) }
37
+ #{ ::File . dirname ( new_resource . nssm_params [ 'AppStderr' ] ) } } . uniq . compact
38
+
39
+ # ::File.dirname '' == '.'
40
+ directories . delete_if { |i | i . eql? '.' } . each do |dirname |
38
41
directory dirname do
39
42
recursive true
40
43
# owner new_resource.user
@@ -46,17 +49,20 @@ def action_enable
46
49
nssm 'consul' do
47
50
action :install
48
51
program join_path ( new_resource . install_path , 'consul.exe' )
49
- params new_resource . nssm_params
52
+ # Don't try and set empty parameters
53
+ params new_resource . nssm_params . select { |_k , v | v != '' }
50
54
args command ( new_resource . config_file , new_resource . config_dir )
55
+ not_if { nssm_service_installed? }
51
56
end
52
57
53
58
if nssm_service_installed?
54
59
# The nssm resource does not check param values after they've been set
55
60
mismatch_params = check_nssm_params
56
61
unless mismatch_params . empty?
57
62
mismatch_params . each do |k , v |
63
+ action = v . eql? ( '' ) ? "reset consul #{ k } " : "set consul #{ k } #{ v } "
58
64
batch "Set nssm parameter - #{ k } " do
59
- code "#{ nssm_exe } set consul #{ k } #{ v } "
65
+ code "#{ nssm_exe } #{ action } "
60
66
notifies :run , 'batch[Trigger consul restart]' , :delayed
61
67
end
62
68
end
@@ -67,7 +73,7 @@ def action_enable
67
73
end
68
74
# Check if the service is running, but don't bother if we're already
69
75
# changing some nssm parameters
70
- unless nssm_service_running? && mismatch_params . empty?
76
+ unless nssm_service_status? ( %w{ SERVICE_RUNNING } ) && mismatch_params . empty?
71
77
batch 'Trigger consul restart' do
72
78
action :run
73
79
code "#{ nssm_exe } restart consul"
@@ -85,6 +91,13 @@ def action_restart
85
91
86
92
def action_disable
87
93
notifying_block do
94
+ # nssm resource doesn't stop the service before it removes it
95
+ batch 'Stop consul' do
96
+ action :run
97
+ code "#{ nssm_exe } stop consul"
98
+ only_if { nssm_service_installed? && nssm_service_status? ( %w{ SERVICE_RUNNING SERVICE_PAUSED } ) }
99
+ end
100
+
88
101
nssm 'consul' do
89
102
action :remove
90
103
end
0 commit comments