Permalink
Browse files

Box#bash sudo to another user with :user optional param

  • Loading branch information...
1 parent e8c9424 commit 369ce612801c8ae7662b3489fae71856c6ecca5a @adamwiggins adamwiggins committed Mar 15, 2008
Showing with 41 additions and 17 deletions.
  1. +7 −4 lib/rush/box.rb
  2. +3 −3 lib/rush/dir.rb
  3. +9 −3 lib/rush/local.rb
  4. +2 −2 lib/rush/remote.rb
  5. +6 −1 spec/box_spec.rb
  6. +6 −0 spec/dir_spec.rb
  7. +6 −2 spec/local_spec.rb
  8. +2 −2 spec/remote_spec.rb
View
@@ -49,10 +49,13 @@ def processes
end
end
- # Execute a command in the standard unix shell. Until the day when it's no
- # longer needed...
- def bash(command)
- connection.bash(command)
+ # Execute a command in the standard unix shell. Optional parameter :user
+ # switches to that user via sudo first, if you have permission. Example:
+ #
+ # box.bash '/etc/init.d/mysql restart', :user => 'root'
+ #
+ def bash(command, options={})
+ connection.bash(command, options[:user])
end
# Returns true if the box is responding to commands.
View
@@ -118,9 +118,9 @@ def nonhidden_files
end
end
- # Run a bash command starting in this directory.
- def bash(command)
- box.bash "cd #{full_path} && #{command}"
+ # Run a bash command starting in this directory. Options are the same as Rush::Box#bash.
+ def bash(command, options={})
+ box.bash "cd #{full_path} && #{command}", options
end
# Destroy all of the contents of the directory, leaving it fresh and clean.
View
@@ -250,11 +250,17 @@ def kill_process(pid)
::Process.kill('KILL', pid) rescue nil
end
- def bash(command)
+ def bash(command, user=nil)
require 'session'
sh = Session::Bash.new
- out, err = sh.execute command
+
+ if user and user != ""
+ out, err = sh.execute "sudo -u #{user} bash", :stdin => command
+ else
+ out, err = sh.execute command
+ end
+
retval = sh.status
sh.close!
@@ -289,7 +295,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)
- when 'bash' then bash(params[:payload])
+ when 'bash' then bash(params[:payload], params[:user])
else
raise UnknownAction
end
View
@@ -74,8 +74,8 @@ def kill_process(pid)
transmit(:action => 'kill_process', :pid => pid)
end
- def bash(command)
- transmit(:action => 'bash', :payload => command)
+ def bash(command, user)
+ transmit(:action => 'bash', :payload => command, :user => user)
end
# Given a hash of parameters (converted by the method call on the connection
View
@@ -22,10 +22,15 @@
end
it "executes bash commands" do
- @box.connection.should_receive(:bash).with('cmd').and_return('output')
+ @box.connection.should_receive(:bash).with('cmd', nil).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')
+ @box.bash('cmd', :user => 'user')
+ end
+
it "checks the connection to determine if it is alive" do
@box.connection.should_receive(:alive?).and_return(true)
@box.should be_alive
View
@@ -150,4 +150,10 @@
system "echo test > #{@dir.full_path}/file"
@dir.bash("cat file").should == "test\n"
end
+
+ it "passes bash options (e.g., :user) through to the box bash command" do
+ @box.should_receive(:bash).with('cmd', 'options')
+ @box.bash('cmd', 'options')
+ end
+
end
View
@@ -88,8 +88,8 @@
end
it "receive -> bash" do
- @con.should_receive(:bash).with('cmd').and_return('output')
- @con.receive(:action => 'bash', :payload => 'cmd').should == 'output'
+ @con.should_receive(:bash).with('cmd', 'user').and_return('output')
+ @con.receive(:action => 'bash', :payload => 'cmd', :user => 'user').should == 'output'
end
it "receive -> unknown action exception" do
@@ -246,6 +246,10 @@
lambda { @con.bash("no_such_bin") }.should raise_error(Rush::BashFailed, /command not found/)
end
+ it "executes a bash command as another user using sudo" do
+ @con.bash("echo test2", ENV['user']).should == "test2\n"
+ end
+
it "ensure_tunnel to match with remote connection" do
@con.ensure_tunnel
end
View
@@ -88,8 +88,8 @@
end
it "transmits bash" do
- @con.should_receive(:transmit).with(:action => 'bash', :payload => 'cmd').and_return('output')
- @con.bash('cmd').should == 'output'
+ @con.should_receive(:transmit).with(:action => 'bash', :payload => 'cmd', :user => 'user').and_return('output')
+ @con.bash('cmd', 'user').should == 'output'
end
it "an http result code of 401 raises NotAuthorized" do

0 comments on commit 369ce61

Please sign in to comment.