libs/tools: add resolve_identifier()
Some checks failed
bundlewrap/pipeline/head There was a failure building this commit
Some checks failed
bundlewrap/pipeline/head There was a failure building this commit
This commit is contained in:
parent
31cc74951b
commit
f2073e72ed
1 changed files with 31 additions and 0 deletions
31
libs/tools.py
Normal file
31
libs/tools.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from bundlewrap.exceptions import NoSuchGroup, NoSuchNode
|
||||
from ipaddress import ip_address
|
||||
|
||||
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():
|
||||
for ip in config.get('ipv4', set()):
|
||||
found_ips.add(ip_address(ip))
|
||||
for ip in config.get('ipv4', set()):
|
||||
found_ips.add(ip_address(ip))
|
||||
|
||||
if node.metadata.get('external_ipv4'):
|
||||
found_ips.add(ip_address(node.metadata.get('external_ipv4')))
|
||||
|
||||
return found_ips
|
Loading…
Reference in a new issue