Skip to content

Commit

Permalink
rebase me: refactor TUI Netinterface configuration
Browse files Browse the repository at this point in the history
Factorize the intrface config form in one function
called once for IPv4 and once for IPv6 if necessary

Signed-off-by: BenjiReis <[email protected]>
  • Loading branch information
benjamreis committed Mar 3, 2023
1 parent f918130 commit dd57535
Showing 1 changed file with 52 additions and 163 deletions.
215 changes: 52 additions & 163 deletions tui/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ def choose_primary_address_type(nic):
tui.screen.popWindow()
return direction, address_type

def get_ipv4_configuration(nic, txt, defaults, include_dns):
def get_ip_configuration(nic, txt, defaults, include_dns, iface_class):
def use_vlan_cb_change():
vlan_field.setFlags(FLAG_DISABLED, vlan_cb.value())

def dhcp_change():
for x in [ ip_field, gateway_field, subnet_field, dns_field ]:
x.setFlags(FLAG_DISABLED, not dhcp_rb.selected())
x.setFlags(FLAG_DISABLED, static_rb.selected())

ipv6 = iface_class == NetInterfaceV6

gf = GridFormHelp(tui.screen, 'Networking', 'ifconfig', 1, 8)
if txt is None:
Expand All @@ -70,160 +72,41 @@ def dhcp_change():
b = [("Ok", "ok"), ("Back", "back")]
buttons = ButtonBar(tui.screen, b)

#TODO? Change size for IPv6? If so which size?
ip_field = Entry(16)
subnet_field = Entry(16)
gateway_field = Entry(16)
dns_field = Entry(16)
vlan_field = Entry(16)

if defaults and defaults.mode == NetInterface.Static:
# static configuration defined previously
dhcp_rb = SingleRadioButton("Automatic configuration (DHCP)", None, 0)
dhcp_rb.setCallback(dhcp_change, ())
static_rb = SingleRadioButton("Static configuration:", dhcp_rb, 1)
static_rb.setCallback(dhcp_change, ())
if defaults.ipaddr:
ip_field.set(defaults.ipaddr)
if defaults.netmask:
subnet_field.set(defaults.netmask)
if defaults.gateway:
gateway_field.set(defaults.gateway)
if defaults.dns:
dns_field.set(defaults.dns[0])
else:
dhcp_rb = SingleRadioButton("Automatic configuration (DHCP)", None, 1)
dhcp_rb.setCallback(dhcp_change, ())
static_rb = SingleRadioButton("Static configuration:", dhcp_rb, 0)
static_rb.setCallback(dhcp_change, ())
ip_field.setFlags(FLAG_DISABLED, False)
subnet_field.setFlags(FLAG_DISABLED, False)
gateway_field.setFlags(FLAG_DISABLED, False)
dns_field.setFlags(FLAG_DISABLED, False)

vlan_cb = Checkbox("Use VLAN:", defaults.isVlan() if defaults else False)
vlan_cb.setCallback(use_vlan_cb_change, ())
if defaults and defaults.isVlan():
vlan_field.set(str(defaults.vlan))
else:
vlan_field.setFlags(FLAG_DISABLED, False)

ip_text = Textbox(15, 1, "IP Address:")
subnet_text = Textbox(15, 1, "Subnet mask:")
gateway_text = Textbox(15, 1, "Gateway:")
dns_text = Textbox(15, 1, "Nameserver:")
vlan_text = Textbox(15, 1, "VLAN (1-4094):")

entry_grid = Grid(2, include_dns and 4 or 3)
entry_grid.setField(ip_text, 0, 0)
entry_grid.setField(ip_field, 1, 0)
entry_grid.setField(subnet_text, 0, 1)
entry_grid.setField(subnet_field, 1, 1)
entry_grid.setField(gateway_text, 0, 2)
entry_grid.setField(gateway_field, 1, 2)
if include_dns:
entry_grid.setField(dns_text, 0, 3)
entry_grid.setField(dns_field, 1, 3)

vlan_grid = Grid(2, 1)
vlan_grid.setField(vlan_text, 0, 0)
vlan_grid.setField(vlan_field, 1, 0)

gf.add(text, 0, 0, padding=(0, 0, 0, 1))
gf.add(dhcp_rb, 0, 2, anchorLeft=True)
gf.add(static_rb, 0, 3, anchorLeft=True)
gf.add(entry_grid, 0, 4, padding=(0, 0, 0, 1))
gf.add(vlan_cb, 0, 5, anchorLeft=True)
gf.add(vlan_grid, 0, 6, padding=(0, 0, 0, 1))
gf.add(buttons, 0, 7, growx=1)

loop = True
while loop:
result = gf.run()

