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-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
|
2015-06-24 19:44:31 +00:00
|
|
|
from subiquity.ui.lists import SimpleList
|
2015-10-08 22:10:54 +00:00
|
|
|
from subiquity.ui.buttons import cancel_btn, menu_btn, done_btn
|
2015-06-24 19:44:31 +00:00
|
|
|
from subiquity.ui.utils import Padding, Color
|
2015-07-23 02:31:21 +00:00
|
|
|
from subiquity.view import ViewPolicy
|
2015-06-24 19:44:31 +00:00
|
|
|
|
|
|
|
|
2015-10-29 20:58:51 +00:00
|
|
|
log = logging.getLogger('subiquity.views.network')
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
|
2015-07-23 02:31:21 +00:00
|
|
|
class NetworkView(ViewPolicy):
|
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 = []
|
|
|
|
self.body = [
|
|
|
|
Padding.center_79(self._build_model_inputs()),
|
|
|
|
Padding.line_break(""),
|
|
|
|
Padding.center_79(self._build_additional_options()),
|
|
|
|
Padding.line_break(""),
|
2015-08-24 15:51:10 +00:00
|
|
|
Padding.center_15(self._build_buttons()),
|
2015-06-24 19:44:31 +00:00
|
|
|
]
|
|
|
|
super().__init__(ListBox(self.body))
|
|
|
|
|
|
|
|
def _build_buttons(self):
|
2015-10-08 22:10:54 +00:00
|
|
|
cancel = cancel_btn(on_press=self.cancel)
|
|
|
|
done = done_btn(on_press=self.done)
|
|
|
|
|
2015-06-24 19:44:31 +00:00
|
|
|
buttons = [
|
2015-10-08 22:10:54 +00:00
|
|
|
Color.button(done, focus_map='button focus'),
|
|
|
|
Color.button(cancel, focus_map='button focus')
|
2015-06-24 19:44:31 +00:00
|
|
|
]
|
|
|
|
return Pile(buttons)
|
|
|
|
|
|
|
|
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-06-29 20:01:16 +00:00
|
|
|
ifaces = self.model.get_interfaces()
|
2015-06-24 19:44:31 +00:00
|
|
|
|
2015-06-29 20:01:16 +00:00
|
|
|
col_1 = []
|
|
|
|
for iface in ifaces:
|
|
|
|
col_1.append(
|
2015-08-24 15:51:10 +00:00
|
|
|
Color.menu_button(
|
|
|
|
menu_btn(label=iface,
|
|
|
|
on_press=self.on_net_dev_press),
|
|
|
|
focus_map='menu_button focus'))
|
2015-09-03 18:35:07 +00:00
|
|
|
col_1.append(Text("")) # vertical holder for ipv6 status
|
|
|
|
col_1.append(Text("")) # vertical holder for ipv4 status
|
2015-06-29 20:01:16 +00:00
|
|
|
col_1 = BoxAdapter(SimpleList(col_1),
|
|
|
|
height=len(col_1))
|
|
|
|
|
|
|
|
col_2 = []
|
|
|
|
for iface in ifaces:
|
|
|
|
ifinfo, iface_vendor, iface_model = self.model.get_iface_info(
|
|
|
|
iface)
|
2015-09-03 16:48:40 +00:00
|
|
|
bonded = self.model.iface_is_bonded(iface)
|
|
|
|
bridged = self.model.iface_is_bridge_member(iface)
|
|
|
|
speed = self.model.iface_get_speed(iface)
|
|
|
|
info = {
|
|
|
|
'bonded': bonded,
|
|
|
|
'bridged': bridged,
|
|
|
|
'speed': speed,
|
|
|
|
'vendor': iface_vendor,
|
|
|
|
'model': iface_model,
|
|
|
|
}
|
2015-09-08 19:49:24 +00:00
|
|
|
log.debug('iface info:{}'.format(info))
|
2015-09-03 16:48:40 +00:00
|
|
|
template = ''
|
|
|
|
if info['bonded']:
|
|
|
|
template += '(Bonded) '
|
|
|
|
if info['speed']:
|
|
|
|
template += '{speed} '.format(**info)
|
|
|
|
if not info['vendor'].lower().startswith('unknown'):
|
|
|
|
template += '{vendor} '.format(**info)
|
|
|
|
if not info['model'].lower().startswith('unknown'):
|
|
|
|
template += '{model} '.format(**info)
|
|
|
|
col_2.append(Text(template))
|
|
|
|
|
2015-09-08 19:49:24 +00:00
|
|
|
if not ifinfo.addr.lower().startswith('unknown'):
|
|
|
|
ip = ifinfo.addr
|
|
|
|
else:
|
|
|
|
ip = 'No IPv4 connection'
|
2015-09-03 16:48:40 +00:00
|
|
|
method = self.model.iface_get_ip_method(iface)
|
|
|
|
provider = self.model.iface_get_ip_provider(iface)
|
|
|
|
ipv4_status = {
|
|
|
|
'ip': ip,
|
|
|
|
'method': method,
|
|
|
|
'provider': provider,
|
|
|
|
}
|
2015-09-08 19:49:24 +00:00
|
|
|
log.debug('ipv4_status: {}'.format(ipv4_status))
|
2015-09-03 16:48:40 +00:00
|
|
|
ipv4_template = ''
|
|
|
|
if ipv4_status['ip']:
|
|
|
|
ipv4_template += '{ip} '.format(**ipv4_status)
|
|
|
|
if ipv4_status['method']:
|
|
|
|
ipv4_template += 'provided by {method} '.format(**ipv4_status)
|
2015-09-03 17:49:21 +00:00
|
|
|
if ipv4_status['provider']:
|
2015-09-03 16:48:40 +00:00
|
|
|
ipv4_template += 'from {provider} '.format(**ipv4_status)
|
|
|
|
col_2.append(Text(ipv4_template))
|
2015-09-10 18:25:54 +00:00
|
|
|
col_2.append(Text("No IPv6 connection")) # vert. holder for ipv6
|
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))
|
|
|
|
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
|
|
|
ifname_width = len(max(ifaces, key=len)) + 4
|
|
|
|
if ifname_width > 14:
|
|
|
|
ifname_width = 14
|
|
|
|
|
|
|
|
return Columns([(ifname_width, col_1), col_2], 2)
|
2015-06-24 19:44:31 +00:00
|
|
|
|
|
|
|
def _build_additional_options(self):
|
|
|
|
opts = []
|
2015-07-22 01:34:46 +00:00
|
|
|
for opt, sig, _ in self.model.get_menu():
|
2015-06-24 19:44:31 +00:00
|
|
|
opts.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),
|
2015-08-24 15:51:10 +00:00
|
|
|
focus_map='menu_button focus'))
|
2015-06-24 19:44:31 +00:00
|
|
|
return Pile(opts)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def done(self, result):
|
2015-09-10 18:25:54 +00:00
|
|
|
actions = [action.get() for _, action in
|
|
|
|
self.model.configured_interfaces.items()]
|
2015-10-08 21:06:25 +00:00
|
|
|
actions += self.model.get_default_route()
|
2015-09-10 18:25:54 +00:00
|
|
|
log.debug('Configured Network Actions:\n{}'.format(
|
|
|
|
yaml.dump(actions, default_flow_style=False)))
|
|
|
|
self.signal.emit_signal('network:finish', actions)
|
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()
|