auto disable inactive NICs

This disables any NIC that does not have a global address by the first
time the network screen is shown
This commit is contained in:
Michael Hudson-Doyle 2019-03-25 11:30:26 +13:00
parent 3bb9268974
commit eae2497d41
3 changed files with 32 additions and 3 deletions

View File

@ -175,6 +175,7 @@ class NetworkController(BaseController):
self.model = self.base_model.network self.model = self.base_model.network
self.answers = self.all_answers.get("Network", {}) self.answers = self.all_answers.get("Network", {})
self.view = None self.view = None
self.view_shown = False
if self.opts.dry_run: if self.opts.dry_run:
self.root = os.path.abspath(".subiquity") self.root = os.path.abspath(".subiquity")
netplan_path = self.netplan_path netplan_path = self.netplan_path
@ -300,8 +301,31 @@ class NetworkController(BaseController):
else: else:
raise Exception("could not process action {}".format(action)) raise Exception("could not process action {}".format(action))
def update_initial_configs(self):
# Any device that does not have a (global) address by the time
# we get to the network screen is marked as disabled, with an
# explanation.
for dev in self.model.get_all_netdevs():
has_global_address = False
if dev.info is None:
continue
for a in dev.info.addresses.values():
if a.scope == "global":
has_global_address = True
break
if not has_global_address:
dev.remove_ip_networks_for_version(4)
dev.remove_ip_networks_for_version(6)
log.debug("disabling %s", dev.name)
dev.disabled_reason = _("autoconfiguration failed")
def default(self): def default(self):
if not self.view_shown:
self.update_initial_configs()
self.view = NetworkView(self.model, self) self.view = NetworkView(self.model, self)
if not self.view_shown:
self.apply_config(silent=True)
self.view_shown = True
self.network_event_receiver.view = self.view self.network_event_receiver.view = self.view
self.ui.set_body(self.view) self.ui.set_body(self.view)
if self.answers.get('accept-default', False): if self.answers.get('accept-default', False):
@ -317,7 +341,7 @@ class NetworkController(BaseController):
netplan_config_file_name = '00-snapd-config.yaml' netplan_config_file_name = '00-snapd-config.yaml'
return os.path.join(self.root, 'etc/netplan', netplan_config_file_name) return os.path.join(self.root, 'etc/netplan', netplan_config_file_name)
def apply_config(self): def apply_config(self, silent=False):
config = self.model.render() config = self.model.render()
devs_to_delete = [] devs_to_delete = []
@ -374,6 +398,7 @@ class NetworkController(BaseController):
('apply', BackgroundProcess(['netplan', 'apply'])), ('apply', BackgroundProcess(['netplan', 'apply'])),
]) ])
if not silent:
self.view.show_apply_spinner() self.view.show_apply_spinner()
ts = TaskSequence(self.run_in_bg, tasks, ApplyWatcher(self.view)) ts = TaskSequence(self.run_in_bg, tasks, ApplyWatcher(self.view))
ts.run() ts.run()

View File

@ -110,6 +110,7 @@ class NetworkDev(object):
self.type = typ self.type = typ
self.config = {} self.config = {}
self.info = None self.info = None
self.disabled_reason = None
def dhcp_addresses(self): def dhcp_addresses(self):
r = {4: [], 6: []} r = {4: [], 6: []}

View File

@ -224,7 +224,10 @@ class NetworkView(BaseView):
if dev2.type == "vlan" and dev.name == dev2.config.get('link'): if dev2.type == "vlan" and dev.name == dev2.config.get('link'):
break break
else: else:
address_info.append((Text(_("disabled")), Text(""))) reason = dev.disabled_reason
if reason is None:
reason = ""
address_info.append((Text(_("disabled")), Text(reason)))
rows = [] rows = []
for label, value in address_info: for label, value in address_info:
rows.append(TableRow([Text(""), label, (2, value)])) rows.append(TableRow([Text(""), label, (2, value)]))