diff --git a/src/hostinfo/networkmanager.py b/src/hostinfo/networkmanager.py index 852618dcda6ad314259262a93c9d656b6974abbf..8b11501c6d379189885265823d239fd30a251b6f 100644 --- a/src/hostinfo/networkmanager.py +++ b/src/hostinfo/networkmanager.py @@ -24,7 +24,7 @@ class Network(object): def reload(self): self._bridge = {} self._wired = {} - self._tunnel = {} + self._ip_tunnel = {} self._tun = {} self._connection = {} # Get devices from network manager @@ -44,7 +44,7 @@ class Network(object): self._bridge[d.HwAddress.lower()] = d pass elif isinstance(d, NetworkManager.IPTunnel): - self._tunnel[d.Interface] = d + self._ip_tunnel[d.Interface] = d pass elif isinstance(d, NetworkManager.Tun): self._tun[d.Interface] = d @@ -85,6 +85,16 @@ class Network(object): return self._bridge[ethernet] return None + def get_ip_tunnel(self, ip_tunnel): + if ip_tunnel in self._ip_tunnel: + return self._ip_tunnel[ip_tunnel] + return None + + def get_tun(self, tun): + if tun in self._tun: + return self._tun[tun] + return None + def get_connections(self, kind, mac=None, id=None, uuid=None): result = [] for k,m,i,u in self._connection.keys(): @@ -264,10 +274,10 @@ def configure_networking(connection, interface, settings=None): after = update_settings(before, update) if before != after: connection.Update(after) - print(before['connection']['id']) - from pprint import pprint - pprint(before) - pprint(after) + # print(before['connection']['id']) + # from pprint import pprint + # pprint(before) + # pprint(after) return True return False @@ -379,13 +389,11 @@ def configure_ethernet_interface(tree, interface): return configure_networking(connection, interface, settings) def configure_ip_tunnel(tree, interface): - print(interface) config_name = interface.ip_tunnel[0] connection = None if connection == None: candidates = network.get_connections(kind='ip-tunnel', id=config_name) - print(candidates) if len(candidates): connection = candidates[0] pass @@ -403,13 +411,38 @@ def configure_ip_tunnel(tree, interface): } } if interface.local[0]: - settings['ip-tunnel', 'local'] = interface.local[0] + settings['ip-tunnel']['local'] = interface.local[0] pass if connection == None: connection = NetworkManager.Settings.AddConnection(settings) pass return configure_networking(connection, interface, settings) +def configure_tun(tree, interface): + config_name = interface.tun[0] + connection = None + if connection == None: + candidates = network.get_connections(kind='tun', + id=config_name) + if len(candidates): + connection = candidates[0] + pass + pass + settings = { + 'connection': { + 'id': config_name, + 'interface-name': config_name, + 'type': 'tun' + }, + 'tun': { + } + } + if connection == None: + connection = NetworkManager.Settings.AddConnection(settings) + pass + result = configure_networking(connection, interface, settings) + return result + def activate_bridge_interface(interface): bridge_name = interface.bridge[0] or interface.bridge_to[0] device_name = network.wired_device_name(interface.ethernet[0]) @@ -447,6 +480,26 @@ def activate_interface(interface): pass pass +def activate_ip_tunnel(interface): + ip_tunnel_name = interface.ip_tunnel[0] + candidates = network.get_connections(kind='ip-tunnel', id=ip_tunnel_name) + if len(candidates): + connection = candidates[0] + NetworkManager.NetworkManager.ActivateConnection( + connection, network.get_ip_tunnel(ip_tunnel_name), '/') + pass + pass + +def activate_tun(interface): + tun_name = interface.tun[0] + candidates = network.get_connections(kind='tun', id=tun_name) + if len(candidates): + connection = candidates[0] + NetworkManager.NetworkManager.ActivateConnection( + connection, network.get_tun(tun_name), '/') + pass + pass + def execute(tree, host): network.parse_tree(tree) for h in tree._host_: @@ -466,6 +519,10 @@ def execute(tree, host): pass elif i.ip_tunnel[0]: changed |= configure_ip_tunnel(tree, i) + pass + elif i.tun[0]: + changed |= configure_tun(tree, i) + pass else: print(i) @@ -483,6 +540,12 @@ def execute(tree, host): elif i.ethernet[0]: activate_interface(i) pass + elif i.ip_tunnel[0]: + activate_ip_tunnel(i) + pass + elif i.tun[0]: + activate_tun(i) + pass pass except dbus.exceptions.DBusException as e: print(e)