Clean up logging a little bit

This commit is contained in:
Pim van Pelt
2022-03-24 12:14:26 +00:00
parent b43d7903fd
commit 8129235031
9 changed files with 32 additions and 51 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
build/ build/
dist/ dist/
hippo*.yaml
__pycache__ __pycache__

View File

@ -60,15 +60,10 @@ class IPInterfaceWithPrefixLength(Validator):
class NullHandler(logging.Handler):
def emit(self, record):
pass
class Validator(object): class Validator(object):
def __init__(self, schema): def __init__(self, schema):
self.logger = logging.getLogger('vppcfg.validator') self.logger = logging.getLogger('vppcfg.validator')
self.logger.addHandler(NullHandler()) self.logger.addHandler(logging.NullHandler())
self.schema = schema self.schema = schema
@ -131,3 +126,19 @@ class Validator(object):
if ret_rv: if ret_rv:
self.logger.debug("Semantics correctly validated") self.logger.debug("Semantics correctly validated")
return ret_rv, ret_msgs return ret_rv, ret_msgs
def valid_config(self, yaml):
""" Validate the given YAML configuration in 'yaml' against syntax
validation given in the yamale 'schema', and all semantic validators.
Returns True if the configuration is valid, False otherwise.
"""
rv, msgs = self.validate(yaml)
if not rv:
for m in msgs:
self.logger.error(m)
return False
self.logger.info("Configuration validated successfully")
return True

View File

@ -14,11 +14,6 @@
import logging import logging
import validator.interface as interface import validator.interface as interface
class NullHandler(logging.Handler):
def emit(self, record):
pass
def get_by_name(yaml, ifname): def get_by_name(yaml, ifname):
""" Return the BondEthernet by name, if it exists. Return None,None otherwise. """ """ Return the BondEthernet by name, if it exists. Return None,None otherwise. """
try: try:
@ -52,7 +47,7 @@ def validate_bondethernets(yaml):
result = True result = True
msgs = [] msgs = []
logger = logging.getLogger('vppcfg.validator') logger = logging.getLogger('vppcfg.validator')
logger.addHandler(NullHandler()) logger.addHandler(logging.NullHandler())
if not 'bondethernets' in yaml: if not 'bondethernets' in yaml:
return result, msgs return result, msgs

View File

@ -16,11 +16,6 @@ import validator.interface as interface
import validator.lcp as lcp import validator.lcp as lcp
import validator.address as address import validator.address as address
class NullHandler(logging.Handler):
def emit(self, record):
pass
def get_bridgedomains(yaml): def get_bridgedomains(yaml):
""" Return a list of all bridgedomains. """ """ Return a list of all bridgedomains. """
ret = [] ret = []
@ -94,7 +89,7 @@ def validate_bridgedomains(yaml):
result = True result = True
msgs = [] msgs = []
logger = logging.getLogger('vppcfg.validator') logger = logging.getLogger('vppcfg.validator')
logger.addHandler(NullHandler()) logger.addHandler(logging.NullHandler())
if not 'bridgedomains' in yaml: if not 'bridgedomains' in yaml:
return result, msgs return result, msgs

View File