if buttons.buttonPressed(result) in ['ok', None]:
# validate input
msg = ''
if static_rb.selected():
if not netutil.valid_ipv4_addr(ip_field.value()):
msg = 'IP Address'
elif not netutil.valid_ipv4_addr(subnet_field.value()):
msg = 'Subnet mask'
elif gateway_field.value() != '' and not netutil.valid_ipv4_addr(gateway_field.value()):
msg = 'Gateway'
elif dns_field.value() != '' and not netutil.valid_ipv4_addr(dns_field.value()):
msg = 'Nameserver'
if vlan_cb.selected():
if not netutil.valid_vlan(vlan_field.value()):
msg = 'VLAN'
if msg != '':
tui.progress.OKDialog("Networking", "Invalid %s, please check the field and try again." % msg)
else:
loop = False
else:
loop = False

tui.screen.popWindow()

if buttons.buttonPressed(result) == 'back': return LEFT_BACKWARDS, None

vlan_value = int(vlan_field.value()) if vlan_cb.selected() else None
if bool(dhcp_rb.selected()):
answers = NetInterface(NetInterface.DHCP, nic.hwaddr, vlan=vlan_value)
else:
answers = NetInterface(NetInterface.Static, nic.hwaddr, ip_field.value(),
subnet_field.value(), gateway_field.value(),
dns_field.value(), vlan=vlan_value)
return RIGHT_FORWARDS, answers

def get_ipv6_configuration(nic, txt, defaults, include_dns):
def dhcp_change():
for x in [ ip_field, gateway_field, subnet_field, dns_field ]:
x.setFlags(FLAG_DISABLED, static_rb.selected())

def use_vlan_cb_change():
vlan_field.setFlags(FLAG_DISABLED, vlan_cb.value())

gf = GridFormHelp(tui.screen, 'Networking', 'ifconfigv6', 1, 10)
if txt is None:
txt = "Configuration for %s (%s)" % (nic.name, nic.hwaddr)
text = TextboxReflowed(45, txt)
b = [("Ok", "ok"), ("Back", "back")]
buttons = ButtonBar(tui.screen, b)

ip_field = Entry(25)
subnet_field = Entry(25)
gateway_field = Entry(25)
dns_field = Entry(25)
vlan_field = Entry(25)

if defaults and defaults.modev6 == NetInterface.Static:
# static configuration defined previously
dhcp_rb = SingleRadioButton("Automatic configuration (DHCP)", None, 0)
dhcp_rb.setCallback(dhcp_change, ())
autoconf_rb = SingleRadioButton("Automatic configuration (Autoconf)", dhcp_rb, 0)
static = defaults and (defaults.modev6 if ipv6 else defaults.mode) == NetInterface.Static
dhcp_rb = SingleRadioButton("Automatic configuration (DHCP)", None, not static)
dhcp_rb.setCallback(dhcp_change, ())
static_rb = SingleRadioButton("Static configuration:", dhcp_rb, static)
static_rb.setCallback(dhcp_change, ())
if ipv6:
autoconf_rb = SingleRadioButton("Automatic configuration (Autoconf)", autoconf_rb, 0)
autoconf_rb.setCallback(dhcp_change, ())
static_rb = SingleRadioButton("Static configuration:", autoconf_rb, 1)
static_rb.setCallback(dhcp_change, ())
if defaults:
dhcp_change()

if defaults:
if ipv6:
if defaults.ipv6addr:
ip6addr, netmask = defaults.ipv6addr.split("/")
ip_field.set(ip6addr)
subnet_field.set(netmask)
if defaults.ipv6_gateway:
gateway_field.set(defaults.ipv6_gateway)
if defaults.dns:
dns_field.set(defaults.dns[0])
else:
dhcp_rb = SingleRadioButton("Automatic configuration (DHCP)", None, 1)
dhcp_rb.setCallback(dhcp_change, ())
autoconf_rb = SingleRadioButton("Automatic configuration (Autoconf)", dhcp_rb, 0)
autoconf_rb.setCallback(dhcp_change, ())
static_rb = SingleRadioButton("Static configuration:", autoconf_rb, 0)
static_rb.setCallback(dhcp_change, ())
ip_field.setFlags(FLAG_DISABLED, False)
subnet_field.setFlags(FLAG_DISABLED, False)
gateway_field.setFlags(FLAG_DISABLED, False)
dns_field.setFlags(FLAG_DISABLED, False)
else:
if defaults.ipaddr:
ip_field.set(defaults.ipaddr)
if defaults.netmask:
subnet_field.set(defaults.netmask)
if defaults.gateway:
gateway_field.set(defaults.gateway)

if defaults.dns:
dns_field.set(defaults.dns[0])

vlan_cb = Checkbox("Use VLAN:", defaults.isVlan() if defaults else False)
vlan_cb.setCallback(use_vlan_cb_change, ())
Expand All @@ -232,8 +115,10 @@ def use_vlan_cb_change():
else:
vlan_field.setFlags(FLAG_DISABLED, False)

