CloudyScripts 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
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 = '0.0.7'
15
+ s.version = '0.0.8'
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.'
@@ -106,19 +106,13 @@ class RemoteCommandHandler
106
106
  # When #raise_exception is set, an exception will be raised instead of
107
107
  # returning false.
108
108
  def self.remote_execute(ssh_session, logger, exec_string, push_data = nil, raise_exception = false)
109
- logger.debug("RemoteCommandHandler: going to execute #{exec_string}")
110
- result = true
111
109
  exec_string = "echo #{push_data} >tmp.txt; #{exec_string} <tmp.txt; rm -f tmp.txt" unless push_data == nil
112
- output = ""
113
- ssh_session.exec(exec_string) do |ch, stream, data|
114
- output += data unless data == nil
115
- if stream == :stderr && data != nil
116
- result = false
117
- end
118
- end
119
- ssh_session.loop
120
- logger.info output unless logger == nil
121
- raise Exception.new("RemoteCommandHandler: #{exec_string} lead to stderr message: #{output}") unless result == true || raise_exception == false
110
+ stdout = []
111
+ stderr = []
112
+ result = remote_exec_helper(ssh_session, exec_string, stdout, stderr, logger)
113
+ em = "RemoteCommandHandler: #{exec_string} lead to stderr message: #{stderr.join().strip}"
114
+ logger.info(em) unless stderr.size == 0
115
+ raise Exception.new(em) unless result == true || raise_exception == false
122
116
  result
123
117
  end
124
118
 
@@ -129,21 +123,12 @@ class RemoteCommandHandler
129
123
  # in stdout contains the specified #search_string, the method returns true
130
124
  # otherwise false. Output to stderr will be logged.
131
125
  def self.stdout_contains?(ssh_session, logger, exec_string, search_string = "", push_data = nil)
132
- logger.debug("RemoteCommandHandler: going to execute #{exec_string}")
126
+ exec_string = "echo #{push_data} >tmp.txt; #{exec_string} <tmp.txt; rm -f tmp.txt" unless push_data == nil
133
127
  stdout = []
134
128
  stderr = []
135
- exec_string = "echo #{push_data} >tmp.txt; #{exec_string} <tmp.txt; rm -f tmp.txt" unless push_data == nil
136
- ssh_session.exec(exec_string) do |ch, stream, data|
137
- if stream == :stdout && data != nil
138
- stdout << data
139
- end
140
- if stream == :stderr && data != nil
141
- stderr << data
142
- end
143
- end
144
- ssh_session.loop
145
- logger.info("RemoteCommandHandler: #{exec_string} lead to stderr message: #{stderr.join("\n")}") unless stderr.size == 0
146
- stdout.join("\n").include?(search_string)
129
+ remote_exec_helper(ssh_session, exec_string, stdout, stderr, logger)
130
+ logger.info("RemoteCommandHandler: #{exec_string} lead to stderr message: #{stderr.join().strip}") unless stderr.size == 0
131
+ stdout.join().include?(search_string)
147
132
  end
148
133
 
149
134
  # Executes the specified #exec_string on a remote session specified as #ssh_session.
@@ -153,17 +138,53 @@ class RemoteCommandHandler
153
138
  # also written into those arrays.
154
139
  def self.get_output(ssh_session, exec_string, push_data = nil, stdout = [], stderr = [])
155
140
  exec_string = "echo #{push_data} >tmp.txt; #{exec_string} <tmp.txt; rm -f tmp.txt" unless push_data == nil
156
- ssh_session.exec(exec_string) do |ch, stream, data|
157
- if stream == :stdout && data != nil
158
- stdout << data
159
- end
160
- if stream == :stderr && data != nil
161
- stderr << data
141
+ stdout = []
142
+ stderr = []
143
+ remote_exec_helper(ssh_session, exec_string, stdout, stderr)
144
+ stdout.join()
145
+ end
146
+
147
+ private
148
+
149
+ # Executes the specified #exec_string on a remote session specified as #ssh_session
150
+ # and logs the command-output into the specified #logger.
151
+ # The method will return true if nothing was written into stderr, otherwise false.
152
+ # All stdout-data is written into #stdout, all stderr-data is written into #stderr
153
+ def self.remote_exec_helper(ssh_session, exec_string, stdout = [], stderr = [], logger = nil, debug = false)
154
+ result = true
155
+ the_channel = ssh_session.open_channel do |channel|
156
+ channel.exec(exec_string) do |ch, success|
157
+ if success
158
+ logger.debug("RemoteCommandHandler: starts executing #{exec_string}") if logger != nil && debug
159
+ ch.on_data() do |ch, data|
160
+ stdout << data unless data == nil
161
+ end
162
+ ch.on_extended_data do |ch, type, data|
163
+ stderr << data unless data == nil
164
+ result = false
165
+ end
166
+ ch.on_eof do |ch|
167
+ logger.debug("RemoteCommandHandler.on_eof:remote end is done sending data") if logger != nil && debug
168
+ end
169
+ ch.on_close do |ch|
170
+ logger.debug("RemoteCommandHandler.on_close:remote end is closing!") if logger != nil && debug
171
+ end
172
+ ch.on_open_failed do |ch, code, desc|
173
+ logger.debug("RemoteCommandHandler.on_open_failed: code=#{code} desc=#{desc}") if logger != nil && debug
174
+ end
175
+ ch.on_process do |ch|
176
+ logger.debug("RemoteCommandHandler.on_process; send line-feed/sleep") if logger != nil && debug
177
+ sleep(1)
178
+ ch.send_data("\n")
179
+ end
180
+ else
181
+ stderr << "the remote command could not be invoked!"
182
+ result = false
183
+ end
162
184
  end
163
185
  end
164
- ssh_session.loop
165
- stdout.join("\n")
186
+ the_channel.wait
187
+ result
166
188
  end
167
189
 
168
-
169
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: CloudyScripts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Jung