@ -19,10 +19,6 @@ import validator.vxlan_tunnel as vxlan_tunnel
import validator.lcp as lcp import validator.lcp as lcp
import validator.address as address import validator.address as address
class NullHandler(logging.Handler):
def emit(self, record):
pass
def get_qinx_parent_by_name(yaml, ifname): def get_qinx_parent_by_name(yaml, ifname):
""" Returns the sub-interface which matches a QinAD or QinQ outer tag, or None,None """ Returns the sub-interface which matches a QinAD or QinQ outer tag, or None,None
if that sub-interface doesn't exist. """ if that sub-interface doesn't exist. """
@ -389,7 +385,7 @@ def validate_interfaces(yaml):
result = True result = True
msgs = [] msgs = []
logger = logging.getLogger('vppcfg.validator') logger = logging.getLogger('vppcfg.validator')
logger.addHandler(NullHandler()) logger.addHandler(logging.NullHandler())
if not 'interfaces' in yaml: if not 'interfaces' in yaml:
return result, msgs return result, msgs

View File

@ -15,10 +15,6 @@ import logging
import validator.lcp as lcp import validator.lcp as lcp
import validator.address as address import validator.address as address
class NullHandler(logging.Handler):
def emit(self, record):
pass
def get_loopbacks(yaml): def get_loopbacks(yaml):
""" Return a list of all loopbacks. """ """ Return a list of all loopbacks. """
ret = [] ret = []
@ -48,7 +44,7 @@ def validate_loopbacks(yaml):
result = True result = True
msgs = [] msgs = []
logger = logging.getLogger('vppcfg.validator') logger = logging.getLogger('vppcfg.validator')
logger.addHandler(NullHandler()) logger.addHandler(logging.NullHandler())
if not 'loopbacks' in yaml: if not 'loopbacks' in yaml:
return result, msgs return result, msgs

View File

@ -15,11 +15,6 @@ import logging
import validator.interface as interface import validator.interface as interface
import ipaddress import ipaddress
class NullHandler(logging.Handler):
def emit(self, record):
pass
def get_by_name(yaml, ifname): def get_by_name(yaml, ifname):
""" Return the VXLAN by name, if it exists. Return None otherwise. """ """ Return the VXLAN by name, if it exists. Return None otherwise. """
try: try:
@ -64,7 +59,7 @@ def validate_vxlan_tunnels(yaml):
result = True result = True
msgs = [] msgs = []
logger = logging.getLogger('vppcfg.validator') logger = logging.getLogger('vppcfg.validator')
logger.addHandler(NullHandler()) logger.addHandler(logging.NullHandler())
if not 'vxlan_tunnels' in yaml: if not 'vxlan_tunnels' in yaml:
return result, msgs return result, msgs

View File

@ -9,22 +9,16 @@ import fnmatch
import logging import logging
import socket import socket
class NullHandler(logging.Handler):
def emit(self, record):
pass
class VPPApi(): class VPPApi():
def __init__(self, address='/run/vpp/api.sock', clientname='vppcfg'): def __init__(self, address='/run/vpp/api.sock', clientname='vppcfg'):
self.logger = logging.getLogger('%s.vppapi' % clientname) self.logger = logging.getLogger('vppcfg.vppapi')
self.logger.addHandler(NullHandler()) self.logger.addHandler(logging.NullHandler())
self.address = address self.address = address
self.connected = False self.connected = False
self.clientname = clientname self.clientname = clientname
self.vpp = None self.vpp = None
self.config = {} self.config = self.clearconfig()
self.clearconfig()
def connect(self): def connect(self):
@ -66,7 +60,7 @@ class VPPApi():
return True return True
def clearconfig(self): def clearconfig(self):
self.config = {"lcps": {}, "interfaces": {}, "interface_addresses": {}, return {"lcps": {}, "interfaces": {}, "interface_addresses": {},
"bondethernets": {}, "bondethernet_members": {}, "bondethernets": {}, "bondethernet_members": {},
"bridgedomains": {}, "vxlan_tunnels": {}, "l2xcs": {}} "bridgedomains": {}, "vxlan_tunnels": {}, "l2xcs": {}}

12
vppcfg
View File

@ -35,6 +35,7 @@ def main():
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help="""Be quiet (only log warnings/errors), default False""") parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help="""Be quiet (only log warnings/errors), default False""")
args = parser.parse_args() args = parser.parse_args()
level = logging.INFO level = logging.INFO
if args.debug: if args.debug:
level = logging.DEBUG level = logging.DEBUG
@ -51,13 +52,10 @@ def main():
logging.error("Couldn't read config from %s" % args.config) logging.error("Couldn't read config from %s" % args.config)
sys.exit(-1) sys.exit(-1)
v = Validator(schema=args.schema) validator = Validator(schema=args.schema)
rv, msgs = v.validate(cfg) if not validator.valid_config(cfg):
if not rv: logging.error("Configuration is not valid, bailing")
for m in msgs: sys.exit(-2)
logging.error(m)
else:
logging.info("Configuration validated successfully")
vpp = VPPApi() vpp = VPPApi()
vpp.readconfig() vpp.readconfig()