Class: Virt::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/virt/connection.rb

Direct Known Subclasses

Host

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/virt/connection.rb', line 6

def initialize uri, options = {}
  raise("Must provide a host to connect to") unless uri
  if uri =~ /^(esx|vpx)/
    raise("Must provide a username and password") unless options[:username] or options[:password]
    @connection = Libvirt::open_auth(uri, [Libvirt::CRED_AUTHNAME, Libvirt::CRED_PASSPHRASE]) do |cred|
      # This may only be required for ESXi connections, not sure.
      @type = "VMWare"
      case cred['type']
      when ::Libvirt::CRED_AUTHNAME
        options[:username]
      when ::Libvirt::CRED_PASSPHRASE
        options[:password]
      end
    end
  else
    @type = "KVM"
    @connection = Libvirt::open uri
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/virt/connection.rb', line 4

def connection
  @connection
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/virt/connection.rb', line 4

def type
  @type
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/virt/connection.rb', line 26

def closed?
  connection.closed?
end

#disconnectObject



38
39
40
# File 'lib/virt/connection.rb', line 38

def disconnect
  connection.close
end

#hostObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/virt/connection.rb', line 42

def host
  case type
  when "KVM"
    KVM::Host.new
  when "VMWare"
    VMWare::Host.new
  else
    raise "Non supported hypervisor"
  end
end

#secure?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/virt/connection.rb', line 30

def secure?
  connection.encrypted?
end

#versionObject



34
35
36
# File 'lib/virt/connection.rb', line 34

def version
  connection.libversion
end