Class: VagrantPlugins::ProviderLibvirt::Action::ConnectLibvirt

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ConnectLibvirt

Returns a new instance of ConnectLibvirt.


8
9
10
11
# File 'lib/vagrant-libvirt/action/connect_libvirt.rb', line 8

def initialize(app, env)
  @logger = Log4r::Logger.new('vagrant_libvirt::action::connect_libvirt')
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-libvirt/action/connect_libvirt.rb', line 13

def call(env)
  # If already connected to libvirt, just use it and don't connect
  # again.
  if ProviderLibvirt.libvirt_connection
    env[:libvirt_compute] = ProviderLibvirt.libvirt_connection
    return @app.call(env)
  end

  # Get config options for libvirt provider.
  config = env[: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[ find /var/lib/libvirt/dnsmasq/ /var/lib/misc/ -name '*leases' -exec grep $mac {} \; | cut -d' ' -f3 ]
  conn_attr[:libvirt_ip_command] = ip_command

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

  @app.call(env)
end