2021-09-25 15:04:29 +05:30
|
|
|
# This file is part of naxalnet.
|
|
|
|
# Copyright (C) 2021 The naxalnet Authors
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
network.py
|
|
|
|
----------
|
|
|
|
|
|
|
|
This submodule manages the systemd-networkd configuration.
|
|
|
|
TODO: Add more details
|
|
|
|
"""
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from shutil import copy
|
|
|
|
from dasbus.connection import SystemMessageBus
|
2021-09-26 13:57:40 +05:30
|
|
|
from naxalnet.config import args
|
2021-09-25 15:04:29 +05:30
|
|
|
|
|
|
|
NETWORKD_BUS = "org.freedesktop.network1"
|
|
|
|
NETWORKD_PATH = "/org/freedesktop/network1"
|
|
|
|
|
|
|
|
|
|
|
|
def copy_glob(directory: str, glob: str, destination: str) -> None:
|
|
|
|
"""copy files in directory matching the glob to destination"""
|
|
|
|
match = Path(directory).glob(glob)
|
|
|
|
for i in match:
|
|
|
|
copy(i, destination)
|
|
|
|
|
|
|
|
|
|
|
|
class Networkd:
|
|
|
|
"""control systemd-networkd"""
|
|
|
|
|
|
|
|
def __init__(self, bus=SystemMessageBus()):
|
|
|
|
self._bus = bus
|
2021-09-26 13:25:35 +05:30
|
|
|
self.proxy_reload()
|
2021-09-26 13:57:40 +05:30
|
|
|
self.files = []
|
|
|
|
self.variables = {"batdev": "bat0", "bridgedev": "bridge0"}
|
2021-09-26 13:25:35 +05:30
|
|
|
|
2021-09-26 13:57:40 +05:30
|
|
|
def proxy_reload(self) -> None:
|
2021-09-26 13:25:35 +05:30
|
|
|
"""reload the proxy"""
|
2021-09-25 15:04:29 +05:30
|
|
|
self.proxy = self._bus.get_proxy(NETWORKD_BUS, NETWORKD_PATH)
|
2021-09-26 13:25:35 +05:30
|
|
|
|
2021-09-26 13:57:40 +05:30
|
|
|
def add_config(self, name: str) -> None:
|
2021-09-26 13:25:35 +05:30
|
|
|
"""add config file to runtime directory and reload networkd"""
|
2021-09-26 13:57:40 +05:30
|
|
|
text = Path(name).read_text(encoding="utf-8")
|
|
|
|
contents = text.format(**self.variables)
|
|
|
|
self.files.append(name)
|
|
|
|
Path(args.networkd_runtime_dir + "/" + name).write_text(
|
|
|
|
contents, encoding="utf-8"
|
|
|
|
)
|