updated core-daemon to use argparse and formally added ovs and grpc flags

This commit is contained in:
bharnden 2019-03-02 13:04:55 -08:00
parent 44f70d0c2e
commit 0ccf5a7456

View file

@ -5,9 +5,9 @@ messages and instantiates emulated nodes and networks within the kernel. Various
message handlers are defined and some support for sending messages. message handlers are defined and some support for sending messages.
""" """
import argparse
import ConfigParser import ConfigParser
import logging import logging
import optparse
import sys import sys
import threading import threading
import time import time
@ -32,7 +32,7 @@ def banner():
logging.info("CORE daemon v.%s started %s", constants.COREDPY_VERSION, time.ctime()) logging.info("CORE daemon v.%s started %s", constants.COREDPY_VERSION, time.ctime())
def cored(cfg, use_ovs): def cored(cfg):
""" """
Start the CoreServer object and enter the server loop. Start the CoreServer object and enter the server loop.
@ -47,7 +47,7 @@ def cored(cfg, use_ovs):
try: try:
server = CoreServer((host, port), CoreHandler, cfg) server = CoreServer((host, port), CoreHandler, cfg)
if use_ovs: if cfg["ovs"] == "True":
from core.netns.openvswitch import OVS_NODES from core.netns.openvswitch import OVS_NODES
server.coreemu.update_nodes(OVS_NODES) server.coreemu.update_nodes(OVS_NODES)
except: except:
@ -55,6 +55,7 @@ def cored(cfg, use_ovs):
sys.exit(1) sys.exit(1)
# initialize grpc api # initialize grpc api
if cfg["grpc"] == "True":
grpc_thread = threading.Thread(target=listen, args=(server.coreemu,)) grpc_thread = threading.Thread(target=listen, args=(server.coreemu,))
grpc_thread.daemon = True grpc_thread.daemon = True
grpc_thread.start() grpc_thread.start()
@ -80,24 +81,24 @@ def get_merged_config(filename):
"numthreads": "1", "numthreads": "1",
} }
usagestr = "usage: %prog [-h] [options] [args]\n\n" + \ parser = argparse.ArgumentParser(
"CORE daemon v.%s instantiates Linux network namespace " \ description="CORE daemon v.%s instantiates Linux network namespace nodes." % constants.COREDPY_VERSION)
"nodes." % constants.COREDPY_VERSION parser.add_argument("-f", "--configfile", dest="configfile",
parser = optparse.OptionParser(usage=usagestr)
parser.add_option("-f", "--configfile", dest="configfile", type="string",
help="read config from specified file; default = %s" % filename) help="read config from specified file; default = %s" % filename)
parser.add_option("-p", "--port", dest="port", type=int, parser.add_argument("-p", "--port", dest="port", type=int,
help="port number to listen on; default = %s" % defaults["port"]) help="port number to listen on; default = %s" % defaults["port"])
parser.add_option("-t", "--numthreads", dest="numthreads", type=int, parser.add_argument("-n", "--numthreads", dest="numthreads", type=int,
help="number of server threads; default = %s" % defaults["numthreads"]) help="number of server threads; default = %s" % defaults["numthreads"])
parser.add_argument("--ovs", action="store_true", help="enable experimental ovs mode, default is false")
parser.add_argument("--grpc", action="store_true", help="enable grpc api, default is false")
# parse command line options # parse command line options
options, args = parser.parse_args() args = parser.parse_args()
# read the config file # read the config file
if options.configfile is not None: if args.configfile is not None:
filename = options.configfile filename = args.configfile
del options.configfile del args.configfile
cfg = ConfigParser.SafeConfigParser(defaults) cfg = ConfigParser.SafeConfigParser(defaults)
cfg.read(filename) cfg.read(filename)
@ -106,12 +107,12 @@ def get_merged_config(filename):
cfg.add_section(section) cfg.add_section(section)
# merge command line with config file # merge command line with config file
for opt in options.__dict__: for opt in args.__dict__:
val = options.__dict__[opt] val = args.__dict__[opt]
if val is not None: if val is not None:
cfg.set(section, opt, val.__str__()) cfg.set(section, opt, str(val))
return dict(cfg.items(section)), args return dict(cfg.items(section))
def main(): def main():
@ -121,22 +122,14 @@ def main():
:return: nothing :return: nothing
""" """
# get a configuration merged from config file and command-line arguments # get a configuration merged from config file and command-line arguments
cfg, args = get_merged_config("%s/core.conf" % constants.CORE_CONF_DIR) cfg = get_merged_config("%s/core.conf" % constants.CORE_CONF_DIR)
for a in args:
logging.error("ignoring command line argument: %s", a)
banner() banner()
# check if ovs flag was provided
use_ovs = len(sys.argv) == 2 and sys.argv[1] == "ovs"
try: try:
cored(cfg, use_ovs) cored(cfg)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("keyboard interrupt, stopping core daemon") logging.info("keyboard interrupt, stopping core daemon")
sys.exit(0)
if __name__ == "__main__": if __name__ == "__main__":
main() main()