Module: RunitRunnerHelper

Defined in:
lib/runit_runner_helper.rb

Overview

This module provides methods to ease creating run scripts in service directories.

The methods are available through runner. in your deploy.rb

Instance Method Summary collapse

Instance Method Details

#create(service_dir, template, options = {}) ⇒ Object

Creates a runner from a template, puts it on the server at the given path and sets file permissions to 0700.

  • service_dir should be the path to your service directory, it should not include run or log/run, these are added by create.
  • template can be :log, :mongrel, :fcgi, the name of a file or a string. :log, :mongrel and :fcgi create the standard templates, see get_template below for a list of places searched for template files.
  • If you're making a custom log runner, include :log_runner => true in the options so create knows to put the contents in service_dir/log/run, you
    don't need this if you're using the standard :log template.
  • options except for :log_runner are assumed to be for the template and are passed through to render.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/runit_runner_helper.rb', line 25

def create(service_dir, template, options = {})
  case template
    when :log
      template = 'default_log_runner'
      options.merge!(:log_runner => true)
    when :fcgi
      template = 'default_fcgi_runner'
    when :mongrel
      template = 'default_mongrel_runner'
  end
  
  path = "#{service_dir}/#{options[:log_runner] ? 'log/run' : 'run'}" 
  options.delete(:log_runner)
  
  options = add_default_options(options)

  runner = render options.merge(:template => get_template(template))

  put runner, path, :mode => 0700    
end

#get_template(template) ⇒ Object

Works out whether the given template is a file or string containing rhtml.

Checks:

  • current directory
  • runner_templates path (defaults to templates/runit)
  • capistrano-runit-tasks-templates dir where cappy-runit is installed (the standard templates are here)

If it can't find the file it assumes it's a one line string template and returns that



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/runit_runner_helper.rb', line 56

def get_template(template)
  if template =~ /<%/ or template =~ /\n/
    template
  else
    [ ".",
      configuration.runner_template_path,
      File.join(File.dirname(__FILE__), 'capistrano-runit-tasks-templates')
    ].each do |dir|
      if File.file?(File.join(dir, template))
        return File.read(File.join(dir, template))
      elsif File.file?(File.join(dir, template + ".rhtml"))
        @file_template_full_path = File.join(dir, template + ".rhtml")
        return File.read(File.join(dir, template + ".rhtml"))
      end
    end
  end
  template
end