brpm_content_framework 0.1.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +38 -0
  3. data/.travis.yml +17 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +21 -0
  6. data/README.md +308 -0
  7. data/Rakefile +23 -0
  8. data/TO_BE_MIGRATED.txt +9 -0
  9. data/architecture.png +0 -0
  10. data/automations/direct_execute.meta +10 -0
  11. data/automations/direct_execute.rb +10 -0
  12. data/automations/install_module.meta +10 -0
  13. data/automations/install_module.rb +13 -0
  14. data/bin/brpm_install +30 -0
  15. data/bin/brpm_uninstall +30 -0
  16. data/bin/event_handler +63 -0
  17. data/bin/webhook_receiver +49 -0
  18. data/brpm_content.gemspec +31 -0
  19. data/config.yml +8 -0
  20. data/infrastructure/.bashrc +6 -0
  21. data/infrastructure/.brpm +2 -0
  22. data/infrastructure/config/customer_include.rb +26 -0
  23. data/infrastructure/config/server.yml +3 -0
  24. data/infrastructure/log.html +39 -0
  25. data/infrastructure/scripts/backup_database.sh +19 -0
  26. data/infrastructure/scripts/ddns.sh +10 -0
  27. data/infrastructure/scripts/install_brpm.sh +63 -0
  28. data/infrastructure/scripts/maintenance.sh +4 -0
  29. data/infrastructure/scripts/patch_brpm.sh +90 -0
  30. data/infrastructure/scripts/restore_database.sh +33 -0
  31. data/infrastructure/scripts/run_event_handler.cmd +19 -0
  32. data/infrastructure/scripts/run_event_handler.sh +20 -0
  33. data/infrastructure/scripts/run_webhook_receiver.cmd +15 -0
  34. data/infrastructure/scripts/run_webhook_receiver.sh +15 -0
  35. data/infrastructure/silent_install_options_4.6.txt +93 -0
  36. data/infrastructure/silent_install_options_upgrade_4.6.txt +92 -0
  37. data/infrastructure/smtp_settings.rb +42 -0
  38. data/lib/brpm_auto.rb +358 -0
  39. data/lib/brpm_script_executor.rb +80 -0
  40. data/lib/logging/brpm_logger.rb +39 -0
  41. data/lib/logging/logger_base.rb +36 -0
  42. data/lib/logging/simple_logger.rb +27 -0
  43. data/lib/module_installer.rb +483 -0
  44. data/lib/params/all_params.rb +80 -0
  45. data/lib/params/integration_settings.rb +27 -0
  46. data/lib/params/params.rb +174 -0
  47. data/lib/params/params_base.rb +81 -0
  48. data/lib/params/request_params.rb +38 -0
  49. data/lib/rest_api.rb +155 -0
  50. data/lib/semaphore.rb +79 -0
  51. data/lib/utilities.rb +317 -0
  52. data/lib/version_control/git.rb +192 -0
  53. data/lib/version_control/svn.rb +221 -0
  54. data/lib/write_to.rb +1 -0
  55. data/tests/all_params_spec.rb +116 -0
  56. data/tests/brpm_auto_spec.rb +84 -0
  57. data/tests/customer_include/config/customer_include.rb +10 -0
  58. data/tests/customer_include/config/server.yml +3 -0
  59. data/tests/customer_include_spec.rb +29 -0
  60. data/tests/gemspec_spec.rb +11 -0
  61. data/tests/module_installer_spec.rb +46 -0
  62. data/tests/params_spec.rb +172 -0
  63. data/tests/request_params_spec.rb +86 -0
  64. data/tests/server_yaml_spec.rb +19 -0
  65. data/tests/spec_helper.rb +64 -0
  66. data/to_be_migrated/brpm_framework.rb +88 -0
  67. data/to_be_migrated/customer_include_default.rb +25 -0
  68. data/to_be_migrated/local_jirb.rb +15 -0
  69. data/to_be_migrated/resource_framework.rb +211 -0
  70. data/transport/dispatch_baa.rb +355 -0
  71. data/transport/dispatch_base.rb +345 -0
  72. data/transport/dispatch_nsh.rb +248 -0
  73. data/transport/dispatch_ssh.rb +154 -0
  74. data/transport/transport_baa.rb +1095 -0
  75. data/transport/transport_nsh.rb +359 -0
  76. data/transport/transport_ssh.rb +220 -0
  77. metadata +204 -0
