Skip to content

Commit

Permalink
Add host with ipv6
Browse files Browse the repository at this point in the history
You can add host using ipv6
Always try to resolve FQDN. Before if you created host
 with domain name but not fully qualified it was stored
 as fqdn
Add ability to find default ipv6 for gateway.
Add ability to find ipv6 by interface
  • Loading branch information
vkondula committed Oct 13, 2016
1 parent bb546ea commit 53d2428
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
6 changes: 2 additions & 4 deletions rrmngmnt/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def __init__(self, ip, service_provider=None):
:type service_provider: class which implement SystemService interface
"""
super(Host, self).__init__()
self._fqdn = None
if not netaddr.valid_ipv4(ip):
self._fqdn = ip
if not netaddr.valid_ipv4(ip) and not netaddr.valid_ipv6(ip):
ip = fqdn2ip(ip)
self.ip = ip
self.users = list()
Expand Down Expand Up @@ -107,7 +105,7 @@ def add(self):

@property
def fqdn(self):
return socket.getfqdn(self.ip) if not self._fqdn else self._fqdn
return socket.getfqdn(self.ip)

def add_power_manager(self, pm_type, **init_params):
"""
Expand Down
43 changes: 42 additions & 1 deletion rrmngmnt/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ def find_default_gw(self):
return default_gw[0]
return None

@keep_session
def find_default_gwv6(self):
"""
Find host default ipv6 gateway
:return: default gateway
:rtype: string
"""
out = self._cmd(["ip", "-6", "route"]).splitlines()
for i in out:
if re.search("default", i):
default_gw = re.findall(
r'(?<=\s)[0-9a-fA-F:]{3,}(?=\s)', i
)
if netaddr.valid_ipv6(default_gw[0]):
return default_gw[0]
return None

@keep_session
def find_ips(self):
"""
Expand Down Expand Up @@ -260,7 +278,7 @@ def find_int_by_ip(self, ip):
@keep_session
def find_ip_by_int(self, interface):
"""
Find host interface by interface or Bridge name
Find host ipv4 by interface or Bridge name
:param interface: interface to get ip from
:type interface: string
Expand All @@ -275,6 +293,29 @@ def find_ip_by_int(self, interface):
return interface_ip
return None

@keep_session
def find_ipv6_by_int(self, interface):
"""
Find host global ipv6 by interface or Bridge name
:param interface: interface to get ipv6 from
:type interface: string
:return: IP or None
:rtype: string or None
"""
out = self._cmd(["ip", "-6", "addr", "show", interface])
for line in out.splitlines():
if re.search("global", line):
match_ip = re.search(
r'(?<=\s)[0-9a-fA-F:]{3,}(?=/[0-9]{1,3}\s)',
line,
)
if match_ip:
interface_ip = match_ip.group()
if netaddr.valid_ipv6(interface_ip):
return interface_ip
return None

@keep_session
def find_int_by_bridge(self, bridge):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class TestHostFqdnIp(object):
def test_host_ip(self):
h = Host('127.0.0.1')
assert h.ip == '127.0.0.1'
assert h.fqdn == 'localhost'
assert 'localhost' in h.fqdn

def test_host_fqdn(self):
h = Host('localhost')
assert h.ip == '127.0.0.1'
assert h.fqdn == 'localhost'
assert 'localhost' in h.fqdn
39 changes: 39 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ class TestNetwork(object):
),
'',
),
'ip -6 route': (
0,
'\n'.join(
[
'unreachable ::/96 dev lo metric 1024 error -101',
'unreachable 2002:a9fe::/32 lo metric 1024 error -101',
'unreachable 2002:ac10::/28 lo metric 1024 error -101',
'fe80:52:0::3fe dev eth0 proto static metric 100 ',
'default via fe80::0:3fe dev eth0 proto static metric 100',
]
),
'',
),
'ip addr': (
0,
''.join(
Expand Down Expand Up @@ -115,6 +128,22 @@ class TestNetwork(object):
),
''
),
'ip addr show eth0': (
0,
'\n'.join(
[
'2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu ...',
'link/ether 00:1a:4a:01:3f:1c brd ff:ff:ff:ff:ff:ff',
'inet 10.11.12.84/22 brd 10.11.12.255 scope global',
'valid_lft 20343sec preferred_lft 20343sec',
'inet6 2620:52:0::fe01:3f1c/64 scope global dynamic',
'valid_lft 2591620sec preferred_lft 604420sec',
'inet6 fe80::4aff:fe01:3f1c/64 scope link ',
'valid_lft forever preferred_lft forever',
]
),
''
),
'brctl show | sed -e "/^bridge name/ d" '
'-e \'s/^\\s\\s*\\(\\S\\S*\\)$/CONT:\\1/I\'': (
0,
Expand Down Expand Up @@ -201,6 +230,16 @@ def test_get_mac_address_by_ip(self):
expected = "44:1e:a1:73:3c:98"
assert get_host().network.get_mac_by_ip("10.11.12.83") == expected

def test_find_ip_by_int(self):
assert get_host().network.find_ip_by_int("eth0") == "10.11.12.84"

def test_find_ipv6_by_int(self):
expected = "2620:52:0::fe01:3f1c"
assert get_host().network.find_ipv6_by_int("eth0") == expected

def test_find_default_gwv6(self):
assert get_host().network.find_default_gwv6() == "fe80::0:3fe"

def if_up(self):
assert get_host().network.if_up("interface")

Expand Down

0 comments on commit 53d2428

Please sign in to comment.