2013-05-22 16:59:27 +04:00
# Copyright (C) 2006-2007, 2013 Red Hat, Inc.
2007-03-30 17:03:32 +04:00
# Copyright (C) 2006 Hugh O. Brock <hbrock@redhat.com>
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2007-03-30 17:03:32 +04:00
2017-10-11 14:35:48 +03:00
import ipaddress
2007-03-30 17:03:32 +04:00
2012-05-14 17:24:56 +04:00
from gi . repository import Gtk
from gi . repository import Gdk
2017-04-04 02:46:16 +03:00
from gi . repository import Pango
2011-04-18 20:39:53 +04:00
2019-06-09 22:29:44 +03:00
import libvirt
from virtinst import generatename
2019-06-17 04:12:39 +03:00
from virtinst import log
2013-09-23 21:21:35 +04:00
from virtinst import Network
2019-06-17 05:19:17 +03:00
from . lib import uiutil
2014-09-13 00:10:45 +04:00
from . asyncjob import vmmAsyncJob
from . baseclass import vmmGObjectUI
2019-05-05 23:12:54 +03:00
from . xmleditor import vmmXMLEditor
2007-03-30 17:03:32 +04:00
2013-09-23 17:41:01 +04:00
_green = Gdk . Color . parse ( " #c0ffc0 " ) [ 1 ]
_red = Gdk . Color . parse ( " #ffc0c0 " ) [ 1 ]
_black = Gdk . Color . parse ( " #000000 " ) [ 1 ]
_white = Gdk . Color . parse ( " #f0f0f0 " ) [ 1 ]
def _make_ipaddr ( addrstr ) :
if addrstr is None :
return None
try :
2018-01-27 22:19:12 +03:00
return ipaddress . ip_network ( str ( addrstr ) , strict = False )
2017-07-24 11:26:48 +03:00
except Exception :
2013-09-23 17:41:01 +04:00
return None
2007-03-30 17:03:32 +04:00
2010-12-09 01:26:19 +03:00
class vmmCreateNetwork ( vmmGObjectUI ) :
def __init__ ( self , conn ) :
2013-09-23 00:10:16 +04:00
vmmGObjectUI . __init__ ( self , " createnet.ui " , " vmm-create-net " )
2007-03-30 17:03:32 +04:00
self . conn = conn
2010-11-30 22:33:21 +03:00
2019-05-05 23:12:54 +03:00
self . _xmleditor = vmmXMLEditor ( self . builder , self . topwin ,
self . widget ( " net-details-align " ) ,
self . widget ( " net-details " ) )
self . _xmleditor . connect ( " xml-requested " ,
self . _xmleditor_xml_requested_cb )
2013-02-16 22:31:46 +04:00
self . builder . connect_signals ( {
2017-08-05 09:39:32 +03:00
" on_create_cancel_clicked " : self . close ,
" on_vmm_create_delete_event " : self . close ,
" on_create_finish_clicked " : self . finish ,
2009-12-15 00:26:46 +03:00
2019-05-05 19:54:52 +03:00
" on_net_forward_mode_changed " : self . _net_forward_mode_changed_cb ,
2019-05-05 20:58:20 +03:00
" on_net_dns_use_toggled " : self . _net_dns_use_toggled_cb ,
2013-05-14 20:15:02 +04:00
2017-08-05 09:39:32 +03:00
" on_net-ipv4-enable_toggled " : self . change_ipv4_enable ,
2013-05-14 20:15:02 +04:00
" on_net-ipv4-network_changed " : self . change_ipv4_network ,
2013-03-29 22:38:29 +04:00
" on_net-dhcpv4-enable_toggled " : self . change_dhcpv4_enable ,
2013-05-14 20:15:02 +04:00
" on_net-dhcpv4-start_changed " : self . change_dhcpv4_start ,
" on_net-dhcpv4-end_changed " : self . change_dhcpv4_end ,
2017-08-05 09:39:32 +03:00
" on_net-ipv6-enable_toggled " : self . change_ipv6_enable ,
2013-05-14 20:15:02 +04:00
" on_net-ipv6-network_changed " : self . change_ipv6_network ,
2013-03-29 22:38:29 +04:00
" on_net-dhcpv6-enable_toggled " : self . change_dhcpv6_enable ,
2013-05-14 20:15:02 +04:00
" on_net-dhcpv6-start_changed " : self . change_dhcpv6_start ,
" on_net-dhcpv6-end_changed " : self . change_dhcpv6_end ,
2011-07-13 04:31:46 +04:00
} )
2011-04-18 19:25:28 +04:00
self . bind_escape_key_close ( )
2007-03-30 17:03:32 +04:00
self . set_initial_state ( )
2013-09-23 17:41:01 +04:00
####################
# Standard methods #
####################
2011-04-14 16:47:42 +04:00
def show ( self , parent ) :
2019-06-17 04:12:39 +03:00
log . debug ( " Showing new network wizard " )
2007-03-30 17:03:32 +04:00
self . reset_state ( )
2011-04-14 16:47:42 +04:00
self . topwin . set_transient_for ( parent )
2007-03-30 17:03:32 +04:00
self . topwin . present ( )
2011-04-13 17:27:02 +04:00
def close ( self , ignore1 = None , ignore2 = None ) :
2019-06-17 04:12:39 +03:00
log . debug ( " Closing new network wizard " )
2011-04-13 17:27:02 +04:00
self . topwin . hide ( )
return 1
2011-07-24 05:16:54 +04:00
def _cleanup ( self ) :
2011-04-13 17:27:02 +04:00
self . conn = None
2019-05-05 23:12:54 +03:00
self . _xmleditor . cleanup ( )
self . _xmleditor = None
###########
# UI init #
###########
2011-04-13 17:27:02 +04:00
2007-03-30 17:03:32 +04:00
def set_initial_state ( self ) :
2013-09-27 21:36:22 +04:00
blue = Gdk . Color . parse ( " #0072A8 " ) [ 1 ]
self . widget ( " header " ) . modify_bg ( Gtk . StateType . NORMAL , blue )
2007-03-30 17:03:32 +04:00
2017-03-31 15:12:01 +03:00
# [ label, dev name ]
2019-05-05 19:54:52 +03:00
pf_list = self . widget ( " net-hostdevs " )
2017-03-31 15:12:01 +03:00
pf_model = Gtk . ListStore ( str , str )
pf_list . set_model ( pf_model )
2019-05-05 19:54:52 +03:00
text = uiutil . init_combo_text_column ( pf_list , 1 )
2017-04-04 02:46:16 +03:00
text . set_property ( " ellipsize " , Pango . EllipsizeMode . MIDDLE )
2017-03-31 15:12:01 +03:00
2009-12-01 19:49:08 +03:00
# [ label, dev name ]
2019-05-05 19:54:52 +03:00
fw_list = self . widget ( " net-forward-device " )
2012-05-14 17:24:56 +04:00
fw_model = Gtk . ListStore ( str , str )
2007-03-30 17:03:32 +04:00
fw_list . set_model ( fw_model )
2019-05-05 19:54:52 +03:00
uiutil . init_combo_text_column ( fw_list , 1 )
2009-12-01 19:49:08 +03:00
# [ label, mode ]
2013-09-23 17:41:01 +04:00
mode_list = self . widget ( " net-forward-mode " )
2012-05-14 17:24:56 +04:00
mode_model = Gtk . ListStore ( str , str )
2009-12-01 19:49:08 +03:00
mode_list . set_model ( mode_model )
2019-05-05 19:54:52 +03:00
uiutil . init_combo_text_column ( mode_list , 1 )
2009-12-01 19:49:08 +03:00
2019-05-05 19:54:52 +03:00
mode_model . append ( [ " nat " , _ ( " NAT " ) ] )
mode_model . append ( [ " route " , _ ( " Routed " ) ] )
mode_model . append ( [ " open " , _ ( " Open " ) ] )
mode_model . append ( [ " isolated " , _ ( " Isolated " ) ] )
mode_model . append ( [ " hostdev " , _ ( " SR-IOV pool " ) ] )
2007-03-30 17:03:32 +04:00
def reset_state ( self ) :
2019-05-05 23:12:54 +03:00
self . _xmleditor . reset_state ( )
2019-06-09 22:29:44 +03:00
basename = " network "
2019-06-11 17:05:15 +03:00
def cb ( n ) :
return generatename . check_libvirt_collision (
self . conn . get_backend ( ) . networkLookupByName , n )
default_name = generatename . generate_name ( basename , cb )
2019-05-05 20:58:20 +03:00
self . widget ( " net-name " ) . set_text ( default_name )
2011-07-13 04:31:46 +04:00
2019-05-05 20:58:20 +03:00
self . widget ( " net-dns-use-netname " ) . set_active ( True )
2007-03-30 17:03:32 +04:00
2019-05-05 20:58:20 +03:00
self . widget ( " net-ipv4-expander " ) . set_visible ( True )
self . widget ( " net-ipv4-expander " ) . set_expanded ( False )
self . widget ( " net-ipv6-expander " ) . set_visible ( True )
self . widget ( " net-ipv6-expander " ) . set_expanded ( False )
self . widget ( " net-dns-expander " ) . set_visible ( True )
self . widget ( " net-dns-expander " ) . set_expanded ( False )
2013-09-23 17:41:01 +04:00
2013-03-29 22:38:29 +04:00
self . widget ( " net-ipv4-enable " ) . set_active ( True )
self . widget ( " net-ipv4-network " ) . set_text ( " 192.168.100.0/24 " )
self . widget ( " net-dhcpv4-enable " ) . set_active ( True )
self . widget ( " net-dhcpv4-start " ) . set_text ( " 192.168.100.128 " )
self . widget ( " net-dhcpv4-end " ) . set_text ( " 192.168.100.254 " )
2013-09-23 17:41:01 +04:00
2013-03-29 22:38:29 +04:00
self . widget ( " net-ipv6-enable " ) . set_active ( False )
self . widget ( " net-ipv6-network " ) . set_text ( " " )
self . widget ( " net-dhcpv6-enable " ) . set_active ( False )
self . widget ( " net-dhcpv6-start " ) . set_text ( " " )
self . widget ( " net-dhcpv6-end " ) . set_text ( " " )
2013-09-23 17:41:01 +04:00
2017-03-31 15:12:01 +03:00
2017-04-04 02:46:16 +03:00
# Populate physical forward devices
2014-06-18 01:04:49 +04:00
devnames = [ ]
2015-04-10 16:37:03 +03:00
for nodedev in self . conn . filter_nodedevs ( " net " ) :
2015-04-07 21:12:00 +03:00
devnames . append ( nodedev . xmlobj . interface )
2014-06-18 01:04:49 +04:00
for iface in self . conn . list_interfaces ( ) :
2014-06-24 11:44:04 +04:00
if iface . get_name ( ) not in devnames :
devnames . append ( iface . get_name ( ) )
2014-06-18 01:04:49 +04:00
2019-05-05 19:54:52 +03:00
fw_model = self . widget ( " net-forward-device " ) . get_model ( )
fw_model . clear ( )
fw_model . append ( [ None , _ ( " Any physical device " ) ] )
2014-06-18 01:04:49 +04:00
for name in devnames :
2019-05-05 19:54:52 +03:00
fw_model . append ( [ name , _ ( " Physical device %s " ) % name ] )
self . widget ( " net-forward-device " ) . set_active ( 0 )
2017-04-04 02:46:16 +03:00
self . widget ( " net-forward-mode " ) . set_active ( 0 )
# Populate hostdev forward devices
2017-03-31 15:12:01 +03:00
devprettynames = [ ]
ifnames = [ ]
for pcidev in self . conn . filter_nodedevs ( " pci " ) :
2019-06-05 22:29:10 +03:00
if not pcidev . xmlobj . is_pci_sriov ( ) :
2017-03-31 15:12:01 +03:00
continue
2019-06-09 22:29:44 +03:00
devdesc = pcidev . pretty_name ( )
2017-03-31 15:12:01 +03:00
for netdev in self . conn . filter_nodedevs ( " net " ) :
if pcidev . xmlobj . name != netdev . xmlobj . parent :
continue
ifname = netdev . xmlobj . interface
devprettyname = " %s ( %s ) " % ( ifname , devdesc )
devprettynames . append ( devprettyname )
ifnames . append ( ifname )
break
2019-05-05 19:54:52 +03:00
pf_model = self . widget ( " net-hostdevs " ) . get_model ( )
pf_model . clear ( )
2017-04-04 02:46:16 +03:00
for devprettyname , ifname in zip ( devprettynames , ifnames ) :
2019-05-05 19:54:52 +03:00
pf_model . append ( [ ifname , devprettyname ] )
2019-04-01 23:18:00 +03:00
if len ( pf_model ) == 0 :
2019-05-05 19:54:52 +03:00
pf_model . append ( [ None , _ ( " No available device " ) ] )
self . widget ( " net-hostdevs " ) . set_active ( 0 )
2007-03-30 17:03:32 +04:00
2009-12-01 19:49:08 +03:00
2017-04-04 02:46:16 +03:00
2013-09-23 17:41:01 +04:00
##################
# UI get helpers #
##################
2007-03-30 17:03:32 +04:00
2013-03-29 22:38:29 +04:00
def get_config_ipv4_enable ( self ) :
2019-05-05 20:58:20 +03:00
return ( self . widget ( " net-ipv4-expander " ) . is_visible ( ) and
self . widget ( " net-ipv4-enable " ) . get_active ( ) )
2013-03-29 22:38:29 +04:00
def get_config_ipv6_enable ( self ) :
2019-05-05 20:58:20 +03:00
return ( self . widget ( " net-ipv6-expander " ) . is_visible ( ) and
self . widget ( " net-ipv6-enable " ) . get_active ( ) )
2013-03-29 22:38:29 +04:00
def get_config_dhcpv4_enable ( self ) :
return self . widget ( " net-dhcpv4-enable " ) . get_active ( )
def get_config_dhcpv6_enable ( self ) :
return self . widget ( " net-dhcpv6-enable " ) . get_active ( )
2013-05-14 20:15:02 +04:00
2019-05-05 20:58:20 +03:00
def get_config_domain_name ( self ) :
widget = self . widget ( " net-domain-name " )
if not widget . is_visible ( ) :
return None
if self . widget ( " net-dns-use-netname " ) . get_active ( ) :
return self . widget ( " net-name " ) . get_text ( )
return widget . get_text ( )
2013-09-23 17:41:01 +04:00
def _get_network_helper ( self , widgetname ) :
widget = self . widget ( widgetname )
if not widget . is_visible ( ) or not widget . is_sensitive ( ) :
return None
return _make_ipaddr ( widget . get_text ( ) )
2009-09-23 18:07:22 +04:00
2013-09-23 17:41:01 +04:00
def get_config_ip4 ( self ) :
return self . _get_network_helper ( " net-ipv4-network " )
def get_config_dhcpv4_start ( self ) :
return self . _get_network_helper ( " net-dhcpv4-start " )
def get_config_dhcpv4_end ( self ) :
return self . _get_network_helper ( " net-dhcpv4-end " )
def get_config_ip6 ( self ) :
return self . _get_network_helper ( " net-ipv6-network " )
def get_config_dhcpv6_start ( self ) :
return self . _get_network_helper ( " net-dhcpv6-start " )
def get_config_dhcpv6_end ( self ) :
return self . _get_network_helper ( " net-dhcpv6-end " )
2013-05-14 20:15:02 +04:00
2013-09-23 17:41:01 +04:00
def get_config_forwarding ( self ) :
2019-05-05 19:54:52 +03:00
mode = uiutil . get_list_selection ( self . widget ( " net-forward-mode " ) )
if mode == " isolated " :
2013-09-23 17:41:01 +04:00
return [ None , None ]
2013-05-14 20:15:02 +04:00
2019-05-05 19:54:52 +03:00
if mode == " hostdev " :
dev = uiutil . get_list_selection ( self . widget ( " net-hostdevs " ) )
else :
dev = uiutil . get_list_selection ( self . widget ( " net-forward-device " ) )
return [ dev , mode ]
2007-03-30 17:03:32 +04:00
2009-12-11 21:45:53 +03:00
2013-09-23 17:41:01 +04:00
#############
# Listeners #
#############
2019-05-05 19:54:52 +03:00
def _net_forward_mode_changed_cb ( self , src ) :
mode = uiutil . get_list_selection ( self . widget ( " net-forward-mode " ) )
fw_visible = mode not in [ " open " , " isolated " , " hostdev " ]
2019-05-05 20:58:20 +03:00
is_hostdev = mode in [ " hostdev " ]
2019-05-05 19:54:52 +03:00
uiutil . set_grid_row_visible (
self . widget ( " net-forward-device " ) , fw_visible )
2019-05-05 20:58:20 +03:00
uiutil . set_grid_row_visible ( self . widget ( " net-hostdevs " ) , is_hostdev )
self . widget ( " net-ipv4-expander " ) . set_visible ( not is_hostdev )
self . widget ( " net-ipv6-expander " ) . set_visible ( not is_hostdev )
self . widget ( " net-dns-expander " ) . set_visible ( not is_hostdev )
def _net_dns_use_toggled_cb ( self , src ) :
custom = self . widget ( " net-dns-use-custom " ) . get_active ( )
self . widget ( " net-domain-name " ) . set_sensitive ( custom )
2017-05-03 18:56:32 +03:00
2013-09-23 17:41:01 +04:00
def change_ipv4_enable ( self , ignore ) :
enabled = self . get_config_ipv4_enable ( )
self . widget ( " net-ipv4-box " ) . set_visible ( enabled )
def change_ipv6_enable ( self , ignore ) :
enabled = self . get_config_ipv6_enable ( )
self . widget ( " net-ipv6-box " ) . set_visible ( enabled )
def change_dhcpv4_enable ( self , ignore ) :
enabled = self . get_config_dhcpv4_enable ( )
start = self . widget ( " net-dhcpv4-start " )
end = self . widget ( " net-dhcpv4-end " )
2014-01-27 03:15:50 +04:00
uiutil . set_grid_row_visible ( start , enabled )
uiutil . set_grid_row_visible ( end , enabled )
2013-09-23 17:41:01 +04:00
def change_dhcpv6_enable ( self , ignore ) :
2013-09-23 21:21:35 +04:00
enabled = self . get_config_dhcpv6_enable ( )
2013-09-23 17:41:01 +04:00
start = self . widget ( " net-dhcpv6-start " )
end = self . widget ( " net-dhcpv6-end " )
2014-01-27 03:15:50 +04:00
uiutil . set_grid_row_visible ( start , enabled )
uiutil . set_grid_row_visible ( end , enabled )
2013-09-23 17:41:01 +04:00
def change_dhcpv4_start ( self , src ) :
start = self . get_config_dhcpv4_start ( )
self . change_dhcpv4 ( src , start )
def change_dhcpv4_end ( self , src ) :
end = self . get_config_dhcpv4_end ( )
self . change_dhcpv4 ( src , end )
def change_dhcpv4 ( self , src , addr ) :
ip = self . get_config_ip4 ( )
if ip is None or addr is None :
src . modify_bg ( Gtk . StateType . NORMAL , _white )
return
if addr . version != 4 or not ip . overlaps ( addr ) :
src . modify_bg ( Gtk . StateType . NORMAL , _red )
else :
src . modify_bg ( Gtk . StateType . NORMAL , _green )
def change_dhcpv6_start ( self , src ) :
start = self . get_config_dhcpv6_start ( )
self . change_dhcpv6 ( src , start )
def change_dhcpv6_end ( self , src ) :
end = self . get_config_dhcpv6_end ( )
self . change_dhcpv6 ( src , end )
def change_dhcpv6 ( self , src , addr ) :
ip = self . get_config_ip6 ( )
if ip is None or addr is None :
src . modify_bg ( Gtk . StateType . NORMAL , _white )
return
if addr . version != 6 or not ip . overlaps ( addr ) :
src . modify_bg ( Gtk . StateType . NORMAL , _red )
else :
src . modify_bg ( Gtk . StateType . NORMAL , _green )
def change_ipv4_network ( self , src ) :
ip = self . get_config_ip4 ( )
# No IP specified or invalid IP
if ip is None or ip . version != 4 :
src . modify_bg ( Gtk . StateType . NORMAL , _red )
return
2017-10-11 14:35:48 +03:00
valid_ip = ( ip . num_addresses > = 8 and ip . is_private )
start = int ( ip . num_addresses / / 2 )
end = int ( ip . num_addresses - 2 )
2013-09-23 17:41:01 +04:00
src . modify_bg ( Gtk . StateType . NORMAL , valid_ip and _green or _red )
2017-10-11 14:35:48 +03:00
self . widget ( " net-dhcpv4-start " ) . set_text (
str ( ip . network_address + start )
)
self . widget ( " net-dhcpv4-end " ) . set_text ( str ( ip . network_address + end ) )
2013-09-23 17:41:01 +04:00
def change_ipv6_network ( self , src ) :
ip = self . get_config_ip6 ( )
if ip is None or ip . version != 6 :
src . modify_bg ( Gtk . StateType . NORMAL , _red )
return
2017-10-11 14:35:48 +03:00
valid_ip = ( ip . num_addresses == 64 and ip . is_private )
2013-09-23 17:41:01 +04:00
start = 256
end = 512 - 1
src . modify_bg ( Gtk . StateType . NORMAL , valid_ip and _green or _red )
2017-10-11 14:35:48 +03:00
self . widget ( " net-dhcpv6-start " ) . set_text (
str ( ip . network_address + start )
)
self . widget ( " net-dhcpv6-end " ) . set_text ( str ( ip . network_address + end ) )
2013-09-23 17:41:01 +04:00
#########################
# XML build and install #
#########################
2019-06-09 22:29:44 +03:00
def _validate ( self , net ) :
net . validate_generic_name ( _ ( " Network " ) , net . name )
try :
net . conn . networkLookupByName ( net . name )
except libvirt . libvirtError :
return
raise ValueError ( _ ( " Name ' %s ' already in use by another network. " %
net . name ) )
2014-01-18 03:40:30 +04:00
2019-05-05 23:12:54 +03:00
def _build_xmlobj_from_xmleditor ( self ) :
xml = self . _xmleditor . get_xml ( )
2019-06-17 04:12:39 +03:00
log . debug ( " Using XML from xmleditor: \n %s " , xml )
2019-05-05 23:12:54 +03:00
return Network ( self . conn . get_backend ( ) , parsexml = xml )
def _build_xmlobj_from_ui ( self ) :
2018-09-03 22:45:26 +03:00
net = Network ( self . conn . get_backend ( ) )
2013-09-23 17:41:01 +04:00
2013-09-23 21:21:35 +04:00
net . name = self . widget ( " net-name " ) . get_text ( )
2019-05-05 20:58:20 +03:00
net . domain_name = self . get_config_domain_name ( )
2013-09-23 17:41:01 +04:00
2013-09-23 21:21:35 +04:00
dev , mode = self . get_config_forwarding ( )
2013-09-23 17:41:01 +04:00
if mode :
2013-09-23 21:21:35 +04:00
net . forward . mode = mode
2017-05-03 18:56:32 +03:00
if mode == " open " :
net . forward . dev = None
else :
net . forward . dev = dev or None
2013-09-23 17:41:01 +04:00
2017-03-31 15:12:01 +03:00
if net . forward . mode == " hostdev " :
net . forward . managed = " yes "
2019-01-15 17:55:44 +03:00
pfobj = net . forward . pf . add_new ( )
2017-03-31 15:12:01 +03:00
pfobj . dev = net . forward . dev
net . forward . dev = None
return net
2013-09-23 17:41:01 +04:00
if self . get_config_ipv4_enable ( ) :
ip = self . get_config_ip4 ( )
2018-02-08 01:27:56 +03:00
ipobj = net . ips . add_new ( )
2017-10-11 14:35:48 +03:00
ipobj . address = str ( ip . network_address + 1 )
2013-09-23 21:21:35 +04:00
ipobj . netmask = str ( ip . netmask )
2013-09-23 17:41:01 +04:00
if self . get_config_dhcpv4_enable ( ) :
2018-02-08 01:27:56 +03:00
dhcpobj = ipobj . ranges . add_new ( )
2017-10-11 14:35:48 +03:00
dhcpobj . start = str (
self . get_config_dhcpv4_start ( ) . network_address
)
dhcpobj . end = str ( self . get_config_dhcpv4_end ( ) . network_address )
2013-09-23 17:41:01 +04:00
if self . get_config_ipv6_enable ( ) :
ip = self . get_config_ip6 ( )
2018-02-08 01:27:56 +03:00
ipobj = net . ips . add_new ( )
2013-09-23 21:21:35 +04:00
ipobj . family = " ipv6 "
2017-10-11 14:35:48 +03:00
ipobj . address = str ( ip . network_address + 1 )
2013-09-23 21:21:35 +04:00
ipobj . prefix = str ( ip . prefixlen )
2013-09-23 17:41:01 +04:00
if self . get_config_dhcpv6_enable ( ) :
2018-02-08 01:27:56 +03:00
dhcpobj = ipobj . ranges . add_new ( )
2017-10-11 14:35:48 +03:00
dhcpobj . start = str (
self . get_config_dhcpv6_start ( ) . network_address
)
dhcpobj . end = str (
self . get_config_dhcpv6_end ( ) . network_address
)
2013-09-23 17:41:01 +04:00
2013-09-23 21:21:35 +04:00
return net
2019-05-05 23:12:54 +03:00
def _build_xmlobj ( self , check_xmleditor ) :
try :
xmlobj = self . _build_xmlobj_from_ui ( )
if check_xmleditor and self . _xmleditor . is_xml_selected ( ) :
xmlobj = self . _build_xmlobj_from_xmleditor ( )
return xmlobj
except Exception as e :
self . err . show_err ( _ ( " Error building XML: %s " ) % str ( e ) )
2013-09-23 21:21:35 +04:00
def _finish_cb ( self , error , details ) :
2017-04-27 22:00:17 +03:00
self . reset_finish_cursor ( )
2013-09-23 21:21:35 +04:00
if error :
error = _ ( " Error creating virtual network: %s " ) % str ( error )
self . err . show_err ( error , details = details )
else :
self . conn . schedule_priority_tick ( pollnet = True )
self . close ( )
2013-09-23 17:41:01 +04:00
2013-09-30 23:06:52 +04:00
def _async_net_create ( self , asyncjob , net ) :
2013-09-23 21:21:35 +04:00
ignore = asyncjob
2019-06-09 22:29:44 +03:00
xml = net . get_xml ( )
2019-06-17 04:12:39 +03:00
log . debug ( " Creating virtual network ' %s ' with xml: \n %s " ,
2019-06-09 22:29:44 +03:00
net . name , xml )
netobj = self . conn . get_backend ( ) . networkDefineXML ( xml )
try :
netobj . create ( )
netobj . setAutostart ( True )
except Exception :
netobj . undefine ( )
raise
2013-09-23 17:41:01 +04:00
def finish ( self , ignore ) :
2019-05-05 23:12:54 +03:00
net = self . _build_xmlobj ( check_xmleditor = True )
if not net :
return
2013-09-23 17:41:01 +04:00
try :
2019-06-09 22:29:44 +03:00
self . _validate ( net )
2017-05-05 19:47:21 +03:00
except Exception as e :
2019-05-05 23:12:54 +03:00
return self . err . show_err (
_ ( " Error validating network: %s " ) % e )
2013-09-23 17:41:01 +04:00
2017-04-27 22:00:17 +03:00
self . set_finish_cursor ( )
2013-09-30 23:06:52 +04:00
progWin = vmmAsyncJob ( self . _async_net_create , [ net ] ,
2013-09-23 21:21:35 +04:00
self . _finish_cb , [ ] ,
_ ( " Creating virtual network... " ) ,
_ ( " Creating the virtual network may take a "
" while... " ) ,
self . topwin )
progWin . run ( )
2019-05-05 23:12:54 +03:00
################
# UI listeners #
################
def _xmleditor_xml_requested_cb ( self , src ) :
xmlobj = self . _build_xmlobj ( check_xmleditor = False )
self . _xmleditor . set_xml ( xmlobj and xmlobj . get_xml ( ) or " " )