2015-06-24 19:44:31 +00:00
|
|
|
# Copyright 2015 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
""" Network Model
|
|
|
|
|
|
|
|
Provides network device listings and extended network information
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-06-24 19:44:31 +00:00
|
|
|
import logging
|
2015-11-06 15:19:44 +00:00
|
|
|
import textwrap
|
2015-07-23 02:31:21 +00:00
|
|
|
from urwid import (ListBox, Pile, BoxAdapter,
|
2015-07-22 01:34:46 +00:00
|
|
|
Text, Columns)
|
2015-09-10 18:25:54 +00:00
|
|
|
import yaml
|
2016-07-06 22:14:13 +00:00
|
|
|
from netifaces import AF_INET, AF_INET6
|
2016-06-30 18:17:01 +00:00
|
|
|
from subiquitycore.ui.lists import SimpleList
|
|
|
|
from subiquitycore.ui.buttons import cancel_btn, menu_btn, done_btn
|
|
|
|
from subiquitycore.ui.utils import Padding, Color
|
2016-07-26 02:21:24 +00:00
|
|
|
from subiquitycore.view import BaseView
|
2015-06-24 19:44:31 +00:00
|
|
|
|
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger('subiquitycore.views.network')
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
|
2016-07-26 02:21:24 +00:00
|
|
|
class NetworkView(BaseView):
|
2015-07-21 20:34:46 +00:00
|
|
|
def __init__(self, model, signal):
|
2015-06-24 19:44:31 +00:00
|
|
|
self.model = model
|
2015-07-21 20:34:46 +00:00
|
|
|
self.signal = signal
|
2015-06-24 19:44:31 +00:00
|
|
|
self.items = []
|
2016-08-16 19:25:58 +00:00
|
|
|
self.error = Text("", align='center')
|
2015-06-24 19:44:31 +00:00
|
|
|
self.body = [
|
|
|
|
Padding.center_79(self._build_model_inputs()),
|
|
|
|
Padding.line_break(""),
|
|
|
|
Padding.center_79(self._build_additional_options()),
|
|
|
|
Padding.line_break(""),
|
2016-08-16 19:25:58 +00:00
|
|
|
Padding.center_79(Color.info_error(self.error)),
|
|
|
|
Padding.line_break(""),
|
2015-11-19 21:24:40 +00:00
|
|
|
Padding.fixed_10(self._build_buttons()),
|
2015-06-24 19:44:31 +00:00
|
|
|
]
|
2015-11-06 19:38:21 +00:00
|
|
|
# FIXME determine which UX widget should have focus
|
2016-06-22 19:19:54 +00:00
|
|
|
self.lb = ListBox(self.body)
|
|
|
|
self.lb.set_focus(4) # _build_buttons
|
|
|
|
super().__init__(self.lb)
|
2015-06-24 19:44:31 +00:00
|
|
|
|
|
|
|
def _build_buttons(self):
|
2016-06-22 19:19:54 +00:00
|
|
|
cancel = Color.button(cancel_btn(on_press=self.cancel),
|
|
|
|
focus_map='button focus')
|
|
|
|
done = Color.button(done_btn(on_press=self.done),
|
|
|
|
focus_map='button focus')
|
|
|
|
self.default_focus = done
|
2015-10-08 22:10:54 +00:00
|
|
|
|
2016-06-22 19:19:54 +00:00
|
|
|
buttons = [done, cancel]
|
|
|
|
return Pile(buttons, focus_item=done)
|
2015-06-24 19:44:31 +00:00
|
|
|
|
|
|
|
def _build_model_inputs(self):
|
2015-06-29 18:57:45 +00:00
|
|
|
log.info("probing for network devices")
|
2015-06-29 15:26:52 +00:00
|
|
|
self.model.probe_network()
|
2015-11-06 15:19:44 +00:00
|
|
|
ifaces = self.model.get_all_interface_names()
|
2016-06-22 19:19:54 +00:00
|
|
|
ifname_width = 8 # default padding
|
2015-06-24 19:44:31 +00:00
|
|
|
|
2015-06-29 20:01:16 +00:00
|
|
|
col_1 = []
|
2016-08-09 19:11:02 +00:00
|
|
|
col_2 = []
|
|
|
|
|
|
|
|
# Display each interface -- name in first column, then configured IPs
|
|
|
|
# in the second.
|
|
|
|
log.debug('interfaces: {}'.format(ifaces))
|
2015-06-29 20:01:16 +00:00
|
|
|
for iface in ifaces:
|
|
|
|
col_1.append(
|
2016-06-30 21:24:31 +00:00
|
|
|
Color.info_major(
|
2015-08-24 15:51:10 +00:00
|
|
|
menu_btn(label=iface,
|
|
|
|
on_press=self.on_net_dev_press),
|
2016-06-22 19:19:54 +00:00
|
|
|
focus_map='button focus'))
|
2015-06-29 20:01:16 +00:00
|
|
|
|
2016-06-30 21:24:31 +00:00
|
|
|
interface = self.model.get_interface(iface)
|
2016-08-09 19:11:02 +00:00
|
|
|
ip_status = {
|
2016-08-16 04:48:15 +00:00
|
|
|
'ipv4_addresses': interface.ipv4_addresses,
|
|
|
|
'ipv6_addresses': interface.ipv6_addresses,
|
|
|
|
'dhcp4_addresses': interface.dhcp4_addresses,
|
|
|
|
'dhcp6_addresses': interface.dhcp6_addresses,
|
2016-08-15 21:05:19 +00:00
|
|
|
'dhcp4': interface.dhcp4,
|
|
|
|
'dhcp6': interface.dhcp6,
|
2016-06-30 21:24:31 +00:00
|
|
|
}
|
2016-08-09 19:11:02 +00:00
|
|
|
|
2016-08-16 04:48:15 +00:00
|
|
|
for addr in ip_status['dhcp4_addresses']:
|
2016-08-15 21:05:19 +00:00
|
|
|
template = '{} (dhcp)'.format(addr[0])
|
|
|
|
col_1.append(Text(""))
|
|
|
|
col_2.append(Color.info_primary(Text(template)))
|
|
|
|
|
2016-08-16 04:48:15 +00:00
|
|
|
for addr in ip_status['ipv4_addresses']:
|
2016-08-15 21:05:19 +00:00
|
|
|
template = '{} (manual)'.format(addr)
|
|
|
|
col_1.append(Text(""))
|
|
|
|
col_2.append(Color.info_primary(Text(template)))
|
|
|
|
|
2016-08-16 04:48:15 +00:00
|
|
|
for addr in ip_status['dhcp6_addresses']:
|
|
|
|
template = '{} (dhcp)'.format(addr[0])
|
|
|
|
col_1.append(Text(""))
|
|
|
|
col_2.append(Color.info_primary(Text(template)))
|
|
|
|
|
|
|
|
for addr in ip_status['ipv6_addresses']:
|
|
|
|
template = '{} (manual)'.format(addr)
|
|
|
|
col_1.append(Text(""))
|
|
|
|
col_2.append(Color.info_primary(Text(template)))
|
|
|
|
|
|
|
|
template = None
|
2016-08-15 21:05:19 +00:00
|
|
|
if ( not ip_status['dhcp4'] and not ip_status['dhcp6'] ) \
|
2016-08-16 04:48:15 +00:00
|
|
|
and len(ip_status['ipv4_addresses']) == 0 and \
|
|
|
|
len(ip_status['ipv6_addresses']) == 0:
|
2016-08-15 21:05:19 +00:00
|
|
|
template = "Not configured"
|
2016-08-16 04:48:15 +00:00
|
|
|
|
|
|
|
if ip_status['dhcp4'] and ip_status['dhcp6'] and \
|
|
|
|
len(ip_status['ipv4_addresses']) == 0 and \
|
|
|
|
len(ip_status['dhcp4_addresses']) == 0 and \
|
|
|
|
len(ip_status['ipv6_addresses']) == 0 and \
|
|
|
|
len(ip_status['dhcp6_addresses']) == 0:
|
|
|
|
template = "DHCP is enabled"
|
|
|
|
elif ip_status['dhcp4'] and \
|
|
|
|
len(ip_status['ipv4_addresses']) == 0 and \
|
|
|
|
len(ip_status['dhcp4_addresses']) == 0:
|
|
|
|
template = "DHCPv4 is enabled"
|
|
|
|
elif ip_status['dhcp6'] and \
|
|
|
|
len(ip_status['ipv6_addresses']) == 0 and \
|
|
|
|
len(ip_status['dhcp6_addresses']) == 0:
|
|
|
|
template = "DHCPv6 is enabled"
|
|
|
|
|
|
|
|
if template is not None:
|
2016-08-15 21:05:19 +00:00
|
|
|
col_1.append(Text(""))
|
|
|
|
col_2.append(Color.info_primary(Text(template)))
|
|
|
|
|
2016-08-09 19:11:02 +00:00
|
|
|
# Other device info (MAC, vendor/model, speed)
|
2015-11-04 15:50:58 +00:00
|
|
|
info = self.model.get_iface_info(iface)
|
2016-06-30 21:24:31 +00:00
|
|
|
hwaddr = self.model.get_hw_addr(iface)
|
2015-09-08 19:49:24 +00:00
|
|
|
log.debug('iface info:{}'.format(info))
|
2015-09-03 16:48:40 +00:00
|
|
|
template = ''
|
2016-06-30 21:24:31 +00:00
|
|
|
if hwaddr:
|
|
|
|
template += '{} '.format(hwaddr)
|
2015-11-09 20:35:54 +00:00
|
|
|
if info['bond_slave']:
|
2015-09-03 16:48:40 +00:00
|
|
|
template += '(Bonded) '
|
|
|
|
if not info['vendor'].lower().startswith('unknown'):
|
2015-11-06 15:19:44 +00:00
|
|
|
vendor = textwrap.wrap(info['vendor'], 15)[0]
|
|
|
|
template += '{} '.format(vendor)
|
2015-09-03 16:48:40 +00:00
|
|
|
if not info['model'].lower().startswith('unknown'):
|
2015-11-06 15:19:44 +00:00
|
|
|
model = textwrap.wrap(info['model'], 20)[0]
|
|
|
|
template += '{} '.format(model)
|
2016-06-30 21:24:31 +00:00
|
|
|
if info['speed']:
|
|
|
|
template += '({speed})'.format(**info)
|
2016-08-15 21:05:19 +00:00
|
|
|
#log.debug('template: {}', template)
|
|
|
|
log.debug('hwaddr:{}, {}'.format(hwaddr, template))
|
2016-08-09 19:11:02 +00:00
|
|
|
|
2016-06-30 21:24:31 +00:00
|
|
|
col_2.append(Color.info_minor(Text(template)))
|
2015-11-06 16:36:58 +00:00
|
|
|
|
2016-08-09 19:11:02 +00:00
|
|
|
col_1 = BoxAdapter(SimpleList(col_1),
|
|
|
|
height=len(col_1))
|
2015-09-03 18:46:51 +00:00
|
|
|
if len(col_2):
|
|
|
|
col_2 = BoxAdapter(SimpleList(col_2, is_selectable=False),
|
|
|
|
height=len(col_2))
|
2015-11-06 16:36:58 +00:00
|
|
|
ifname_width += len(max(ifaces, key=len))
|
2016-06-22 19:19:54 +00:00
|
|
|
if ifname_width > 20:
|
2016-07-25 02:44:51 +00:00
|
|
|
ifname_width = 20
|
2015-09-03 18:46:51 +00:00
|
|
|
else:
|
|
|
|
col_2 = Pile([Text("No network interfaces detected.")])
|
2015-06-29 20:01:16 +00:00
|
|
|
|
2015-09-08 19:49:24 +00:00
|
|
|
return Columns([(ifname_width, col_1), col_2], 2)
|
2015-06-24 19:44:31 +00:00
|
|
|
|
|
|
|
def _build_additional_options(self):
|
2016-08-16 19:01:51 +00:00
|
|
|
labels = []
|
2015-11-06 19:38:21 +00:00
|
|
|
ifaces = self.model.get_all_interface_names()
|
|
|
|
|
|
|
|
# Display default route status
|
2016-08-16 16:59:19 +00:00
|
|
|
if self.model.default_v4_gateway is not None:
|
|
|
|
v4_route_source = "via " + self.model.default_v4_gateway
|
|
|
|
|
|
|
|
default_v4_route_w = Color.info_minor(
|
|
|
|
Text(" IPv4 default route " + v4_route_source + "."))
|
2016-08-16 19:01:51 +00:00
|
|
|
labels.append(default_v4_route_w)
|
2016-08-16 16:59:19 +00:00
|
|
|
|
|
|
|
if self.model.default_v6_gateway is not None:
|
|
|
|
v6_route_source = "via " + self.model.default_v6_gateway
|
|
|
|
|
|
|
|
default_v6_route_w = Color.info_minor(
|
|
|
|
Text(" IPv6 default route " + v6_route_source + "."))
|
2016-08-16 19:01:51 +00:00
|
|
|
labels.append(default_v6_route_w)
|
2015-11-06 19:38:21 +00:00
|
|
|
|
2016-08-16 19:01:51 +00:00
|
|
|
max_btn_len = 0
|
|
|
|
buttons = []
|
2015-07-22 01:34:46 +00:00
|
|
|
for opt, sig, _ in self.model.get_menu():
|
2015-11-06 19:38:21 +00:00
|
|
|
if ':set-default-route' in sig:
|
|
|
|
if len(ifaces) < 2:
|
|
|
|
log.debug('Skipping default route menu option'
|
|
|
|
' (only one nic)')
|
|
|
|
continue
|
2015-11-09 20:35:54 +00:00
|
|
|
if ':bond-interfaces' in sig:
|
|
|
|
not_bonded = [iface for iface in ifaces
|
|
|
|
if not self.model.iface_is_bonded(iface)]
|
|
|
|
if len(not_bonded) < 2:
|
|
|
|
log.debug('Skipping bonding menu option'
|
|
|
|
' (not enough available nics)')
|
|
|
|
continue
|
2016-08-16 19:01:51 +00:00
|
|
|
|
|
|
|
if len(opt) > max_btn_len:
|
|
|
|
max_btn_len = len(opt)
|
|
|
|
|
|
|
|
buttons.append(
|
2015-08-24 15:51:10 +00:00
|
|
|
Color.menu_button(
|
2015-08-28 14:33:25 +00:00
|
|
|
menu_btn(label=opt,
|
|
|
|
on_press=self.additional_menu_select),
|
2016-06-22 19:19:54 +00:00
|
|
|
focus_map='button focus'))
|
2016-08-16 19:01:51 +00:00
|
|
|
|
|
|
|
padding = getattr(Padding, 'left_{}'.format(max_btn_len))
|
|
|
|
buttons = [ padding(button) for button in buttons ]
|
|
|
|
return Pile(labels + buttons)
|
2015-06-24 19:44:31 +00:00
|
|
|
|
2015-07-21 21:04:28 +00:00
|
|
|
def additional_menu_select(self, result):
|
2015-07-21 20:34:46 +00:00
|
|
|
self.signal.emit_signal(self.model.get_signal_by_name(result.label))
|
2015-06-24 19:44:31 +00:00
|
|
|
|
2015-07-21 21:04:28 +00:00
|
|
|
def on_net_dev_press(self, result):
|
|
|
|
log.debug("Selected network dev: {}".format(result.label))
|
2015-10-29 20:58:51 +00:00
|
|
|
self.signal.emit_signal('menu:network:main:configure-interface',
|
2015-10-08 22:10:54 +00:00
|
|
|
result.label)
|
|
|
|
|
2016-08-16 19:25:58 +00:00
|
|
|
def show_network_error(self):
|
|
|
|
self.error.set_text("Network configuration failed; please verify your settings.")
|
|
|
|
|
2015-10-08 22:10:54 +00:00
|
|
|
def done(self, result):
|
2016-08-13 04:13:22 +00:00
|
|
|
self.signal.emit_signal('network:finish', self.model.render())
|
2015-07-21 21:04:28 +00:00
|
|
|
|
2015-06-24 19:44:31 +00:00
|
|
|
def cancel(self, button):
|
2015-09-10 18:25:54 +00:00
|
|
|
self.model.reset()
|
2015-10-29 20:58:51 +00:00
|
|
|
self.signal.prev_signal()
|