Skip to content

Commit

Permalink
Do not assume hostname binary is available (#179)
Browse files Browse the repository at this point in the history
* Do not assume hostname binary is available

* Use canonical name

* Try Addrinfo.getaddrinfo if hostname file not found
  • Loading branch information
pschipitsch authored and zippolyte committed Sep 4, 2019
1 parent 3ba08d1 commit 2c2606b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/capistrano/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def record_hostname(hostname)
end

def report()
hostname = %x[hostname -f].strip
hostname = Dogapi.find_localhost
user = Etc.getlogin

# Lazy randomness
Expand Down
11 changes: 5 additions & 6 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'net/https'
require 'socket'
require 'uri'
require 'English'

require 'rubygems'
require 'multi_json'
Expand Down Expand Up @@ -163,11 +164,9 @@ def Dogapi.find_datadog_host
@@hostname = nil

def Dogapi.find_localhost
begin
# prefer hostname -f over Socket.gethostname
@@hostname ||= %x[hostname -f].strip
rescue
raise 'Cannot determine local hostname via hostname -f'
end
@@hostname ||= %x[hostname -f].strip
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
end

0 comments on commit 2c2606b

Please sign in to comment.