Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ddollar/foreman
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Jun 12, 2010
2 parents 9004bf6 + 86654d7 commit 41a0620
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 18 deletions.
12 changes: 11 additions & 1 deletion README.rdoc
Expand Up @@ -15,12 +15,22 @@
[foreman] [Tue May 18 01:27:08 UTC 2010] [bravo] started with pid 4394
[foreman] [Tue May 18 01:27:08 UTC 2010] [charlie] started with pid 4395

=== Using Screen

Launch the processes in a screen session in indivudal windows

$ foreman screen

=== Standardized Logging

log/alpha.log
log/bravo.log
log/charlie.log

== Single Process Execution

$ foreman execute alpha

== Upstart

=== Export to upstart scripts
Expand Down Expand Up @@ -74,4 +84,4 @@ MIT

== Copyright

(c) 2010 David Dollar
(c) 2010 David Dollar
2 changes: 1 addition & 1 deletion Rakefile
@@ -1,5 +1,5 @@
require "rubygems"
require "rake"
require "rspec"
require "rspec/core/rake_task"

$:.unshift File.expand_path("../lib", __FILE__)
Expand Down
5 changes: 2 additions & 3 deletions foreman.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{foreman}
s.version = "0.1.0"
s.version = "0.2.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["David Dollar"]
s.date = %q{2010-05-21}
s.date = %q{2010-06-09}
s.default_executable = %q{foreman}
s.description = %q{Process manager for applications with multiple components}
s.email = %q{ddollar@gmail.com}
Expand All @@ -36,7 +36,6 @@ Gem::Specification.new do |s|
"spec/foreman_spec.rb",
"spec/spec_helper.rb"
]
s.has_rdoc = false
s.homepage = %q{http://github.com/ddollar/foreman}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
Expand Down
2 changes: 1 addition & 1 deletion lib/foreman.rb
@@ -1,6 +1,6 @@
module Foreman

VERSION = "0.1.0"
VERSION = "0.2.0"

class AppDoesNotExist < Exception; end

Expand Down
14 changes: 14 additions & 0 deletions lib/foreman/cli.rb
Expand Up @@ -13,6 +13,20 @@ def start(procfile="Procfile")
Foreman::Engine.new(procfile).start
end

desc "execute PROCESS [PROCFILE]", "Run an instance of the specified process from PROCFILE"

def execute(process, procfile="Procfile")
error "#{procfile} does not exist." unless procfile_exists?(procfile)
Foreman::Engine.new(procfile).execute(process)
end

desc "screen [PROCFILE]", "Run the app described in PROCFILE as screen windows"

def screen(procfile="Procfile")
error "#{procfile} does not exist." unless procfile_exists?(procfile)
Foreman::Engine.new(procfile).screen
end

desc "export APP [PROCFILE] [FORMAT]", "Export the app described in PROCFILE as APP to another FORMAT"

def export(app, procfile="Procfile", format="upstart")
Expand Down
38 changes: 31 additions & 7 deletions lib/foreman/engine.rb
@@ -1,5 +1,6 @@
require "foreman"
require "foreman/process"
require "tempfile"

class Foreman::Engine

Expand Down Expand Up @@ -34,23 +35,46 @@ def start
run_loop
end

def screen
tempfile = Tempfile.new("foreman")
tempfile.puts "sessionname foreman"
processes.each do |name, process|
tempfile.puts "screen -t #{name} #{process.command}"
end
tempfile.close

system "screen -c #{tempfile.path}"

tempfile.delete
end

def execute(name)
run(processes[name], false)
end

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

def fork(process)
pid = Process.fork do
proctitle "ruby: foreman #{process.name}"

Dir.chdir directory do
FileUtils.mkdir_p "log"
system "#{process.command} >>log/#{process.name}.log 2>&1"
exit $?.exitstatus || 255
end
run(process)
end

info "started with pid #{pid}", process
running_processes[pid] = process
end

def run(process, log_to_file=true)
proctitle "ruby: foreman #{process.name}"

Dir.chdir directory do
FileUtils.mkdir_p "log"
command = process.command
command << " >>log/#{process.name}.log 2>&1" if log_to_file
system command
exit $?.exitstatus || 255
end
end

def kill_and_exit(signal="TERM")
info "termination requested"
running_processes.each do |pid, process|
Expand Down
27 changes: 23 additions & 4 deletions spec/foreman/cli_spec.rb
Expand Up @@ -5,9 +5,7 @@
subject { Foreman::CLI.new }

describe "start" do
#let(:engine) { stub_engine }

describe "with a non-existent Procifile" 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
Expand All @@ -27,8 +25,29 @@
end
end

describe "execute" 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.execute("alpha")
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).execute("alpha")
subject.execute("alpha")
end
end
end

describe "export" do
describe "with a non-existent Procifile" 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
Expand Down
8 changes: 8 additions & 0 deletions spec/foreman/engine_spec.rb
Expand Up @@ -29,4 +29,12 @@
subject.start
end
end

describe "execute" do
it "runs the processes" do
write_procfile
mock(subject).run(subject.processes["alpha"], false)
subject.execute("alpha")
end
end
end
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
@@ -1,5 +1,7 @@
require "fakefs/spec_helpers"
require "rubygems"
require "rspec"
require "fakefs/safe"
require "fakefs/spec_helpers"

$:.unshift "lib"

Expand Down

0 comments on commit 41a0620

Please sign in to comment.