Commit 131f0bec authored by Mark Schouten's avatar Mark Schouten
Browse files

Add building a configuration from the current situation

parent b00dc70a
#!/usr/bin/env python3
from pyroute2 import IPRoute
from pyroute2 import IPDB
def GetNow():
ip = IPRoute()
ret = {}
for iface in ip.get_links():
this = {}
this['type'] = 'default'
this['name'] = iface.get_attr('IFLA_IFNAME')
addrs = []
for addr in ip.get_addr(index=iface['index']):
if addr.get_attr('IFA_ADDRESS').startswith('fe80:'):
# We don't store link-locals
continue
fa = '/'.join([addr.get_attr('IFA_ADDRESS'), str(addr['prefixlen'])])
addrs.append(fa)
if len(addrs) > 0:
this['addresses'] = addrs
this['adminstate'] = iface.get_attr('IFLA_OPERSTATE')
this['mtu'] = iface.get_attr('IFLA_MTU')
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['vlanid'] = linfo.get_attr('IFLA_INFO_DATA').get_attr('IFLA_VLAN_ID')
ret[pname]['vlans'].append(this)
except Exception as e:
print(e)
pass
else:
ret[this['name']] = this
return ret
......@@ -13,7 +13,7 @@ Options:
import sys, os
sys.path.insert(0, '/home/mark/src/ypconfig/lib')
from ypconfig import Config
from ypconfig import Config, Netlink
from docopt import docopt
from schema import Schema, And, Or, Use, SchemaError, Optional
from pprint import pprint
......@@ -32,11 +32,12 @@ if __name__ == '__main__':
except SchemaError as e:
sys.exit(e)
try:
cfgdoc = Config.Get(args['--cfg'])
except FileNotFoundError as e:
print(e)
sys.exit(1)
if not args['createconfig']:
try:
cfgdoc = Config.Get(args['--cfg'])
except FileNotFoundError as e:
print(e)
sys.exit(1)
if args['configtest']:
try:
......@@ -46,3 +47,6 @@ if __name__ == '__main__':
sys.exit(1)
else:
print("Configuration is ok!")
elif args['createconfig']:
cfg = Netlink.GetNow()
pprint(Config.Validate(cfg))
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