Skip to content

Commit

Permalink
add -p flag to specify the base port for apps
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Jul 1, 2010
1 parent 9848651 commit 3151663
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion export/upstart/process.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ stop on stopping <%= app %>-<%= process.name %>
respawn

chdir <%= engine.directory %>
exec su <%= user %> -c "<%= process.command %> >> <%= log_root %>/<%=process.name%>-<%=num%>.log 2>&1"
exec su <%= user %> -c "PORT=<%= port %> <%= process.command %> >> <%= log_root %>/<%=process.name%>-<%=num%>.log 2>&1"
17 changes: 7 additions & 10 deletions lib/foreman/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def start(process=nil)

desc "export FORMAT LOCATION", "Export the application to another process management format"

method_option :app, :type => :string, :aliases => "-a"
method_option :log, :type => :string, :aliases => "-l"
method_option :user, :type => :string, :aliases => "-u"
method_option :concurrency, :type => :string, :aliases => "-c",
method_option :app, :type => :string, :aliases => "-a"
method_option :log, :type => :string, :aliases => "-l"
method_option :port, :type => :numeric, :aliases => "-p"
method_option :user, :type => :string, :aliases => "-u"
method_option :concurrency, :type => :string, :aliases => "-c",
:banner => '"alpha=5,bar=3"'

def export(format, location=nil)
Expand All @@ -40,12 +41,8 @@ def export(format, location=nil)
else error "Unknown export format: #{format}."
end

formatter.new(engine).export(location,
:name => options[:app],
:user => options[:user],
:log => options[:log],
:concurrency => options[:concurrency]
)
formatter.new(engine).export(location, options)

rescue Foreman::Export::Exception => ex
error ex.message
end
Expand Down
6 changes: 6 additions & 0 deletions lib/foreman/export/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def parse_concurrency(concurrency)
end
end

def port_for(base_port, app, num)
base_port ||= 5000
offset = engine.processes.keys.sort.index(app) * 100
base_port.to_i + offset + num - 1
end

def write_file(filename, contents)
say "writing: #{filename}"

Expand Down
13 changes: 10 additions & 3 deletions lib/foreman/export/inittab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ def export(fname=nil, options={})

inittab = []
inittab << "# ----- foreman #{app} processes -----"
engine.processes.values.each_with_index do |process, num|
id = app.slice(0, 2).upcase + sprintf("%02d", num+1)
inittab << "#{id}:4:respawn:/bin/su - #{user} -c '#{process.command} >> #{log_root}/#{process.name}-#{num+1}.log 2>&1'"

engine.processes.values.inject(1) do |index, process|
1.upto(concurrency[process.name]) do |num|
id = app.slice(0, 2).upcase + sprintf("%02d", index)
port = port_for(options[:port], process.name, num)
inittab << "#{id}:4:respawn:/bin/su - #{user} -c 'PORT=#{port} #{process.command} >> #{log_root}/#{process.name}-#{num}.log 2>&1'"
index += 1
end
index
end

inittab << "# ----- end foreman #{app} processes -----"

inittab = inittab.join("\n") + "\n"
Expand Down
1 change: 1 addition & 0 deletions lib/foreman/export/upstart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def export(location, options={})
write_file "#{location}/#{app}-#{process.name}.conf", process_master_config

1.upto(concurrency[process.name]) do |num|
port = port_for(options[:port], process.name, num)
process_config = ERB.new(process_template).result(binding)
write_file "#{location}/#{app}-#{process.name}-#{num}.conf", process_config
end
Expand Down
10 changes: 7 additions & 3 deletions man/foreman.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ The following options control how the application is run:
Use this name rather than the application's root directory name as the
name of the application when exporting.

* `-l`, `--log`:
Specify the directory to place process logs in.

* `-c`, `--concurrency`:
Specify the number of each process type to run. The value passed in
should be in the format `process=num,process=num`

* `-l`, `--log`:
Specify the directory to place process logs in.

* `-p`, `--port`:
Specify which port to use as the base for this application. Should be
a multiple of 1000.

* `-u`, `--user`:
Specify the user the application should be run as. Defaults to the
app name
Expand Down

0 comments on commit 3151663

Please sign in to comment.