Skip to content

Commit

Permalink
Check exit code of hostname -f for failures (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
zippolyte authored Jan 30, 2020
1 parent 8a3b4e4 commit 309664f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'rubygems'
require 'multi_json'
require 'set'
require 'open3'

module Dogapi

Expand Down Expand Up @@ -200,9 +201,13 @@ def Dogapi.find_datadog_host
@@hostname = nil

def Dogapi.find_localhost
@@hostname ||= %x[hostname -f].strip
unless @@hostname
out, status = Open3.capture2('hostname', '-f', err: File::NULL)
@@hostname = out.strip
# Get status to check if the call was successful
raise SystemCallError, 'Could not get hostname with `hostname -f`' unless status.exitstatus.zero?
end
rescue SystemCallError
raise $ERROR_INFO unless $ERROR_INFO.class.name == 'Errno::ENOENT'
@@hostname = Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
end

Expand Down

0 comments on commit 309664f

Please sign in to comment.