Class: Quickmox::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/quickmox/host.rb

Overview

This class represents a SSH server host. It may or may not be connected to a Proxmox server. If it’s not, proxmox specific methods like guests(), guest_params() etc. will return empty data structures.

Defined Under Namespace

Classes: HostError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Host

Returns a new instance of Host.



22
23
24
25
26
27
28
29
30
31
# File 'lib/quickmox/host.rb', line 22

def initialize(transport)
  @session = transport
  @guests = Guestlist.new

  begin
    @ip = Resolv.getaddress(@session.host)
  rescue => e
    @ip = String.new
  end
end

Instance Attribute Details

#guestsObject

Returns the value of attribute guests.



15
16
17
# File 'lib/quickmox/host.rb', line 15

def guests
  @guests
end

#hostnameObject

Returns the value of attribute hostname.



15
16
17
# File 'lib/quickmox/host.rb', line 15

def hostname
  @hostname
end

#ipObject

Returns the value of attribute ip.



15
16
17
# File 'lib/quickmox/host.rb', line 15

def ip
  @ip
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'lib/quickmox/host.rb', line 15

def password
  @password
end

#sessionObject

Returns the value of attribute session.



15
16
17
# File 'lib/quickmox/host.rb', line 15

def session
  @session
end

#usernameObject

Returns the value of attribute username.



15
16
17
# File 'lib/quickmox/host.rb', line 15

def username
  @username
end

Instance Method Details

#closeObject



75
76
77
# File 'lib/quickmox/host.rb', line 75

def close
  handle_exceptions { disconnect }
end

#connectObject



33
34
35
36
# File 'lib/quickmox/host.rb', line 33

def connect
  handle_exceptions { @session.connect }
  self
end

#disconnectObject



79
80
81
# File 'lib/quickmox/host.rb', line 79

def disconnect
  handle_exceptions { session.close }
end

#exec(cmd) ⇒ Object



89
90
91
# File 'lib/quickmox/host.rb', line 89

def exec(cmd)
  handle_exceptions {session.exec!(cmd).chomp}
end

#guestlistObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quickmox/host.rb', line 52

def guestlist
  list = String.new
  handle_exceptions do
    list = Array.new
    table = session.exec!('qm list')
    lines = table.split("\n")
    lines.each do |line|
      if line =~ /^ *([0-9]{1,4}) */
        list << $1
      end
    end
  end
  list
end

#is_proxmox?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'lib/quickmox/host.rb', line 83

def is_proxmox?
    output = String.new
    handle_exceptions { output = session.exec!('qm list') }
    (output =~ /VMID NAME/) ? true : false
end

#localnameObject



67
68
69
# File 'lib/quickmox/host.rb', line 67

def localname
  handle_exceptions { session.exec!('hostname').chomp }
end

#rescanObject



38
39
40
# File 'lib/quickmox/host.rb', line 38

def rescan
  scan
end

#scanObject



42
43
44
45
46
47
48
49
50
# File 'lib/quickmox/host.rb', line 42

def scan
  handle_exceptions do
    guestlist.each do |id|
      @guests << Guest.new(id, self)
    end
    @guests.scan
  end
  self
end

#uptimeObject



71
72
73
# File 'lib/quickmox/host.rb', line 71

def uptime
  handle_exceptions { session.exec!('uptime').chomp }
end