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