Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add the ability to reset the environment for a child process

  • Loading branch information...
commit 3919b2b69d5e79a4377cc6dc52c2cefbb84884ba 1 parent 0ae8d82
@ricardochimal ricardochimal authored
View
6 lib/rush/box.rb
@@ -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
View
20 lib/rush/local.rb
@@ -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
@@ -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
@@ -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
@@ -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
View
4 lib/rush/remote.rb
@@ -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
View
6 spec/box_spec.rb
@@ -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
View
9 spec/local_spec.rb
@@ -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
View
4 spec/remote_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.