__author__ = 'szaydel'
import pickle
import plyvel
from staticconf import loader, NamespaceGetters
from staticconf.errors import ConfigurationError
from staticconf import schema
db_conn = plyvel.DB('/tmp/testdb/', create_if_missing=False)
def leveldb_loader(*args, **kwargs):
""" Load levelDB persistent configuration data, optionally,
selecting only a subset of data by specifying a key prefix.
"""
try:
prefix = kwargs["prefix"]
db = kwargs["db"]
except KeyError as e:
return {}
return {k: pickle.loads(v) for k, v in
db.iterator(prefix=b'{0}'.format(prefix))}
namespace = "network"
data = loader.build_loader(leveldb_loader)
network = loader.load_config_data(data,
db=db_conn,
prefix=namespace,
namespace=namespace)
def get_network_interface(**kwargs):
class NetIfModel(schema.Schema):
# namespace is optional, and will default to DEFAULT
namespace = kwargs["namespace"]
interface = kwargs["interface"]
s = "network:interfaces:physical"
#u'ixgbe0': {u'address': None,
# u'broadcast': None,
# u'create': False,
# u'gateway': None,
# u'mac': u'0:1b:21:94:97:68',
# u'mtu': 9000,
# u'netmask': u'255.255.255.0',
# u'tag': [u'fast', u'service', u'10gb']},
address = schema.string(config_key="{0}:{1}:address".format(s, interface), default="")
broadcast = schema.string(config_key="{0}:{1}:broadcast".format(s, interface), default="")
netmask = schema.string(config_key="{0}:{1}:netmask".format(s, interface), default="")
gateway = schema.string(config_key="{0}:{1}:gateway".format(s, interface), default="")
create = schema.bool(config_key="{0}:{1}:create".format(s, interface), default=False)
mtu = schema.int(config_key="{0}:{1}:mtu".format(s, interface), default=1500)
mac = schema.string(config_key="{0}:{1}:mac".format(s, interface), default="")
tag = schema.list(config_key="{0}:{1}:tag".format(s, interface), default=[])
return NetIfModel()
if __name__ == "__main__":
obj = get_network_interface(interface="ixgbe0",
namespace=namespace)
print {
"address": obj.address,
"broadcast": obj.broadcast,
"netmask": obj.netmask,
"gateway": obj.gateway,
"create": obj.create,
"mtu": obj.mtu,
"mac": obj.mac,
"tag": obj.tag
}
cfg = NamespaceGetters(namespace)
print cfg.get_int('network:interfaces:physical:igb1:mtu')
print cfg.get_bool('network:interfaces:physical:ixgbe0:create', namespace=namespace)
try:
print cfg.get_string('network:interfaces:physical:ixgbe0:netmask')
print cfg.get_float('network:interfaces:physical:igb1:mtu')
print cfg.get_string('network:interfaces:physical:ixgbe0:address')
print cfg.get_bool('network:interfaces:physical:ixgbe0:mac') # True if exists
except ConfigurationError as e:
print e.args
try:
print cfg.get_bool('network:interfaces:physical:ixgbe3:mac') # True if exists
except ConfigurationError as e:
print e.args
__author__ = 'szaydel'
import plyvel
import pickle
d = {u'auth': {u'users': {u'root': {u'shadow': u'$5$O2EFsgjs$IF1U3Y2m4ETHzt82cwlIaNgpQR8Gpj32f8wDIAe.XuC'}}},
u'network': {u'aggregates': {u'aggr1': {u'create': False,
u'interfaces': [u'ixgbe0', u'ixgbe1'],
u'lacp-mode': u'off',
u'name': u'aggr1',
u'policy': u'L4'},
u'aggr2': {u'create': False}},
u'identify': {u'hostname': u'moc-bsr-03',
u'ntp': [u'0.us.pool.ntp.org',
u'1.us.pool.ntp.org',
u'2.us.pool.ntp.org',
u'3.us.pool.ntp.org'],
u'timezone': u'US/Eastern'},
u'interfaces': {u'alias': {u'alias0': {u'address': u'10.11.10.1',
u'create': True,
u'over': u'ixgbe0'},
u'alias1': {u'address': u'10.11.10.2',
u'create': True,
u'over': u'ixgbe0'}},
u'physical': {u'igb0': {u'address': u'dhcp',
u'alias': [u'alias0', u'alias1'],
u'broadcast': None,
u'create': True,
u'gateway': None,
u'mac': u'0:1e:67:34:4a:c4',
u'mtu': 1500,
u'netmask': None,
u'tag': [u'admin', u'primary']},
u'igb1': {u'address': None,
u'broadcast': None,
u'create': False,
u'gateway': None,
u'mac': u'0:1e:67:34:4a:c5',
u'mtu': 1500,
u'netmask': u'255.255.255.0'},
u'ixgbe0': {u'address': None,
u'broadcast': None,
u'create': False,
u'gateway': None,
u'mac': u'0:1b:21:94:97:68',
u'mtu': 9000,
u'netmask': u'255.255.255.0',
u'tag': [u'fast', u'service', u'10gb']},
u'ixgbe1': {u'address': None,
u'broadcast': None,
u'create': False,
u'gateway': None,
u'mac': u'0:1b:21:94:97:69',
u'mtu': 9000,
u'netmask': None,
u'tag': [u'fast', u'service', u'10gb']}},
u'virtual': {u'vnic0': {u'address': u'10.11.10.1',
u'broadcast': None,
u'create': True,
u'gateway': None,
u'netmask': None,
u'over': u'ixgbe0',
u'vlan': 0}}}},
u'storage': {u'zfs': {u'zfstune': {u'arc_shrink_shift': None,
u'l2arc_noprefetch': u'0x0',
u'l2arc_write_boost': None,
u'zfs_arc_meta_limit': u'0x4000000',
u'zfs_no_write_throttle': u'0x1',
u'zfs_nocacheflush': u'0x1',
u'zfs_prefetch_disable': u'0x0',
u'zfs_txg_synctime_ms': None,
u'zfs_txg_timeout': None,
u'zfs_vdev_max_pending': u'0xa',
u'zfs_vdev_min_pending': None},
u'zpools': [u'p000t4']}}}
def flatten_dict(config_data):
for key, value in config_data.iteritems():
# Recursively walk through objects and flatten them into a wide namespace.
if hasattr(value, 'iteritems'):
for k, v in flatten_dict(value):
yield '%s:%s' % (key, k), v
continue
yield key, value
if __name__ == "__main__":
data = list(flatten_dict(d))
db = plyvel.DB('/tmp/testdb/', create_if_missing=True)
for k,v in data:
db.put(b'{0}'.format(k), b'{0}'.format(pickle.dumps(v)))