Skip to content

Commit

Permalink
find_localhost: Try both hostname and hostname -f before raising (#…
Browse files Browse the repository at this point in the history
…242)

Do not explicitly use `hostname -s` (the opposite of `hostname -f`)
since it fails on Windows.
  • Loading branch information
albertvaka authored Dec 4, 2020
1 parent b2fd098 commit a7eddf9
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,17 @@ def Dogapi.find_datadog_host
@@hostname = nil

def Dogapi.find_localhost
unless @@hostname
out, status = Open3.capture2('hostname', '-f', err: File::NULL)
# Get status to check if the call was successful
raise SystemCallError, 'Could not get hostname with `hostname -f`' unless status.exitstatus.zero?
@@hostname = out.strip
end
rescue SystemCallError
@@hostname = Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
return @@hostname if @@hostname
out, status = Open3.capture2('hostname', '-f', err: File::NULL)
unless status.exitstatus.zero?
begin
out = Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
rescue SocketError
out, status = Open3.capture2('hostname', err: File::NULL)
raise SystemCallError, 'Both `hostname` and `hostname -f` failed.' unless status.exitstatus.zero?
end
end
@@hostname = out.strip
end

def Dogapi.find_proxy
Expand Down

0 comments on commit a7eddf9

Please sign in to comment.