Skip to content

Commit

Permalink
more testing and implemntation
Browse files Browse the repository at this point in the history
  • Loading branch information
smith committed Jun 23, 2011
1 parent 0944af5 commit d047f91
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 9 deletions.
2 changes: 2 additions & 0 deletions data/export/supervisor/group.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[group:<%= app %>]
programs=<%= programs %>
12 changes: 12 additions & 0 deletions data/export/supervisor/program.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[program:<%= app %>-<%= program.name %>]
command=<%= program.command %>
<% if num_procs > 1 %>
process_name=%(program_name)-%(process_num)
num_procs=<%= num_procs %>
<% end %>
directory=<%= engine.directory %>
<% if user %>user=<%= user %><% end %>
<% if log_root %>
stdout_logfile=<%= log_root %>/%(program_name)-%(process_num)-stdout.log
stderr_logfile=<%= log_root %>/%(program_name)-%(process_num)-stderr.log
<% end %>
5 changes: 3 additions & 2 deletions lib/foreman/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def export(format, location=nil)
check_procfile!

formatter = case format
when "inittab" then Foreman::Export::Inittab
when "upstart" then Foreman::Export::Upstart
when "inittab" then Foreman::Export::Inittab
when "supervisor" then Foreman::Export::Supervisor
when "upstart" then Foreman::Export::Upstart
else error "Unknown export format: #{format}."
end

Expand Down
1 change: 1 addition & 0 deletions lib/foreman/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ class Exception < ::Exception; end
require "foreman/export/base"
require "foreman/export/inittab"
require "foreman/export/upstart"
require "foreman/export/supervisor"
31 changes: 30 additions & 1 deletion lib/foreman/export/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,36 @@

class Foreman::Export::Supervisor < Foreman::Export::Base

def export(location, options=[])
def export(location="/etc/supervisor/conf.d", options={})
FileUtils.mkdir_p location
app = options[:app] || File.basename(engine.directory)
processes = engine.processes.values

# App group
programs = processes.map {|v| "#{app}-#{v.name}" }.join(",")
group_template = export_template("supervisor/group.conf.erb")
group_config = ERB.new(group_template).result(binding)
write_file "#{location}/#{app}.conf", group_config

# Individual programs
user = options[:user] || app
log_root = options[:log]
program_template = export_template("supervisor/program.conf.erb")
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])

processes.each do |program|
num_procs = concurrency[program.name] || 1
program_config = ERB.new(program_template, nil, "<>").result(binding)
write_file "#{location}/#{app}-#{program.name}.conf", program_config
end

ctl "update"
end

private ######################################################################

# Shortcut to run supervisorctl
def ctl(cmd="")
system("supervisorctl #{cmd}") unless cmd.empty?
end
end
9 changes: 3 additions & 6 deletions spec/foreman/export/supervisor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
it "exports to the filesystem" do
supervisor.export("/tmp/init")

#File.read("/tmp/init/app.conf").should == example_export_file("supervisor/app.conf")
#File.read("/tmp/init/app-alpha.conf").should == example_export_file("supervisor/app-alpha.conf")
#File.read("/tmp/init/app-alpha-1.conf").should == example_export_file("supervisor/app-alpha-1.conf")
#File.read("/tmp/init/app-alpha-2.conf").should == example_export_file("supervisor/app-alpha-2.conf")
#File.read("/tmp/init/app-bravo.conf").should == example_export_file("supervisor/app-bravo.conf")
#File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("supervisor/app-bravo-1.conf")
File.read("/tmp/init/app.conf").should == example_export_file("supervisor/app.conf")
File.read("/tmp/init/app-alpha.conf").should == example_export_file("supervisor/app-alpha.conf")
File.read("/tmp/init/app-bravo.conf").should == example_export_file("supervisor/app-bravo.conf")
end
end
4 changes: 4 additions & 0 deletions spec/resources/export/supervisor/app-alpha.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[program:app-alpha]
command=./alpha
directory=/tmp/app
user=app
6 changes: 6 additions & 0 deletions spec/resources/export/supervisor/app-bravo.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[program:app-bravo]
command=./bravo
directory=/tmp/app
process_name=%(program_name)-%(process_num)
num_procs=2
user=app
2 changes: 2 additions & 0 deletions spec/resources/export/supervisor/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[group:app]
programs=app-alpha,app-bravo

0 comments on commit d047f91

Please sign in to comment.