def get_name_service_configuration(answers): # horrible hack - need a tuple due to bug in snack that means # we don't get an arge passed if we try to just pass False def hn_callback((enabled, )): hostname.setFlags(FLAG_DISABLED, enabled) def ns_callback((enabled, )): for entry in [ns1_entry, ns2_entry, ns3_entry]: entry.setFlags(FLAG_DISABLED, enabled) hide_rb = answers['net-admin-configuration'].isStatic() # HOSTNAME: hn_title = Textbox(len("Hostname Configuration"), 1, "Hostname Configuration") # the hostname radio group: if not answers.has_key('manual-hostname'): # no current value set - if we currently have a useful hostname, # use that, else make up a random one: current_hn = socket.gethostname() if current_hn in [None, '', '(none)', 'localhost', 'localhost.localdomain']: answers['manual-hostname'] = True, util.mkRandomHostname() else: answers['manual-hostname'] = True, current_hn use_manual_hostname, manual_hostname = answers['manual-hostname'] if manual_hostname == None: manual_hostname = "" hn_rbgroup = RadioGroup() hn_dhcp_rb = hn_rbgroup.add("Automatically set via DHCP", "hn_dhcp", not use_manual_hostname) hn_dhcp_rb.setCallback(hn_callback, data = (False,)) hn_manual_rb = hn_rbgroup.add("Manually specify:", "hn_manual", use_manual_hostname) hn_manual_rb.setCallback(hn_callback, data = (True,)) # the hostname text box: hostname = Entry(hide_rb and 30 or 42, text = manual_hostname) hostname.setFlags(FLAG_DISABLED, use_manual_hostname) hostname_grid = Grid(2, 1) if hide_rb: hostname_grid.setField(Textbox(15, 1, "Hostname:"), 0, 0) else: hostname_grid.setField(Textbox(4, 1, ""), 0, 0) # spacer hostname_grid.setField(hostname, 1, 0) # NAMESERVERS: def nsvalue(answers, id): nameservers = None if 'manual-nameservers' in answers: manual, nsl = answers['manual-nameservers'] if manual: nameservers = nsl elif 'runtime-iface-configuration' in answers: all_dhcp, netdict = answers['runtime-iface-configuration'] if not all_dhcp and isinstance(netdict, dict): nameservers = netdict.values()[0].dns if isinstance(nameservers, list) and id < len(nameservers): return nameservers[id] return "" ns_title = Textbox(len("DNS Configuration"), 1, "DNS Configuration") use_manual_dns = nsvalue(answers, 0) != "" if hide_rb: use_manual_dns = True # Name server radio group ns_rbgroup = RadioGroup() ns_dhcp_rb = ns_rbgroup.add("Automatically set via DHCP", "ns_dhcp", not use_manual_dns) ns_dhcp_rb.setCallback(ns_callback, (False,)) ns_manual_rb = ns_rbgroup.add("Manually specify:", "ns_dhcp", use_manual_dns) ns_manual_rb.setCallback(ns_callback, (True,)) # Name server text boxes ns1_text = Textbox(15, 1, "DNS Server 1:") ns1_entry = Entry(30, nsvalue(answers, 0)) ns1_grid = Grid(2, 1) ns1_grid.setField(ns1_text, 0, 0) ns1_grid.setField(ns1_entry, 1, 0) ns2_text = Textbox(15, 1, "DNS Server 2:") ns2_entry = Entry(30, nsvalue(answers, 1)) ns2_grid = Grid(2, 1) ns2_grid.setField(ns2_text, 0, 0) ns2_grid.setField(ns2_entry, 1, 0) ns3_text = Textbox(15, 1, "DNS Server 3:") ns3_entry = Entry(30, nsvalue(answers, 2)) ns3_grid = Grid(2, 1) ns3_grid.setField(ns3_text, 0, 0) ns3_grid.setField(ns3_entry, 1, 0) if nsvalue(answers, 0) == "": for entry in [ns1_entry, ns2_entry, ns3_entry]: entry.setFlags(FLAG_DISABLED, use_manual_dns) done = False while not done: buttons = ButtonBar(tui.screen, [('Ok', 'ok'), ('Back', 'back')]) # The form itself: i = 1 gf = GridFormHelp(tui.screen, 'Hostname and DNS Configuration', 'dns', 1, 11) gf.add(hn_title, 0, 0, padding = (0, 0, 0, 0)) if not hide_rb: gf.add(hn_dhcp_rb, 0, 1, anchorLeft = True) gf.add(hn_manual_rb, 0, 2, anchorLeft = True) i += 2 gf.add(hostname_grid, 0, i, padding = (0, 0, 0, 1), anchorLeft = True) gf.add(ns_title, 0, i+1, padding = (0, 0, 0, 0)) if not hide_rb: gf.add(ns_dhcp_rb, 0, 5, anchorLeft = True) gf.add(ns_manual_rb, 0, 6, anchorLeft = True) i += 2 gf.add(ns1_grid, 0, i+2) gf.add(ns2_grid, 0, i+3) gf.add(ns3_grid, 0, i+4, padding = (0, 0, 0, 1)) gf.add(buttons, 0, 10, growx = 1) button = buttons.buttonPressed(gf.runOnce()) if button == 'back': return LEFT_BACKWARDS # manual hostname? if hn_manual_rb.selected(): answers['manual-hostname'] = (True, hostname.value()) else: answers['manual-hostname'] = (False, None) # manual nameservers? if ns_manual_rb.selected(): answers['manual-nameservers'] = (True, [ns1_entry.value()]) if ns2_entry.value() != '': answers['manual-nameservers'][1].append(ns2_entry.value()) if ns3_entry.value() != '': answers['manual-nameservers'][1].append(ns3_entry.value()) if 'net-admin-configuration' in answers and answers['net-admin-configuration'].isStatic(): answers['net-admin-configuration'].dns = answers['manual-nameservers'][1] else: answers['manual-nameservers'] = (False, None) # validate before allowing the user to continue: done = True if hn_manual_rb.selected(): if not netutil.valid_hostname(hostname.value(), fqdn = True): done = False ButtonChoiceWindow(tui.screen, "Name Service Configuration", "The hostname you entered was not valid.", ["Back"]) continue if ns_manual_rb.selected(): if not netutil.valid_ip_addr(ns1_entry.value()) or \ (ns2_entry.value() != '' and not netutil.valid_ip_addr(ns2_entry.value())) or \ (ns3_entry.value() != '' and not netutil.valid_ip_addr(ns3_entry.value())): done = False ButtonChoiceWindow(tui.screen, "Name Service Configuration", "Please check that you have entered at least one nameserver, and that the nameservers you specified are valid.", ["Back"]) return RIGHT_FORWARDS
def get_iface_configuration(nic, txt=None, defaults=None, include_dns=False): 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()) gf = GridFormHelp(tui.screen, 'Networking', 'ifconfig', 1, 8) if txt == 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(16) subnet_field = Entry(16) gateway_field = Entry(16) dns_field = Entry(16) vlan_field = Entry(16) if defaults and defaults.isStatic(): # 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_ip_addr(ip_field.value()): msg = 'IP Address' elif not netutil.valid_ip_addr(subnet_field.value()): msg = 'Subnet mask' elif gateway_field.value() != '' and not netutil.valid_ip_addr( gateway_field.value()): msg = 'Gateway' elif dns_field.value() != '' and not netutil.valid_ip_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_iface_configuration(nic, txt = None, defaults = None, include_dns = False): def dhcp_change(): for x in [ ip_field, gateway_field, subnet_field, dns_field ]: x.setFlags(FLAG_DISABLED, not dhcp_rb.selected()) gf = GridFormHelp(tui.screen, 'Networking', 'ifconfig', 1, 6) if txt == 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(16) subnet_field = Entry(16) gateway_field = Entry(16) dns_field = Entry(16) if defaults and defaults.isStatic(): # 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) 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:") 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) 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(buttons, 0, 5, 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_ip_addr(ip_field.value()): msg = 'IP Address' elif not netutil.valid_ip_addr(subnet_field.value()): msg = 'Subnet mask' elif gateway_field.value() != '' and not netutil.valid_ip_addr(gateway_field.value()): msg = 'Gateway' elif dns_field.value() != '' and not netutil.valid_ip_addr(dns_field.value()): msg = 'Nameserver' 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 if bool(dhcp_rb.selected()): answers = NetInterface(NetInterface.DHCP, nic.hwaddr) else: answers = NetInterface(NetInterface.Static, nic.hwaddr, ip_field.value(), subnet_field.value(), gateway_field.value(), dns_field.value()) return RIGHT_FORWARDS, answers