Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ipv6, dns-*, source and more hooks #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class NetworkAdapter:
'netmask': {'type': 'IP'},
'network': {'type': 'IP'},
'broadcast': {'type': 'IP'},
'gateway': {'type': 'IP'},
'gateway': {'type': str},
'nameservers': {'type': list},
'bridge-opts': {'type': dict},
'addrFam': {'in': ['inet', 'inet6', 'ipx']},
'source': {'in': ['dhcp', 'static', 'loopback', 'manual', 'bootp', 'ppp', 'wvdial', 'dynamic', 'ipv4ll', 'v4tunnel']},
'includes': {'type': list},
'hostapd': {}
}

Expand Down Expand Up @@ -58,7 +60,10 @@ def validateIP(self, ip):
Raise socket.error on invalid IP
Works for subnet masks too.
'''
socket.inet_aton(ip)
try:
socket.inet_aton(ip)
except socket.error:
socket.inet_pton(socket.AF_INET6, ip)

def setName(self, n):
''' Set the name option of an interface. '''
Expand Down Expand Up @@ -107,6 +112,18 @@ def setNetwork(self, w):
self.validateOne('network', self._valid['network'], w)
self._ifAttributes['network'] = w

def setNameservers(self, n):
''' Set the ipaddress of an interface. '''

self.validateOne('nameservers', self._valid['nameservers'], n)
self._ifAttributes['nameservers'] = n

def setIncludes(self, i):
self._ifAttributes['includes'] = i

def appendIncludes(self, i):
self._ifAttributes['includes'].append(i);

def setAuto(self, t):
''' Set the option to autostart the interface. '''

Expand Down Expand Up @@ -245,7 +262,7 @@ def set_options(self, options):
for key in options.keys():
if key == 'name':
self.setName(options[key])
if key == 'addrFam':
elif key == 'addrFam':
self.setAddrFam(options[key])
elif key == 'source':
self.setAddressSource(options[key])
Expand Down
8 changes: 7 additions & 1 deletion interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ def interfaces_path(self):
def backup_path(self):
return self._backup_path

@property
def includes(self):
return self._includes

def updateAdapters(self):
''' (re)read interfaces file and save adapters '''
self._adapters = InterfacesReader(self._interfaces_path).parse_interfaces()
reader = InterfacesReader(self._interfaces_path)
self._adapters = reader.parse_interfaces()
self._includes = reader.includes
if not self._adapters:
self._adapters = []

Expand Down
35 changes: 28 additions & 7 deletions interfacesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def __init__(self, interfaces_path):
def adapters(self):
return self._adapters

@property
def includes(self):
return self._include_list

def parse_interfaces(self):
''' Read /etc/network/interfaces. '''
self._reset()
Expand Down Expand Up @@ -67,6 +71,7 @@ def _read_lines_from_file(self, fileObj):
self._parse_details(line)
self._read_auto(line)
self._read_hotplug(line)
self._read_source(line)

def _parse_iface(self, line):
if line.startswith('iface'):
Expand All @@ -78,14 +83,16 @@ def _parse_iface(self, line):
self._adapters[self._context].setAddrFam(sline[2])

def _parse_details(self, line):
if line[0].isspace() is True:
sline = line.split()
sline = line.split()
#if line[0].isspace() is True:
if not sline[0] in {'auto', 'iface', 'allow-auto', 'allow-hotplug', 'source'}:
if sline[0] == 'address':
self._adapters[self._context].setAddress(sline[1])
elif sline[0] == 'netmask':
self._adapters[self._context].setNetmask(sline[1])
elif sline[0] == 'gateway':
self._adapters[self._context].setGateway(sline[1])
ud = sline.pop(0)
self._adapters[self._context].setGateway(' '.join(sline))
elif sline[0] == 'broadcast':
self._adapters[self._context].setBroadcast(sline[1])
elif sline[0] == 'network':
Expand All @@ -95,20 +102,27 @@ def _parse_details(self, line):
sline.pop(0)
ifs = " ".join(sline)
self._adapters[self._context].replaceBropt(opt[1], ifs)
elif sline[0] == 'up' or sline[0] == 'down' or sline[0] == 'pre-up' or sline[0] == 'post-down':
elif sline[0] == 'nameservers' or sline[0] == 'dns-nameservers':
ns = sline.pop(0)
self._adapters[self._context].setNameservers(sline)
elif sline[0] == 'pre-up' or sline[0] == 'up' or sline[0] == 'post-up' or sline[0] == 'down' or sline[0] == 'pre-down' or sline[0] == 'post-down':
ud = sline.pop(0)
cmd = ' '.join(sline)
if ud == 'up':
if ud == 'up' or ud == 'post-up':
self._adapters[self._context].appendUp(cmd)
elif ud == 'down':
elif ud == 'down' or ud == 'pre-down':
self._adapters[self._context].appendDown(cmd)
elif ud == 'pre-up':
self._adapters[self._context].appendPreUp(cmd)
elif ud == 'post-down':
self._adapters[self._context].appendPostDown(cmd)
else:
# store as if so as not to loose it
self._adapters[self._context].setUnknown(sline[0], sline[1])
key = sline.pop(0)
try:
self._adapters[self._context].setUnknown(key, ' '.join(sline))
except:
pass

def _read_auto(self, line):
''' Identify which adapters are flagged auto. '''
Expand All @@ -130,13 +144,20 @@ def _read_hotplug(self, line):
else:
self._hotplug_list.append(word)

def _read_source(self, line):
if line.startswith('source'):
sline = line.split()
sline.pop(0)
self._include_list.append(' '.join(sline))

def _reset(self):
# Initialize a place to store created networkAdapter objects.
self._adapters = []

# Keep a list of adapters that have the auto or allow-hotplug flags set.
self._auto_list = []
self._hotplug_list = []
self._include_list = []

# Store the interface context.
# This is the index of the adapters collection.
Expand Down
108 changes: 108 additions & 0 deletions parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python

import debinterface
import debinterface.interfaces
import debinterface.adapter
import sys

if __name__ == "__main__":
intfile = sys.argv[1]
interfaces = debinterface.interfaces.Interfaces(interfaces_path=intfile)
interfaces.updateAdapters()

adapters = interfaces.adapters
options = {
'hotplug'
, 'auto'
, 'name'
, 'hwaddress'
, 'address'
, 'netmask'
, 'network'
, 'broadcast'
, 'gateway'
, 'bridge-opts'
, 'addrFam'
, 'source'
, 'nameservers'
, 'unknown'
, 'up'
, 'pre-up'
, 'post-up'
, 'down'
, 'post-down'
}

print "network_managed_by_ansible: True"
print "network_manage_devices: True"
print "network_host_interfaces:"
for apt in adapters:
attrs = apt.export(options)
if attrs['auto'] == None:
attrs['auto'] = True
if attrs['name'] == 'lo' and attrs['source'] == 'loopback':
continue
else:
print " - device: %s" % attrs['name']
print " description: %s network configs" % attrs['name']
print " auto: %s" % attrs['auto']
print " family: %s" % attrs['addrFam']
print " method: %s" % attrs['source']
if attrs['address']:
print " address: %s" % attrs['address']
if attrs['unknown'] and 'hwaddress' in attrs['unknown'] and attrs['unknown']['hwaddress']:
print " hwaddress: %s" % attrs['unknown']['hwaddress']
if 'network' in attrs and attrs['network']:
print " network: %s" % attrs['network']
if attrs['broadcast']:
print " broadcast: %s" % attrs['broadcast']
if 'netmask' in attrs and attrs['netmask']:
print " netmask: %s" % attrs['netmask']
if 'gateway' in attrs and attrs['gateway']:
print " gateway: %s" % attrs['gateway']
if 'nameservers' in attrs and attrs['nameservers']:
print " nameservers:"
for ns in attrs['nameservers']:
print " - %s" % ns
if attrs['unknown'] and 'dns-nameservers' in attrs['unknown'] and attrs['unknown']['dns-nameservers']:
print " nameservers: %s" % attrs['unknown']['dns-nameservers']
if attrs['unknown']:
if 'dns-search' in attrs['unknown'] and attrs['unknown']['dns-search']:
print " dns_search: %s" % attrs['unknown']['dns-search']
else:
print " dns_search: %s" % argv[2]
if 'up' in attrs and attrs['up']:
print " up:"
for cmd in attrs['up']:
if cmd.startswith('ip addr add 10.'):
print " - /usr/local/sbin/do_anchor_ip.sh %s" % attrs['name']
else:
print " - %s" % cmd
if 'pre-up' in attrs and attrs['pre-up']:
print " pre-up:"
for cmd in attrs['pre-up']:
print " - %s" % cmd
if attrs['unknown'] and len(attrs['unknown']) > 0:
bond = {}
vlan = {}
for key in attrs['unknown']:
if not key in ['hwaddress', 'address', 'netmask', 'network', 'broadcast', 'gateway', 'dns-search', 'dns-nameservers']:
if key.startswith('bond-'):
skey = key[5:]
bond[skey] = attrs['unknown'][key]
elif key.startswith('vlan-'):
skey = key[5:]
vlan[skey] = attrs['unknown'][key]
else:
print " %s: %s ## unknown option" % (key, attrs['unknown'][key])
if len(bond) > 0:
print " bond:"
for key in bond:
print " %s: %s" % (key, bond[key])
if len(vlan) > 0:
print " vlan:"
for key in vlan:
print " %s: %s" % (key, vlan[key])

if interfaces.includes:
print "## WARNING: there are more configs for this host"