def get_free_host(cls, as_int=None, node=None, ip=None):
"""Return free host if available.
:param as_int: return ip as long int
:param node: if set, get free host only for this node
:param ip: if set, first try to check if this ip available, if not,
then return any other available ip
:return: ip address, as str or int, depends on as_int value
"""
if ip:
network = cls.get_network_by_ip(ip)
if network and network.is_ip_available(ip, node):
return int(ipaddress.ip_address(ip)) if as_int else ip
if node is None:
networks = cls.all()
else:
networks = cls.filter(cls.node.has(hostname=node))
for n in networks:
free_host = n.get_first_free_host(as_int=as_int)
if free_host is not None:
return free_host
raise NoFreeIPs()
评论列表
文章目录