Use the first address if cannot connect to any

This fixes #39995. The previous work happened in #39289.
This commit is contained in:
Ivan Babrou 2017-03-18 13:42:52 -07:00
parent 116201f345
commit af1545deed
No known key found for this signature in database
GPG Key ID: DBF6C142408CB4F0
3 changed files with 19 additions and 12 deletions

View File

@ -133,7 +133,7 @@ log = logging.getLogger(__name__)
# 6. Handle publications
def resolve_dns(opts, fallback=True, connect=True):
def resolve_dns(opts, fallback=True):
'''
Resolves the master_ip and master_uri options
'''
@ -150,7 +150,7 @@ def resolve_dns(opts, fallback=True, connect=True):
if opts['master'] == '':
raise SaltSystemExit
ret['master_ip'] = \
salt.utils.dns_check(opts['master'], opts['master_port'], True, opts['ipv6'], connect)
salt.utils.dns_check(opts['master'], opts['master_port'], True, opts['ipv6'])
except SaltClientError:
if opts['retry_dns']:
while True:
@ -164,7 +164,7 @@ def resolve_dns(opts, fallback=True, connect=True):
time.sleep(opts['retry_dns'])
try:
ret['master_ip'] = salt.utils.dns_check(
opts['master'], opts['master_port'], True, opts['ipv6'], connect
opts['master'], opts['master_port'], True, opts['ipv6']
)
break
except SaltClientError:

View File

@ -716,11 +716,12 @@ def ip_bracket(addr):
return addr
def dns_check(addr, port, safe=False, ipv6=None, connect=True):
def dns_check(addr, port, safe=False, ipv6=None):
'''
Return the ip resolved by dns, but do not exit on failure, only raise an
exception. Obeys system preference for IPv4/6 address resolution.
Tries to connect to the address before considering it useful.
Tries to connect to the address before considering it useful. If no address
can be reached, the first one resolved is used as a fallback.
'''
error = False
try:
@ -734,18 +735,21 @@ def dns_check(addr, port, safe=False, ipv6=None, connect=True):
error = True
else:
resolved = False
candidates = []
for h in hostnames:
# It's an IP address, just return it
if h[4][0] == addr:
resolved = addr
break
if h[0] == socket.AF_INET and ipv6 is True:
continue
if h[0] == socket.AF_INET6 and ipv6 is False:
continue
if h[0] == socket.AF_INET6 and connect is False and ipv6 is None:
continue
candidate_addr = ip_bracket(h[4][0])
if not connect:
resolved = candidate_addr
if h[0] != socket.AF_INET6 or ipv6 is not None:
candidates.append(candidate_addr)
s = socket.socket(h[0], socket.SOCK_STREAM)
try:
@ -757,7 +761,10 @@ def dns_check(addr, port, safe=False, ipv6=None, connect=True):
except socket.error:
pass
if not resolved:
error = True
if len(candidates) > 0:
resolved = candidates[0]
else:
error = True
except TypeError:
err = ('Attempt to resolve address \'{0}\' failed. Invalid or unresolveable address').format(addr)
raise SaltSystemExit(code=42, msg=err)

View File

@ -364,7 +364,7 @@ class ConfigTestCase(TestCase, integration.AdaptedConfigurationTestCaseMixIn):
syndic_opts = sconfig.syndic_config(
syndic_conf_path, minion_conf_path
)
syndic_opts.update(salt.minion.resolve_dns(syndic_opts, connect=False))
syndic_opts.update(salt.minion.resolve_dns(syndic_opts))
root_dir = syndic_opts['root_dir']
# id & pki dir are shared & so configured on the minion side
self.assertEqual(syndic_opts['id'], 'minion')