def getDNS(self, resolver="/etc/resolv.conf"): nameservers = [] domain = None searches = None try: with open(resolver, 'r') as f: for line in f: line = line.strip() if line.startswith("search "): searches = ' '.join(line.split(' ')[1:]) if line.startswith("domain "): domain = line.split(' ')[1] if line.startswith("nameserver "): nameservers.append(line.split(' ')[1]) except EnvironmentError: log.warning("Unable to open /etc/resolv.conf") # Always remove local IPs from nameserver list host_ips = network.list_host_ip_addresses() for nameserver in nameservers: if nameserver in host_ips: nameservers.remove(nameserver) return searches, domain, ",".join(nameservers)
def test_list_host_ip_addresses_ignore_no_ip(self, netifaces_mock): all_ifaces = ['eth0'] netifaces_mock.AF_INET = netifaces.AF_INET netifaces_mock.interfaces.return_value = all_ifaces netifaces_mock.ifaddresses.return_value = [] data = network.list_host_ip_addresses() netifaces_mock.interfaces.assert_called_once_with() self.assertEqual([], data)
def test_list_host_ip_addresses(self, netifaces_mock): all_ifaces = ['eth0', 'lo', 'veth0'] netifaces_mock.AF_INET = netifaces.AF_INET netifaces_mock.interfaces.return_value = all_ifaces netifaces_mock.ifaddresses.side_effect = [ {netifaces.AF_INET: [{'addr': '10.20.0.2'}]}, {netifaces.AF_INET: [{'addr': '127.0.0.1'}]}, {netifaces.AF_INET: [{'addr': '192.168.122.1'}]}, ] data = network.list_host_ip_addresses() netifaces_mock.interfaces.assert_called_once_with() self.assertEqual(['10.20.0.2', '127.0.0.1', '192.168.122.1'], data)
def check(self, args): """Validate that all fields have valid values through sanity checks.""" self.parent.footer.set_text("Checking data...") self.parent.refreshScreen() # Get field information responses = dict() for index, fieldname in enumerate(self.fields): if fieldname == "blank": pass else: responses[fieldname] = self.edits[index].get_edit_text() if self.parent.save_only: return responses # Validate each field errors = [] # hostname must be under 60 chars if len(responses["HOSTNAME"]) >= 60: errors.append("Hostname must be under 60 chars.") # hostname must not be empty if len(responses["HOSTNAME"]) == 0: errors.append("Hostname must not be empty.") # hostname needs to have valid chars if re.search('[^a-z0-9-]', responses["HOSTNAME"]): errors.append( "Hostname must contain only alphanumeric and hyphen.") # domain must be under 180 chars if len(responses["DNS_DOMAIN"]) >= 180: errors.append("Domain must be under 180 chars.") # domain must not be empty if len(responses["DNS_DOMAIN"]) == 0: errors.append("Domain must not be empty.") # domain needs to have valid chars if re.match('[^a-z0-9-.]', responses["DNS_DOMAIN"]): errors.append( "Domain must contain only alphanumeric, period and hyphen.") # ensure external DNS is valid if len(responses["DNS_UPSTREAM"]) == 0: # We will allow empty if user doesn't need external networking # and present a strongly worded warning msg = "If you continue without DNS, you may not be able to access"\ + " external data necessary for installation needed for " \ + "some OpenStack Releases." dialog.display_dialog( self, widget.TextLabel(msg), "Empty DNS Warning") else: upstream_nameservers = responses["DNS_UPSTREAM"].split(',') # external DNS must contain only numbers, periods, and commas # Needs more serious ip address checking if re.match('[^0-9.,]', responses["DNS_UPSTREAM"]): errors.append( "External DNS must contain only IP addresses and commas.") # Ensure local IPs are not in upstream list host_ips = network.list_host_ip_addresses() for nameserver in upstream_nameservers: if nameserver in host_ips: errors.append("Host IPs cannot be in upstream DNS.") break if len(upstream_nameservers) > 3: errors.append( "Unable to specify more than 3 External DNS addresses.") # ensure test DNS name isn't empty if len(responses["TEST_DNS"]) == 0: errors.append("Test DNS must not be empty.") # Validate first IP address for nameserver in upstream_nameservers: if not netaddr.valid_ipv4(nameserver): errors.append("Not a valid IP address for DNS server:" " {0}".format(nameserver)) # Try to resolve with first address if not self.checkDNS(upstream_nameservers[0]): # Warn user that DNS resolution failed, but continue msg = "Unable to resolve %s.\n\n" % responses['TEST_DNS']\ + "Possible causes for DNS failure include:\n"\ + "* Invalid DNS server\n"\ + "* Invalid gateway\n"\ + "* Other networking issue\n\n"\ + "Fuel Setup can save this configuration, but "\ + "you may want to correct your settings." dialog.display_dialog(self, widget.TextLabel(msg), "DNS Failure Warning") self.parent.refreshScreen() if len(errors) > 0: log.error("Errors: %s %s" % (len(errors), errors)) modulehelper.ModuleHelper.display_failed_check_dialog(self, errors) return False else: self.parent.footer.set_text("No errors found.") return responses
def check(self, args): """Validate that all fields have valid values through sanity checks.""" self.parent.footer.set_text("Checking data...") self.parent.refreshScreen() # Get field information responses = dict() for index, fieldname in enumerate(self.fields): if fieldname == "blank": pass else: responses[fieldname] = self.edits[index].get_edit_text() if self.parent.save_only: return responses # Validate each field errors = [] # hostname must be under 60 chars if len(responses["HOSTNAME"]) >= 60: errors.append("Hostname must be under 60 chars.") # hostname must not be empty if len(responses["HOSTNAME"]) == 0: errors.append("Hostname must not be empty.") # hostname needs to have valid chars if re.search('[^a-z0-9-]', responses["HOSTNAME"]): errors.append( "Hostname must contain only alphanumeric and hyphen.") # domain must be under 180 chars if len(responses["DNS_DOMAIN"]) >= 180: errors.append("Domain must be under 180 chars.") # domain must not be empty if len(responses["DNS_DOMAIN"]) == 0: errors.append("Domain must not be empty.") # domain needs to have valid chars if re.match('[^a-z0-9-.]', responses["DNS_DOMAIN"]): errors.append( "Domain must contain only alphanumeric, period and hyphen.") # ensure external DNS is valid if len(responses["DNS_UPSTREAM"]) == 0: # We will allow empty if user doesn't need external networking # and present a strongly worded warning msg = "If you continue without DNS, you may not be able to access"\ + " external data necessary for installation needed for " \ + "some OpenStack Releases." dialog.display_dialog(self, widget.TextLabel(msg), "Empty DNS Warning") else: upstream_nameservers = responses["DNS_UPSTREAM"].split(',') # external DNS must contain only numbers, periods, and commas # Needs more serious ip address checking if re.match('[^0-9.,]', responses["DNS_UPSTREAM"]): errors.append( "External DNS must contain only IP addresses and commas.") # Ensure local IPs are not in upstream list host_ips = network.list_host_ip_addresses() for nameserver in upstream_nameservers: if nameserver in host_ips: errors.append("Host IPs cannot be in upstream DNS.") break if len(upstream_nameservers) > 3: errors.append( "Unable to specify more than 3 External DNS addresses.") # ensure test DNS name isn't empty if len(responses["TEST_DNS"]) == 0: errors.append("Test DNS must not be empty.") # Validate first IP address for nameserver in upstream_nameservers: if not netaddr.valid_ipv4(nameserver): errors.append("Not a valid IP address for DNS server:" " {0}".format(nameserver)) # Try to resolve with first address if not self.checkDNS(upstream_nameservers[0]): # Warn user that DNS resolution failed, but continue msg = "Unable to resolve %s.\n\n" % responses['TEST_DNS']\ + "Possible causes for DNS failure include:\n"\ + "* Invalid DNS server\n"\ + "* Invalid gateway\n"\ + "* Other networking issue\n\n"\ + "Fuel Setup can save this configuration, but "\ + "you may want to correct your settings." dialog.display_dialog(self, widget.TextLabel(msg), "DNS Failure Warning") self.parent.refreshScreen() if len(errors) > 0: log.error("Errors: %s %s" % (len(errors), errors)) modulehelper.ModuleHelper.display_failed_check_dialog(self, errors) return False else: self.parent.footer.set_text("No errors found.") return responses