Commit 3e1a6017 authored by Mark Schouten's avatar Mark Schouten
Browse files

Commit the new configuration and be done with it

parent bb60dcee
......@@ -2,6 +2,8 @@
from pyroute2 import IPRoute
from pyroute2 import IPDB
from pprint import pprint
from dictdiffer import diff
def GetNow():
ip = IPRoute()
......@@ -16,9 +18,10 @@ def GetNow():
this['type'] = 'default'
this['name'] = iface.get_attr('IFLA_IFNAME')
if this['name'] == 'lo':
this['type'] = 'loopback'
if iface.get_attr('IFLA_IFALIAS'):
this['description'] = iface.get_attr('IFLA_IFALIAS')
else:
this['description'] = this['name']
addrs = []
for addr in ip.get_addr(index=iface['index']):
......@@ -34,29 +37,23 @@ def GetNow():
this['adminstate'] = iface.get_attr('IFLA_OPERSTATE')
this['mtu'] = iface.get_attr('IFLA_MTU')
if this['name'] == 'lo':
this['type'] = 'loopback'
this['adminstate'] = 'UP'
if iface.get_attr('IFLA_LINKINFO'):
try:
linfo = iface.get_attr('IFLA_LINKINFO')
if linfo.get_attr('IFLA_INFO_KIND') == 'vlan':
# Get the parents name
pname = ip.get_links(iface.get_attr('IFLA_LINK'))[0].get_attr('IFLA_IFNAME')
try:
ret[pname]
except KeyError:
ret[pname] = {}
try:
ret[pname]['vlans']
except KeyError:
ret[pname]['vlans'] = []
this['type'] = 'vlan'
this['parent'] = pname
this['vlanid'] = linfo.get_attr('IFLA_INFO_DATA').get_attr('IFLA_VLAN_ID')
ret[pname]['vlans'].append(this)
ret[this['name']] = this
elif linfo.get_attr('IFLA_INFO_SLAVE_KIND') == 'bond':
# Get the parents name
#print(ip.get_links(iface.get_attr('IFLA_MASTER')))
pname = ip.get_links(iface.get_attr('IFLA_MASTER'))[0].get_attr('IFLA_IFNAME')
try:
ret[pname]
......@@ -85,13 +82,172 @@ def GetNow():
return ret
# def AddIface(ipdb, iface):
# def DelIface(ipdb, iface):
# def UpIface(ipdb, iface):
# def DownIface(ipdb, iface):
# def Commit(ipdb):
# ipdb.commit()
\ No newline at end of file
def Commit(cur, new):
global ip
curif = set(cur.keys())
newif = set(new.keys())
with IPDB(mode='implicit') as ip:
curif = set(cur.keys())
newif = set(new.keys())
# These interfaces should be deleted
for iface in curif.difference(newif):
Delif(iface)
# These interfaces need to be created
for iface in newif.difference(curif):
if new[iface]['type'] == 'vlan':
Addvlan(new[iface])
elif new[iface]['type'] == 'bond':
Addbond(new[iface])
# Processes changes in remaining interfaces
for iface in newif.intersection(curif):
c = cur[iface]
n = new[iface]
for v in ['description', 'addresses', 'adminstate', 'mtu', 'ratelimit', 'slaves', 'vlanid', 'parent', 'bond-mode', 'miimon', 'lacp_rate']:
try:
n[v]
except KeyError:
if v == 'addresses':
n[v] = []
else:
n[v] = None
try:
c[v]
except KeyError:
if v == 'addresses':
c[v] = []
else:
c[v] = None
try:
if n[v] != c[v]:
if v == 'adminstate':
Ifstate(iface, n[v])
elif v == 'mtu':
Ifmtu(iface, n[v])
elif v == 'description':
Ifalias(iface, n[v])
elif v == 'addresses':
for addr in set(n[v]).difference(set(c[v])):
Addaddr(iface, addr)
for addr in set(c[v]).difference(set(n[v])):
Deladdr(iface, addr)
elif v == 'slaves':
for slave in set(n[v]).difference(set(c[v])):
Addslave(iface, slave)
for slave in set(c[v]).difference(set(n[v])):
Removeslave(iface, slave)
elif v in ['vlanid', 'parent']:
Delif(iface)
Addvlan(new[iface])
elif v in ['bond-mode', 'miimon', 'lacp_rate']:
Delif(iface)
Addbond(new[iface])
except KeyError as e:
print("%s on %s" % (e, iface))
pass
except Exception as e:
raise e
ip.commit()
ip.release()
def Delif(iface):
print("Removing interface %s" % (iface))
global ip
i = ip.interfaces[iface]
i.remove()
ip.commit()
def Addvlan(vals):
print("Creating vlan interface %s on %s with id %s" % (vals['name'], vals['parent'], vals['vlanid']))
global ip
parent = ip.interfaces[vals['parent']]
parent.up()
i = ip.create(kind='vlan', ifname=vals['name'], link=parent, vlan_id=vals['vlanid'], reuse=True)
Ifstate(vals['name'], vals['adminstate'])
Ifmtu(vals['name'], vals['mtu'])
try:
vals['addresses']
for addr in vals['addresses']:
Addaddr(vals['name'], addr)
except KeyError:
pass
except Exception as e:
raise e
ip.commit()
def Addbond(vals):
print("Creating bond interface %s with %s" % (vals['name'], str(vals)))
global ip
iface = vals['name']
i = ip.create(kind='bond', ifname=iface, bond_mode=vals['bond_mode'], bond_miimon=vals['miimon'], reuse=True)
for child in vals['slaves']:
Ifmtu(child, vals['mtu'])
Addslave(iface, child)
Ifmtu(iface, vals['mtu'])
Ifstate(iface, vals['adminstate'])
try:
vals['addresses']
for addr in vals['addresses']:
Addaddr(vals['name'], addr)
except KeyError:
pass
except Exception as e:
raise e
ip.commit()
def Addslave(iface, slave):
print("Adding interface %s as slave on %s" % (slave, iface))
global ip
i = ip.interfaces[iface]
i.add_port(ip.interfaces[slave])
ip.commit()
def Delslave(iface, slave):
print("Removing interface %s as slave from %s" % (slave, iface))
global ip
i = ip.interfaces[iface]
i.del_port(ip.interfaces[slave])
ip.commit()
def Deladdr(iface, addr):
print("Removing IP %s from %s" % (addr, iface))
global ip
i = ip.interfaces[iface]
i.del_ip(addr)
ip.commit()
def Addaddr(iface, addr):
print("Adding IP %s to %s" % (addr, iface))
global ip
i = ip.interfaces[iface]
i.add_ip(addr)
ip.commit()
def Ifstate(iface, state):
print("Setting state of interface %s to %s" % (iface, state))
global ip
i = ip.interfaces[iface]
if state == 'UP':
i.up()
if state == 'DOWN':
i.down()
ip.commit()
def Ifmtu(iface, mtu):
print("Setting MTU of interface %s to %s" % (iface, mtu))
global ip
i = ip.interfaces[iface]
i['mtu'] = int(mtu)
ip.commit()
def Ifalias(iface, alias):
print("Setting alias of interface %s to %s" % (iface, alias))
global ip
i = ip.interfaces[iface]
i['ifalias'] = alias
ip.commit()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment