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
|
|
|
|
"""
|
2021-09-27 22:48:13 +05:30
|
|
|
import subprocess
|
2021-09-25 15:04:29 +05:30
|
|
|
from pathlib import Path
|
|
|
|
from dasbus.connection import SystemMessageBus
|
2021-09-27 10:00:59 +05:30
|
|
|
|
2021-09-25 15:04:29 +05:30
|
|
|
|
|
|
|
NETWORKD_BUS = "org.freedesktop.network1"
|
|
|
|
NETWORKD_PATH = "/org/freedesktop/network1"
|
|
|
|
|
|
|
|
|
2021-09-27 10:00:59 +05:30
|
|
|
class NetworkD:
|
2021-09-25 15:04:29 +05:30
|
|
|
"""control systemd-networkd"""
|
|
|
|
|
2021-09-27 10:00:59 +05:30
|
|
|
def __init__(self, runtime_dir="/run/systemd/network", bus=SystemMessageBus()):
|
2021-09-25 15:04:29 +05:30
|
|
|
self._bus = bus
|
2021-09-26 13:25:35 +05:30
|
|
|
self.proxy_reload()
|
2021-09-27 22:30:07 +05:30
|
|
|
|
2021-09-27 10:00:59 +05:30
|
|
|
self.variables = {}
|
|
|
|
self.runtime_path = Path(runtime_dir)
|
2021-09-27 12:43:09 +05:30
|
|
|
# Create the runtime directory if it doesn't exist
|
|
|
|
self.runtime_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2021-09-27 10:00:59 +05:30
|
|
|
def set_vars(self, **variables):
|
|
|
|
"""set the variables to replace with str.format"""
|
|
|
|
self.variables = variables
|
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-27 10:00:59 +05:30
|
|
|
def reload(self) -> None:
|
|
|
|
"""reload the systemd-networkd configuration"""
|
|
|
|
self.proxy.Reload()
|
|
|
|
|
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-27 10:00:59 +05:30
|
|
|
source = Path(name)
|
|
|
|
destination = self.runtime_path / source.name
|
|
|
|
|
|
|
|
# Substitute variables in the config
|
|
|
|
text = source.read_text(encoding="utf-8").format(**self.variables)
|
2021-09-27 22:30:07 +05:30
|
|
|
# now write it to a runtime config of the same name
|
2021-09-27 10:00:59 +05:30
|
|
|
destination.write_text(text, encoding="utf-8")
|
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def is_routable(self) -> bool:
|
|
|
|
"""returns true if any interface is routable"""
|
|
|
|
return self.proxy.AddressState == "routable"
|
2021-09-27 12:43:09 +05:30
|
|
|
|
2021-09-27 22:30:07 +05:30
|
|
|
def delete_interface(self, name: str) -> None:
|
|
|
|
"""delete the given interface"""
|
2021-09-27 22:48:13 +05:30
|
|
|
subprocess.run(["networkctl", "delete", name], check=True)
|
2021-09-27 22:30:07 +05:30
|
|
|
|
2021-09-27 12:43:09 +05:30
|
|
|
def remove_config(self, name: str) -> None:
|
|
|
|
"""
|
|
|
|
remove the file called 'name' from the runtime dir and reload
|
|
|
|
"""
|
|
|
|
path = self.runtime_path / name
|
|
|
|
path.unlink()
|
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def remove_all_configs(self) -> None:
|
|
|
|
"""
|
2021-09-27 22:30:07 +05:30
|
|
|
Remove all configs in runtime_path. This will remove all files
|
|
|
|
in runtime_path without checking who put them there.
|
2021-09-27 12:43:09 +05:30
|
|
|
"""
|
2021-09-27 22:30:07 +05:30
|
|
|
for i in self.runtime_path.iterdir():
|
|
|
|
self.remove_config(i.name)
|