Skip to content

Commit

Permalink
[COOK-3663] Add ./check scripts support
Browse files Browse the repository at this point in the history
Signed-off-by: Seth Vargo <sethvargo@gmail.com>
  • Loading branch information
zuazo authored and sethvargo committed Oct 3, 2013
1 parent 59f0574 commit 49f253c
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ Many of these parameters are only used in the `:enable` action.
- **cookbook** - A cookbook where templates are located instead of
where the resource is used. Applies for all the templates in the
`enable` action.
- **check** - whether the service has a check script, requires a
template `sv-service_name-check.erb`
- **finish** - whether the service has a finish script, requires a
template `sv-service_name-finish.erb`
- **control** - An array of signals to customize control of the service,
Expand All @@ -150,6 +152,8 @@ Many of these parameters are only used in the `:enable` action.
use replacing `service_name`.
- **log_template_name** - alternate filename of the log run script to
use replacing `service_name`.
- **check_script_template_name** - alternate filename of the check
script to use, replacing `service_name`.
- **finish_script_template_name** - alternate filename of the finish
script to use, replacing `service_name`.
- **control_template_names** - a hash of control signals (see *control*
Expand Down Expand Up @@ -228,6 +232,18 @@ runit_service "no-svlog" do
end
```

**Check Script**

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.

```ruby
runit_service "checker" do
check true
end
```

This will create `/etc/sv/checker/check`.

**Finish Script**

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.
Expand Down
22 changes: 22 additions & 0 deletions libraries/provider_runit_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def initialize(*args)
@log_config_file = nil
@env_dir = nil
@env_files = nil
@check_script = nil
@finish_script = nil
@control_dir = nil
@control_signal_files = nil
Expand Down Expand Up @@ -124,6 +125,13 @@ def configure_service
Chef::Log.debug("Environment not specified for #{new_resource.service_name}, continuing")
end

if new_resource.check
Chef::Log.debug("Creating check script for #{new_resource.service_name}")
check_script.run_action(:create)
else
Chef::Log.debug("Check script not specified for #{new_resource.service_name}, continuing")
end

if new_resource.finish
Chef::Log.debug("Creating finish script for #{new_resource.service_name}")
finish_script.run_action(:create)
Expand Down Expand Up @@ -407,6 +415,20 @@ def env_files
@env_files
end

def check_script
return @check_script unless @check_script.nil?
@check_script = Chef::Resource::Template.new(::File.join(sv_dir_name, 'check'), run_context)
@check_script.owner(new_resource.owner)
@check_script.group(new_resource.group)
@check_script.source("sv-#{new_resource.check_script_template_name}-check.erb")
@check_script.cookbook(template_cookbook)
@check_script.mode(00755)
if new_resource.options.respond_to?(:has_key?)
@check_script.variables(:options => new_resource.options)
end
@check_script
end

def finish_script
return @finish_script unless @finish_script.nil?
@finish_script = Chef::Resource::Template.new(::File.join(sv_dir_name, 'finish'), run_context)
Expand Down
10 changes: 10 additions & 0 deletions libraries/resource_runit_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def initialize(name, run_context=nil)
@env = {}
@log = true
@cookbook = nil
@check = false
@finish = false
@owner = nil
@group = nil
Expand All @@ -55,6 +56,7 @@ def initialize(name, run_context=nil)
@restart_on_update = true
@run_template_name = @service_name
@log_template_name = @service_name
@check_script_template_name = @service_name
@finish_script_template_name = @service_name
@control_template_names = {}
@status_command = "#{@sv_bin} status #{@service_dir}"
Expand Down Expand Up @@ -139,6 +141,10 @@ def finish(arg=nil)
set_or_return(:finish, arg, :kind_of => [TrueClass, FalseClass])
end

def check(arg=nil)
set_or_return(:check, arg, :kind_of => [TrueClass, FalseClass])
end

def owner(arg=nil)
set_or_return(:owner, arg, :regex => [Chef::Config[:user_valid_regex]])
end
Expand All @@ -164,6 +170,10 @@ def log_template_name(arg=nil)
set_or_return(:log_template_name, arg, :kind_of => [String])
end

def check_script_template_name(arg=nil)
set_or_return(:check_script_template_name, arg, :kind_of => [String])
end

def finish_script_template_name(arg=nil)
set_or_return(:finish_script_template_name, arg, :kind_of => [String])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
file('/etc/service/default-svlog/log/run').must_match(regexp)
end

it 'creates a service that has a check script' do
service('checker').must_be_running
file('/etc/service/checker/check').must_exist
end

it 'creates a service that has a finish script' do
service('finisher').must_be_running
file('/etc/service/finisher/finish').must_exist
Expand Down
6 changes: 6 additions & 0 deletions test/cookbooks/runit_test/recipes/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
default_logger true
end

# Create a service that has a check script
runit_service "checker" do
check true
end

# Create a service that has a finish script
runit_service "finisher" do
finish true
Expand Down Expand Up @@ -92,6 +97,7 @@
# Create a service with differently named template files
runit_service "yerba" do
log_template_name "yerba-matte"
check_script_template_name "yerba-matte"
finish_script_template_name "yerba-matte"
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

lsof -itcp:6709
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec svlogd -tt ./main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
exec 2>&1
exec nc -l 6709
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

lsof -itcp:6711
9 changes: 9 additions & 0 deletions test/spec/libraries/provider_runit_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@
provider.send(:env_files)[0].content.should eq('$PATH:/usr/local/bin')
end

it 'creates a check script as a template if check_script parameter is true' do
provider.send(:check_script).path.should eq(::File.join(sv_dir_name, 'check'))
provider.send(:check_script).owner.should eq(new_resource.owner)
provider.send(:check_script).group.should eq(new_resource.group)
provider.send(:check_script).mode.should eq(00755)
provider.send(:check_script).source.should eq("sv-#{new_resource.check_script_template_name}-check.erb")
provider.send(:check_script).cookbook.should be_empty
end

it 'creates a finish script as a template if finish_script parameter is true' do
provider.send(:finish_script).path.should eq(::File.join(sv_dir_name, 'finish'))
provider.send(:finish_script).owner.should eq(new_resource.owner)
Expand Down
18 changes: 18 additions & 0 deletions test/spec/libraries/resource_runit_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@
resource.cookbook.should eq('noodles')
end

it 'has a check parameter that is false by default' do
resource.check.should be_false
end

it 'hash a check parameter that controls whether a check script is created' do
resource.check(true)
resource.check.should be_true
end

it 'has a finish parameter that is false by default' do
resource.finish.should be_false
end
Expand Down Expand Up @@ -228,6 +237,15 @@
resource.control_template_names['u'].should eq('noodle_up')
end

it 'sets the check_script_template_name to the service_name by default' do
resource.check_script_template_name.should eq(resource.service_name)
end

it 'has a check_script_template_name parameter to allow a custom template name for the check script' do
resource.check_script_template_name('eat_bananas')
resource.check_script_template_name.should eq('eat_bananas')
end

it 'sets the finish_script_template_name to the service_name by default' do
resource.finish_script_template_name.should eq(resource.service_name)
end
Expand Down

0 comments on commit 49f253c

Please sign in to comment.