Class: VagrantPlugins::ProviderLibvirt::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Driver

Returns a new instance of Driver.



21
22
23
24
# File 'lib/vagrant-libvirt/driver.rb', line 21

def initialize(machine)
  @logger = Log4r::Logger.new('vagrant_libvirt::driver')
  @machine = machine
end

Instance Method Details

#connectionObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant-libvirt/driver.rb', line 26

def connection
  # If already connected to Libvirt, just use it and don't connect
  # again.
  return @connection if @connection

  # Get config options for Libvirt provider.
  config = @machine.provider_config
  uri = config.uri

  conn_attr = {}
  conn_attr[:provider] = 'libvirt'
  conn_attr[:libvirt_uri] = uri
  conn_attr[:libvirt_username] = config.username if config.username
  conn_attr[:libvirt_password] = config.password if config.password

  # Setup command for retrieving IP address for newly created machine
  # with some MAC address. Get it from dnsmasq leases table
  ip_command = %q( awk "/$mac/ {print \$1}" /proc/net/arp )
  conn_attr[:libvirt_ip_command] = ip_command

  @logger.info("Connecting to Libvirt (#{uri}) ...")
  begin
    @connection = Fog::Compute.new(conn_attr)
  rescue Fog::Errors::Error => e
    raise Errors::FogLibvirtConnectionError,
          error_message: e.message
  end

  @connection
end

#created?(machine) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/vagrant-libvirt/driver.rb', line 83

def created?(machine)
  domain = get_domain(machine)
  !domain.nil?
end

#get_domain(machine) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vagrant-libvirt/driver.rb', line 68

def get_domain(machine)
  begin
    domain = connection.servers.get(machine.id)
  rescue Libvirt::RetrieveError => e
    if e.libvirt_code == ProviderLibvirt::Util::ErrorCodes::VIR_ERR_NO_DOMAIN
      @logger.debug("machine #{machine.name} domain not found #{e}.")
      return nil
    else
      raise e
    end
  end

  domain
end

#get_domain_ipaddress(machine, domain) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vagrant-libvirt/driver.rb', line 100

def get_domain_ipaddress(machine, domain)
  if @machine.provider_config.qemu_use_session
    return get_ipaddress_from_system domain.mac
  end

  # attempt to get ip address from qemu agent
  if @machine.provider_config.qemu_use_agent == true
    @logger.info('Get IP via qemu agent')
    return get_ipaddress_from_qemu_agent(domain, machine.id)
  end

  # Get IP address from dhcp leases table
  begin
    ip_address = get_ipaddress_from_domain(domain)
  rescue Fog::Errors::TimeoutError
    @logger.info('Timeout at waiting for an ip address for machine %s' % machine.name)

    raise
  end

  unless ip_address
    @logger.info('No arp table entry found for machine %s' % machine.name)
    return nil
  end

  ip_address
end

#get_ipaddress(machine) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-libvirt/driver.rb', line 88

def get_ipaddress(machine)
  # Find the machine
  domain = get_domain(machine)

  if domain.nil?
    # The machine can't be found
    return nil
  end

  get_domain_ipaddress(machine, domain)
end

#state(machine) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/vagrant-libvirt/driver.rb', line 128

def state(machine)
  # may be other error states with initial retreival we can't handle
  begin
    domain = get_domain(machine)
  rescue Libvirt::RetrieveError => e
    @logger.debug("Machine #{machine.id} not found #{e}.")
    return :not_created
  end

  # TODO: terminated no longer appears to be a valid fog state, remove?
  return :not_created if domain.nil? || domain.state.to_sym == :terminated

  state = domain.state.tr('-', '_').to_sym
  if state == :running
    begin
      get_domain_ipaddress(machine, domain)
    rescue Fog::Errors::TimeoutError => e
      @logger.debug("Machine #{machine.id} running but no IP address available: #{e}.")
      return :inaccessible
    end
  end

  return state
end

#system_connectionObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/vagrant-libvirt/driver.rb', line 57

def system_connection
  # If already connected to Libvirt, just use it and don't connect
  # again.
  return @system_connection if @system_connection

  config = @machine.provider_config

  @system_connection = Libvirt::open(config.system_uri)
  @system_connection
end