From 675ad2630d28d16fbb207c3adde82f8b39042db8 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Wed, 9 Jun 2010 11:51:10 -0400 Subject: [PATCH 1/6] cleanup --- Rakefile | 2 +- lib/foreman/engine.rb | 24 +++++++++++++++++------- spec/foreman/cli_spec.rb | 6 ++---- spec/spec_helper.rb | 4 +++- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Rakefile b/Rakefile index 189b5ed..dca4848 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ -require "rubygems" require "rake" +require "rspec" require "rspec/core/rake_task" $:.unshift File.expand_path("../lib", __FILE__) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index d55ff93..a6c0d1f 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -34,23 +34,33 @@ class Foreman::Engine run_loop 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| diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index 8ddffeb..70317ba 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -5,9 +5,7 @@ describe "Foreman::CLI" do 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 @@ -28,7 +26,7 @@ describe "Foreman::CLI" do 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2c4b9c8..b8fe97f 100644 --- a/spec/spec_helper.rb +++ b/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" From 3f48d7c541a59f166cfa10cda0d55c56358e8b7a Mon Sep 17 00:00:00 2001 From: David Dollar Date: Wed, 9 Jun 2010 11:51:18 -0400 Subject: [PATCH 2/6] add execute command --- lib/foreman/cli.rb | 7 +++++++ spec/foreman/cli_spec.rb | 21 +++++++++++++++++++++ spec/foreman/engine_spec.rb | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 8625305..32db1a6 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -13,6 +13,13 @@ class Foreman::CLI < Thor 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 "export APP [PROCFILE] [FORMAT]", "Export the app described in PROCFILE as APP to another FORMAT" def export(app, procfile="Procfile", format="upstart") diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index 70317ba..5468a5d 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -25,6 +25,27 @@ describe "Foreman::CLI" do 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 Procfile" do it "prints an error" do diff --git a/spec/foreman/engine_spec.rb b/spec/foreman/engine_spec.rb index f2aae70..827421e 100644 --- a/spec/foreman/engine_spec.rb +++ b/spec/foreman/engine_spec.rb @@ -29,4 +29,12 @@ describe "Foreman::Engine" do 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 From 56f8603a5ddded9236815dabee198f1e06b6b6c7 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Wed, 9 Jun 2010 12:13:02 -0400 Subject: [PATCH 3/6] first attempt at screen-based running --- lib/foreman/cli.rb | 7 +++++++ lib/foreman/engine.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 32db1a6..a1be7ea 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -20,6 +20,13 @@ class Foreman::CLI < Thor 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") diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index a6c0d1f..d7d125d 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -1,5 +1,6 @@ require "foreman" require "foreman/process" +require "tempfile" class Foreman::Engine @@ -34,6 +35,19 @@ class Foreman::Engine 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 From 2b8d575aabeba36ae3975e5cebe3529004e6db3e Mon Sep 17 00:00:00 2001 From: David Dollar Date: Wed, 9 Jun 2010 12:32:33 -0400 Subject: [PATCH 4/6] 0.2.0 --- foreman.gemspec | 5 ++--- lib/foreman.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/foreman.gemspec b/foreman.gemspec index c54323e..5c52675 100644 --- a/foreman.gemspec +++ b/foreman.gemspec @@ -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} @@ -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"] diff --git a/lib/foreman.rb b/lib/foreman.rb index c9b4c58..65ab824 100644 --- a/lib/foreman.rb +++ b/lib/foreman.rb @@ -1,6 +1,6 @@ module Foreman - VERSION = "0.1.0" + VERSION = "0.2.0" class AppDoesNotExist < Exception; end From 9929165d1710faa87b54166f278decfa47eda309 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Thu, 10 Jun 2010 15:26:28 -0400 Subject: [PATCH 5/6] update docs --- README.rdoc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index da76fe4..0329bce 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 +== Process Execution + + $ foreman execute alpha + == Upstart === Export to upstart scripts @@ -74,4 +84,4 @@ MIT == Copyright -(c) 2010 David Dollar \ No newline at end of file +(c) 2010 David Dollar From 86654d7918ba2b5398b236d9baa6f8482ea3006f Mon Sep 17 00:00:00 2001 From: David Dollar Date: Thu, 10 Jun 2010 15:27:18 -0400 Subject: [PATCH 6/6] update docs --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 0329bce..233e4ea 100644 --- a/README.rdoc +++ b/README.rdoc @@ -27,7 +27,7 @@ Launch the processes in a screen session in indivudal windows log/bravo.log log/charlie.log -== Process Execution +== Single Process Execution $ foreman execute alpha