mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Added a way to locally retrieve the ipaddress through the ip_command
More checks on correctness of ipaddress And checks on ssh failures
This commit is contained in:
parent
4eb72dabc9
commit
43bf83cd6b
1 changed files with 66 additions and 24 deletions
|
@ -250,50 +250,92 @@ module Fog
|
||||||
##Note this requires arpwatch to be running
|
##Note this requires arpwatch to be running
|
||||||
##and chmod o+x /var/lib/arpwatch
|
##and chmod o+x /var/lib/arpwatch
|
||||||
|
|
||||||
def addresses
|
# This retrieves the ip address of the mac address
|
||||||
|
# It returns an array of public and private ip addresses
|
||||||
|
# Currently only one ip address is returned, but in the future this could be multiple
|
||||||
|
# if the server has multiple network interface
|
||||||
|
#
|
||||||
|
def addresses(options={})
|
||||||
mac=self.mac
|
mac=self.mac
|
||||||
|
|
||||||
|
# Check if another ip_command string was provided
|
||||||
|
ip_command=options[:ip_command].nil? ? "grep #{mac} /var/log/arpwatch.log |cut -d ':' -f 4-| cut -d ' ' -f 4" : options[:ip_command]
|
||||||
|
|
||||||
ipaddress=nil
|
ipaddress=nil
|
||||||
|
|
||||||
if @connection.uri.ssh_enabled?
|
if @connection.uri.ssh_enabled?
|
||||||
#command="arp -an|grep #{mac}|cut -d ' ' -f 2| cut -d '(' -f 2| cut -d ')' -f 1"
|
|
||||||
#command="grep #{mac} /var/log/daemon.log |sed -e 's/^.*address //'|cut -d ' ' -f 1"
|
|
||||||
# TODO: check if this files exists
|
|
||||||
# Check if it is readable
|
|
||||||
command="grep #{mac} /var/log/arpwatch.log |cut -d ':' -f 4-| cut -d ' ' -f 4"
|
|
||||||
# command="grep #{mac} /var/lib/arpwatch/arp.dat|cut -f 2|tail -1"
|
|
||||||
|
|
||||||
# TODO: we need to take the time into account, when IP's are re-allocated, we might be executing
|
# Retrieve the parts we need from the connection to setup our ssh options
|
||||||
# On the wrong host
|
|
||||||
|
|
||||||
# We can get the host, the
|
|
||||||
user=connection.uri.user #could be nil
|
user=connection.uri.user #could be nil
|
||||||
host=connection.uri.host
|
host=connection.uri.host
|
||||||
keyfile=connection.uri.keyfile
|
keyfile=connection.uri.keyfile
|
||||||
port=connection.uri.port
|
port=connection.uri.port
|
||||||
|
|
||||||
options={}
|
# Setup the options
|
||||||
options[:keys]=[ keyfile ] unless keyfile.nil?
|
ssh_options={}
|
||||||
options[:port]=port unless keyfile.nil?
|
ssh_options[:keys]=[ keyfile ] unless keyfile.nil?
|
||||||
options[:paranoid]=true if connection.uri.no_verify?
|
ssh_options[:port]=port unless keyfile.nil?
|
||||||
|
ssh_options[:paranoid]=true if connection.uri.no_verify?
|
||||||
|
|
||||||
result=Fog::SSH.new(host, user, options).run(command)
|
# TODO: we need to take the time into account, when IP's are re-allocated, we might be executing
|
||||||
|
# On the wrong host
|
||||||
|
|
||||||
|
begin
|
||||||
|
result=Fog::SSH.new(host, user, ssh_options).run(ip_command)
|
||||||
|
rescue Errno::ECONNREFUSED
|
||||||
|
raise Fog::Errors::Error.new("Connection was refused to host #{host} to retrieve the ipaddress for #{mac}")
|
||||||
|
rescue Net::SSH::AuthenticationFailed
|
||||||
|
raise Fog::Errors::Error.new("Error authenticating over ssh to host #{host} and user #{user}")
|
||||||
|
end
|
||||||
|
|
||||||
|
#TODO: We currently just retrieve the ip address through the ip_command
|
||||||
|
#TODO: We need to check if that Ipaddress is still valid for that mac-address
|
||||||
|
|
||||||
|
# Check for a clean exit code
|
||||||
if result.first.status == 0
|
if result.first.status == 0
|
||||||
ipaddress=result.first.stdout.strip
|
ipaddress=result.first.stdout.strip
|
||||||
#TODO check for valid IP
|
|
||||||
#TODO check time validity
|
|
||||||
else
|
else
|
||||||
# We couldn't retrieve any IP information
|
# We got a failure executing the command
|
||||||
return { :public => nil , :private => nil}
|
raise Fog::Errors::Error.new("The command #{ip_command} failed to execute with a clean exit code")
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
|
# It's not ssh enabled, so we assume it is
|
||||||
|
if @connection.uri.transport=="tls"
|
||||||
|
raise Fog::Errors::Error.new("TlS remote transport is not currently supported, only ssh")
|
||||||
|
end
|
||||||
|
|
||||||
|
IO.popen("#{ip_command}") do |p|
|
||||||
|
p.each_line do |l|
|
||||||
|
ipaddress=+l
|
||||||
|
end
|
||||||
|
status=Process.waitpid2(p.pid)[1].exitstatus
|
||||||
|
if status!=0
|
||||||
|
raise Fog::Errors::Error.new("The command #{ip_command} failed to execute with a clean exit code")
|
||||||
|
end
|
||||||
|
end
|
||||||
# TODO for locat execute
|
# TODO for locat execute
|
||||||
#No ssh just do it locally
|
#No ssh just do it locally
|
||||||
#cat /var/log/daemon.log|grep "52:54:00:52:f6:22"|
|
#cat /var/log/daemon.log|grep "52:54:00:52:f6:22"|
|
||||||
# or local execute arp -an to get the ip (as a last resort)
|
# or local execute arp -an to get the ip (as a last resort)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if ipaddress==""
|
||||||
|
#The grep didn't find an ip address result"
|
||||||
|
ipaddress=nil
|
||||||
|
else
|
||||||
|
# To be sure that the command didn't return another random string
|
||||||
# We check if the result is an actual ip-address
|
# We check if the result is an actual ip-address
|
||||||
# otherwise we return nil
|
# otherwise we return nil
|
||||||
ipaddress=nil unless ipaddress=~/^(\d{1,3}\.){3}\d{1,3}$/
|
unless ipaddress=~/^(\d{1,3}\.){3}\d{1,3}$/
|
||||||
|
raise Fog::Errors::Error.new(
|
||||||
|
"The command #{ip_command} failed to execute with a clean exit code\n"+
|
||||||
|
"Result was: #{ipaddress}\n"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return { :public => [ipaddress], :private => [ipaddress]}
|
return { :public => [ipaddress], :private => [ipaddress]}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -407,8 +449,8 @@ module Fog
|
||||||
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
|
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Retrieves the mac address from parsing the XML of the domain
|
||||||
def mac
|
def mac
|
||||||
|
|
||||||
mac = document("domain/devices/interface/mac", "address")
|
mac = document("domain/devices/interface/mac", "address")
|
||||||
return mac
|
return mac
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue