Class: Foreman::Export::Daemontools

Inherits:
Base
  • Object
show all
Defined in:
lib/foreman/export/daemontools.rb

Overview

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.

Constant Summary collapse

VERSION =

Library version constant

'0.1.0'
REVISION =

Version-control revision constant

%q$Revision: 7220ff1516d7 $
DEFAULT_DATADIR =

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

if ENV['FOREMAN_EXPORT_DATADIR']
	Pathname( ENV['FOREMAN_EXPORT_DATADIR'] )
elsif File.directory?( 'data/foreman-export-daemontools' )
	Pathname( 'data/foreman-export-daemontools' )
elsif path = Gem.datadir( 'foreman-export-daemontools' )
	Pathname( path )
else
	raise ScriptError, "can't find the data directory!"
end
HOME_TEMPLATEDIR =

Directory to look in for personal templates

Pathname( "~/.foreman/templates" ).expand_path
ENV_VARIABLE_REGEX =

Pattern used to extract inline env variables from the command

/([a-zA-Z_]+[a-zA-Z0-9_]*)=(\S+)/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, engine, options = {}) ⇒ Daemontools

Set up the template root



50
51
52
53
54
55
# 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

Class Attribute Details

.datadirObject

Returns the value of attribute datadir.



45
46
47
# File 'lib/foreman/export/daemontools.rb', line 45

def datadir
  @datadir
end

Instance Attribute Details

#loggerObject

The Logger object that gets exporter output



68
69
70
# File 'lib/foreman/export/daemontools.rb', line 68

def logger
  @logger
end

#template_search_pathObject

The list of directories to search in for templates



64
65
66
# File 'lib/foreman/export/daemontools.rb', line 64

def template_search_path
  @template_search_path
end

Instance Method Details

#exportObject

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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.each_process do |name, process|
		say "Setting up %s-%s service directories..." % [ app, name ]
		count   = engine.formation[ name ]
		say "  concurrency = #{count}"
		next unless count >= 1
		procdir = servicedir + "%s-%s" % [ app, 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) ⇒ Object

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



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/foreman/export/daemontools.rb', line 146

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) ⇒ Object

Write a supervise directory to targetdir



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/foreman/export/daemontools.rb', line 105

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
	say "  writing the 'down' file"
	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 )
	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