Class: ConnectableServer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbvppc/connectable_server.rb

Direct Known Subclasses

Hmc, Nim

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, username, options) ⇒ ConnectableServer

Returns a new instance of ConnectableServer.



23
24
25
26
27
28
29
# File 'lib/rbvppc/connectable_server.rb', line 23

def initialize(hostname, username, options)
    options[:protocol] = :ssh if !options.has_key? :protocol
    options[:port] = 22 if (options[:protocol] == :ssh) && (!options.has_key? :port)
    @hostname, @username, @options = hostname, username, options
    @session = nil
    @debug ||= true
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



21
22
23
# File 'lib/rbvppc/connectable_server.rb', line 21

def debug
  @debug
end

Instance Method Details

#connectObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rbvppc/connectable_server.rb', line 41

def connect
    if (@options[:key] && !@options[:password]) then
        @session = Net::SSH.start(@hostname, @username, :key => @options[:key], :port => @options[:port])
    elsif (@options[:key] && @options[:password])
        @session = Net::SSH.start(@hostname, @username, :key => @options[:key], :passphrase => @options[:password], :port => @options[:port])
    elsif (@options[:key_data] && @options[:passphrase])
        @session = Net::SSH.start(@hostname, @username, :keys => @options[:keys], :key_data => @options[:key_data], :keys_only => @options[:keys_only], :passphrase => @options[:passphrase], :port => @options[:port])    
    else
        @session = Net::SSH.start(@hostname, @username, :password => @options[:password], :port => @options[:port])
    end
    true
end

#connected?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rbvppc/connectable_server.rb', line 31

def connected?
    ! @session.nil?
end

#disconnectObject



88
89
90
91
# File 'lib/rbvppc/connectable_server.rb', line 88

def disconnect
  @session.close
  @session = nil
end

#execute_cmd(command) ⇒ Object

Raises:

  • (StandardError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbvppc/connectable_server.rb', line 54

def execute_cmd(command)
    raise StandardError.new("No connection has been established") if !connected?
    stdout_data = ""
    stderr_data = ""
    exit_code = nil
    exit_signal = nil
    @session.open_channel do |channel|
	 channel.exec(command) do |ch, success|
  	  unless success
       abort "FAILED: couldn't execute command (@session.channel.exec)"
      end
  	  channel.on_data do |ch,data|
       stdout_data+=data
      end

      channel.on_extended_data do |ch,type,data|
       stderr_data+=data
      end

      channel.on_request("exit-status") do |ch,data|
       exit_code = data.read_long
  	  end

  	  channel.on_request("exit-signal") do |ch, data|
       exit_signal = data.read_long
  	  end
    end
   end
  @session.loop
  raise CommandFailure.new(stderr_data, exit_code, exit_signal) if exit_code != 0
  return stdout_data
  # {:stdout => stdout_data, :stderr => stderr_data, :exit_code => exit_code, :exit_signal => exit_signal}
end

#toggle_debugObject

Debug attribute toggle for childen classes to employ for logging purposes



37
38
39
# File 'lib/rbvppc/connectable_server.rb', line 37

def toggle_debug
  @debug ^= true
end