Commit 46cdc52c authored by Дмитрий Никулин's avatar Дмитрий Никулин Committed by Никита Ефремов

Network: support for changing ip addresses per-interface

parent df840443
...@@ -4,7 +4,7 @@ import socket ...@@ -4,7 +4,7 @@ import socket
import os import os
from pyroute2 import IPDB from pyroute2 import IPDB
from pyroute2.netlink.rtnl.ifinfmsg import IFF_UP from pyroute2.netlink.rtnl.ifinfmsg import IFF_UP
from ipaddress import IPv4Address from ipaddress import IPv4Address, IPv4Network
from yaml import dump, safe_load from yaml import dump, safe_load
from settingsd import const from settingsd import const
...@@ -101,6 +101,27 @@ class Network(service.FunctionObject) : ...@@ -101,6 +101,27 @@ class Network(service.FunctionObject) :
def _apply_interface_settings(self, interface_settings): def _apply_interface_settings(self, interface_settings):
with IPDB() as ipdb: with IPDB() as ipdb:
for interface in ipdb.interfaces.values():
if not interface.ifname in interface_settings:
continue
if 'address' in interface_settings[interface.ifname]:
with interface as interface:
already_set = False
for addr, mask in interface.ipaddr:
prefixlen = IPv4Network('0.0.0.0/' + interface_settings[interface.ifname]['mask']).prefixlen
if interface_settings[interface.ifname]['address'] == addr and prefixlen == mask:
already_set = True
break
if not already_set:
interface.add_ip(
interface_settings[interface.ifname]['address'],
IPv4Network('0.0.0.0/' + interface_settings[interface.ifname]['mask']).prefixlen
)
for addr, mask in interface.ipaddr:
interface.del_ip(addr, mask)
interfaces = [i.ifname for i in ipdb.interfaces.values()] interfaces = [i.ifname for i in ipdb.interfaces.values()]
port_settings = {} port_settings = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment