def write(daemon, global_conf, overlay_conf_dir): ''' Write a daemon's global configuration to the given file, and overlay configurations to the given directory. ''' global_config = util.config() global_config["log"] = daemon.log global_config["log-level"] = daemon.log_level global_config["use-ipsec"] = str(daemon.use_ipsec).lower() global_config["ipsec-manage"] = str(daemon.ipsec_manage).lower() global_config["ipsec-psk"] = daemon.ipsec_psk global_config["lib-dir"] = daemon.lib_dir global_config["overlay-dir"] = daemon.overlay_dir global_config["fwbuilder-script-dir"] = daemon.fwbuilder_script_dir if daemon.overlay_conf_dir: global_config["overlay-conf-dir"] = daemon.overlay_conf_dir global_config["template-dir"] = daemon.template_dir global_config["ipsec-conf"] = daemon.ipsec_conf global_config["ipsec-secrets"] = daemon.ipsec_secrets global_config.write(global_conf) for o in daemon.overlays.values(): overlay_config = util.config() o.write(overlay_config) overlay_config.write(os.path.join(overlay_conf_dir, "%s.conf" % o.name))
def read(log, log_level, conf=None, config=None): ''' Parse a configuration, file or dictionary, and return an overlay object. ''' # If specified, read the overlay configuration file. if conf: config = util.config(conf) elif config: config = copy.deepcopy(config) else: raise NoOverlayConfigError() # Get the overlay configuration. section = config["overlay"] # Fetch just enough configuration to start an overlay logger. name = util.name_get(section["name"]) lg = logger.create(log, log_level, "l3overlay", name) lg.start() # Global overlay configuration. enabled = util.boolean_get(section["enabled"]) if "enabled" in section else True asn = util.integer_get(section["asn"], minval=0, maxval=65535) linknet_pool = util.ip_network_get(section["linknet-pool"]) fwbuilder_script_file = section["fwbuilder-script"] if "fwbuilder-script" in section else None # Generate the list of nodes, sorted numerically. nodes = [] for key, value in section.items(): if key.startswith("node-"): node = util.list_get(value, length=2, pattern="\\s") nodes.append((util.name_get(node[0]), util.ip_address_get(node[1]))) if not nodes: raise NoNodeListError(name) # Get the node object for this node from the list of nodes. this_node = next((n for n in nodes if n[0] == util.name_get(section["this-node"])), None) if not this_node: raise MissingThisNodeError(name, util.name_get(section["this-node"])) # Static interfaces. interfaces = [] for s, c in config.items(): if s.startswith("static"): interfaces.append(interface.read(lg, s, c)) elif s == "DEFAULT" or s == "overlay": continue else: raise UnsupportedSectionTypeError(name, s) # Return overlay object. return Overlay(lg, name, enabled, asn, linknet_pool, fwbuilder_script_file, nodes, this_node, interfaces)
def read(args): ''' Create a daemon object using the given argument dictionary. ''' # Load the global configuration file (if specified), # and create a ValueReader based on that and the given arguments. global_conf = args["global_conf"] if "global_conf" in args else None global_config = util.config(global_conf)["global"] if global_conf else None reader = ValueReader(args, global_conf, global_config) # Get enough configuration to start a logger. log = reader.get("log") log_level = util.enum_get( reader.get("log-level", default="INFO"), ["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], ) # Start the logger. lg = logger.create(log, log_level, "l3overlay") lg.start() # Log exceptions for the rest of the initialisation process. try: if global_config: lg.debug("loaded global configuration file '%s'" % global_conf) # Get (general) global configuration. dry_run = reader.boolean_get("dry-run", default=False) use_ipsec = reader.boolean_get("use-ipsec", default=True) ipsec_manage = reader.boolean_get("ipsec-manage", default=True) _psk = reader.get("ipsec-psk", args_optional=True) ipsec_psk = util.hex_get_string(_psk, min=6, max=64) if _psk is not None else None # Get required directory paths. lib_dir = reader.path_get( "lib-dir", default = os.path.join(util.path_root(), "var", "lib", "l3overlay"), ) overlay_dir = os.path.join(lib_dir, "overlays") fwbuilder_script_dir = reader.path_get( "fwbuilder-script-dir", default = util.path_search("fwbuilder-scripts"), ) overlay_conf_dir = reader.path_get( "overlay-conf-dir", default = util.path_search("overlays"), ) template_dir = reader.path_get( "template-dir", default = util.path_search("templates"), ) # Get required file paths. pid = reader.path_get( "pid", default = os.path.join(util.path_root(), "var", "run", "l3overlayd.pid"), ) ipsec_conf = reader.path_get( "ipsec-conf", default = os.path.join(util.path_root(), "etc", "ipsec.d", "l3overlay.conf"), ) ipsec_secrets = reader.path_get( "ipsec-secrets", default = os.path.join(util.path_root(), "etc", "ipsec.secrets" if ipsec_manage else "ipsec.l3overlay.secrets"), ) # Get overlay configuration file paths. overlay_confs = args["overlay_conf"] if overlay_confs is not None: if isinstance(overlay_confs, str): overlay_confs = tuple(util.path_get(overlay_confs, relative_dir=os.getcwd())) elif isinstance(overlay_confs, list) or isinstance(overlay_confs, dict): overlay_confs = tuple((util.path_get(oc, relative_dir=os.getcwd()) for oc in overlay_confs)) else: raise ReadError("expected string, list or dict for overlay_confs, got %s: %s" % (type(overlay_confs), overlay_confs)) elif overlay_conf_dir is not None: overlay_confs = tuple((os.path.join(overlay_conf_dir, oc) for oc in os.listdir(overlay_conf_dir))) else: raise NoOverlayConfError() lg.debug("Global configuration:") lg.debug(" dry-run = %s" % dry_run) lg.debug(" use-ipsec = %s" % use_ipsec) lg.debug(" ipsec-manage = %s" % ipsec_manage) lg.debug(" ipsec-psk = %s" % ("<redacted, length %i>" % len(ipsec_psk) if ipsec_psk else None)) lg.debug(" lib-dir = %s" % lib_dir) lg.debug(" fwbuilder-script-dir = %s" % fwbuilder_script_dir) lg.debug(" overlay-conf-dir = %s" % overlay_conf_dir) lg.debug(" template-dir = %s" % template_dir) lg.debug("") lg.debug("Overlay configuration files:") for overlay_conf in overlay_confs: lg.debug(" %s" % overlay_conf) lg.debug("") # Create the application state for each overlay. overlays = {} for overlay_conf in overlay_confs: o = overlay.read(log, log_level, conf=overlay_conf) overlays[o.name] = o # Return a set up daemon object. return Daemon( dry_run, lg, log, log_level, use_ipsec, ipsec_manage, ipsec_psk, lib_dir, overlay_dir, fwbuilder_script_dir, overlay_conf_dir, template_dir, pid, ipsec_conf, ipsec_secrets, overlays, ) except Exception as e: lg.exception(e) raise