Updated host_to_ip to return all the IPs instead of the first one.

This fixes the bug #36866 where minion gets __master_disconnected right
after connect because '::1' isn't in the list of connected masters that
is ['127.0.0.1'].
This commit is contained in:
Dmitry Kuzmenko 2016-10-20 18:15:27 +03:00
parent 96a1292a7e
commit f625e6d3a9
3 changed files with 40 additions and 40 deletions

View file

@ -24,7 +24,7 @@ import salt.config
import salt.minion
import salt.utils
import salt.utils.event
from salt.utils.network import host_to_ip as _host_to_ip
from salt.utils.network import host_to_ips as _host_to_ips
from salt.utils.network import remote_port_tcp as _remote_port_tcp
from salt.ext.six.moves import zip
from salt.utils.decorators import with_deprecated
@ -1031,7 +1031,7 @@ def master(master=None, connected=True):
# the default publishing port
port = 4505
master_ip = None
master_ips = None
if __salt__['config.get']('publish_port') != '':
port = int(__salt__['config.get']('publish_port'))
@ -1040,20 +1040,21 @@ def master(master=None, connected=True):
# address and try resolving it first. _remote_port_tcp
# only works with IP-addresses.
if master is not None:
tmp_ip = _host_to_ip(master)
if tmp_ip is not None:
master_ip = tmp_ip
master_ips = _host_to_ips(master)
ips = _remote_port_tcp(port)
if connected:
if master_ip not in ips:
event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)
event.fire_event({'master': master}, '__master_disconnected')
else:
master_connection_status = False
for master_ip in master_ips:
if master_ip in ips:
event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)
master_connection_status = True
break
if master_connection_status is not connected:
event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)
if master_connection_status:
event.fire_event({'master': master}, '__master_connected')
else:
event.fire_event({'master': master}, '__master_disconnected')
def ping_master(master):

View file

@ -19,7 +19,7 @@ import salt.utils
import salt.ext.six as six
import salt.utils.event
from salt._compat import subprocess
from salt.utils.network import host_to_ip as _host_to_ip
from salt.utils.network import host_to_ips as _host_to_ips
import os
import ctypes
@ -331,7 +331,7 @@ def master(master=None, connected=True):
# the default publishing port
port = 4505
master_ip = None
master_ips = None
if __salt__['config.get']('publish_port') != '':
port = int(__salt__['config.get']('publish_port'))
@ -340,21 +340,18 @@ def master(master=None, connected=True):
# address and try resolving it first. _remote_port_tcp
# only works with IP-addresses.
if master is not None:
tmp_ip = _host_to_ip(master)
if tmp_ip is not None:
master_ip = tmp_ip
master_ips = _host_to_ips(master)
ips = _win_remotes_on(port)
if connected:
if master_ip not in ips:
event = salt.utils.event.get_event(
'minion', opts=__opts__, listen=False
)
event.fire_event({'master': master}, '__master_disconnected')
else:
master_connection_status = False
for master_ip in master_ips:
if master_ip in ips:
event = salt.utils.event.get_event(
'minion', opts=__opts__, listen=False
)
master_connection_status = True
break
if master_connection_status is not connected:
event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)
if master_connection_status:
event.fire_event({'master': master}, '__master_connected')
else:
event.fire_event({'master': master}, '__master_disconnected')

View file

@ -53,22 +53,24 @@ def isportopen(host, port):
return out
def host_to_ip(host):
def host_to_ips(host):
'''
Returns the IP address of a given hostname
Returns a list of IP addresses of a given hostname or None if not found.
'''
ips = []
try:
family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
host, 0, socket.AF_UNSPEC, socket.SOCK_STREAM)[0]
if family == socket.AF_INET:
ip, port = sockaddr
elif family == socket.AF_INET6:
ip, port, flow_info, scope_id = sockaddr
for family, socktype, proto, canonname, sockaddr in socket.getaddrinfo(
host, 0, socket.AF_UNSPEC, socket.SOCK_STREAM):
if family == socket.AF_INET:
ip, port = sockaddr
elif family == socket.AF_INET6:
ip, port, flow_info, scope_id = sockaddr
ips.append(ip)
if not ips:
ips = None
except Exception:
ip = None
return ip
ips = None
return ips
def _filter_localhost_names(name_list):