CloudyScripts 1.5.20 → 1.5.21
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/help/remote_command_handler.rb +8 -4
- metadata +2 -2
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ require 'rake/testtask'
|
|
12
12
|
|
13
13
|
spec = Gem::Specification.new do |s|
|
14
14
|
s.name = 'CloudyScripts'
|
15
|
-
s.version = '1.5.
|
15
|
+
s.version = '1.5.21'
|
16
16
|
s.has_rdoc = true
|
17
17
|
s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
|
18
18
|
s.summary = 'Scripts to facilitate programming for infrastructure clouds.'
|
@@ -5,9 +5,10 @@ require 'timeout'
|
|
5
5
|
|
6
6
|
# Provides methods to be executed via ssh to remote instances.
|
7
7
|
class RemoteCommandHandler
|
8
|
-
attr_accessor :logger, :ssh_session
|
9
|
-
def initialize
|
8
|
+
attr_accessor :logger, :ssh_session, :use_sudo
|
9
|
+
def initialize()
|
10
10
|
@logger = Logger.new(STDOUT)
|
11
|
+
@use_sudo = false
|
11
12
|
end
|
12
13
|
|
13
14
|
# Connect to the machine as root using a keyfile.
|
@@ -16,6 +17,7 @@ class RemoteCommandHandler
|
|
16
17
|
# * keyfile: path of the keyfile to be used for authentication
|
17
18
|
def connect_with_keyfile(ip, user_name, keyfile, timeout = 30)
|
18
19
|
@ssh_session = Net::SSH.start(ip, user_name, {:keys => [keyfile], :timeout => timeout})
|
20
|
+
@use_sudo = true unless user_name.strip == 'root'
|
19
21
|
end
|
20
22
|
|
21
23
|
# Connect to the machine as root using keydata from a keyfile.
|
@@ -25,6 +27,7 @@ class RemoteCommandHandler
|
|
25
27
|
# * key_data: key_data to be used for authentication
|
26
28
|
def connect(ip, user, key_data, timeout = 30)
|
27
29
|
@ssh_session = Net::SSH.start(ip, user, {:key_data => [key_data], :timeout => timeout})
|
30
|
+
@use_sudo = true unless user.strip == 'root'
|
28
31
|
end
|
29
32
|
|
30
33
|
# Disconnect the current handler
|
@@ -227,10 +230,11 @@ class RemoteCommandHandler
|
|
227
230
|
# All stdout-data is written into #stdout, all stderr-data is written into #stderr
|
228
231
|
def remote_exec_helper(exec_string, stdout = [], stderr = [], debug = false)
|
229
232
|
result = true
|
233
|
+
sudo = (@use_sudo ? "sudo " : "")
|
230
234
|
the_channel = @ssh_session.open_channel do |channel|
|
231
|
-
channel.exec("sudo
|
235
|
+
channel.exec("#{sudo}#{exec_string}") do |ch, success|
|
232
236
|
if success
|
233
|
-
@logger.debug("RemoteCommandHandler: starts executing sudo
|
237
|
+
@logger.debug("RemoteCommandHandler: starts executing #{sudo}#{exec_string}") if debug
|
234
238
|
ch.on_data() do |ch, data|
|
235
239
|
stdout << data unless data == nil || stdout == nil
|
236
240
|
end
|