def save(self, header = True): """ Save the dynamic rules to a file/path. Returns boolean indicating success or failure. """ fd = None try: try: # If we were given a path, try opening and writing to it if self.path: fd = open(self.path, "w") fd.write(self.write(header)) # else if we were given a file descriptor, just write to it elif self.fd: self.fd.write(self.write(header)) # else there is nothing we can do else: LOG.error("No source of data to parse") return False except IOError, e: LOG.error("IOError while reading file: %s" % (e,)) return False finally: # Ensure we alway close the file descriptor we opened if fd: fd.close() return True
def save(self, header = True): fd = None try: try: # If we were given a path, try opening and writing to it if self.path: fd = open(self.path, "w") fd.write(self.write(header)) # else if we were given a file descriptor, just read it elif self.fd: self.fd.write(self.write(header)) # else there is nothing we can do else: LOG.error("No source of data to parse") return False except IOError, e: LOG.error("IOError while reading file: %s" % (e,)) return False finally: # Ensure we alway close the file descriptor we opened if fd: fd.close() return True
def save(self, header=True): fd = None try: try: # If we were given a path, try opening and writing to it if self.path: fd = open(self.path, "w") fd.write(self.write(header)) # else if we were given a file descriptor, just read it elif self.fd: self.fd.write(self.write(header)) # else there is nothing we can do else: LOG.error("No source of data to parse") return False except IOError, e: LOG.error("IOError while reading file: %s" % (e, )) return False finally: # Ensure we alway close the file descriptor we opened if fd: fd.close() return True
def ip_link_set_name(src_name, dst_name): """ Rename network interface src_name to dst_name using "ip link set $src_name name $dst_name" """ LOG.debug("Attempting rename %s -> %s" % (src_name, dst_name)) # Is the interface currently up? link_show = Popen(["ip", "link", "show", src_name], stdout = PIPE) stdout, _ = link_show.communicate() if link_show.returncode != 0: LOG.error("performing \"ip link show %s\" returned %d - skipping" % (src_name, link_show.returncode)) return # Does the string "UP" appear? isup = 'UP' in (stdout.split("<", 1)[1].split(">", 1)[0].split(',')) # If it is up, bring it down for the rename if isup: link_down = Popen(["ip", "link", "set", src_name, "down"]) link_down.wait() if link_down.returncode != 0: LOG.error("Unable to bring link %s down. (Exit %d)" % (src_name, link_down.returncode)) return # Perform the rename link_rename = Popen(["ip", "link", "set", src_name, "name", dst_name]) link_rename.wait() if link_rename.returncode != 0: LOG.error("Unable to rename link %s to %s. (Exit %d)" % (src_name, dst_name, link_rename.returncode)) return # if the device was up before, bring it back up if isup: # Performace note: if we are doing an intermediate rename to # move a device sideways, we shouldnt bring it back until it has # its final name. However, i cant think of a non-hacky way of doing # this with the current implementation link_up = Popen(["ip", "link", "set", dst_name, "up"]) link_up.wait() if link_up.returncode != 0: LOG.error("Unable to bring link %s back up. (Exit %d)" % (src_name, link_down.returncode)) return LOG.info("Succesfully renamed link %s to %s" % (src_name, dst_name))
def load_and_parse(self): """ Parse the static rules file. Returns boolean indicating success or failure. """ fd = None try: try: # If we were given a path, try opening and reading it if self.path: if not pathexists(self.path): LOG.error("Static rule file '%s' does not exist" % (self.path,)) return False fd = open(self.path, "r") raw_lines = fd.readlines() # else if we were given a file descriptor, just read it elif self.fd: raw_lines = self.fd.readlines() # else there is nothing we can do else: LOG.error("No source of data to parse") return False except IOError, e: LOG.error("IOError while reading file: %s" % (e,)) return False finally: # Ensure we alway close the file descriptor we opened if fd: fd.close() # Generator to strip blank lines and line comments lines = ( (n, l.strip()) for (n, l) in enumerate(raw_lines) if (len(l.strip()) and l.strip()[0] != '#') ) for num, line in lines: # Check the line is valid res = VALID_LINE.match(line) if res is None: LOG.warning("Unrecognised line '%s' in static rules (line %d)" % (line, num)) continue groups = res.groupdict() target = groups["target"].strip() if groups["method"] is None: # As method is optional, set to 'guess' if not present method = "guess" LOG.debug("Guessing method for interface %s on line %d" % (target, num) ) else: method = groups["method"].strip() value = groups["val"].strip() if value[0] == value[-1] and value[0] in ("'", '"'): value = value[1:-1] # If we should guess the value, quotes imply a label if value == "guess": value = "label" # Check that it is a recognised method if method not in StaticRules.methods: LOG.warning("Unrecognised static identification method " "'%s' on line %d - Ignoring" % (method, num)) continue # If we need to guess the method from the value if method == "guess": for k, v in StaticRules.validators.iteritems(): if v.match(value) is not None: method = k break else: # If no validators, assume label method = "label" # If we have a validator, test the valididy else: if method in StaticRules.validators: if StaticRules.validators[method].match(value) is None: LOG.warning("Invalid %s value '%s' on line %d - Ignoring" % (method, value, num)) continue # Warn if aliasing a previous static rule if target in self.formulae: LOG.warning("Static rule for '%s' already found. Discarding " "older entry" % (target,)) # CA-82901 - Accept old-style ppns (pciXpY), but translate to # new-style ones (pXpY) if method == "ppn" and value.startswith("pci"): value = "p" + value[3:] LOG.warning("Detected use of old-style ppn reference for %s " "on line %d - Translating to %s" % (target, num, value) ) self.formulae[target] = (method, value) return True
def load_and_parse(self): """ Parse the dynamic rules file. Returns boolean indicating success or failure. """ fd = None try: try: # If we were given a path, try opening and reading it if self.path: if not pathexists(self.path): LOG.error("Dynamic rule file '%s' does not exist" % (self.path,)) return False fd = open(self.path, "r") raw_lines = fd.readlines() # else if we were given a file descriptor, just read it elif self.fd: raw_lines = self.fd.readlines() # else there is nothing we can do else: LOG.error("No source of data to parse") return False except IOError, e: LOG.error("IOError while reading file: %s" % (e,)) return False finally: # Ensure we alway close the file descriptor we opened if fd: fd.close() # Strip out line comments data = "\n".join([ l.strip() for l in raw_lines if len(l.strip()) and l.strip()[0] != '#' ] ) # If the data is empty, dont pass it to json if not len( data.strip() ): return True try: info = json.loads(data) except ValueError: LOG.warning("Dynamic rules appear to be corrupt") return False # The installer has no json. except NameError: LOG.warning("Module json not available. Cant parse dynamic rules.") return False if "lastboot" in info: for entry in info["lastboot"]: try: if len(entry) != 3: raise ValueError("Expected 3 entries") macpci = MACPCI(entry[0], entry[1], tname=entry[2]) except (TypeError, ValueError), e: LOG.warning("Invalid lastboot data entry: %s" % (e,)) continue self.lastboot.append(macpci)
def load_and_parse(self): """ Parse the static rules file. Returns boolean indicating success or failure. """ fd = None try: try: # If we were given a path, try opening and reading it if self.path: if not pathexists(self.path): LOG.error("Static rule file '%s' does not exist" % (self.path, )) return False fd = open(self.path, "r") raw_lines = fd.readlines() # else if we were given a file descriptor, just read it elif self.fd: raw_lines = self.fd.readlines() # else there is nothing we can do else: LOG.error("No source of data to parse") return False except IOError, e: LOG.error("IOError while reading file: %s" % (e, )) return False finally: # Ensure we alway close the file descriptor we opened if fd: fd.close() # Generator to strip blank lines and line comments lines = ((n, l.strip()) for (n, l) in enumerate(raw_lines) if (len(l.strip()) and l.strip()[0] != '#')) for num, line in lines: # Check the line is valid res = VALID_LINE.match(line) if res is None: LOG.warning( "Unrecognised line '%s' in static rules (line %d)" % (line, num)) continue groups = res.groupdict() target = groups["target"].strip() if groups["method"] is None: # As method is optional, set to 'guess' if not present method = "guess" LOG.debug("Guessing method for interface %s on line %d" % (target, num)) else: method = groups["method"].strip() value = groups["val"].strip() if value[0] == value[-1] and value[0] in ("'", '"'): value = value[1:-1] # If we should guess the value, quotes imply a label if value == "guess": value = "label" # Check that it is a recognised method if method not in StaticRules.methods: LOG.warning("Unrecognised static identification method " "'%s' on line %d - Ignoring" % (method, num)) continue # If we need to guess the method from the value if method == "guess": for k, v in StaticRules.validators.iteritems(): if v.match(value) is not None: method = k break else: # If no validators, assume label method = "label" # If we have a validator, test the valididy else: if method in StaticRules.validators: if StaticRules.validators[method].match(value) is None: LOG.warning( "Invalid %s value '%s' on line %d - Ignoring" % (method, value, num)) continue # Warn if aliasing a previous static rule if target in self.formulae: LOG.warning("Static rule for '%s' already found. Discarding " "older entry" % (target, )) # CA-82901 - Accept old-style ppns (pciXpY), but translate to # new-style ones (pXpY) if method == "ppn" and value.startswith("pci"): value = "p" + value[3:] LOG.warning("Detected use of old-style ppn reference for %s " "on line %d - Translating to %s" % (target, num, value)) self.formulae[target] = (method, value) return True