Skip to content

Commit 49f253c

Browse files
zuazosethvargo
authored andcommitted
[COOK-3663] Add ./check scripts support
Signed-off-by: Seth Vargo <[email protected]>
1 parent 59f0574 commit 49f253c

File tree

11 files changed

+97
-0
lines changed

11 files changed

+97
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Many of these parameters are only used in the `:enable` action.
136136
- **cookbook** - A cookbook where templates are located instead of
137137
where the resource is used. Applies for all the templates in the
138138
`enable` action.
139+
- **check** - whether the service has a check script, requires a
140+
template `sv-service_name-check.erb`
139141
- **finish** - whether the service has a finish script, requires a
140142
template `sv-service_name-finish.erb`
141143
- **control** - An array of signals to customize control of the service,
@@ -150,6 +152,8 @@ Many of these parameters are only used in the `:enable` action.
150152
use replacing `service_name`.
151153
- **log_template_name** - alternate filename of the log run script to
152154
use replacing `service_name`.
155+
- **check_script_template_name** - alternate filename of the check
156+
script to use, replacing `service_name`.
153157
- **finish_script_template_name** - alternate filename of the finish
154158
script to use, replacing `service_name`.
155159
- **control_template_names** - a hash of control signals (see *control*
@@ -228,6 +232,18 @@ runit_service "no-svlog" do
228232
end
229233
```
230234

235+
**Check Script**
236+
237+
To create a service that has a check script in its service directory, set the `check` parameter to `true`, and create a `sv-checker-check.erb` template.
238+
239+
```ruby
240+
runit_service "checker" do
241+
check true
242+
end
243+
```
244+
245+
This will create `/etc/sv/checker/check`.
246+
231247
**Finish Script**
232248

233249
To create a service that has a finish script in its service directory, set the `finish` parameter to `true`, and create a `sv-finisher-finish.erb` template.

libraries/provider_runit_service.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def initialize(*args)
4747
@log_config_file = nil
4848
@env_dir = nil
4949
@env_files = nil
50+
@check_script = nil
5051
@finish_script = nil
5152
@control_dir = nil
5253
@control_signal_files = nil
@@ -124,6 +125,13 @@ def configure_service
124125
Chef::Log.debug("Environment not specified for #{new_resource.service_name}, continuing")
125126
end
126127

128+
if new_resource.check
129+
Chef::Log.debug("Creating check script for #{new_resource.service_name}")
130+
check_script.run_action(:create)
131+
else
132+
Chef::Log.debug("Check script not specified for #{new_resource.service_name}, continuing")
133+
end
134+
127135
if new_resource.finish
128136
Chef::Log.debug("Creating finish script for #{new_resource.service_name}")
129137
finish_script.run_action(:create)
@@ -407,6 +415,20 @@ def env_files
407415
@env_files
408416
end
409417

418+
def check_script
419+
return @check_script unless @check_script.nil?
420+
@check_script = Chef::Resource::Template.new(::File.join(sv_dir_name, 'check'), run_context)
421+
@check_script.owner(new_resource.owner)
422+
@check_script.group(new_resource.group)
423+
@check_script.source("sv-#{new_resource.check_script_template_name}-check.erb")
424+
@check_script.cookbook(template_cookbook)
425+
@check_script.mode(00755)
426+
if new_resource.options.respond_to?(:has_key?)
427+
@check_script.variables(:options => new_resource.options)
428+
end
429+
@check_script
430+
end
431+
410432
def finish_script
411433
return @finish_script unless @finish_script.nil?
412434
@finish_script = Chef::Resource::Template.new(::File.join(sv_dir_name, 'finish'), run_context)

libraries/resource_runit_service.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def initialize(name, run_context=nil)
4646
@env = {}
4747
@log = true
4848
@cookbook = nil
49+
@check = false
4950
@finish = false
5051
@owner = nil
5152
@group = nil
@@ -55,6 +56,7 @@ def initialize(name, run_context=nil)
5556
@restart_on_update = true
5657
@run_template_name = @service_name
5758
@log_template_name = @service_name
59+
@check_script_template_name = @service_name
5860
@finish_script_template_name = @service_name
5961
@control_template_names = {}
6062
@status_command = "#{@sv_bin} status #{@service_dir}"
@@ -139,6 +141,10 @@ def finish(arg=nil)
139141
set_or_return(:finish, arg, :kind_of => [TrueClass, FalseClass])
140142
end
141143

144+
def check(arg=nil)
145+
set_or_return(:check, arg, :kind_of => [TrueClass, FalseClass])
146+
end
147+
142148
def owner(arg=nil)
143149
set_or_return(:owner, arg, :regex => [Chef::Config[:user_valid_regex]])
144150
end
@@ -164,6 +170,10 @@ def log_template_name(arg=nil)
164170
set_or_return(:log_template_name, arg, :kind_of => [String])
165171
end
166172

173+
def check_script_template_name(arg=nil)
174+
set_or_return(:check_script_template_name, arg, :kind_of => [String])
175+
end
176+
167177
def finish_script_template_name(arg=nil)
168178
set_or_return(:finish_script_template_name, arg, :kind_of => [String])
169179
end

test/cookbooks/runit_test/files/default/tests/minitest/service_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
file('/etc/service/default-svlog/log/run').must_match(regexp)
4545
end
4646

47+
it 'creates a service that has a check script' do
48+
service('checker').must_be_running
49+
file('/etc/service/checker/check').must_exist
50+
end
51+
4752
it 'creates a service that has a finish script' do
4853
service('finisher').must_be_running
4954
file('/etc/service/finisher/finish').must_exist

test/cookbooks/runit_test/recipes/service.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
default_logger true
5959
end
6060

61+
# Create a service that has a check script
62+
runit_service "checker" do
63+
check true
64+
end
65+
6166
# Create a service that has a finish script
6267
runit_service "finisher" do
6368
finish true
@@ -92,6 +97,7 @@
9297
# Create a service with differently named template files
9398
runit_service "yerba" do
9499
log_template_name "yerba-matte"
100+
check_script_template_name "yerba-matte"
95101
finish_script_template_name "yerba-matte"
96102
end
97103

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
lsof -itcp:6709
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
exec svlogd -tt ./main
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
exec 2>&1
3+
exec nc -l 6709
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
lsof -itcp:6711

test/spec/libraries/provider_runit_service_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@
260260
provider.send(:env_files)[0].content.should eq('$PATH:/usr/local/bin')
261261
end
262262

263+
it 'creates a check script as a template if check_script parameter is true' do
264+
provider.send(:check_script).path.should eq(::File.join(sv_dir_name, 'check'))
265+
provider.send(:check_script).owner.should eq(new_resource.owner)
266+
provider.send(:check_script).group.should eq(new_resource.group)
267+
provider.send(:check_script).mode.should eq(00755)
268+
provider.send(:check_script).source.should eq("sv-#{new_resource.check_script_template_name}-check.erb")
269+
provider.send(:check_script).cookbook.should be_empty
270+
end
271+
263272
it 'creates a finish script as a template if finish_script parameter is true' do
264273
provider.send(:finish_script).path.should eq(::File.join(sv_dir_name, 'finish'))
265274
provider.send(:finish_script).owner.should eq(new_resource.owner)

0 commit comments

Comments
 (0)