From 3151663f373b2c8e5b7de8aa7da37e16ce5db9ef Mon Sep 17 00:00:00 2001 From: David Dollar Date: Wed, 30 Jun 2010 21:32:26 -0400 Subject: [PATCH] add -p flag to specify the base port for apps --- export/upstart/process.conf.erb | 2 +- lib/foreman/cli.rb | 17 +++++++---------- lib/foreman/export/base.rb | 6 ++++++ lib/foreman/export/inittab.rb | 13 ++++++++++--- lib/foreman/export/upstart.rb | 1 + man/foreman.1.ronn | 10 +++++++--- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/export/upstart/process.conf.erb b/export/upstart/process.conf.erb index 12d1b82..40f73eb 100644 --- a/export/upstart/process.conf.erb +++ b/export/upstart/process.conf.erb @@ -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" diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 129fbb0..48d00d8 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -25,10 +25,11 @@ class Foreman::CLI < Thor 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) @@ -40,12 +41,8 @@ class Foreman::CLI < Thor 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 diff --git a/lib/foreman/export/base.rb b/lib/foreman/export/base.rb index 32ac57d..3af0f18 100644 --- a/lib/foreman/export/base.rb +++ b/lib/foreman/export/base.rb @@ -36,6 +36,12 @@ private ###################################################################### 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}" diff --git a/lib/foreman/export/inittab.rb b/lib/foreman/export/inittab.rb index f401682..d3f410f 100644 --- a/lib/foreman/export/inittab.rb +++ b/lib/foreman/export/inittab.rb @@ -11,10 +11,17 @@ class Foreman::Export::Inittab < Foreman::Export::Base 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" diff --git a/lib/foreman/export/upstart.rb b/lib/foreman/export/upstart.rb index 6b906b2..3a693ad 100644 --- a/lib/foreman/export/upstart.rb +++ b/lib/foreman/export/upstart.rb @@ -31,6 +31,7 @@ class Foreman::Export::Upstart < Foreman::Export::Base 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 diff --git a/man/foreman.1.ronn b/man/foreman.1.ronn index 728bb7b..c56a56c 100644 --- a/man/foreman.1.ronn +++ b/man/foreman.1.ronn @@ -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