From 553cb3b8daf0f550f3731a3f3be02fc0e11369a0 Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Mon, 1 Aug 2016 17:04:35 +1200 Subject: [PATCH] sketches --- subiquitycore/models/netconfig.py | 98 +++++++++++++++++++++++++++++++ subiquitycore/models/network.py | 3 +- 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 subiquitycore/models/netconfig.py diff --git a/subiquitycore/models/netconfig.py b/subiquitycore/models/netconfig.py new file mode 100644 index 00000000..89c6f39f --- /dev/null +++ b/subiquitycore/models/netconfig.py @@ -0,0 +1,98 @@ +# Copyright 2016 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from probert.network import NetworkInfo + +IGNORED_DEVICE_TYPES = ['lo', 'tun', 'tap'] + +class Device: + # In general, an element of config['ethernets'] (or + # config['wifis']) matches an arbitrary set of interfaces. In + # subiquity, though, it just matches a single interface by + # name. + def __init__(self, name, vendor): + self.name = name + self.vendor = vendor + self.dhcp4 = False + self.dhcp6 = False + self.addresses = [] + self.ignored = False + + @classmethod + def from_probe_data(cls, data): + name = data['hardware']['INTERFACE'] + info = NetworkInfo({name:data}) + return cls(name, info.vendor) + + def render(self): + addresses = [] + for address in self.addresses: + if address.version == 4 and not self.dhcp4: + addresses.append(addresses.with_prefixlen) + if address.version == 6 and not self.dhcp6: + addresses.append(addresses.with_prefixlen) + data = { + 'dhcp4': str(self.dhcp4).lower(), + 'dhcp4': str(self.dhcp4).lower(), + } + if addresses: + data['addresses'] = addresses + return data + +class PhysicalDevice(Device): + pass + +class EthernetDevice(PhysicalDevice): + pass + +## class WifiDevice(PhysicalDevice): +## ## lots of things go here + +## class BridgeDevice(Device): +## def __init__(self, name): +## Device.__init__(name) +## self.interfaces = [] + +class NetworkConfig: + def __init__(self): + self.ethernets = {} + ## self.wifis = {} + ## self.bridges = {} + + def render(self): + ethernets = {} + for ethernet in ethernets: + if not ethernet.ignored: + ethernets[ethernet.name] = ethernet.render() + data = { + 'version': 2, + } + if ethernets: + data['ethernets'] = ethernets + return data + + @classmethod + def from_probe_data(self, results): + network = results['network'] + for name, data in network.items(): + type = data['type'] + if type in IGNORED_DEVICE_TYPES: + continue + if data['bridge']['is_bridge']: + pass ## bridges not yet supported + elif data['bridge']['bond']['is_master']: + pass ## bridges not yet supported + if type == 'eth': + self.ethernets[name] = EthernetDevice.from_probe_data(data) diff --git a/subiquitycore/models/network.py b/subiquitycore/models/network.py index dd4ce705..f31453c1 100644 --- a/subiquitycore/models/network.py +++ b/subiquitycore/models/network.py @@ -73,8 +73,7 @@ class Networkdev(): @property def is_configured(self): - return (self.action is not None and - self.probe_info is not None) + return self.action is not None and self.probe_info is not None @property def type(self):