ip_text = Textbox(15, 1, "IPv6 Address:")
subnet_text = Textbox(15, 1, "CIDR (4-128):")
ip_msg = "IPv6 Address" if ipv6 else "IP Address"
mask_msg = "CIDR (4-128)" if ipv6 else "Subnet mask"
ip_text = Textbox(15, 1, "%s:" % ip_msg)
subnet_text = Textbox(15, 1, "%s:" % mask_msg)
gateway_text = Textbox(15, 1, "Gateway:")
dns_text = Textbox(15, 1, "Nameserver:")
vlan_text = Textbox(15, 1, "VLAN (1-4094):")
Expand All @@ -255,14 +140,17 @@ def use_vlan_cb_change():

gf.add(text, 0, 0, padding=(0, 0, 0, 1))
gf.add(dhcp_rb, 0, 2, anchorLeft=True)
gf.add(autoconf_rb, 0, 3, anchorLeft=True)
gf.add(static_rb, 0, 4, anchorLeft=True)
gf.add(entry_grid, 0, 5, padding=(0, 0, 0, 1))
gf.add(vlan_cb, 0, 6, anchorLeft=True)
gf.add(vlan_grid, 0, 7, padding=(0, 0, 0, 1))
gf.add(buttons, 0, 8, growx=1)
gf.add(static_rb, 0, 3, anchorLeft=True)
if ipv6:
gf.add(autoconf_rb, 0, 4, anchorLeft=True)
# One more line for IPv6 autoconf
gf.add(entry_grid, 0, 4 + ipv6, padding=(0, 0, 0, 1))
gf.add(vlan_cb, 0, 5 + ipv6, anchorLeft=True)
gf.add(vlan_grid, 0, 6 + ipv6, padding=(0, 0, 0, 1))
gf.add(buttons, 0, 7 + ipv6, growx=1)

loop = True
ip_family = socket.AF_INET6 if ipv6 else socket.AF_INET
while loop:
result = gf.run()

Expand All @@ -271,13 +159,14 @@ def use_vlan_cb_change():
msg = ''
if static_rb.selected():
subnet_value = int(subnet_field.value())
if not netutil.valid_ipv6_addr(ip_field.value()):
msg = 'IPv6 Address'
elif subnet_value > 128 or subnet_value < 4:
msg = 'CIDR'
elif gateway_field.value() != '' and not netutil.valid_ipv6_addr(gateway_field.value()):
invalid_subnet = subnet_value > 128 or subnet_value < 4 if ipv6 else not netutil.valid_ipv4_addr(subnet_field.value())
if not netutil.valid_ip_address_family(ip_field.value(), ip_family):
msg = ip_msg
elif invalid_subnet:
msg = mask_msg
elif gateway_field.value() != '' and not netutil.valid_ip_address_family(gateway_field.value(), ip_family):
msg = 'Gateway'
elif dns_field.value() != '' and not netutil.valid_ipv6_addr(dns_field.value()):
elif dns_field.value() != '' and not netutil.valid_ip_address_family(dns_field.value(), ip_family):
msg = 'Nameserver'
if vlan_cb.selected():
if not netutil.valid_vlan(vlan_field.value()):
Expand All @@ -295,11 +184,11 @@ def use_vlan_cb_change():

vlan_value = int(vlan_field.value()) if vlan_cb.selected() else None
if bool(dhcp_rb.selected()):
answers = NetInterfaceV6(NetInterface.DHCP, nic.hwaddr, vlan=vlan_value)
elif bool(autoconf_rb.selected()):
answers = NetInterfaceV6(NetInterface.Autoconf, nic.hwaddr, vlan=vlan_value)
answers = iface_class(NetInterface.DHCP, nic.hwaddr, vlan=vlan_value)
elif ipv6 and bool(autoconf_rb.selected()):
answers = iface_class(NetInterface.Autoconf, nic.hwaddr, vlan=vlan_value)
else:
answers = NetInterfaceV6(NetInterface.Static, nic.hwaddr, ip_field.value(),
answers = iface_class(NetInterface.Static, nic.hwaddr, ip_field.value(),
subnet_field.value(), gateway_field.value(),
dns_field.value(), vlan=vlan_value)

Expand All @@ -311,12 +200,12 @@ def use_vlan_cb_change():

answers = None
if address_type in ["ipv4", "dual"]:
direction, answers = get_ipv4_configuration(nic, txt, defaults, include_dns)
direction, answers = get_ip_configuration(nic, txt, defaults, include_dns, NetInterface)
if direction == LEFT_BACKWARDS:
return LEFT_BACKWARDS, None

if address_type in ["ipv6", "dual"]:
direction, answers_ipv6 = get_ipv6_configuration(nic, txt, defaults, include_dns)
direction, answers_ipv6 = get_ip_configuration(nic, txt, defaults, include_dns, NetInterfaceV6)
if direction == LEFT_BACKWARDS:
return LEFT_BACKWARDS, None

Expand Down

0 comments on commit dd57535

Please sign in to comment.