2021-08-13 10:35:13 +05:30
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
config.py
|
|
|
|
---------
|
|
|
|
|
|
|
|
This file contains functions to parse configuration files
|
|
|
|
and arguments. Most of these functions are meant to be used
|
|
|
|
by parse_args() internally, so only parse_args() should
|
|
|
|
be imported outside this file.
|
2021-08-14 22:00:07 +05:30
|
|
|
|
|
|
|
Some parts of naxalnet can be configured by configuration
|
|
|
|
files and arguments. First, the default values from
|
|
|
|
default.py is taken. Then, key-value pairs from the
|
|
|
|
configuration files are read, if they exist, in the
|
|
|
|
following order:
|
|
|
|
|
|
|
|
- First it reads /usr/share/naxalnet/naxalnet.conf
|
|
|
|
and then from /usr/share/naxalnet/naxalnet.conf.d/*.conf
|
|
|
|
where *.conf means any file with the name ending with
|
|
|
|
.conf
|
|
|
|
- Then, it looks for the files naxalnet.conf and
|
|
|
|
naxalnet.conf.d/*.conf from the directory
|
|
|
|
/etc/naxalnet, like it did up above.
|
|
|
|
- Then it parses the arguments from the commandline,
|
|
|
|
storing the values in the files parsed until now
|
|
|
|
as fallback. Finally you get an argpase.Namespace object
|
|
|
|
from parse_args().
|
|
|
|
Because of the way this is implemented, all key-value
|
|
|
|
pairs in the configuration should have an argument
|
|
|
|
too, or they won't be parsed.
|
|
|
|
|
|
|
|
All the key-value pairs are replaced if they exist
|
|
|
|
by each new configuration file parsed, similar
|
|
|
|
to how systemd parses configuration and service files.
|
|
|
|
If any of the files checked does not exist, then they are
|
|
|
|
ignored and the next combination is checked. If none of
|
|
|
|
the config files exist and no arguments are given, the
|
|
|
|
fallback data from default.py is used.
|
2021-08-13 10:35:13 +05:30
|
|
|
"""
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from configparser import ConfigParser
|
2021-08-14 22:00:07 +05:30
|
|
|
from argparse import ArgumentParser, Namespace
|
2021-08-13 10:35:13 +05:30
|
|
|
from naxalnet.default import CONFIG, CONFIG_FILES, CONFIG_DIRS
|
|
|
|
|
|
|
|
|
|
|
|
def get_config_files():
|
2021-08-14 22:00:07 +05:30
|
|
|
"""
|
|
|
|
Read list of configuration files and return a list
|
|
|
|
of files that exists as pathlib.Path objects
|
|
|
|
"""
|
2021-08-13 10:35:13 +05:30
|
|
|
config_files = []
|
|
|
|
for directory in CONFIG_DIRS:
|
|
|
|
path = Path(directory)
|
|
|
|
if path.exists():
|
|
|
|
for i in CONFIG_FILES:
|
|
|
|
glob = path.glob(i)
|
|
|
|
config_files.extend(glob)
|
|
|
|
return config_files
|
|
|
|
|
|
|
|
|
|
|
|
def parse_config():
|
|
|
|
"""
|
|
|
|
Parse all configuration files, with the values in
|
|
|
|
default.py as fallback
|
|
|
|
"""
|
|
|
|
parser = ConfigParser()
|
|
|
|
# encoded defaults
|
|
|
|
parser.read_dict(CONFIG)
|
|
|
|
# read config files
|
|
|
|
files = get_config_files()
|
|
|
|
for i in files:
|
|
|
|
parser.read_file(i.open())
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
2021-08-14 22:00:07 +05:30
|
|
|
def parse_args() -> Namespace:
|
2021-08-13 10:35:13 +05:30
|
|
|
"""
|
|
|
|
Parse all arguments and return ArgumentParser.parse_args(),
|
|
|
|
with values in config files as fallback. Ideally, only this
|
|
|
|
function should be used by naxalnet to get arguments and
|
|
|
|
configuration.
|
|
|
|
"""
|
|
|
|
config = parse_config()
|
|
|
|
parser = ArgumentParser(
|
|
|
|
description="setup batman-adv networks with systemd and iwd"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ap-ssid",
|
|
|
|
"-n",
|
|
|
|
type=str,
|
|
|
|
help="SSID of the WiFi AP",
|
|
|
|
default=config["ap"]["ssid"],
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ap-passwd",
|
|
|
|
"-p",
|
|
|
|
"--ap-password",
|
|
|
|
type=str,
|
|
|
|
help="password of the WiFi AP",
|
|
|
|
default=config["ap"]["passwd"],
|
|
|
|
)
|
2021-08-14 22:00:07 +05:30
|
|
|
|
2021-08-13 10:35:13 +05:30
|
|
|
parser.add_argument(
|
|
|
|
"--adhoc-name",
|
|
|
|
"-a",
|
|
|
|
type=str,
|
|
|
|
default=config["adhoc"]["name"],
|
|
|
|
help="name of adhoc network",
|
|
|
|
)
|
2021-08-14 22:00:07 +05:30
|
|
|
|
2021-08-13 10:35:13 +05:30
|
|
|
parser.add_argument(
|
|
|
|
"--print-wifi",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="prints the ssid and password of the WiFi network and exit",
|
|
|
|
)
|
2021-08-14 22:00:07 +05:30
|
|
|
|
2021-08-13 10:35:13 +05:30
|
|
|
parser.add_argument(
|
|
|
|
"--networkd-config-dir",
|
|
|
|
type=str,
|
|
|
|
default=config["networkd"]["confdir"],
|
|
|
|
help="the directory where systemd-networkd configuration files are stored",
|
|
|
|
)
|
2021-08-14 22:00:07 +05:30
|
|
|
|
2021-08-13 10:35:13 +05:30
|
|
|
parser.add_argument(
|
|
|
|
"--networkd-runtime-dir",
|
|
|
|
type=str,
|
|
|
|
default=config["networkd"]["runtimedir"],
|
2021-08-14 22:00:07 +05:30
|
|
|
help="volatile directory where configuration files of systemd-networkd should be copied",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--version",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help="prints the version and exit",
|
2021-08-13 10:35:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
return parser.parse_args()
|