2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
core-daemon: the CORE daemon is a server process that receives CORE API
|
2013-08-29 15:21:13 +01:00
|
|
|
messages and instantiates emulated nodes and networks within the kernel. Various
|
|
|
|
message handlers are defined and some support for sending messages.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2019-03-02 21:04:55 +00:00
|
|
|
import argparse
|
2019-02-16 17:50:19 +00:00
|
|
|
import logging
|
2019-10-04 20:36:15 +01:00
|
|
|
import os
|
2017-04-25 16:45:34 +01:00
|
|
|
import time
|
2019-06-07 18:05:40 +01:00
|
|
|
from configparser import ConfigParser
|
2021-03-19 23:54:24 +00:00
|
|
|
from pathlib import Path
|
2019-06-07 18:05:40 +01:00
|
|
|
|
2019-09-28 07:29:15 +01:00
|
|
|
from core import constants
|
2019-06-07 18:05:40 +01:00
|
|
|
from core.api.grpc.server import CoreGrpcServer
|
2019-10-18 18:33:31 +01:00
|
|
|
from core.constants import CORE_CONF_DIR, COREDPY_VERSION
|
2022-03-08 22:18:47 +00:00
|
|
|
from core.emulator.coreemu import CoreEmu
|
|
|
|
from core.utils import load_logging_config
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def banner():
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-05-04 21:49:14 +01:00
|
|
|
Output the program banner printed to the terminal or log file.
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("CORE daemon v.%s started %s", constants.COREDPY_VERSION, time.ctime())
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
|
2019-09-26 23:20:32 +01:00
|
|
|
def cored(cfg):
|
2017-05-04 21:49:14 +01:00
|
|
|
"""
|
|
|
|
Start the CoreServer object and enter the server loop.
|
|
|
|
|
|
|
|
:param dict cfg: core configuration
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-02-19 06:54:14 +00:00
|
|
|
# initialize grpc api
|
2022-03-08 22:18:47 +00:00
|
|
|
coreemu = CoreEmu(cfg)
|
|
|
|
grpc_server = CoreGrpcServer(coreemu)
|
2019-10-18 22:28:50 +01:00
|
|
|
address_config = cfg["grpcaddress"]
|
|
|
|
port_config = cfg["grpcport"]
|
|
|
|
grpc_address = f"{address_config}:{port_config}"
|
2022-03-08 22:18:47 +00:00
|
|
|
grpc_server.listen(grpc_address)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
def get_merged_config(filename):
|
2017-05-04 21:49:14 +01:00
|
|
|
"""
|
|
|
|
Return a configuration after merging config file and command-line arguments.
|
|
|
|
|
|
|
|
:param str filename: file name to merge configuration settings with
|
|
|
|
:return: merged configuration
|
|
|
|
:rtype: dict
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# these are the defaults used in the config file
|
2019-10-18 18:33:31 +01:00
|
|
|
default_log = os.path.join(constants.CORE_CONF_DIR, "logging.conf")
|
|
|
|
default_grpc_port = "50051"
|
|
|
|
default_address = "localhost"
|
2018-03-23 16:57:37 +00:00
|
|
|
defaults = {
|
2019-10-18 18:33:31 +01:00
|
|
|
"grpcport": default_grpc_port,
|
|
|
|
"grpcaddress": default_address,
|
2022-07-28 00:00:10 +01:00
|
|
|
"logfile": default_log,
|
2018-03-23 16:57:37 +00:00
|
|
|
}
|
2019-03-02 21:04:55 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2022-07-28 00:00:10 +01:00
|
|
|
description=f"CORE daemon v.{COREDPY_VERSION} instantiates Linux network namespace nodes."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--configfile",
|
|
|
|
dest="configfile",
|
|
|
|
help=f"read config from specified file; default = {filename}",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ovs",
|
|
|
|
action="store_true",
|
|
|
|
help="enable experimental ovs mode, default is false",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--grpc-port",
|
|
|
|
dest="grpcport",
|
|
|
|
help=f"grpc port to listen on; default {default_grpc_port}",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--grpc-address",
|
|
|
|
dest="grpcaddress",
|
|
|
|
help=f"grpc address to listen on; default {default_address}",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l", "--logfile", help=f"core logging configuration; default {default_log}"
|
|
|
|
)
|
2013-08-29 15:21:13 +01:00
|
|
|
# parse command line options
|
2019-03-02 21:04:55 +00:00
|
|
|
args = parser.parse_args()
|
2020-06-26 06:05:10 +01:00
|
|
|
# convert ovs to internal format
|
|
|
|
args.ovs = "1" if args.ovs else "0"
|
2013-08-29 15:21:13 +01:00
|
|
|
# read the config file
|
2019-03-02 21:04:55 +00:00
|
|
|
if args.configfile is not None:
|
|
|
|
filename = args.configfile
|
|
|
|
del args.configfile
|
2019-06-04 02:22:25 +01:00
|
|
|
cfg = ConfigParser(defaults)
|
2013-08-29 15:21:13 +01:00
|
|
|
cfg.read(filename)
|
|
|
|
section = "core-daemon"
|
|
|
|
if not cfg.has_section(section):
|
|
|
|
cfg.add_section(section)
|
2019-09-30 19:51:40 +01:00
|
|
|
# merge argparse with configparser
|
|
|
|
for opt in vars(args):
|
|
|
|
val = getattr(args, opt)
|
2013-08-29 15:21:13 +01:00
|
|
|
if val is not None:
|
2019-03-02 21:04:55 +00:00
|
|
|
cfg.set(section, opt, str(val))
|
|
|
|
return dict(cfg.items(section))
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def main():
|
2017-05-04 21:49:14 +01:00
|
|
|
"""
|
|
|
|
Main program startup.
|
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-10-18 18:33:31 +01:00
|
|
|
cfg = get_merged_config(f"{CORE_CONF_DIR}/core.conf")
|
2021-03-19 23:54:24 +00:00
|
|
|
log_config_path = Path(cfg["logfile"])
|
|
|
|
load_logging_config(log_config_path)
|
2013-08-29 15:21:13 +01:00
|
|
|
banner()
|
|
|
|
try:
|
2019-09-26 23:20:32 +01:00
|
|
|
cored(cfg)
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyboardInterrupt:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("keyboard interrupt, stopping core daemon")
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|