Skip to content

Commit

Permalink
Fix the 'redirect to master' functionality. When authenticating towar…
Browse files Browse the repository at this point in the history
…ds a slave host, the error HOST_IS_SLAVE is returned by the slave host together with the IP address of the master host. Previously this resulted in an InvalidLogin exception which was hiding the actual cause. Now it results in an HostIsSlave exception which is then used to redirect the connection to the master host in case :xenserver_redirect_to_master is true.

This fix also removes the previous behaviour of doing a double login when the host is a master.
  • Loading branch information
kongslund committed Apr 14, 2015
1 parent dc4929d commit 5dfaded
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
17 changes: 9 additions & 8 deletions lib/fog/xenserver/compute.rb
Expand Up @@ -126,17 +126,18 @@ def initialize(options={})
@timeout = options[:xenserver_timeout] || 30
@redirect_to_master = options[:xenserver_redirect_to_master] || false
@connection = Fog::XenServer::Connection.new(@host, @timeout)

if @redirect_to_master == false
@connection = Fog::XenServer::Connection.new(@host, @timeout)
elsif @redirect_to_master == true
host_master = @connection.find_pool_master(@username, @password)
if host_master && host_master!= @host
@host = host_master
begin
@connection.authenticate(@username, @password)
rescue Fog::XenServer::HostIsSlave => e
if @redirect_to_master
Fog::Logger.debug "Redirecting to master #{e.master_ip}"
@host = e.master_ip
@connection = Fog::XenServer::Connection.new(@host, @timeout)
@connection.authenticate(@username, @password)
else
raise e
end
end
@connection.authenticate(@username, @password)
end

def reload
Expand Down
32 changes: 16 additions & 16 deletions lib/fog/xenserver/core.rb
Expand Up @@ -6,6 +6,14 @@ module XenServer
class InvalidLogin < Fog::Errors::Error; end
class NotFound < Fog::Errors::Error; end
class RequestFailed < Fog::Errors::Error; end
class HostIsSlave < Fog::Errors::Error
attr_reader :master_ip

def initialize(master_ip)
@master_ip = master_ip
end

end

extend Fog::Provider

Expand All @@ -21,24 +29,16 @@ def initialize(host, timeout)
@factory.timeout = timeout
end

def find_pool_master( username, password )
@credentials = authenticate( username, password )
response = @factory.call('host.get_all_records', @credentials)
if response['Status'] == "Failure"
if response['ErrorDescription'][0] == "HOST_IS_SLAVE"
ip_address = response['ErrorDescription'][1]
ip_address = ip_address.chomp
valid = !(IPAddr.new(ip_address) rescue nil).nil?
if valid
response['ErrorDescription'][1]
end
end
end
end

def authenticate( username, password )
response = @factory.call('session.login_with_password', username.to_s, password.to_s)
raise Fog::XenServer::InvalidLogin.new unless response["Status"] =~ /Success/
if response['Status'] == 'Failure'
if response['ErrorDescription'][0] == 'HOST_IS_SLAVE'
master_ip = response['ErrorDescription'][1]
raise Fog::XenServer::HostIsSlave.new(master_ip)
else
raise Fog::XenServer::InvalidLogin.new
end
end
@credentials = response["Value"]
end

Expand Down

0 comments on commit 5dfaded

Please sign in to comment.