143 lines
5.4 KiB
Python
Executable File
143 lines
5.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
#
|
|
# Copyright (c) 2022 Pim van Pelt
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at:
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import sys
|
|
import yaml
|
|
import logging
|
|
from config import Validator
|
|
from vpp.reconciler import Reconciler
|
|
from vpp.vppapi import VPPApiDumper
|
|
|
|
try:
|
|
import argparse
|
|
except ImportError:
|
|
print("ERROR: install argparse manually: sudo pip install argparse")
|
|
sys.exit(-2)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
|
|
parser.add_argument('-d', '--debug', dest='debug', action='store_true', help="""enable debug logging, default False""")
|
|
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help="""be quiet (only warnings/errors), default False""")
|
|
parser.add_argument('-f', '--force', dest='force', action='store_true', help="""force progress despite warnings, default False""")
|
|
|
|
subparsers = parser.add_subparsers(dest='command')
|
|
check_p = subparsers.add_parser('check', help="check given YAML config for validity (no VPP)")
|
|
check_p.add_argument('-s', '--schema', dest='schema', type=str, default='./schema.yaml', help="""YAML schema validation file""")
|
|
check_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""")
|
|
|
|
dump_p = subparsers.add_parser('dump', help="dump current running VPP configuration (VPP readonly)")
|
|
|
|
plan_p = subparsers.add_parser('plan', help="plan changes from current VPP dataplane to target config (VPP readonly)")
|
|
plan_p.add_argument('-s', '--schema', dest='schema', type=str, default='./schema.yaml', help="""YAML schema validation file""")
|
|
plan_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""")
|
|
plan_p.add_argument('-o', '--output', dest='outfile', required=False, default='-', type=str, help="""Output file for VPP CLI commands, default stdout""")
|
|
|
|
apply_p = subparsers.add_parser('apply', help="apply changes from current VPP dataplane to target config")
|
|
apply_p.add_argument('-s', '--schema', dest='schema', type=str, default='./schema.yaml', help="""YAML schema validation file""")
|
|
apply_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""")
|
|
|
|
args = parser.parse_args()
|
|
if not args.command:
|
|
parser.print_help()
|
|
print("\nPlease see vppcfg <command> -h for per-command arguments")
|
|
sys.exit(0)
|
|
|
|
level = logging.INFO
|
|
if args.debug:
|
|
level = logging.DEBUG
|
|
if args.quiet:
|
|
level = logging.WARNING
|
|
logging.basicConfig(format='[%(levelname)-8s] %(name)s.%(funcName)s: %(message)s', level=level)
|
|
|
|
if args.command=="dump":
|
|
d = VPPApiDumper()
|
|
if not d.readconfig():
|
|
logging.error("Could not retrieve config from VPP")
|
|
sys.exit(-7)
|
|
d.dump()
|
|
sys.exit(0)
|
|
|
|
try:
|
|
with open(args.config, "r") as f:
|
|
logging.info("Loading configfile %s" % args.config)
|
|
cfg = yaml.load(f, Loader = yaml.FullLoader)
|
|
logging.debug("Config: %s" % cfg)
|
|
except:
|
|
logging.error("Couldn't read config from %s" % args.config)
|
|
sys.exit(-1)
|
|
|
|
v = Validator(schema=args.schema)
|
|
if not v.valid_config(cfg):
|
|
logging.error("Configuration is not valid, bailing")
|
|
sys.exit(-2)
|
|
logging.info("Configuration is valid")
|
|
if args.command=="check":
|
|
sys.exit(0)
|
|
|
|
r = Reconciler(cfg)
|
|
if not r.vpp_readconfig():
|
|
sys.exit(-3)
|
|
|
|
if not r.phys_exist_in_vpp():
|
|
logging.error("Not all PHYs in the config exist in VPP")
|
|
sys.exit(-4)
|
|
|
|
if not r.phys_exist_in_config():
|
|
logging.error("Not all PHYs in VPP exist in the config")
|
|
sys.exit(-5)
|
|
|
|
if not r.lcps_exist_with_lcp_enabled():
|
|
logging.error("Linux Control Plane is needed, but neither lcpng nor linux-cp are available")
|
|
sys.exit(-6)
|
|
|
|
failed = False
|
|
if not r.prune():
|
|
if not args.force:
|
|
logging.error("Planning prune failure")
|
|
sys.exit(-10)
|
|
failed = True
|
|
logging.warning("Planning prune failure, continuing due to --force")
|
|
|
|
if not r.create():
|
|
if not args.force:
|
|
logging.error("Planning create failure")
|
|
sys.exit(-20)
|
|
failed = True
|
|
logging.warning("Planning create failure, continuing due to --force")
|
|
|
|
if not r.sync():
|
|
if not args.force:
|
|
logging.error("Planning sync failure")
|
|
sys.exit(-30)
|
|
failed = True
|
|
logging.warning("Planning sync failure, continuing due to --force")
|
|
|
|
if args.command=="plan":
|
|
r.write(args.outfile, ok=not failed)
|
|
|
|
if failed:
|
|
logging.error("Planning failed")
|
|
sys.exit(-40)
|
|
|
|
logging.info("Planning succeeded")
|
|
if args.command=="plan":
|
|
sys.exit(0)
|
|
|
|
sys.exit(0)
|
|
if __name__ == "__main__":
|
|
main()
|