Compare commits

...
20 Commits
Author SHA1 Message Date
David Dollar 83748cb538 0.32.0 2012-01-12 15:25:43 -08:00
David Dollar d2c9ce0f34 Merge pull request #125 from brainopia/master
Support for complex cmds in Procfile
2012-01-12 15:23:15 -08:00
David Dollar 98337c92e1 Merge pull request #121 from Viximo/feature/run
Add "exec" action to allow execution of commands within the app environment
2012-01-09 16:02:42 -08:00
Matt Griffin 33d738b3f8 Return some whitespace that was accidentally removed 2012-01-09 17:15:20 -05:00
Matt Griffin 9432989fbe Steal the run method back from Thor so that it can be used in place for exec for running commands in the foreman environment.
Fix some error reporting.
2012-01-09 17:11:32 -05:00
brainopia 66b1483a75 Remove old cruft 2012-01-08 10:18:48 +07:00
brainopia 64bd4db128 In case someone wants to use bin/runner directly 2012-01-08 10:15:23 +07:00
brainopia b561555f3a Fix for double fork 2012-01-08 09:42:51 +07:00
brainopia baa7b7685c Use ruby exec which works with escaped cmd and replaces shell 2012-01-07 20:19:57 +07:00
brainopia cfa6e6f259 Fix foreman to work with cmds containing pipes and redirects 2012-01-07 18:19:54 +07:00
Matt Griffin a34bc59721 Add "exec" action to allow execution of arbitrary commands with the app's environment. 2012-01-04 15:22:10 -05:00
David Dollar 07e8ca4a4b tweak readme 2012-01-04 12:36:34 -05:00
David Dollar 342d30bbb8 0.31.0 2012-01-04 12:16:51 -05:00
David Dollar 268dd6240e make fork more robust 2012-01-04 12:15:55 -05:00
David Dollar 9e60b3e1a4 remove unnecessary debug 2012-01-04 12:15:38 -05:00
David Dollar 1c6285f8af add more information when shutting down 2012-01-04 12:15:17 -05:00
David Dollar fff15bc627 Merge pull request #110 from lstoll/master
Different port range for each process type on 'foreman start'
2011-12-24 22:47:36 -08:00
Lincoln Stoll a66157d611 Use different port ranges for each process type 2011-12-25 15:46:21 +11:00
David Dollar fcfa913fb0 0.30.1 2011-12-23 08:48:34 -05:00
David Dollar fc438472f9 require thread for mutex 2011-12-23 08:48:17 -05:00
10 changed files with 125 additions and 20 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
foreman (0.30.0) foreman (0.32.0)
term-ansicolor (~> 1.0.5) term-ansicolor (~> 1.0.5)
thor (>= 0.13.6) thor (>= 0.13.6)
+2 -1
View File
@@ -20,7 +20,8 @@ http://blog.daviddollar.org/2011/05/06/introducing-foreman.html
## Manual ## Manual
See the [man page](http://ddollar.github.com/foreman) for usage. * [man page](http://ddollar.github.com/foreman)
* [wiki](http://github.com/ddollar/foreman/wiki)
## Authorship ## Authorship
+2 -2
View File
@@ -1,2 +1,2 @@
#!/bin/sh #!/usr/bin/env ruby
exec $1 2>&1 exec "#{ARGV.join(' ')} 2>&1"
+23 -3
View File
@@ -5,15 +5,23 @@ require "thor"
require "yaml" require "yaml"
class Foreman::CLI < Thor class Foreman::CLI < Thor
class_option :procfile, :type => :string, :aliases => "-f", :desc => "Default: Procfile" class_option :procfile, :type => :string, :aliases => "-f", :desc => "Default: Procfile"
desc "start", "Start the application" desc "start", "Start the application"
method_option :env, :type => :string, :aliases => "-e", :desc => "Specify an environment file to load, defaults to .env" method_option :env, :type => :string, :aliases => "-e", :desc => "Specify an environment file to load, defaults to .env"
method_option :port, :type => :numeric, :aliases => "-p" method_option :port, :type => :numeric, :aliases => "-p"
method_option :concurrency, :type => :string, :aliases => "-c", :banner => '"alpha=5,bar=3"' method_option :concurrency, :type => :string, :aliases => "-c", :banner => '"alpha=5,bar=3"'
class << self
# Hackery. Take the run method away from Thor so that we can redefine it.
def is_thor_reserved_word?(word, type)
return false if word == 'run'
super
end
end
def start def start
check_procfile! check_procfile!
engine.start engine.start
@@ -53,7 +61,20 @@ class Foreman::CLI < Thor
error "no processes defined" unless engine.procfile.entries.length > 0 error "no processes defined" unless engine.procfile.entries.length > 0
display "valid procfile detected (#{engine.procfile.process_names.join(', ')})" display "valid procfile detected (#{engine.procfile.process_names.join(', ')})"
end end
desc "run COMMAND", "Run a command using your application's environment"
def run(*args)
engine.apply_environment!
begin
exec args.join(" ")
rescue Errno::EACCES
error "not executable: #{args.first}"
rescue Errno::ENOENT
error "command not found: #{args.first}"
end
end
private ###################################################################### private ######################################################################
def check_procfile! def check_procfile!
@@ -87,5 +108,4 @@ private ######################################################################
defaults = YAML::load_file(".foreman") || {} defaults = YAML::load_file(".foreman") || {}
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options)) Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
end end
end end
+10 -2
View File
@@ -7,6 +7,7 @@ require "tempfile"
require "timeout" require "timeout"
require "term/ansicolor" require "term/ansicolor"
require "fileutils" require "fileutils"
require "thread"
class Foreman::Engine class Foreman::Engine
@@ -57,7 +58,7 @@ private ######################################################################
procfile.entries.each do |entry| procfile.entries.each do |entry|
reader, writer = IO.pipe reader, writer = IO.pipe
entry.spawn(concurrency[entry.name], writer, @directory, @environment, base_port).each do |process| entry.spawn(concurrency[entry.name], writer, @directory, @environment, port_for(entry, 1, base_port)).each do |process|
running_processes[process.pid] = process running_processes[process.pid] = process
readers[process] = reader readers[process] = reader
end end
@@ -70,10 +71,17 @@ private ######################################################################
def kill_all(signal="SIGTERM") def kill_all(signal="SIGTERM")
running_processes.each do |pid, process| running_processes.each do |pid, process|
Process.kill(signal, pid) rescue Errno::ESRCH info "sending #{signal} to pid #{pid}"
kill(signal, -pid) or kill(signal, pid)
end end
end end
def kill(signal, pid)
Process.kill signal, pid
rescue Errno::ESRCH
false
end
def terminate_gracefully def terminate_gracefully
info "sending SIGTERM to all processes" info "sending SIGTERM to all processes"
kill_all "SIGTERM" kill_all "SIGTERM"
+9 -10
View File
@@ -28,21 +28,20 @@ class Foreman::Process
private private
def fork_with_io(command) def fork_with_io(command)
io = case RUBY_VERSION reader, writer = IO.pipe
when /^1\.9\./ pid = fork do
IO.popen([Foreman.runner, replace_command_env(command)], "w+") Process.setpgrp
when /^1\.8\./ trap("INT", "IGNORE")
full_command = replace_command_env(command).gsub("'", "\\'") $stdout.reopen writer
IO.popen("#{Foreman.runner} '#{full_command}'", "w+") reader.close
else exec Foreman.runner, replace_command_env(command)
raise "Unknown Ruby version: #{RUBY_VERSION}"
end end
[ io, io.pid ] Process.detach pid
[ reader, pid ]
end end
def run_process(command, pipe) def run_process(command, pipe)
io, @pid = fork_with_io(command) io, @pid = fork_with_io(command)
trap("SIGTERM") { "got sigterm for %d" % @pid }
output pipe, "started with pid %d" % @pid output pipe, "started with pid %d" % @pid
Thread.new do Thread.new do
until io.eof? until io.eof?
+1 -1
View File
@@ -1,5 +1,5 @@
module Foreman module Foreman
VERSION = "0.30.0" VERSION = "0.32.0"
end end
+49
View File
@@ -89,5 +89,54 @@ describe "Foreman::CLI" do
end end
end end
end end
describe "run" do
describe "with a valid Procfile" do
before { write_procfile }
describe "and a command" do
let(:command) { ["ls", "-l"] }
before(:each) do
stub(subject).exec
end
it "should load the environment file" do
write_env
preserving_env do
subject.run *command
ENV["FOO"].should == "bar"
end
ENV["FOO"].should be_nil
end
it "should runute the command as a string" do
mock(subject).exec(command.join(" "))
subject.run *command
end
end
describe "and a non-existent command" do
let(:command) { "iuhtngrglhulhdfg" }
it "should print an error" do
mock_error(subject, "command not found: #{command}") do
subject.run command
end
end
end
describe "and a non-executable command" do
let(:command) { __FILE__ }
it "should print an error" do
mock_error(subject, "not executable: #{command}") do
subject.run command
end
end
end
end
end
end end
+18
View File
@@ -0,0 +1,18 @@
require "spec_helper"
describe "spec helpers" do
describe "#preserving_env" do
after { ENV.delete "FOO" }
it "should remove added environment vars" do
preserving_env { ENV["FOO"] = "baz" }
ENV["FOO"].should == nil
end
it "should reset modified environment vars" do
ENV["FOO"] = "bar"
preserving_env { ENV["FOO"] = "baz"}
ENV["FOO"].should == "bar"
end
end
end
+10
View File
@@ -63,6 +63,16 @@ def example_export_file(filename)
data data
end end
def preserving_env
old_env = ENV.to_hash
begin
yield
ensure
ENV.clear
ENV.update(old_env)
end
end
RSpec.configure do |config| RSpec.configure do |config|
config.color_enabled = true config.color_enabled = true
config.include FakeFS::SpecHelpers config.include FakeFS::SpecHelpers