rbvppc 1.0.1

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.
@@ -0,0 +1,95 @@
1
+ #
2
+ # Authors: Christopher M Wood (<woodc@us.ibm.com>)
3
+ # John F Hutchinson (<jfhutchi@us.ibm.com)
4
+ # © Copyright IBM Corporation 2015.
5
+ #
6
+ # LICENSE: MIT (http://opensource.org/licenses/MIT)
7
+ #
8
+ =begin
9
+ TODO:
10
+ 1.Add Connectivity Methods for Non SSH connections.
11
+ 2.Check if the password is passed in clear text or not
12
+
13
+ =end
14
+
15
+ require 'rubygems'
16
+ require 'net/ssh'
17
+ require_relative 'command_failure'
18
+
19
+ class ConnectableServer
20
+
21
+ attr_reader :debug
22
+
23
+ def initialize(hostname, username, options)
24
+ options[:protocol] = :ssh if !options.has_key? :protocol
25
+ options[:port] = 22 if (options[:protocol] == :ssh) && (!options.has_key? :port)
26
+ @hostname, @username, @options = hostname, username, options
27
+ @session = nil
28
+ @debug ||= false
29
+ end
30
+
31
+ def connected?
32
+ ! @session.nil?
33
+ end
34
+
35
+ #Debug attribute toggle for childen classes
36
+ #to employ for logging purposes
37
+ def toggle_debug
38
+ @debug ^= true
39
+ end
40
+
41
+ def connect
42
+ # @session = Net::SSH.start(@hostname, @username, :password => @password, :port => @port)
43
+ if (@options[:key] && !@options[:password]) then
44
+ @session = Net::SSH.start(@hostname, @username, :key => @options[:key], :port => @options[:port])
45
+ elsif (@options[:key] && @options[:password])
46
+ @session = Net::SSH.start(@hostname, @username, :key => @options[:key], :passphrase => @options[:password], :port => @options[:port])
47
+ else
48
+ @session = Net::SSH.start(@hostname, @username, :password => @options[:password], :port => @options[:port])
49
+ end
50
+ true
51
+ end
52
+
53
+ def execute_cmd(command)
54
+ raise StandardError.new("No connection has been established") if !connected?
55
+ stdout_data = ""
56
+ stderr_data = ""
57
+ exit_code = nil
58
+ exit_signal = nil
59
+ @session.open_channel do |channel|
60
+ channel.exec(command) do |ch, success|
61
+ unless success
62
+ abort "FAILED: couldn't execute command (@session.channel.exec)"
63
+ end
64
+ channel.on_data do |ch,data|
65
+ stdout_data+=data
66
+ end
67
+
68
+ channel.on_extended_data do |ch,type,data|
69
+ stderr_data+=data
70
+ end
71
+
72
+ channel.on_request("exit-status") do |ch,data|
73
+ exit_code = data.read_long
74
+ end
75
+
76
+ channel.on_request("exit-signal") do |ch, data|
77
+ exit_signal = data.read_long
78
+ end
79
+ end
80
+ end
81
+ @session.loop
82
+ raise CommandFailure.new(stderr_data, exit_code, exit_signal) if exit_code != 0
83
+ return stdout_data
84
+ # {:stdout => stdout_data, :stderr => stderr_data, :exit_code => exit_code, :exit_signal => exit_signal}
85
+ end
86
+
87
+ def disconnect
88
+ @session.close
89
+ @session = nil
90
+ end
91
+
92
+ def toggle_debug
93
+ @debug ^= true
94
+ end
95
+ end