Class: VagrantPlugins::OVirtProvider::Action::ConnectOVirt

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-ovirt/action/connect_ovirt.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ConnectOVirt

Returns a new instance of ConnectOVirt.



10
11
12
13
# File 'lib/vagrant-ovirt/action/connect_ovirt.rb', line 10

def initialize(app, env)
  @logger = Log4r::Logger.new("vagrant_ovirt::action::connect_ovirt")
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vagrant-ovirt/action/connect_ovirt.rb', line 15

def call(env)

  # We need both, fog and rbovirt client. Sometimes fog doesn't
  # support some operations like managing quotas, or working with
  # networks. For this rbovirt client is used.
  env[:ovirt_client] = OVirtProvider.ovirt_client if \
    OVirtProvider.ovirt_client != nil

  if OVirtProvider.ovirt_connection != nil
    env[:ovirt_compute] = OVirtProvider.ovirt_connection
    return @app.call(env)
  end
  
  # Get config options for ovirt provider.
  config = env[:machine].provider_config

  conn_attr = {}
  conn_attr[:provider] = 'ovirt'
  conn_attr[:ovirt_url] = "#{config.url}/api"
  conn_attr[:ovirt_username] = config.username if config.username
  conn_attr[:ovirt_password] = config.password if config.password

  # We need datacenter id in fog connection initialization. But it's
  # much simpler to use datacenter name in Vagrantfile. So get
  # datacenter id here from rbovirt client before connecting to fog.
  env[:ovirt_client] = ovirt_connect(conn_attr)
  begin
    datacenter = OVirtProvider::Util::Collection.find_matching(
      env[:ovirt_client].datacenters, config.datacenter)
  rescue OVIRT::OvirtException => e
    raise Errors::FogOVirtConnectionError,
      :error_message => e.message
  end

  raise Errors::NoDatacenterError if datacenter == nil
  conn_attr[:ovirt_datacenter] = datacenter.id

  # Reconnect and prepar rbovirt client with datacenter set from
  # configuration.
  env[:ovirt_client] = ovirt_connect(conn_attr)
  OVirtProvider.ovirt_client = env[:ovirt_client]
  
  # Establish fog connection now.
  @logger.info("Connecting to oVirt (#{config.url}) ...")
  begin
    env[:ovirt_compute] = Fog::Compute.new(conn_attr)
  rescue OVIRT::OvirtException => e
    raise Errors::FogOVirtConnectionError,
      :error_message => e.message
  end
  OVirtProvider.ovirt_connection = env[:ovirt_compute]

  @app.call(env)
end