@@ -0,0 +1,359 @@
1
+ # Wrapper class for NSH interactions
2
+ class TransportNSH
3
+
4
+ attr_writer :test_mode
5
+
6
+ # Initialize the class
7
+ #
8
+ # ==== Attributes
9
+ #
10
+ # * +nsh_path+ - path to NSH dir on files system (must contain br directory too)
11
+ # * +options+ - hash of options to use, send "output_file" to point to the logging file
12
+ # * +test_mode+ - true/false to simulate commands instead of running them
13
+ #
14
+ def initialize(nsh_path, options = {}, compat_options = {})
15
+ self.extend Utilities
16
+ if options.has_key?("SS_output_dir")
17
+ BrpmAuto.log "Load for this class has changed, no longer necessary to send params as 2nd argument"
18
+ options = compat_options
19
+ end
20
+ @nsh_path = nsh_path
21
+ @test_mode = get_option(options, "test_mode", false)
22
+ @verbose = get_option(options, "verbose", false)
23
+ @opts = options
24
+ @run_key = get_option(options,"timestamp",Time.now.strftime("%Y%m%d%H%M%S"))
25
+ outf = get_option(options,"output_file", SS_output_file)
26
+ @output_dir = File.dirname(outf)
27
+ insure_proxy
28
+ end
29
+
30
+ # Verifies that proxy cred is set
31
+ #
32
+ # ==== Returns
33
+ #
34
+ # * blcred cred -acquire output
35
+ def insure_proxy
36
+ return true if get_option(@opts, "bl_profile") == ""
37
+ res = get_cred
38
+ puts res
39
+ end
40
+
41
+ # Displays any errors from a cred status
42
+ #
43
+ # ==== Attributes
44
+ #
45
+ # * +status+ - output from cred command
46
+ #
47
+ # ==== Returns
48
+ #
49
+ # * true/false
50
+ def cred_errors?(status)
51
+ errors = ["EXPIRED","cache is empty"]
52
+ errors.each do |err|
53
+ return true if status.include?(err)
54
+ end
55
+ return false
56
+ end
57
+
58
+ # Performs a cred -acquire
59
+ #
60
+ # ==== Returns
61
+ #
62
+ # * cred result message
63
+ def get_cred
64
+ bl_cred_path = safe_cmd("blcred")
65
+ cred_status = `#{bl_cred_path} cred -list`
66
+ puts "Current Status:\n#{cred_status}" if @test_mode
67
+ if (cred_errors?(cred_status))
68
+ # get cred
69
+ cmd = "#{bl_cred_path} cred -acquire -profile #{get_option(@opts,"bl_profile")} -username #{get_option(@opts,"bl_username")} -password #{get_option(@opts,"bl_password")}"
70
+ res = execute_shell(cmd)
71
+ puts display_result(res) if @test_mode
72
+ result = "Acquiring new credential"
73
+ else
74
+ result = "Current credential is valid"
75
+ end
76
+ result
77
+ end
78
+
79
+ # Runs an nsh script
80
+ #
81
+ # ==== Attributes
82
+ #
83
+ # * +script_path+ - path (local to rpm server) to script file
84
+ #
85
+ # ==== Returns
86
+ #
87
+ # * results of script
88
+ def nsh(script_path, raw_result = false)
89
+ cmd = "#{nsh_cmd("nsh")} #{script_path}"
90
+ cmd = @test_mode ? "echo \"#{cmd}\"" : cmd
91
+ result = execute_shell(cmd)
92
+ return result if raw_result
93
+ display_result(result)
94
+ end
95
+
96
+ # Runs a simple one-line command in NSH
97
+ #
98
+ # ==== Attributes
99
+ #
100
+ # * +command+ - command to run
101
+ #
102
+ # ==== Returns
103
+ #
104
+ # * results of command
105
+ def nsh_command(command, raw_result = false)
106
+ path = create_temp_script("echo Running #{command.gsub("\n"," - ")}\n#{command}\n")
107
+ result = nsh(path, raw_result)
108
+ File.delete path unless @test_mode
109
+ result
110
+ end
111
+
112
+ # Copies all files (recursively) from source to destination on target hosts
113
+ #
114
+ # ==== Attributes
115
+ #
116
+ # * +target_hosts+ - blade hostnames to copy to
117
+ # * +src_path+ - NSH path to source files (may be an array)
118
+ # * +target_path+ - path to copy to (same for all target_hosts)
119
+ #
120
+ # ==== Returns
121
+ #
122
+ # * results of command
123
+ def ncp(target_hosts, src_path, target_path)
124
+ #ncp -vr /c/dev/SmartRelease_2/lib -h bradford-96204e -d "/c/dev/BMC Software/file_store"
125
+ src_path = [src_path] if src_path.is_a?(String)
126
+ if target_hosts.nil?
127
+ res = split_nsh_path(src_path[0])
128
+ target_hosts = [res[0]] unless res[0] == ""
129
+ src_path[0] = res[1] unless res[0] == ""
130
+ end
131
+ paths = src_path.map{|pth| pth.include?(" ") ? "\"#{pth}\"" : pth }
132
+ path_arg = paths.join(" ")
133
+ cmd = "#{nsh_cmd("ncp")} -vrA #{path_arg} -h #{target_hosts.join(" ")} -d \"#{target_path}\"" unless target_hosts.nil?
134
+ #cmd = "#{nsh_cmd("cp")} -vr #{path_arg.gsub("localhost","@")} #{target_path}" if target_hosts.nil?
135
+ if target_hosts.nil? # Local copy
136
+ FileUtils.cp_r path_arg.gsub("//localhost",""), target_path, :verbose => true
137
+ res = "cp #{path_arg.gsub("//localhost","")} #{target_path}"
138
+ else
139
+ cmd = @test_mode ? "echo \"#{cmd}\"" : cmd
140
+ log cmd if @verbose
141
+ result = execute_shell(cmd)
142
+ res = display_result(result)
143
+ end
144
+ res
145
+ end
146
+
147
+ # Runs a command via nsh on a windows target
148
+ #
149
+ # ==== Attributes
150
+ #
151
+ # * +target_hosts+ - blade hostnames to copy to
152
+ # * +target_path+ - path to copy to (same for all target_hosts)
153
+ # * +command+ - command to run
154
+ #
155
+ # ==== Returns
156
+ #
157
+ # * results of command per host
158
+ def nexec_win(target_hosts, target_path, command)
159
+ # if source_script exists, transport it to the hosts
160
+ result = "Running: #{command}\n"
161
+ target_hosts.each do |host|
162
+ cmd = "#{nsh_cmd("nexec")} #{host} cmd /c \"cd #{target_path}; #{command}\""
163
+ cmd = @test_mode ? "echo \"#{cmd}\"" : cmd
164
+ result += "Host: #{host}\n"
165
+ res = execute_shell(cmd)
166
+ result += display_result(res)
167
+ end
168
+ result
169
+ end
170
+
171
+ # Runs a script on a remote server via NSH
172
+ #
173
+ # ==== Attributes
174
+ #
175
+ # * +target_hosts+ - blade hostnames to copy to
176
+ # * +script_path+ - nsh path to the script
177
+ # * +target_path+ - path from which to execute the script on the remote host
178
+ # * +options+ - hash of options (raw_result = true)
179
+ #
180
+ # ==== Returns
181
+ #
182
+ # * results of command per host
183
+ def script_exec(target_hosts, script_path, target_path, options = {})
184
+ raw_result = get_option(options,"raw_result", false)
185
+ script_dir = File.dirname(script_path)
186
+ err_file = touch_file("#{script_dir}/nsh_errors_#{Time.now.strftime("%Y%m%d%H%M%S%L")}.txt")
187
+ script_path = "\"#{script_path}\"" if script_path.include?(" ")
188
+ cmd = "#{nsh_cmd("scriptutil")} -d \"#{nsh_path(target_path)}\" -h #{target_hosts.join(" ")} -s #{script_path}"
189
+ cmd = cmd + " 2>#{err_file}" unless BrpmAuto.windows?
190
+ result = execute_shell(cmd)
191
+ result["stderr"] = "#{result["stderr"]}\n#{File.open(err_file).read}"
192
+ result = display_result(result) unless raw_result
193
+ result
194
+ end
195
+
196
+ # Executes a text variable as a script on remote targets
197
+ #
198
+ # ==== Attributes
199
+ #
200
+ # * +target_hosts+ - array of target hosts
201
+ # * +script_body+ - body of script
202
+ # * +target_path+ - path on targets to store/execute script
203
+ #
204
+ # ==== Returns
205
+ #
206
+ # * output of script
207
+ #
208
+ def script_execute_body(target_hosts, script_body, target_path, options = {})
209
+ ext = get_option(options,"platform", "linux").downcase == "linux" ? ".sh" : ".bat"
210
+ script_file = "nsh_script_#{Time.now.strftime("%Y%m%d%H%M%S")}#{ext}"
211
+ full_path = File.join(@params["SS_output_dir"],script_file)
212
+ fil = File.open(full_path,"w+")
213
+ fil.write script_body.gsub("\r", "")
214
+ fil.flush
215
+ fil.close
216
+ result = script_exec(target_hosts, full_path, target_path, options)
217
+ end
218
+
219
+ # Runs a simple ls command in NSH
220
+ #
221
+ # ==== Attributes
222
+ #
223
+ # * +nsh_path+ - path to list files
224
+ #
225
+ # ==== Returns
226
+ #
227
+ # * array of path contents
228
+ def ls(nsh_path)
229
+ res = nsh_command("ls #{nsh_path}")
230
+ res.split("\n").reject{|l| l.start_with?("Running ")}
231
+ end
232
+
233
+ # Provides a host status for the passed targets
234
+ #
235
+ # ==== Attributes
236
+ #
237
+ # * +target_hosts+ - array of hosts
238
+ #
239
+ # ==== Returns
240
+ #
241
+ # * hash of agentinfo on remote hosts
242
+ def status(target_hosts)
243
+ result = {}
244
+ target_hosts.each do |host|
245
+ res = nsh_command("agentinfo #{host}")
246
+ result[host] = res
247
+ end
248
+ result
249
+ end
250
+
251
+ # Returns the nsh path from a dos path
252
+ #
253
+ # ==== Attributes
254
+ #
255
+ # * +source_path+ - path in nsh
256
+ # * +server+ - optional, adds a server in nsh format
257
+ #
258
+ # ==== Returns
259
+ #
260
+ # * nsh compatible path
261
+ #
262
+ def nsh_path(source_path, server = nil)
263
+ path = ""
264
+ if source_path.include?(":\\")
265
+ path_array = source_path.split("\\")
266
+ path = "/#{path_array[0].gsub(":","/")}"
267
+ path += path_array[1..-1].join("/")
268
+ else
269
+ path = source_path.gsub(":","")
270
+ path = "/#{path}" unless path.start_with?("/")
271
+ end
272
+ path = "//#{server}#{path}" unless server.nil?
273
+ path.chomp("/")
274
+ end
275
+
276
+ # Builds an NSH compatible path for an uploaded file to BRPM
277
+ #
278
+ # ==== Attributes
279
+ #
280
+ # * +attachment_local_path+ - path to attachment from params
281
+ # * +brpm_hostname+ - name of brpm host (as accessible from NSH)
282
+ # ==== Returns
283
+ #
284
+ # nsh path
285
+ #
286
+ def get_attachment_nsh_path(attachment_local_path, brpm_hostname)
287
+ if attachment_local_path[1] == ":"
288
+ attachment_local_path[1] = attachment_local_path[0]
289
+ attachment_local_path[0] = '/'
290
+ end
291
+ attachment_local_path = attachment_local_path.gsub(/\\/, "/")
292
+ "//#{brpm_hostname}#{attachment_local_path}"
293
+ end
294
+
295
+ # Zip files using NSH
296
+ #
297
+ # ==== Attributes
298
+ #
299
+ # * +staging_path+ - path to files
300
+ # * +package_name+ - name of zip file to create
301
+ # ==== Returns
302
+ #
303
+ # hash of instance_path and md5 - {"instance_path" => "", "md5" => ""}
304
+ def package_staged_artifacts(staging_path, package_name)
305
+ instance_path = File.join(staging_path, package_name)
306
+ staging_artifacts = Dir.entries(staging_path).reject{|k| [".",".."].include?(k) }
307
+ return {"instance_path" => "ERROR - no files in staging area", "md5" => ""} if staging_artifacts.size < 1
308
+ FileUtils.cd(staging_path, :verbose => true)
309
+ cmd = "#{nsh_cmd("zip")} -r #{package_name} *"
310
+ result = execute_shell(cmd)
311
+ md5 = Digest::MD5.file(instance_path).hexdigest
312
+ {"instance_path" => instance_path, "md5" => md5, "manifest" => staging_artifacts }
313
+ end
314
+
315
+ # Copies a single file from source to destination via nsh paths
316
+ #
317
+ # ==== Attributes
318
+ #
319
+ # * +src_path+ - NSH path to source files
320
+ # * +target_path+ - NSH path to copy to
321
+ #
322
+ # ==== Returns
323
+ #
324
+ # * results of command
325
+ def cp(src_path, target_path)
326
+ cmd = "#{nsh_cmd("cp")} -f #{src_path} \"#{target_path}\""
327
+ cmd = @test_mode ? "echo \"#{cmd}\"" : cmd
328
+ log cmd if @verbose
329
+ result = execute_shell(cmd)
330
+ res = display_result(result)
331
+ res
332
+ end
333
+
334
+ private
335
+
336
+ def create_temp_script(body, options = {})
337
+ script_type = get_option(options,"script_type", "nsh")
338
+ base_path = get_option(options, "temp_path", platform_temp)
339
+ tmp_file = "#{script_type}_temp_#{precision_timestamp}.#{script_type}"
340
+ full_path = "#{base_path}/#{tmp_file}"
341
+ fil = File.open(full_path,"w+")
342
+ fil.puts body
343
+ fil.flush
344
+ fil.close
345
+ full_path
346
+ end
347
+
348
+ def nsh_cmd(cmd)
349
+ res = File.join(@nsh_path, "bin", cmd)
350
+ res = "\"#{res}\"" if res.include?(" ")
351
+ res
352
+ end
353
+
354
+ def platform_temp
355
+ res = "/tmp"
356
+ res = "C:/Windows/temp" if BrpmAuto.windows?
357
+ res
358
+ end
359
+ end
@@ -0,0 +1,220 @@
1
+
2
+ class TransportSSH
3
+ # Helper Routines for Capistrano
4
+ require 'capistrano'
5
+ require 'capistrano/cli'
6
+ require 'timeout'
7
+
8
+ # Initialize the class
9
+ #
10
+ # ==== Attributes
11
+ #
12
+ # * +servers+ - array of servers to use
13
+ # * +params+ - the params hash
14
+ # * +options+ - hash of options to use, user, password, sudo (yes/no), timeout (minutes)
15
+ #
16
+ def initialize(servers, options = {}, compat_options = {})
17
+ self.extend Utilities
18
+ if options.has_key?("SS_output_dir")
19
+ BrpmAuto.log "Load for this class has changed, no longer necessary to send params as 2nd argument"
20
+ options = compat_options
21
+ end
22
+ @user = get_option(options, "user", nil)
23
+ @password = get_option(options, "password", nil)
24
+ @servers = servers
25
+ @sudo = get_option(options, "sudo", nil)
26
+ @debug = get_option(options, "debug", false)
27
+ @cap = Capistrano::Configuration.new
28
+ @cap.logger.level = Capistrano::Logger::TRACE
29
+ maxtime = get_option(options, "timeout", 60)
30
+ @maxtime = maxtime.to_i * 60
31
+ end
32
+
33
+ # Resets the security credentials
34
+ #
35
+ # ==== Attributes
36
+ #
37
+ # * +options+ - hash of options [user,password,sudo, debug, servers]
38
+ #
39
+ def set_credential(options = {})
40
+ @user = get_option(options, "user", @user)
41
+ @password = get_option(options, "password", @password)
42
+ @servers = get_option(options, "servers", @servers)
43
+ @sudo = get_option(options, "sudo", @sudo)
44
+ @debug = get_option(options, "debug", @debug)
45
+ end
46
+
47
+ # Execute a command on remote targets
48
+ #
49
+ # ==== Attributes
50
+ #
51
+ # * +command+ - command to execute
52
+ # * +options+ - hash of options includes servers to override class servers
53
+ #
54
+ # ==== Returns
55
+ #
56
+ # command_run hash {stdout => <results>, stderr => any errors, pid => process id, status => exit_code}
57
+ def execute_command(command, options = {})
58
+ @servers = get_option(options, "servers", @servers)
59
+ execute_cap "command", {"command" => command}
60
+ end
61
+
62
+ # Copy files to remote targets
63
+ #
64
+ # ==== Attributes
65
+ #
66
+ # * +source_files+ - array of files to copy
67
+ # * +target_path+ - path on target hosts to copy to
68
+ # * +options+ - hash of options includes servers to override class servers
69
+ #
70
+ # ==== Returns
71
+ #
72
+ # command_run hash {stdout => <results>, stderr => any errors, pid => process id, status => exit_code}
73
+ def copy_files(source_files, target_path, options = {})
74
+ @servers = get_option(options, "servers", @servers)
75
+ source_files = [source_files] if source_files.is_a?(String)
76
+ execute_cap "upload", {"source_files" => source_files, "target_path" => target_path}
77
+ end
78
+
79
+ # Download files to staging from remote targets
80
+ #
81
+ # ==== Attributes
82
+ #
83
+ # * +source_files+ - array of file paths to copy
84
+ # * +staging_path+ - path on local server to copy to
85
+ # * +options+ - hash of options includes servers to override class servers
86
+ #
87
+ # ==== Returns
88
+ #
89
+ # command_run hash {stdout => <results>, stderr => any errors, pid => process id, status => exit_code}
90
+ def download_files(source_files, target_path, options = {})
91
+ @servers = get_option(options, "servers", @servers)
92
+ source_files = [source_files] if source_files.is_a?(String)
93
+ execute_cap "download", {"source_files" => source_files, "target_path" => target_path}
94
+ end
95
+
96
+ # Copies script to remote targets and executes it (bin/bash)
97
+ #
98
+ # ==== Attributes
99
+ #
100
+ # * +script_path+ - path to script file on local host
101
+ # * +target_path+ - path on target servers to copy to
102
+ # * +options+ - hash of options includes servers to override class servers
103
+ #
104
+ # ==== Returns
105
+ #
106
+ # command_run hash {stdout => <results>, stderr => any errors, pid => process id, status => exit_code}
107
+ def script_exec(script_path, target_path, options = {})
108
+ @servers = get_option(options, "servers", @servers)
109
+ copy_files [script_path], target_path
110
+ cmd = "/bin/bash #{File.join(target_path, File.basename(script_path))}"
111
+ result = execute_command(cmd)
112
+ cmd = "rm -f #{File.join(target_path, File.basename(script_path))}"
113
+ cleanunp_result = execute_command(cmd) unless @debug
114
+ result
115
+ end
116
+
117
+ # Resets the servers for ssh execution
118
+ #
119
+ # ==== Attributes
120
+ #
121
+ # * +servers+ - array of servers
122
+ #
123
+ def set_servers(servers)
124
+ @servers = servers
125
+ end
126
+
127
+ private
128
+
129
+ # Executes the capistrano command in a timeout loop integrating stderr and stdout
130
+ #
131
+ # ==== Attributes
132
+ #
133
+ # * +cap_method+ - can be command, upload, download
134
+ # * +options+ - hash of options specific to the command type
135
+ #
136
+ # ==== Returns
137
+ #
138
+ # * command_run hash {stdout => <results>, stderr => any errors, pid => process id, status => exit_code}
139
+ def execute_cap(cap_method, options = {}, max_time = @max_time)
140
+ cmd_result = {"stdout" => "","stderr" => "", "pid" => "", "status" => 1}
141
+ show_errors = true #@params.has_key?("ignore_exit_codes") ? !(@params["ignore_exit_codes"] == 'yes') : false
142
+ @cap.set :user, @user unless @user.nil?
143
+ @cap.set :password, @password unless @password.nil?
144
+ @cap.role :all do
145
+ @servers
146
+ end
147
+ cmd_result["stdout"] = "Capistrano Execution\n"
148
+ output_dir = File.join("/tmp","#{"brady333"}") #FIXME
149
+ outfile = "#{output_dir}_stderr.txt"
150
+ cmd_result["stdout"] += "Script Output:\n"
151
+ begin
152
+ orig_stderr = $stderr.clone
153
+ $stderr.reopen File.open(outfile, 'a' )
154
+ fil = File.open(outfile, 'a' )
155
+ timer_status = Timeout.timeout(max_time) {
156
+ rescue_cap_errors(show_errors) do
157
+ if cap_method == "command"
158
+ command = options["command"]
159
+ cmd_result["stdout"] += "Command: #{command}"
160
+ use_sudo = @sudo.nil? ? "no" : @sudo
161
+ @cap.run "#{use_sudo == 'yes' ? sudo : '' } #{command}", :pty => (use_sudo == 'yes') do |ch, str, data|
162
+ if str == :out
163
+ fil.puts data
164
+ fil.flush
165
+ elsif str == :err
166
+ cmd_result["stderr"] += data if data.length > 4
167
+ end
168
+ end
169
+ elsif cap_method == "upload"
170
+ source_files = options["source_files"]
171
+ target_path = options["target_path"]
172
+ cmd_result["stdout"] += "Upload: #{source_files.join(",")} |To: #{target_path}"
173
+ source_files.each do |file_path|
174
+ ans = split_nsh_path(file_path)
175
+ f_path = ans[1]
176
+ cmd_result["stdout"] += "File: #{f_path}\n"
177
+ @cap.upload f_path, target_path, :via => :scp
178
+ end
179
+ elsif cap_method == "download"
180
+ source_files = options["source_files"]
181
+ target_path = options["target_path"]
182
+ cmd_result["stdout"] += "Download: #{source_files.join(",")} |To: #{target_path}"
183
+ source_files.each do |file_path|
184
+ ans = split_nsh_path(file_path)
185
+ f_path = ans[1]
186
+ cmd_result["stdout"] += "File: #{f_path}\n"
187
+ target_file = File.join(target_path, File.basename(f_path))
188
+ @cap.download f_path, target_file, :via => :scp
189
+ end
190
+ end
191
+ end
192
+ }
193
+ fil.close
194
+ fil1 = File.open(outfile)
195
+ output = fil1.read
196
+ fil1.close
197
+ cmd_result["stdout"] = output if output.length > 2
198
+ cmd_result["status"] = 0
199
+ rescue Exception => e
200
+ $stderr.reopen orig_stderr
201
+ cmd_result["stderr"] += "ERROR\n#{e.message}\n#{e.backtrace}"
202
+ ensure
203
+ $stderr.reopen orig_stderr
204
+ end
205
+ File.delete(outfile)
206
+ cmd_result
207
+ end
208
+
209
+ def rescue_cap_errors(show_errors, &block)
210
+ begin
211
+ yield
212
+ rescue RuntimeError => failure
213
+ if show_errors
214
+ BrpmAuto.log "SSH-Capistrano_Error: #{failure.message}\n#{failure.backtrace}"
215
+ BrpmAuto.log "#{EXIT_CODE_FAILURE}"
216
+ end
217
+ end
218
+ end
219
+
220
+ end