Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #41 from dergachev/start-login-shell
Browse files Browse the repository at this point in the history
Ensures all ssh commands are run through a login shell
  • Loading branch information
Vasily Mikhaylichenko committed Dec 16, 2013
2 parents e2c1d6c + a4ba0da commit 8b4faf9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/gusteau/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def send_files(files, dest_dir, strip_c = nil)
private

def prepared_cmd(cmd)
user == 'root' ? cmd : "sudo -- sh -c '#{cmd}'"
# wrap all invocations in a login shell
cmd = "sh -l -c '#{cmd}'"
# use sudo if necessary
output = user == 'root' ? cmd : "sudo -- #{cmd}"
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/gusteau/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ def server.log_error(msg, opts={})
subject { server.send(:prepared_cmd, 'cd /etc/chef && touch test') }

context "user is root" do
it { subject.must_equal 'cd /etc/chef && touch test' }
it { subject.must_equal "sh -l -c 'cd /etc/chef && touch test'" }
end

context "user is not root" do
before { server.stubs(:user).returns('vaskas') }
it { subject.must_equal "sudo -- sh -c 'cd /etc/chef && touch test'" }
it { subject.must_equal "sudo -- sh -l -c 'cd /etc/chef && touch test'" }
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/lib/gusteau/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def conn.open_channel
context "user is root" do
before { connector.user = 'root' }

it "should execute the command as is" do
channel.expects(:exec).with('cowsay')
it "should execute the command without sudo" do
channel.expects(:exec).with("sh -l -c 'cowsay'")
connector.send_command 'cowsay'
end
end
Expand All @@ -63,7 +63,7 @@ def conn.open_channel
before { connector.user = 'vaskas' }

it "should execute the command with sudo" do
channel.expects(:exec).with("sudo -- sh -c 'cowsay'")
channel.expects(:exec).with("sudo -- sh -l -c 'cowsay'")
connector.send_command 'cowsay'
end
end
Expand Down Expand Up @@ -101,12 +101,12 @@ def channel.exec(cmd); yield true, success; end
end

it "should execute the extraction command and send the data" do
channel.expects(:exec).with("tar zxf - -C /etc/chef ")
channel.expects(:exec).with("sh -l -c 'tar zxf - -C /etc/chef '")
connector.send_files(%w{ a b }, '/etc/chef')
end

it "should strip tar components" do
channel.expects(:exec).with("tar zxf - -C /etc/chef --strip-components=3")
channel.expects(:exec).with("sh -l -c 'tar zxf - -C /etc/chef --strip-components=3'")
connector.send_files(%w{ c d }, '/etc/chef', 3)
end
end
Expand Down

0 comments on commit 8b4faf9

Please sign in to comment.