Skip to content

Commit

Permalink
add the ability to reset the environment for a child process
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardochimal committed Jan 21, 2010
1 parent 0ae8d82 commit 3919b2b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/rush/box.rb
Expand Up @@ -64,15 +64,17 @@ def processes
# box.bash '/etc/init.d/mysql restart', :user => 'root'
# box.bash 'rake db:migrate', :user => 'www', :env => { :RAILS_ENV => 'production' }
# box.bash 'mongrel_rails start', :background => true
# box.bash 'rake db:migrate', :user => 'www', :env => { :RAILS_ENV => 'production' }, :reset_environment => true
#
def bash(command, options={})
cmd_with_env = command_with_environment(command, options[:env])
options[:reset_environment] ||= false

if options[:background]
pid = connection.bash(cmd_with_env, options[:user], true)
pid = connection.bash(cmd_with_env, options[:user], true, options[:reset_environment])
processes.find_by_pid(pid)
else
connection.bash(cmd_with_env, options[:user], false)
connection.bash(cmd_with_env, options[:user], false, options[:reset_environment])
end
end

Expand Down
20 changes: 12 additions & 8 deletions lib/rush/local.rb
Expand Up @@ -307,17 +307,19 @@ def kill_process(pid, options={})
# if it's dead, great - do nothing
end

def bash(command, user=nil, background=false)
return bash_background(command, user) if background
def bash(command, user=nil, background=false, reset_environment=false)
return bash_background(command, user, reset_environment) if background

require 'session'

sh = Session::Bash.new

shell = reset_environment ? "env -i bash" : "bash"

if user and user != ""
out, err = sh.execute "cd /; sudo -H -u #{user} bash", :stdin => command
out, err = sh.execute "cd /; sudo -H -u #{user} \"#{shell}\"", :stdin => command
else
out, err = sh.execute command
out, err = sh.execute shell, :stdin => command
end

retval = sh.status
Expand All @@ -328,7 +330,7 @@ def bash(command, user=nil, background=false)
out
end

def bash_background(command, user)
def bash_background(command, user, reset_environment)
pid = fork do
inpipe, outpipe = IO.pipe

Expand All @@ -338,10 +340,12 @@ def bash_background(command, user)

close_all_descriptors([inpipe.to_i])

shell = reset_environment ? "env -i bash" : "bash"

if user and user != ''
exec "cd /; sudo -H -u #{user} bash"
exec "cd /; sudo -H -u #{user} \"#{shell}\""
else
exec "bash"
exec shell
end
end

Expand Down Expand Up @@ -385,7 +389,7 @@ def receive(params)
when 'processes' then YAML.dump(processes)
when 'process_alive' then process_alive(params[:pid]) ? '1' : '0'
when 'kill_process' then kill_process(params[:pid].to_i, YAML.load(params[:payload]))
when 'bash' then bash(params[:payload], params[:user], params[:background] == 'true')
when 'bash' then bash(params[:payload], params[:user], params[:background] == 'true', params[:reset_environment] == 'true')
else
raise UnknownAction
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rush/remote.rb
Expand Up @@ -82,8 +82,8 @@ def kill_process(pid, options={})
transmit(:action => 'kill_process', :pid => pid, :payload => YAML.dump(options))
end

def bash(command, user, background)
transmit(:action => 'bash', :payload => command, :user => user, :background => background)
def bash(command, user, background, reset_environment)
transmit(:action => 'bash', :payload => command, :user => user, :background => background, :reset_environment => reset_environment)
end

# Given a hash of parameters (converted by the method call on the connection
Expand Down
6 changes: 3 additions & 3 deletions spec/box_spec.rb
Expand Up @@ -22,17 +22,17 @@
end

it "executes bash commands" do
@box.connection.should_receive(:bash).with('cmd', nil, false).and_return('output')
@box.connection.should_receive(:bash).with('cmd', nil, false, false).and_return('output')
@box.bash('cmd').should == 'output'
end

it "executes bash commands with an optional user" do
@box.connection.should_receive(:bash).with('cmd', 'user', false)
@box.connection.should_receive(:bash).with('cmd', 'user', false, false)
@box.bash('cmd', :user => 'user')
end

it "executes bash commands in the background, returning a Rush::Process" do
@box.connection.should_receive(:bash).with('cmd', nil, true).and_return(123)
@box.connection.should_receive(:bash).with('cmd', nil, true, false).and_return(123)
@box.stub!(:processes).and_return([ mock('ps', :pid => 123) ])
@box.bash('cmd', :background => true).pid.should == 123
end
Expand Down
9 changes: 7 additions & 2 deletions spec/local_spec.rb
Expand Up @@ -100,13 +100,18 @@
@con.receive(:action => 'kill_process', :pid => '123', :payload => YAML.dump(:wait => 10))
end

it "receive -> bash (reset environment)" do
@con.should_receive(:bash).with('cmd', 'user', false, true).and_return('output')
@con.receive(:action => 'bash', :payload => 'cmd', :user => 'user', :background => 'false', :reset_environment => 'true').should == 'output'
end

it "receive -> bash (foreground)" do
@con.should_receive(:bash).with('cmd', 'user', false).and_return('output')
@con.should_receive(:bash).with('cmd', 'user', false, false).and_return('output')
@con.receive(:action => 'bash', :payload => 'cmd', :user => 'user', :background => 'false').should == 'output'
end

it "receive -> bash (background)" do
@con.should_receive(:bash).with('cmd', 'user', true).and_return('output')
@con.should_receive(:bash).with('cmd', 'user', true, false).and_return('output')
@con.receive(:action => 'bash', :payload => 'cmd', :user => 'user', :background => 'true').should == 'output'
end

Expand Down
4 changes: 2 additions & 2 deletions spec/remote_spec.rb
Expand Up @@ -98,8 +98,8 @@
end

it "transmits bash" do
@con.should_receive(:transmit).with(:action => 'bash', :payload => 'cmd', :user => 'user', :background => 'bg').and_return('output')
@con.bash('cmd', 'user', 'bg').should == 'output'
@con.should_receive(:transmit).with(:action => 'bash', :payload => 'cmd', :user => 'user', :background => 'bg', :reset_environment => false).and_return('output')
@con.bash('cmd', 'user', 'bg', false).should == 'output'
end

it "an http result code of 401 raises NotAuthorized" do
Expand Down

0 comments on commit 3919b2b

Please sign in to comment.