Foreman::Export::

Daemontools

class

A Foreman exporter for daemontools. This exports processes from the Procfile as a hierarchy of directories intended to be run under ‘supervise’

Some of this code was borrowed from the ‘runit’ exporter.

Constants

DEFAULT_DATADIR

The data directory in the project if that exists, otherwise the gem datadir

ENV_VARIABLE_REGEX

Pattern used to extract inline env variables from the command

HOME_TEMPLATEDIR

Directory to look in for personal templates

REVISION

Version-control revision constant

VERSION

Library version constant

Attributes

datadir[RW]
logger[RW]

The Logger object that gets exporter output

template_search_path[RW]

The list of directories to search in for templates

Public Instance Methods

export()

Main API method – export the loaded Procfile as supervise service directories

# File lib/foreman/export/daemontools.rb, line 72
def export
        servicedir = self.location or
                raise Foreman::Export::Exception, "No service directory specified."
        servicedir = Pathname( servicedir )
        app        = self.app || File.basename( self.engine.directory )
        user       = self.user || app

        unless servicedir.exist?
                say "Creating #{servicedir}..."
                servicedir.mkpath
        end

    engine.procfile.entries.each do |process|
                say "Setting up %s-%s service directories..." % [ app, process.name ]
                count   = self.concurrency[ process.name ]
                say "  concurrency = #{count}"
                procdir = servicedir + "%s-%s" % [ app, process.name ]

                # Create a numbered service dir for each instance if there are
                # more than one
                if count != 1
                        1.upto( count ) do |i|
                                self.write_servicedir( process, Pathname(procdir.to_s + "-#{i}"), user, i )
                        end
                else
                        self.write_servicedir( process, procdir, user )
                end
        end
end
load_template( name )

Load the daemontools template for the file named name, and return it as an ERB object.

# File lib/foreman/export/daemontools.rb, line 144
def load_template( name )
        template_name = "#{name}.erb"
        template = self.template_search_path.
                map {|dir| dir + template_name }.
                find {|tmpl| tmpl.exist? }

        template or raise Foreman::Export::Exception,
                "Can't find the %p template in any of: %p" %
                [ name, self.template_search_path.map(&:to_s) ]

        erbtmpl = ERB.new( template.read, nil, '<%>' )
end
write_servicedir( process, procdir, user, num=1 )

Write a supervise directory to targetdir

# File lib/foreman/export/daemontools.rb, line 104
def write_servicedir( process, procdir, user, num=1  )
        say "Making directory %s..." % [ procdir ]
        procdir.mkpath

        # Write the down file to keep the service from spinning up before the user has
        # a chance to look things over
        write_file( procdir + 'down', '' )

        # Set up logging
        say "  setting up logging..."
        logdir = procdir + 'log'
        logdir.mkpath
        logruntmpl = self.load_template( 'log-run' )
        runfile = logdir + 'run'
        write_file( runfile, logruntmpl.result(binding()) )
        runfile.chmod( 0755 )

        # Set up the envdir
        say "  setting up environment variables..."
        envdir = procdir + 'env'
        envdir.mkpath
port = engine.port_for( process, num, self.port )
        environment_variables = { 'PORT' => port }.
                merge( engine.environment ).
                merge( inline_variables(process.command) )
        environment_variables.each_pair do |var, env|
                write_file( envdir + var, env )
        end

        # Set up the runfile
        runtmpl = self.load_template( 'run' )
        runfile = procdir + 'run'
        write_file( runfile, runtmpl.result(binding()) )
        runfile.chmod( 0755 )

end

Protected Instance Methods

initialize( location, engine, options={} )

Set up the template root

# File lib/foreman/export/daemontools.rb, line 50
def initialize( location, engine, options={} ) # :notnew:
        super
        @logger = Logger.new( $stderr )
        @template_search_path = [ HOME_TEMPLATEDIR, DEFAULT_DATADIR + 'templates' ]
        @template_search_path.unshift( Pathname(options[:template]) ) if options.key?( :template )
end
say( message )

Override to output to the logger instead of STDERR.

# File lib/foreman/export/daemontools.rb, line 163
def say( message )
        @logger.info( '[foreman export]' ) { message }
end