Class: VagrantPlugins::XenServer::Action::ConnectXS

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-xenserver/action/connect_xs.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ConnectXS

Returns a new instance of ConnectXS.



9
10
11
12
# File 'lib/vagrant-xenserver/action/connect_xs.rb', line 9

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("vagrant::xenserver::actions::connect_xs")
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-xenserver/action/connect_xs.rb', line 14

def call(env)
  config = env[:machine].provider_config

  # Only even try to connect if we've got a hostname
  if (not env[:xc]) && (not config.xs_host.nil?)
    uri = URI::Generic.new(config.xs_use_ssl ? 'https' : 'http',
                      nil,
                      config.xs_host,
                      config.xs_port,
                      nil,
                      "/",
                      nil,
                      nil, nil)
    env[:xc] = XenApi::Client.new(uri.to_s, timeout = config.api_timeout)

    @logger.info("Connecting to XenServer")

    begin
      result = env[:xc].(config.xs_username, config.xs_password)
    rescue XenApi::Errors::SessionAuthenticationFailed
      raise Errors::LoginError
    rescue
      raise Errors::ConnectionError
    end

    @logger.info("Connected to XenServer")
  end

  @app.call(env)
end