2020-10-16 20:19:45 +00:00
|
|
|
from bundlewrap.exceptions import NoSuchGroup, NoSuchNode
|
2020-11-09 17:46:37 +00:00
|
|
|
from ipaddress import ip_address, IPv4Address
|
2020-10-16 20:19:45 +00:00
|
|
|
|
|
|
|
def resolve_identifier(repo, identifier):
|
|
|
|
"""
|
|
|
|
Try to resolve an identifier (group or node). Return a set of ip
|
|
|
|
addresses valid for this identifier.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
nodes = {repo.get_node(identifier)}
|
|
|
|
except NoSuchNode:
|
|
|
|
try:
|
|
|
|
nodes = repo.nodes_in_group(identifier)
|
|
|
|
except NoSuchGroup:
|
|
|
|
try:
|
|
|
|
return {ip_address(identifier)}
|
|
|
|
except:
|
|
|
|
return set()
|
|
|
|
|
|
|
|
found_ips = set()
|
|
|
|
for node in nodes:
|
|
|
|
for interface, config in node.metadata.get('interfaces', {}).items():
|
2020-11-09 14:23:44 +00:00
|
|
|
for ip in config.get('ips', set()):
|
|
|
|
if '/' in ip:
|
|
|
|
found_ips.add(ip_address(ip.split('/')[0]))
|
|
|
|
else:
|
|
|
|
found_ips.add(ip_address(ip))
|
2020-10-16 20:19:45 +00:00
|
|
|
|
2020-11-08 09:41:41 +00:00
|
|
|
if node.metadata.get('external_ipv4', None):
|
2020-10-16 20:19:45 +00:00
|
|
|
found_ips.add(ip_address(node.metadata.get('external_ipv4')))
|
|
|
|
|
2020-11-09 17:46:37 +00:00
|
|
|
ip_dict = {
|
|
|
|
'ipv4': set(),
|
|
|
|
'ipv6': set(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for ip in found_ips:
|
|
|
|
if isinstance(ip, IPv4Address):
|
|
|
|
ip_dict['ipv4'].add(ip)
|
|
|
|
else:
|
|
|
|
ip_dict['ipv6'].add(ip)
|
|
|
|
|
|
|
|
return ip_dict
|