MCPcopy Create free account
hub / github.com/saltstack/salt / dns_check

Function dns_check

salt/utils/network.py:2178–2236  ·  view source on GitHub ↗

Return an ip address resolved by dns in a format usable in URLs (ipv6 in brackets). Obeys system preference for IPv4/6 address resolution - this can be overridden by the ipv6 flag. Tries to connect to the address before considering it useful. If no address can be reached, the first

(addr, port, safe=False, ipv6=None)

Source from the content-addressed store, hash-verified

2176
2177@jinja_filter("dns_check")
2178def dns_check(addr, port, safe=False, ipv6=None):
2179 """
2180 Return an ip address resolved by dns in a format usable in URLs (ipv6 in brackets).
2181 Obeys system preference for IPv4/6 address resolution - this can be overridden by
2182 the ipv6 flag. Tries to connect to the address before considering it useful. If no
2183 address can be reached, the first one resolved is used as a fallback.
2184 Does not exit on failure, raises an exception.
2185 """
2186 ip_addrs = []
2187 family = (
2188 socket.AF_INET6
2189 if ipv6
2190 else socket.AF_INET if ipv6 is False else socket.AF_UNSPEC
2191 )
2192 socket_error = False
2193 try:
2194 refresh_dns()
2195 addrinfo = socket.getaddrinfo(addr, port, family, socket.SOCK_STREAM)
2196 ip_addrs = _test_addrs(addrinfo, port)
2197 except TypeError:
2198 raise SaltSystemExit(
2199 code=42,
2200 msg=(
2201 "Attempt to resolve address '{}' failed. Invalid or unresolveable"
2202 " address".format(addr)
2203 ),
2204 )
2205 except OSError:
2206 socket_error = True
2207
2208 # If ipv6 is set to True, attempt another lookup using the IPv4 family,
2209 # just in case we're attempting to lookup an IPv4 IP
2210 # as an IPv6 hostname.
2211 if socket_error and ipv6:
2212 try:
2213 refresh_dns()
2214 addrinfo = socket.getaddrinfo(
2215 addr, port, socket.AF_INET, socket.SOCK_STREAM
2216 )
2217 ip_addrs = _test_addrs(addrinfo, port)
2218 except TypeError:
2219 raise SaltSystemExit(
2220 code=42,
2221 msg=(
2222 "Attempt to resolve address '{}' failed. Invalid or unresolveable"
2223 " address".format(addr)
2224 ),
2225 )
2226 except OSError:
2227 error = True
2228
2229 if not ip_addrs:
2230 err = f"DNS lookup or connection check of '{addr}' failed."
2231 if safe:
2232 log.error(err)
2233 raise SaltClientError()
2234 raise SaltSystemExit(code=42, msg=err)
2235

Callers

nothing calls this directly

Calls 7

SaltSystemExitClass · 0.90
SaltClientErrorClass · 0.90
refresh_dnsFunction · 0.85
_test_addrsFunction · 0.85
formatMethod · 0.80
ip_bracketFunction · 0.70
errorMethod · 0.45

Tested by

no test coverage detected