def handle_sigusr1(self, signum, stack): log.info("Received signal: %s" % signum) try: savetimeout = 60 success, modulename = timeout.run_with_timeout( self.global_save, timeout=savetimeout) if success: log.info("Save successful!") else: log.error("Save failed on module %s" % modulename) except timeout.TimeoutError: log.exception("Save on signal timed out. Save not complete.") except KeyboardInterrupt: log.exception("Save was interrupted by the user.") except Exception: log.exception("Save failed for unknown reason:") self.exit_program(None)
def sigusr1_handler(self, signum, stack): log.info("Received signal: %s" % signum) try: savetimeout = 60 success, modulename = timeout.run_with_timeout( self.global_save, timeout=savetimeout) if success: log.info("Save successful!") else: log.error("Save failed on module %s" % modulename) except timeout.TimeoutError: log.exception("Save on signal timed out. Save not complete.") except KeyboardInterrupt: log.exception("Save was interrupted by the user.") except Exception: log.exception("Save failed for unknown reason:") self.exit(None)
def check(self, args): """Validates all fields have valid values and some sanity checks.""" self.parent.footer.set_text("Checking data...") self.parent.refreshScreen() #Refresh networking to make sure IP matches self.getNetwork() #Get field information responses = dict() for index, fieldname in enumerate(self.fields): if fieldname != "blank" and "label" not in fieldname: responses[fieldname] = self.edits[index].get_edit_text() ###Validate each field errors = [] #Set internal_{ipaddress,netmask,interface} responses["ADMIN_NETWORK/interface"] = self.activeiface responses["ADMIN_NETWORK/netmask"] = self.netsettings[ self.activeiface]["netmask"] responses["ADMIN_NETWORK/mac"] = self.netsettings[ self.activeiface]["mac"] responses["ADMIN_NETWORK/ipaddress"] = self.netsettings[ self.activeiface]["addr"] #ensure management interface is valid if responses["ADMIN_NETWORK/interface"] not in self.netsettings.keys(): errors.append("Management interface not valid") else: self.parent.footer.set_text("Scanning for DHCP servers. \ Please wait...") self.parent.refreshScreen() ###Start DHCP check on this interface #dhcp_server_data=[{'server_id': '192.168.200.2', 'iface': 'eth2', # 'yiaddr': '192.168.200.15', 'mac': # '52:54:00:12:35:02', 'server_ip': '192.168.200.2', # 'dport': 67, 'message': 'offer', # 'gateway': '0.0.0.0'}] try: dhcptimeout = 5 default = [] with timeout.run_with_timeout(dhcp_checker.utils.IfaceState, [self.activeiface], timeout=dhcptimeout) as iface: dhcp_server_data = timeout.run_with_timeout( dhcp_checker.api.check_dhcp_on_eth, [iface, dhcptimeout], timeout=dhcptimeout, default=default) except (KeyboardInterrupt, timeout.TimeoutError): log.debug("DHCP scan timed out") log.warning(traceback.format_exc()) dhcp_server_data = default num_dhcp = len(dhcp_server_data) if num_dhcp == 0: log.debug("No DHCP servers found") else: #Problem exists, but permit user to continue log.error("%s foreign DHCP server(s) found: %s" % (num_dhcp, dhcp_server_data)) #Build dialog elements dhcp_info = [] dhcp_info.append(urwid.Padding( urwid.Text(("header", "!!! WARNING !!!")), "center")) dhcp_info.append(widget.TextLabel("You have selected an \ interface that contains one or more DHCP servers. This will impact \ provisioning. You should disable these DHCP servers before you continue, or \ else deployment will likely fail.")) dhcp_info.append(widget.TextLabel("")) for index, dhcp_server in enumerate(dhcp_server_data): dhcp_info.append(widget.TextLabel("DHCP Server #%s:" % (index + 1))) dhcp_info.append(widget.TextLabel("IP address: %-10s" % dhcp_server['server_ip'])) dhcp_info.append(widget.TextLabel("MAC address: %-10s" % dhcp_server['mac'])) dhcp_info.append(widget.TextLabel("")) dialog.display_dialog(self, urwid.Pile(dhcp_info), "DHCP Servers Found on %s" % self.activeiface) ###Ensure pool start and end are on the same subnet as mgmt_if #Ensure mgmt_if has an IP first if len(self.netsettings[responses[ "ADMIN_NETWORK/interface"]]["addr"]) == 0: errors.append("Go to Interfaces to configure management \ interface first.") else: #Ensure ADMIN_NETWORK/interface is not running DHCP if self.netsettings[responses[ "ADMIN_NETWORK/interface"]]["bootproto"] == "dhcp": errors.append("%s is running DHCP. Change it to static " "first." % self.activeiface) #Ensure DHCP Pool Start and DHCP Pool are valid IPs try: if netaddr.valid_ipv4(responses[ "ADMIN_NETWORK/dhcp_pool_start"]): dhcp_start = netaddr.IPAddress( responses["ADMIN_NETWORK/dhcp_pool_start"]) if not dhcp_start: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except Exception: errors.append("Invalid IP address for DHCP Pool Start") try: if netaddr.valid_ipv4(responses[ "ADMIN_NETWORK/dhcp_gateway"]): dhcp_gateway = netaddr.IPAddress( responses["ADMIN_NETWORK/dhcp_gateway"]) if not dhcp_gateway: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except Exception: errors.append("Invalid IP address for DHCP Gateway") try: if netaddr.valid_ipv4(responses[ "ADMIN_NETWORK/dhcp_pool_end"]): dhcp_end = netaddr.IPAddress( responses["ADMIN_NETWORK/dhcp_pool_end"]) if not dhcp_end: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except Exception: errors.append("Invalid IP address for DHCP Pool end") #Ensure pool start and end are in the same subnet of each other netmask = self.netsettings[responses[ "ADMIN_NETWORK/interface" ]]["netmask"] if not network.inSameSubnet( responses["ADMIN_NETWORK/dhcp_pool_start"], responses["ADMIN_NETWORK/dhcp_pool_end"], netmask): errors.append("DHCP Pool start and end are not in the " "same subnet.") #Ensure pool start and end are in the right netmask mgmt_if_ipaddr = self.netsettings[responses[ "ADMIN_NETWORK/interface"]]["addr"] if network.inSameSubnet(responses[ "ADMIN_NETWORK/dhcp_pool_start"], mgmt_if_ipaddr, netmask) is False: errors.append("DHCP Pool start does not match management" " network.") if network.inSameSubnet(responses[ "ADMIN_NETWORK/dhcp_pool_end"], mgmt_if_ipaddr, netmask) is False: errors.append("DHCP Pool end does not match management " "network.") if network.inSameSubnet(responses[ "ADMIN_NETWORK/dhcp_gateway"], mgmt_if_ipaddr, netmask) is False: errors.append("DHCP Gateway does not match management " "network.") self.parent.footer.set_text("Scanning for duplicate IP address" "es. Please wait...") # Bind arping to mgmt_if_ipaddr if it assigned assigned_ips = [v.get('addr') for v in self.netsettings.itervalues()] arping_bind = mgmt_if_ipaddr in assigned_ips if network.duplicateIPExists(mgmt_if_ipaddr, self.activeiface, arping_bind): errors.append("Duplicate host found with IP {0}.".format( mgmt_if_ipaddr)) if len(errors) > 0: self.parent.footer.set_text("Error: %s" % (errors[0])) log.error("Errors: %s %s" % (len(errors), 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 and sanity checks.""" #Get field information responses = dict() self.parent.footer.set_text("Checking data...") for index, fieldname in enumerate(self.fields): if fieldname == "blank" or fieldname == "ifname": pass elif fieldname == "bootproto": rb_group = self.edits[index].rb_group if rb_group[0].state: responses["bootproto"] = "dhcp" else: responses["bootproto"] = "none" elif fieldname == "onboot": rb_group = self.edits[index].rb_group if rb_group[0].state: responses["onboot"] = "yes" else: responses["onboot"] = "no" else: responses[fieldname] = self.edits[index].get_edit_text() ###Validate each field errors = [] if responses["onboot"] == "no": numactiveifaces = 0 for iface in self.netsettings: if self.netsettings[iface]['addr'] != "": numactiveifaces += 1 if numactiveifaces < 2 and \ self.netsettings[self.activeiface]['addr'] != "": #Block user because puppet l23network fails if all intefaces #are disabled. errors.append("Cannot disable all interfaces.") elif responses["bootproto"] == "dhcp": self.parent.footer.set_text("Scanning for DHCP servers. " "Please wait...") self.parent.refreshScreen() try: dhcptimeout = 5 with timeout.run_with_timeout(dhcp_checker.utils.IfaceState, [self.activeiface], timeout=dhcptimeout) as iface: dhcp_server_data = timeout.run_with_timeout( dhcp_checker.api.check_dhcp_on_eth, [iface, dhcptimeout], timeout=dhcptimeout) except (KeyboardInterrupt, timeout.TimeoutError): self.log.debug("DHCP scan timed out") self.log.warning(traceback.format_exc()) dhcp_server_data = [] except Exception: self.log.warning("dhcp_checker failed to check on %s" % self.activeiface) dhcp_server_data = [] responses["dhcp_nowait"] = False if len(dhcp_server_data) < 1: self.log.debug("No DHCP servers found. Warning user about " "dhcp_nowait.") #Build dialog elements dhcp_info = [] dhcp_info.append( urwid.Padding(urwid.Text(("header", "!!! WARNING !!!")), "center")) dhcp_info.append( widget.TextLabel( "Unable to detect DHCP server" + "on interface %s." % (self.activeiface) + "\nDHCP will be set up in the background, " + "but may not receive an IP address. You may " + "want to check your DHCP connection manually " + "using the Shell Login menu to the left.")) dialog.display_dialog( self, urwid.Pile(dhcp_info), "DHCP Servers Found on %s" % self.activeiface) self.parent.refreshScreen() responses["dhcp_nowait"] = True #Check ipaddr, netmask, gateway only if static elif responses["bootproto"] == "none": try: if netaddr.valid_ipv4(responses["ipaddr"]): if not netaddr.IPAddress(responses["ipaddr"]): raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except (BadIPException, Exception): errors.append("Not a valid IP address: %s" % responses["ipaddr"]) try: if netaddr.valid_ipv4(responses["netmask"]): netmask = netaddr.IPAddress(responses["netmask"]) if netmask.is_netmask is False: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except (BadIPException, Exception): errors.append("Not a valid netmask: %s" % responses["netmask"]) try: if len(responses["gateway"]) > 0: #Check if gateway is valid if netaddr.valid_ipv4(responses["gateway"]) is False: raise BadIPException("Gateway IP address is not valid") #Check if gateway is in same subnet if network.inSameSubnet(responses["ipaddr"], responses["gateway"], responses["netmask"]) is False: raise BadIPException("Gateway IP is not in same " "subnet as IP address") except (BadIPException, Exception) as e: errors.append(e) self.parent.footer.set_text("Scanning for duplicate IP address..") if len(responses["ipaddr"]) > 0: if self.netsettings[self.activeiface]['link'].upper() != "UP": try: network.upIface(self.activeiface) except NetworkException as e: errors.append("Cannot activate {0} to check for " "duplicate IP.".format(self.activeiface)) if network.duplicateIPExists(responses["ipaddr"], self.activeiface): errors.append("Duplicate host found with IP {0}.".format( responses["ipaddr"])) if len(errors) > 0: self.parent.footer.set_text("Error: %s" % (errors[0])) self.log.error("Errors: %s %s" % (len(errors), 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 and sanity checks.""" #Get field information responses = dict() self.parent.footer.set_text("Checking data...") for index, fieldname in enumerate(fields): if fieldname == "blank" or fieldname == "ifname": pass elif fieldname == "bootproto": rb_group = self.edits[index].rb_group if rb_group[0].state: responses["bootproto"] = "dhcp" else: responses["bootproto"] = "none" elif fieldname == "onboot": rb_group = self.edits[index].rb_group if rb_group[0].state: responses["onboot"] = "yes" else: responses["onboot"] = "no" else: responses[fieldname] = self.edits[index].get_edit_text() ###Validate each field errors = [] if responses["onboot"] == "no": numactiveifaces = 0 for iface in self.netsettings: if self.netsettings[iface]['addr'] != "": numactiveifaces += 1 if numactiveifaces < 2 and \ self.netsettings[self.activeiface]['addr'] != "": #Block user because puppet l23network fails if all intefaces #are disabled. errors.append("Cannot disable all interfaces.") elif responses["bootproto"] == "dhcp": self.parent.footer.set_text("Scanning for DHCP servers. " "Please wait...") self.parent.refreshScreen() try: dhcptimeout = 5 with timeout.run_with_timeout(dhcp_checker.utils.IfaceState, [self.activeiface], timeout=dhcptimeout) as iface: dhcp_server_data = timeout.run_with_timeout( dhcp_checker.api.check_dhcp_on_eth, [iface, dhcptimeout], timeout=dhcptimeout) except (KeyboardInterrupt, timeout.TimeoutError): self.log.debug("DHCP scan timed out") self.log.warning(traceback.format_exc()) dhcp_server_data = [] except Exception: self.log.warning("dhcp_checker failed to check on %s" % self.activeiface) dhcp_server_data = [] responses["dhcp_nowait"] = False if len(dhcp_server_data) < 1: self.log.debug("No DHCP servers found. Warning user about " "dhcp_nowait.") #Build dialog elements dhcp_info = [] dhcp_info.append(urwid.Padding( urwid.Text(("header", "!!! WARNING !!!")), "center")) dhcp_info.append( widget.TextLabel( "Unable to detect DHCP server" + "on interface %s." % (self.activeiface) + "\nDHCP will be set up in the background, " + "but may not receive an IP address. You may " + "want to check your DHCP connection manually " + "using the Shell Login menu to the left.")) dialog.display_dialog(self, urwid.Pile(dhcp_info), "DHCP Servers Found on %s" % self.activeiface) self.parent.refreshScreen() responses["dhcp_nowait"] = True #Check ipaddr, netmask, gateway only if static elif responses["bootproto"] == "none": try: if netaddr.valid_ipv4(responses["ipaddr"]): if not netaddr.IPAddress(responses["ipaddr"]): raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except (BadIPException, Exception): errors.append("Not a valid IP address: %s" % responses["ipaddr"]) try: if netaddr.valid_ipv4(responses["netmask"]): netmask = netaddr.IPAddress(responses["netmask"]) if netmask.is_netmask is False: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except (BadIPException, Exception): errors.append("Not a valid netmask: %s" % responses["netmask"]) try: if len(responses["gateway"]) > 0: #Check if gateway is valid if netaddr.valid_ipv4(responses["gateway"]) is False: raise BadIPException("Gateway IP address is not valid") #Check if gateway is in same subnet if network.inSameSubnet(responses["ipaddr"], responses["gateway"], responses["netmask"]) is False: raise BadIPException("Gateway IP is not in same " "subnet as IP address") except (BadIPException, Exception) as e: errors.append(e) if len(errors) > 0: self.parent.footer.set_text("Error: %s" % (errors[0])) self.log.error("Errors: %s %s" % (len(errors), errors)) return False else: self.parent.footer.set_text("No errors found.") return responses
def check(self, args): """Validates all fields have valid values and some sanity checks.""" self.parent.footer.set_text("Checking data...") self.parent.refreshScreen() #Refresh networking to make sure IP matches self.getNetwork() #Get field information responses = dict() for index, fieldname in enumerate(self.fields): if fieldname != "blank" and "label" not in fieldname: responses[fieldname] = self.edits[index].get_edit_text() ###Validate each field errors = [] #Set internal_{ipaddress,netmask,interface} responses["ADMIN_NETWORK/interface"] = self.activeiface responses["ADMIN_NETWORK/netmask"] = self.netsettings[ self.activeiface]["netmask"] responses["ADMIN_NETWORK/mac"] = self.netsettings[ self.activeiface]["mac"] responses["ADMIN_NETWORK/ipaddress"] = self.netsettings[ self.activeiface]["addr"] #ensure management interface is valid if responses["ADMIN_NETWORK/interface"] not in self.netsettings.keys(): errors.append("Management interface not valid") else: self.parent.footer.set_text("Scanning for DHCP servers. \ Please wait...") self.parent.refreshScreen() ###Start DHCP check on this interface #dhcp_server_data=[{'server_id': '192.168.200.2', 'iface': 'eth2', # 'yiaddr': '192.168.200.15', 'mac': # '52:54:00:12:35:02', 'server_ip': '192.168.200.2', # 'dport': 67, 'message': 'offer', # 'gateway': '0.0.0.0'}] try: dhcptimeout = 5 default = [] with timeout.run_with_timeout(dhcp_checker.utils.IfaceState, [self.activeiface], timeout=dhcptimeout) as iface: dhcp_server_data = timeout.run_with_timeout( dhcp_checker.api.check_dhcp_on_eth, [iface, dhcptimeout], timeout=dhcptimeout, default=default) except (KeyboardInterrupt, timeout.TimeoutError): log.debug("DHCP scan timed out") log.warning(traceback.format_exc()) dhcp_server_data = default num_dhcp = len(dhcp_server_data) if num_dhcp == 0: log.debug("No DHCP servers found") else: #Problem exists, but permit user to continue log.error("%s foreign DHCP server(s) found: %s" % (num_dhcp, dhcp_server_data)) #Build dialog elements dhcp_info = [] dhcp_info.append(urwid.Padding( urwid.Text(("header", "!!! WARNING !!!")), "center")) dhcp_info.append(widget.TextLabel("You have selected an \ interface that contains one or more DHCP servers. This will impact \ provisioning. You should disable these DHCP servers before you continue, or \ else deployment will likely fail.")) dhcp_info.append(widget.TextLabel("")) for index, dhcp_server in enumerate(dhcp_server_data): dhcp_info.append(widget.TextLabel("DHCP Server #%s:" % (index + 1))) dhcp_info.append(widget.TextLabel("IP address: %-10s" % dhcp_server['server_ip'])) dhcp_info.append(widget.TextLabel("MAC address: %-10s" % dhcp_server['mac'])) dhcp_info.append(widget.TextLabel("")) dialog.display_dialog(self, urwid.Pile(dhcp_info), "DHCP Servers Found on %s" % self.activeiface) ###Ensure pool start and end are on the same subnet as mgmt_if #Ensure mgmt_if has an IP first if len(self.netsettings[responses[ "ADMIN_NETWORK/interface"]]["addr"]) == 0: errors.append("Go to Interfaces to configure management \ interface first.") else: #Ensure ADMIN_NETWORK/interface is not running DHCP if self.netsettings[responses[ "ADMIN_NETWORK/interface"]]["bootproto"] == "dhcp": errors.append("%s is running DHCP. Change it to static " "first." % self.activeiface) #Ensure DHCP Pool Start and DHCP Pool are valid IPs try: if netaddr.valid_ipv4(responses[ "ADMIN_NETWORK/dhcp_pool_start"]): dhcp_start = netaddr.IPAddress( responses["ADMIN_NETWORK/dhcp_pool_start"]) if not dhcp_start: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except Exception: errors.append("Invalid IP address for DHCP Pool Start") try: if netaddr.valid_ipv4(responses[ "ADMIN_NETWORK/dhcp_gateway"]): dhcp_gateway = netaddr.IPAddress( responses["ADMIN_NETWORK/dhcp_gateway"]) if not dhcp_gateway: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except Exception: errors.append("Invalid IP address for DHCP Gateway") try: if netaddr.valid_ipv4(responses[ "ADMIN_NETWORK/dhcp_pool_end"]): dhcp_end = netaddr.IPAddress( responses["ADMIN_NETWORK/dhcp_pool_end"]) if not dhcp_end: raise BadIPException("Not a valid IP address") else: raise BadIPException("Not a valid IP address") except Exception: errors.append("Invalid IP address for DHCP Pool end") #Ensure pool start and end are in the same subnet of each other netmask = self.netsettings[responses[ "ADMIN_NETWORK/interface" ]]["netmask"] if not network.inSameSubnet( responses["ADMIN_NETWORK/dhcp_pool_start"], responses["ADMIN_NETWORK/dhcp_pool_end"], netmask): errors.append("DHCP Pool start and end are not in the " "same subnet.") #Ensure pool start and end are in the right netmask mgmt_if_ipaddr = self.netsettings[responses[ "ADMIN_NETWORK/interface"]]["addr"] if network.inSameSubnet(responses[ "ADMIN_NETWORK/dhcp_pool_start"], mgmt_if_ipaddr, netmask) is False: errors.append("DHCP Pool start does not match management" " network.") if network.inSameSubnet(responses[ "ADMIN_NETWORK/dhcp_pool_end"], mgmt_if_ipaddr, netmask) is False: errors.append("DHCP Pool end does not match management " "network.") if network.inSameSubnet(responses[ "ADMIN_NETWORK/dhcp_gateway"], mgmt_if_ipaddr, netmask) is False: errors.append("DHCP Gateway does not match management " "network.") self.parent.footer.set_text("Scanning for duplicate IP address" "es. Please wait...") if network.duplicateIPExists(mgmt_if_ipaddr, self.activeiface): errors.append("Duplicate host found with IP {0}.".format( mgmt_if_ipaddr)) if len(errors) > 0: self.parent.footer.set_text("Error: %s" % (errors[0])) log.error("Errors: %s %s" % (len(errors), errors)) return False else: self.parent.footer.set_text("No errors found.") return responses
def test_run_with_zero_timeout(self): method = mock.Mock(return_value='result') self.assertEqual('result', timeout.run_with_timeout(method, timeout=0)) method.assert_called_once_with()
def test_run_with_timeout_reached(self): with self.assertRaises(timeout.TimeoutError): timeout.run_with_timeout(time.sleep, (2, ), timeout=0.01)
def test_run_with_params(self): method = mock.Mock(return_value='result') self.assertEqual( 'result', timeout.run_with_timeout(method, ('a1', ), {'k1': 'v1'})) method.assert_called_once_with('a1', k1='v1')
def test_run_with_timeout_reached(self): with self.assertRaises(timeout.TimeoutError): timeout.run_with_timeout(time.sleep, (2,), timeout=0.01)
def test_run_with_params(self): method = mock.Mock(return_value='result') self.assertEqual( 'result', timeout.run_with_timeout(method, ('a1',), {'k1': 'v1'})) method.assert_called_once_with('a1', k1='v1')