Compare commits

..

42 Commits

Author SHA1 Message Date
David Dollar 54b40d7e64 saving man page to github docs 2013-04-15 15:36:56 -04:00
David Dollar c10f51ad56 saving man page to github docs 2013-01-14 08:19:40 -05:00
David Dollar 2d6cbc282d saving man page to github docs 2012-10-08 10:47:40 -04:00
David Dollar 759eee41b7 saving man page to github docs 2012-10-08 10:31:29 -04:00
David Dollar 5c2907575b saving man page to github docs 2012-09-25 12:39:17 -05:00
David Dollar 733580dac6 saving man page to github docs 2012-08-21 10:59:21 -04:00
David Dollar 2aef4b91ea saving man page to github docs 2012-07-11 18:33:13 -04:00
David Dollar 6c07d8431f saving man page to github docs 2012-07-11 16:17:13 -04:00
David Dollar 47edd88f6e saving man page to github docs 2012-07-11 16:07:03 -04:00
David Dollar 16585d7993 saving man page to github docs 2012-05-02 13:21:02 -04:00
David Dollar 1f746769b8 saving man page to github docs 2012-04-26 17:50:04 -04:00
David Dollar eb325f7a49 saving man page to github docs 2012-04-23 15:57:06 -04:00
David Dollar ff6a539b93 saving man page to github docs 2012-04-20 19:33:14 -04:00
David Dollar 6be84449ea saving man page to github docs 2012-04-20 17:42:47 -04:00
David Dollar ab0d95d446 saving man page to github docs 2012-04-18 13:04:45 -04:00
David Dollar 68442bba75 saving man page to github docs 2012-02-21 11:58:57 -05:00
David Dollar cfd361695d saving man page to github docs 2012-02-21 11:50:11 -05:00
David Dollar 3582410c63 saving man page to github docs 2012-01-29 21:28:49 -05:00
David Dollar e45c7c7623 saving man page to github docs 2012-01-29 14:06:09 -05:00
David Dollar e9f75edaf3 add gitignore 2012-01-29 01:53:13 -05:00
David Dollar a9e7ed3100 saving man page to github docs 2012-01-29 01:46:03 -05:00
David Dollar 1cc3f9112a saving man page to github docs 2011-09-16 18:46:19 -04:00
David Dollar c1ef2ef8d5 saving man page to github docs 2011-09-16 18:43:02 -04:00
David Dollar 634e045e5b saving man page to github docs 2011-08-18 12:54:26 -04:00
David Dollar 69243df17e saving man page to github docs 2011-08-18 12:52:26 -04:00
David Dollar 097e3ca51b saving man page to github docs 2011-05-07 17:49:57 -04:00
David Dollar 0b4cff586c rebuilding man page 2011-03-08 12:18:11 -05:00
David Dollar 6e4224a0e7 rebuilding man page 2010-12-13 18:12:07 -05:00
David Dollar 5fdd935004 rebuilding man page 2010-06-30 23:11:38 -04:00
David Dollar d943912f82 rebuilding man page 2010-06-30 21:48:21 -04:00
David Dollar 279c8c03a9 rebuilding man page 2010-06-30 21:33:34 -04:00
David Dollar ac1098bd15 rebuilding man page 2010-06-29 16:49:24 -04:00
David Dollar d0eb079f38 rebuilding man page 2010-06-29 16:05:29 -04:00
David Dollar 269c6fe94a rebuilding man page 2010-06-23 19:11:56 -04:00
David Dollar 7954ab0a60 rebuilding man page 2010-06-23 19:10:16 -04:00
David Dollar 6478bb749f rebuilding man page 2010-06-23 19:05:09 -04:00
David Dollar 83814d62f8 rebuilding man page 2010-06-23 19:04:26 -04:00
David Dollar aeaf29e3aa rebuilding man page 2010-06-23 19:03:46 -04:00
David Dollar 40949e352f rebuilding man page 2010-06-23 19:02:05 -04:00
David Dollar 3ae466ad49 rebuilding man page 2010-06-23 18:54:55 -04:00
David Dollar 5ad20f962f rebuilding man page 2010-06-23 18:53:25 -04:00
David Dollar f1df0887cf initial index 2010-06-23 18:24:38 -04:00
33 changed files with 269 additions and 893 deletions
+4 -7
View File
@@ -1,7 +1,4 @@
.bundle
coverage
example/log/*
man/*.?
man/*.html
man/*.markdown
pkg
/.bundle
/coverage
/man
/pkg
-17
View File
@@ -1,17 +0,0 @@
source "http://rubygems.org"
group :development do
gem 'parka'
gem 'rake'
gem 'ronn'
end
group :test do
gem 'fakefs', '~> 0.2.1'
gem 'rcov', '~> 0.9.8'
gem 'rr', '~> 1.0.2'
gem 'rspec', '~> 2.0.0'
end
gem 'term-ansicolor', '~> 1.0.5'
gem 'thor', '~> 0.13.6'
-45
View File
@@ -1,45 +0,0 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
fakefs (0.2.1)
hpricot (0.8.2)
mime-types (1.16)
mustache (0.11.2)
parka (0.3.1)
rest-client
thor
rake (0.8.7)
rcov (0.9.8)
rdiscount (1.6.5)
rest-client (1.6.0)
mime-types (>= 1.16)
ronn (0.7.3)
hpricot (>= 0.8.2)
mustache (>= 0.7.0)
rdiscount (>= 1.5.8)
rr (1.0.2)
rspec (2.0.0.beta.19)
rspec-core (= 2.0.0.beta.19)
rspec-expectations (= 2.0.0.beta.19)
rspec-mocks (= 2.0.0.beta.19)
rspec-core (2.0.0.beta.19)
rspec-expectations (2.0.0.beta.19)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0.beta.19)
term-ansicolor (1.0.5)
thor (0.13.8)
PLATFORMS
ruby
DEPENDENCIES
fakefs (~> 0.2.1)
parka
rake
rcov (~> 0.9.8)
ronn
rr (~> 1.0.2)
rspec (~> 2.0.0)
term-ansicolor (~> 1.0.5)
thor (~> 0.13.6)
-9
View File
@@ -1,9 +0,0 @@
# Foreman
## Manual
See the [man page](http://ddollar.github.com/foreman) for usage.
## License
MIT
-52
View File
@@ -1,52 +0,0 @@
require "rubygems"
require "bundler"
Bundler.setup
require "rake"
require "rspec"
require "rspec/core/rake_task"
$:.unshift File.expand_path("../lib", __FILE__)
require "foreman"
task :default => :spec
task :release => :man
desc "Run all specs"
Rspec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
end
desc "Generate RCov code coverage report"
task :rcov => "rcov:build" do
%x{ open coverage/index.html }
end
Rspec::Core::RakeTask.new("rcov:build") do |t|
t.pattern = 'spec/**/*_spec.rb'
t.rcov = true
t.rcov_opts = [ "--exclude", Gem.default_dir , "--exclude", "spec" ]
end
desc 'Build the manual'
task :man do
ENV['RONN_MANUAL'] = "Foreman Manual"
ENV['RONN_ORGANIZATION'] = "Foreman #{Foreman::VERSION}"
sh "ronn -w -s toc -r5 --markdown man/*.ronn"
sh "git add README.markdown"
sh "git commit -m 'update readme' || echo 'nothing to commit'"
end
desc "Generate the Github docs"
task :pages => :man do
sh %{
cp man/foreman.1.html /tmp/foreman.1.html
git checkout gh-pages
rm ./index.html
cp /tmp/foreman.1.html ./index.html
git add -u index.html
git commit -m "rebuilding man page"
git push origin -f gh-pages
git checkout master
}
end
-1
View File
@@ -1 +0,0 @@
Autotest.add_discovery { "rspec2" }
-7
View File
@@ -1,7 +0,0 @@
#!/usr/bin/env ruby
$:.unshift File.expand_path("../../lib", __FILE__)
require "foreman/cli"
Foreman::CLI.start
-2
View File
@@ -1,2 +0,0 @@
ticker ./ticker $PORT
error ./error
-5
View File
@@ -1,5 +0,0 @@
#!/usr/bin/env ruby
puts "will error in 10s"
sleep 5
raise "Dying"
-4
View File
@@ -1,4 +0,0 @@
tick
tick
./never_die:6:in `sleep': Interrupt
from ./never_die:6
-6
View File
@@ -1,6 +0,0 @@
#!/usr/bin/env ruby
while true
puts "tick: #{ARGV.inspect}"
sleep 1
end
-8
View File
@@ -1,8 +0,0 @@
pre-start script
bash << "EOF"
mkdir -p <%= log_root %>
chown -R <%= user %> <%= log_root %>
EOF
end script
-6
View File
@@ -1,6 +0,0 @@
start on starting <%= app %>-<%= process.name %>
stop on stopping <%= app %>-<%= process.name %>
respawn
chdir <%= engine.directory %>
exec su - <%= user %> -c 'export PORT=<%= port %>; <%= process.command %> >> <%= log_root %>/<%=process.name%>-<%=num%>.log 2>&1'
@@ -1,2 +0,0 @@
start on starting <%= app %>
stop on stopping <%= app %>
-12
View File
@@ -1,12 +0,0 @@
require "rubygems"
require "parka/specification"
Parka::Specification.new do |gem|
gem.name = "foreman"
gem.version = Foreman::VERSION
gem.summary = "Process manager for applications with multiple components"
gem.homepage = "http://github.com/ddollar/foreman"
gem.executables = "foreman"
gem.files << "man/foreman.1"
end
+265
View File
@@ -0,0 +1,265 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' value='text/html;charset=utf8'>
<meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tree/0.7.3)'>
<title>foreman(1) - manage Procfile-based applications</title>
<style type='text/css' media='all'>
/* style: man */
body#manpage {margin:0}
.mp {max-width:100ex;padding:0 9ex 1ex 4ex}
.mp p,.mp pre,.mp ul,.mp ol,.mp dl {margin:0 0 20px 0}
.mp h2 {margin:10px 0 0 0}
.mp > p,.mp > pre,.mp > ul,.mp > ol,.mp > dl {margin-left:8ex}
.mp h3 {margin:0 0 0 4ex}
.mp dt {margin:0;clear:left}
.mp dt.flush {float:left;width:8ex}
.mp dd {margin:0 0 0 9ex}
.mp h1,.mp h2,.mp h3,.mp h4 {clear:left}
.mp pre {margin-bottom:20px}
.mp pre+h2,.mp pre+h3 {margin-top:22px}
.mp h2+pre,.mp h3+pre {margin-top:5px}
.mp img {display:block;margin:auto}
.mp h1.man-title {display:none}
.mp,.mp code,.mp pre,.mp tt,.mp kbd,.mp samp,.mp h3,.mp h4 {font-family:monospace;font-size:14px;line-height:1.42857142857143}
.mp h2 {font-size:16px;line-height:1.25}
.mp h1 {font-size:20px;line-height:2}
.mp {text-align:justify;background:#fff}
.mp,.mp code,.mp pre,.mp pre code,.mp tt,.mp kbd,.mp samp {color:#131211}
.mp h1,.mp h2,.mp h3,.mp h4 {color:#030201}
.mp u {text-decoration:underline}
.mp code,.mp strong,.mp b {font-weight:bold;color:#131211}
.mp em,.mp var {font-style:italic;color:#232221;text-decoration:none}
.mp a,.mp a:link,.mp a:hover,.mp a code,.mp a pre,.mp a tt,.mp a kbd,.mp a samp {color:#0000ff}
.mp b.man-ref {font-weight:normal;color:#434241}
.mp pre {padding:0 4ex}
.mp pre code {font-weight:normal;color:#434241}
.mp h2+pre,h3+pre {padding-left:0}
ol.man-decor,ol.man-decor li {margin:3px 0 10px 0;padding:0;float:left;width:33%;list-style-type:none;text-transform:uppercase;color:#999;letter-spacing:1px}
ol.man-decor {width:100%}
ol.man-decor li.tl {text-align:left}
ol.man-decor li.tc {text-align:center;letter-spacing:4px}
ol.man-decor li.tr {text-align:right;float:right}
</style>
<style type='text/css' media='all'>
/* style: toc */
.man-navigation {display:block !important;position:fixed;top:0;left:113ex;height:100%;width:100%;padding:48px 0 0 0;border-left:1px solid #dbdbdb;background:#eee}
.man-navigation a,.man-navigation a:hover,.man-navigation a:link,.man-navigation a:visited {display:block;margin:0;padding:5px 2px 5px 30px;color:#999;text-decoration:none}
.man-navigation a:hover {color:#111;text-decoration:underline}
</style>
</head>
<!--
The following styles are deprecated and will be removed at some point:
div#man, div#man ol.man, div#man ol.head, div#man ol.man.
The .man-page, .man-decor, .man-head, .man-foot, .man-title, and
.man-navigation should be used instead.
-->
<body id='manpage'>
<div class='mp' id='man'>
<div class='man-navigation' style='display:none'>
<a href="#NAME">NAME</a>
<a href="#SYNOPSIS">SYNOPSIS</a>
<a href="#DESCRIPTION">DESCRIPTION</a>
<a href="#RUNNING">RUNNING</a>
<a href="#EXPORTING">EXPORTING</a>
<a href="#GLOBAL-OPTIONS">GLOBAL OPTIONS</a>
<a href="#EXPORT-FORMATS">EXPORT FORMATS</a>
<a href="#INITTAB-EXPORT">INITTAB EXPORT</a>
<a href="#UPSTART-EXPORT">UPSTART EXPORT</a>
<a href="#PROCFILE">PROCFILE</a>
<a href="#ENVIRONMENT">ENVIRONMENT</a>
<a href="#DEFAULT-OPTIONS">DEFAULT OPTIONS</a>
<a href="#EXAMPLES">EXAMPLES</a>
<a href="#COPYRIGHT">COPYRIGHT</a>
</div>
<ol class='man-decor man-head man head'>
<li class='tl'>foreman(1)</li>
<li class='tc'>Foreman Manual</li>
<li class='tr'>foreman(1)</li>
</ol>
<h2 id="NAME">NAME</h2>
<p class="man-name">
<code>foreman</code> - <span class="man-whatis">manage Procfile-based applications</span>
</p>
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>foreman start [process]</code><br />
<code>foreman run &lt;command></code><br />
<code>foreman export &lt;format> [location]</code></p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
<p>Foreman is a manager for Procfile-based applications. Its aim is to
abstract away the details of the Procfile format, and allow you to either run
your application directly or export it to some other process management
format.</p>
<h2 id="RUNNING">RUNNING</h2>
<p><code>foreman start</code> is used to run your application directly from the command line.</p>
<p>If no additional parameters are passed, foreman will run one instance of each
type of process defined in your Procfile.</p>
<p>If a parameter is passed, foreman will run one instance of the specified
application type.</p>
<p>The following options control how the application is run:</p>
<dl>
<dt><code>-c</code>, <code>--concurrency</code></dt><dd><p>Specify the number of each process type to run. The value passed in
should be in the format <code>process=num,process=num</code></p></dd>
<dt><code>-e</code>, <code>--env</code></dt><dd><p>Specify one or more .env files to load</p></dd>
<dt><code>-f</code>, <code>--procfile</code></dt><dd><p>Specify an alternate Procfile to load, implies <code>-d</code> at the Procfile root.</p></dd>
<dt><code>-p</code>, <code>--port</code></dt><dd><p>Specify which port to use as the base for this application. Should be
a multiple of 1000.</p></dd>
</dl>
<p><code>foreman run</code> is used to run one-off commands using the same environment
as your defined processes.</p>
<h2 id="EXPORTING">EXPORTING</h2>
<p><code>foreman export</code> is used to export your application to another process
management format.</p>
<p>An location to export can be passed as an argument. This argument may be
either required or optional depending on the export format.</p>
<p>The following options control how the application is run:</p>
<dl>
<dt><code>-a</code>, <code>--app</code></dt><dd><p>Use this name rather than the application's root directory name as the
name of the application when exporting.</p></dd>
<dt><code>-c</code>, <code>--concurrency</code></dt><dd><p>Specify the number of each process type to run. The value passed in
should be in the format <code>process=num,process=num</code></p></dd>
<dt><code>-l</code>, <code>--log</code></dt><dd><p>Specify the directory to place process logs in.</p></dd>
<dt><code>-p</code>, <code>--port</code></dt><dd><p>Specify which port to use as the base for this application. Should be
a multiple of 1000.</p></dd>
<dt><code>-t</code>, <code>--template</code></dt><dd><p>Specify an alternate template to use for creating export files.
See <a href="https://github.com/ddollar/foreman/tree/master/data/export" data-bare-link="true">https://github.com/ddollar/foreman/tree/master/data/export</a> for examples.</p></dd>
<dt><code>-u</code>, <code>--user</code></dt><dd><p>Specify the user the application should be run as. Defaults to the
app name</p></dd>
</dl>
<h2 id="GLOBAL-OPTIONS">GLOBAL OPTIONS</h2>
<p>These options control all modes of foreman's operation.</p>
<dl>
<dt><code>-d</code>, <code>--root</code></dt><dd><p>Specify an alternate application root. This defaults to the directory
containing the Procfile.</p></dd>
<dt><code>-e</code>, <code>--env</code></dt><dd><p>Specify an alternate environment file. You can specify more than one
file by using: <code>--env file1,file2</code>.</p></dd>
<dt><code>-f</code>, <code>--procfile</code></dt><dd><p>Specify an alternate location for the application's Procfile. This file's
containing directory will be assumed to be the root directory of the
application.</p></dd>
</dl>
<h2 id="EXPORT-FORMATS">EXPORT FORMATS</h2>
<p>foreman currently supports the following output formats:</p>
<ul>
<li><p>bluepill</p></li>
<li><p>inittab</p></li>
<li><p>runit</p></li>
<li><p>upstart</p></li>
</ul>
<h2 id="INITTAB-EXPORT">INITTAB EXPORT</h2>
<p>Will export a chunk of inittab-compatible configuration:</p>
<pre><code># ----- foreman example processes -----
EX01:4:respawn:/bin/su - example -c 'PORT=5000 bundle exec thin start &gt;&gt; /var/log/web-1.log 2&gt;&amp;1'
EX02:4:respawn:/bin/su - example -c 'PORT=5100 bundle exec rake jobs:work &gt;&gt; /var/log/job-1.log 2&gt;&amp;1'
# ----- end foreman example processes -----
</code></pre>
<h2 id="UPSTART-EXPORT">UPSTART EXPORT</h2>
<p>Will create a series of upstart scripts in the location you specify. Scripts
will be structured to make the following commands valid:</p>
<p> <code>start appname</code></p>
<p> <code>stop appname-processname</code></p>
<p> <code>restart appname-processname-3</code></p>
<h2 id="PROCFILE">PROCFILE</h2>
<p>A Procfile should contain both a name for the process and the command used
to run it.</p>
<pre><code>web: bundle exec thin start
job: bundle exec rake jobs:work
</code></pre>
<p>A process name may contain letters, numbers amd the underscore character.
You can validate your Procfile format using the <code>check</code> command:</p>
<pre><code>$ foreman check
</code></pre>
<h2 id="ENVIRONMENT">ENVIRONMENT</h2>
<p>If a <code>.env</code> file exists in the current directory, the default environment will
be read from it. This file should contain key/value pairs, separated by <code>=</code>, with
one key/value pair per line.</p>
<pre><code>FOO=bar
BAZ=qux
</code></pre>
<h2 id="DEFAULT-OPTIONS">DEFAULT OPTIONS</h2>
<p>If a <code>.foreman</code> file exists in the current directory, default options will
be read from it. This file should be in YAML format with the long option
name as keys. Example:</p>
<pre><code>concurrency: alpha=0,bravo=1
port: 15000
</code></pre>
<h2 id="EXAMPLES">EXAMPLES</h2>
<p>Start one instance of each process type, interleave the output on stdout:</p>
<pre><code>$ foreman start
</code></pre>
<p>Export the application in upstart format:</p>
<pre><code>$ foreman export upstart /etc/init
</code></pre>
<p>Run one process type from the application defined in a specific Procfile:</p>
<pre><code>$ foreman start alpha -p ~/myapp/Procfile
</code></pre>
<h2 id="COPYRIGHT">COPYRIGHT</h2>
<p>Foreman is Copyright (C) 2010 David Dollar <a href="http://daviddollar.org" data-bare-link="true">http://daviddollar.org</a></p>
<ol class='man-decor man-foot man foot'>
<li class='tl'>Foreman 0.63.0</li>
<li class='tc'>April 2013</li>
<li class='tr'>foreman(1)</li>
</ol>
</div>
</body>
</html>
-8
View File
@@ -1,8 +0,0 @@
module Foreman
VERSION = "0.10.1"
class AppDoesNotExist < Exception; end
end
-75
View File
@@ -1,75 +0,0 @@
require "foreman"
require "foreman/engine"
require "foreman/export"
require "thor"
class Foreman::CLI < Thor
class_option :procfile, :type => :string, :aliases => "-f", :desc => "Default: Procfile"
desc "start [PROCESS]", "Start the application, or a specific process"
method_option :port, :type => :numeric, :aliases => "-p"
method_option :concurrency, :type => :string, :aliases => "-c",
:banner => '"alpha=5,bar=3"'
def start(process=nil)
check_procfile!
if process
engine.execute(process, options)
else
engine.start(options)
end
end
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 :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)
check_procfile!
formatter = case format
when "upstart" then Foreman::Export::Upstart
when "inittab" then Foreman::Export::Inittab
else error "Unknown export format: #{format}."
end
formatter.new(engine).export(location, options)
rescue Foreman::Export::Exception => ex
error ex.message
end
private ######################################################################
def check_procfile!
error("#{procfile} does not exist.") unless File.exist?(procfile)
end
def engine
@engine ||= Foreman::Engine.new(procfile)
end
def procfile
options[:procfile] || "Procfile"
end
private ######################################################################
def error(message)
puts "ERROR: #{message}"
exit 1
end
def procfile_exists?(procfile)
File.exist?(procfile)
end
end
-181
View File
@@ -1,181 +0,0 @@
require "foreman"
require "foreman/process"
require "foreman/utils"
require "pty"
require "tempfile"
require "term/ansicolor"
require "fileutils"
class Foreman::Engine
attr_reader :procfile
attr_reader :directory
extend Term::ANSIColor
COLORS = [ cyan, yellow, green, magenta, red ]
def initialize(procfile)
@procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile))
end
def processes
@processes ||= begin
@order = []
procfile.split("\n").inject({}) do |hash, line|
next if line.strip == ""
name, command = line.split(/:? +/, 2)
process = Foreman::Process.new(name, command)
process.color = next_color
@order << process.name
hash.update(process.name => process)
end
end
end
def process_order
processes
@order.uniq
end
def processes_in_order
process_order.map do |name|
[name, processes[name]]
end
end
def start(options={})
proctitle "ruby: foreman master"
processes_in_order.each do |name, process|
fork process, options
end
trap("TERM") { puts "SIGTERM received"; kill_all("TERM") }
trap("INT") { puts "SIGINT received"; kill_all("INT") }
watch_for_termination
end
def execute(name, options={})
fork processes[name], options
trap("TERM") { puts "SIGTERM received"; kill_all("TERM") }
trap("INT") { puts "SIGINT received"; kill_all("INT") }
watch_for_termination
end
def port_for(process, num, base_port=nil)
base_port ||= 5000
offset = processes_in_order.map { |p| p.first }.index(process.name) * 100
base_port.to_i + offset + num - 1
end
private ######################################################################
def fork(process, options={})
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
1.upto(concurrency[process.name]) do |num|
fork_individual(process, num, port_for(process, num, options[:port]))
end
end
def fork_individual(process, num, port)
ENV["PORT"] = port.to_s
ENV["PS"] = "#{process.name}.#{num}"
pid = Process.fork do
run(process)
end
info "started with pid #{pid}", process
running_processes[pid] = process
end
def run(process)
proctitle "ruby: foreman #{process.name}"
begin
Dir.chdir directory do
command = process.command
PTY.spawn("#{process.command} 2>&1") do |stdin, stdout, pid|
until stdin.eof?
info stdin.gets, process
end
end
end
rescue PTY::ChildExited, Interrupt
begin
info "process exiting", process
rescue Interrupt
end
end
end
def kill_all(signal="TERM")
info "terminating"
running_processes.each do |pid, process|
info "killing #{process.name} in pid #{pid}"
Process.kill(signal, pid)
end
end
def info(message, process=nil)
print process.color if process
print "#{Time.now.strftime("%H:%M:%S")} #{pad_process_name(process)} | "
print Term::ANSIColor.reset
print message.chomp
puts
end
def longest_process_name
@longest_process_name ||= begin
longest = processes.keys.map { |name| name.length }.sort.last
longest = 6 if longest < 6 # system
longest
end
end
def pad_process_name(process)
name = process ? "#{ENV["PS"]}" : "system"
name.ljust(longest_process_name + 3) # add 3 for process number padding
end
def print_info
info "currently running processes:"
running_processes.each do |pid, process|
info "pid #{pid}", process
end
end
def proctitle(title)
$0 = title
end
def read_procfile(procfile)
File.read(procfile)
end
def watch_for_termination
pid, status = Process.wait2
process = running_processes.delete(pid)
info "process terminated", process
kill_all
Process.waitall
end
def running_processes
@running_processes ||= {}
end
def next_color
@current_color ||= -1
@current_color += 1
@current_color >= COLORS.length ? "" : COLORS[@current_color]
end
end
-8
View File
@@ -1,8 +0,0 @@
require "foreman"
module Foreman::Export
class Exception < ::Exception; end
end
require "foreman/export/upstart"
require "foreman/export/inittab"
-38
View File
@@ -1,38 +0,0 @@
require "foreman/export"
require "foreman/utils"
class Foreman::Export::Base
attr_reader :engine
def initialize(engine)
@engine = engine
end
def export
raise "export method must be overridden"
end
private ######################################################################
def error(message)
raise Foreman::Export::Exception.new(message)
end
def say(message)
puts "[foreman export] %s" % message
end
def export_template(name)
File.read(File.expand_path("../../../../data/export/#{name}", __FILE__))
end
def write_file(filename, contents)
say "writing: #{filename}"
File.open(filename, "w") do |file|
file.puts contents
end
end
end
-38
View File
@@ -1,38 +0,0 @@
require "foreman/export/base"
class Foreman::Export::Inittab < Foreman::Export::Base
def export(fname=nil, options={})
app = options[:app] || File.basename(engine.directory)
user = options[:user] || app
log_root = options[:log] || "/var/log/#{app}"
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
inittab = []
inittab << "# ----- foreman #{app} processes -----"
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 = engine.port_for(process, num, options[:port])
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"
if fname
FileUtils.mkdir_p(log_root) rescue error "could not create #{log_root}"
FileUtils.chown(user, nil, log_root) rescue error "could not chown #{log_root} to #{user}"
write_file(fname, inittab)
else
puts inittab
end
end
end
-41
View File
@@ -1,41 +0,0 @@
require "erb"
require "foreman/export/base"
class Foreman::Export::Upstart < Foreman::Export::Base
def export(location, options={})
error("Must specify a location") unless location
FileUtils.mkdir_p location
app = options[:app] || File.basename(engine.directory)
user = options[:user] || app
log_root = options[:log] || "/var/log/#{app}"
Dir["#{location}/#{app}*.conf"].each do |file|
say "cleaning up: #{file}"
FileUtils.rm(file)
end
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
master_template = export_template("upstart/master.conf.erb")
master_config = ERB.new(master_template).result(binding)
write_file "#{location}/#{app}.conf", master_config
process_template = export_template("upstart/process.conf.erb")
engine.processes.values.each do |process|
process_master_template = export_template("upstart/process_master.conf.erb")
process_master_config = ERB.new(process_master_template).result(binding)
write_file "#{location}/#{app}-#{process.name}.conf", process_master_config
1.upto(concurrency[process.name]) do |num|
port = engine.port_for(process, num, options[:port])
process_config = ERB.new(process_template).result(binding)
write_file "#{location}/#{app}-#{process.name}-#{num}.conf", process_config
end
end
end
end
-14
View File
@@ -1,14 +0,0 @@
require "foreman"
class Foreman::Process
attr_reader :name
attr_reader :command
attr_accessor :color
def initialize(name, command)
@name = name
@command = command
end
end
-15
View File
@@ -1,15 +0,0 @@
require "foreman"
class Foreman::Utils
def self.parse_concurrency(concurrency)
@concurrency ||= begin
pairs = concurrency.to_s.gsub(/\s/, "").split(",")
pairs.inject(Hash.new(1)) do |hash, pair|
process, amount = pair.split("=")
hash.update(process => amount.to_i)
end
end
end
end
-126
View File
@@ -1,126 +0,0 @@
foreman(1) -- manage Procfile-based applications
================================================
## SYNOPSIS
`foreman start [process]`<br>
`foreman export <format> [location]`
## DESCRIPTION
**Foreman** is a manager for Procfile-based applications. Its aim is to
abstract away the details of the Procfile format, and allow you to either run
your application directly or export it to some other process management
format.
## RUNNING
`foreman start` is used to run your application directly from the command line.
If no additional parameters are passed, foreman will run one instance of each
type of process defined in your Procfile.
If a parameter is passed, foreman will run one instance of the specified
application type.
The following options control how the application is run:
* `-c`, `--concurrency`:
Specify the number of each process type to run. The value passed in
should be in the format `process=num,process=num`
* `-p`, `--port`:
Specify which port to use as the base for this application. Should be
a multiple of 1000.
## EXPORTING
`foreman export` is used to export your application to another process
management format.
An location to export can be passed as an argument. This argument may be
either required or optional depending on the export format.
The following options control how the application is run:
* `-a`, `--app`:
Use this name rather than the application's root directory name as the
name of the application when exporting.
* `-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
## OPTIONS
These options control all modes of foreman's operation.
* `-f`, `--procfile`:
Specify an alternate location for the application's Procfile. This file's
containing directory will be assumed to be the root directory of the
application.
## EXPORT FORMATS
foreman currently supports the following output formats:
* inittab
* upstart
## INITTAB EXPORT
Will export a chunk of inittab-compatible configuration:
# ----- foreman example processes -----
EX01:4:respawn:/bin/su - example -c 'PORT=5000 bundle exec thin start >> /var/log/web-1.log 2>&1'
EX02:4:respawn:/bin/su - example -c 'PORT=5100 bundle exec rake jobs:work >> /var/log/job-1.log 2>&1'
# ----- end foreman example processes -----
## UPSTART EXPORT
Will create a series of upstart scripts in the location you specify. Scripts
will be structured to make the following commands valid:
`start appname`
`stop appname-processname`
`restart appname-processname-3`
## PROCFILE
A Procfile should contain both a name for the process and the command used
to run it.
web: bundle exec thin start
job: bundle exec rake jobs:work
## EXAMPLES
Start one instance of each process type, interleave the output on stdout:
$ foreman start
Export the application in upstart format:
$ foreman export upstart /etc/init
Run one process type from the application defined in a specific Procfile:
$ foreman start alpha -p ~/myapp/Procfile
## COPYRIGHT
Foreman is Copyright (C) 2010 David Dollar <http://daviddollar.org>
-61
View File
@@ -1,61 +0,0 @@
require "spec_helper"
require "foreman/cli"
describe "Foreman::CLI" do
subject { Foreman::CLI.new }
describe "start" do
describe "with a non-existent Procfile" do
it "prints an error" do
mock_error(subject, "Procfile does not exist.") do
dont_allow.instance_of(Foreman::Engine).start
subject.start
end
end
end
describe "with a Procfile" do
before(:each) { write_procfile }
it "runs successfully" do
dont_allow(subject).error
mock.instance_of(Foreman::Engine).start({})
subject.start
end
end
end
describe "export" do
describe "with a non-existent Procfile" do
it "prints an error" do
mock_error(subject, "Procfile does not exist.") do
dont_allow.instance_of(Foreman::Engine).export
subject.export("testapp")
end
end
end
describe "with a Procfile" do
before(:each) { write_procfile }
describe "with an invalid formatter" do
it "prints an error" do
mock_error(subject, "Unknown export format: invalidformatter.") do
subject.export("invalidformatter")
end
end
end
describe "with a valid config" do
before(:each) { write_foreman_config("testapp") }
it "runs successfully" do
dont_allow(subject).error
mock.instance_of(Foreman::Export::Upstart).export("/tmp/foo", {})
subject.export("upstart", "/tmp/foo")
end
end
end
end
end
-50
View File
@@ -1,50 +0,0 @@
require "spec_helper"
require "foreman/engine"
describe "Foreman::Engine" do
subject { Foreman::Engine.new("Procfile") }
describe "initialize" do
describe "without an existing Procfile" do
it "raises an error" do
lambda { subject }.should raise_error
end
end
describe "with a Procfile" do
it "reads the processes" do
write_procfile
subject.processes["alpha"].command.should == "./alpha"
subject.processes["bravo"].command.should == "./bravo"
end
end
end
describe "start" do
it "forks the processes" do
write_procfile
mock(subject).fork(subject.processes["alpha"], {})
mock(subject).fork(subject.processes["bravo"], {})
mock(subject).watch_for_termination
subject.start
end
it "handles concurrency" do
write_procfile
mock(subject).fork_individual(subject.processes["alpha"], 1, 5000)
mock(subject).fork_individual(subject.processes["alpha"], 2, 5001)
mock(subject).fork_individual(subject.processes["bravo"], 1, 5100)
mock(subject).watch_for_termination
subject.start(:concurrency => "alpha=2")
end
end
describe "execute" do
it "runs the processes" do
write_procfile
mock(subject).fork(subject.processes["alpha"], {})
mock(subject).watch_for_termination
subject.execute("alpha")
end
end
end
-2
View File
@@ -1,2 +0,0 @@
require "spec_helper"
require "foreman/export/upstart"
-2
View File
@@ -1,2 +0,0 @@
require "spec_helper"
require "foreman/export"
-2
View File
@@ -1,2 +0,0 @@
require "spec_helper"
require "foreman/process"
-11
View File
@@ -1,11 +0,0 @@
require "spec_helper"
require "foreman"
describe Foreman do
describe "VERSION" do
subject { Foreman::VERSION }
it { should be_a String }
end
end
-38
View File
@@ -1,38 +0,0 @@
require "rubygems"
require "rspec"
require "fakefs/safe"
require "fakefs/spec_helpers"
$:.unshift "lib"
def mock_error(subject, message)
mock_exit do
mock(subject).puts("ERROR: #{message}")
yield
end
end
def mock_exit(&block)
block.should raise_error(SystemExit)
end
def write_foreman_config(app)
File.open("/etc/foreman/#{app}.conf", "w") do |file|
file.puts %{#{app}_processes="alpha bravo"}
file.puts %{#{app}_alpha="1"}
file.puts %{#{app}_bravo="2"}
end
end
def write_procfile(procfile="Procfile")
File.open(procfile, "w") do |file|
file.puts "alpha ./alpha"
file.puts "bravo: ./bravo"
end
end
Rspec.configure do |config|
config.color_enabled = true
config.include FakeFS::SpecHelpers
config.mock_with :rr
end