示例#1
0
    def copy(self, dest):
        """
        Copy the file
        """
        if not isinstance(dest, FileMgr):
            raise ValueError("dest file must be instance of FileMgr")

        if self.remote and dest.remote:
            cmd = ["cp", "--force", self.filename, dest.filename]
            raise ValueError(
                "Can't copy source->dest if both are remote files, not implemented"
            )

        elif self.remote:
            cmd = ["scp"]
            if self.remote.port:
                cmd += ["-P", self.remote.port]
            cmd += ["%s:%s" % (self.remote.host, self.filename), dest.filename]
            return util.runCmd(cmd=cmd)

        elif dest.remote:
            cmd = ["scp"]
            if dest.remote.port:
                cmd += ["-P", dest.remote.port]
            cmd += [self.filename, "%s:%s" % (dest.remote.host, dest.filename)]
            return util.runCmd(cmd=cmd)

        cmd = ["cp", "--force", self.filename, dest.filename]
        return util.runCmd(cmd=cmd)
示例#2
0
 def restart(self):
     """
     Restart the bind process
     """
     cmd = self.cmd.restart
     cmd = cmd.split(" ")
     return util.runCmd(self.remote, cmd)
示例#3
0
 def size(self):
     """
     Returns size of the file
     """
     cmd = ["stat", "-c", "%s", self.filename]
     out = util.runCmd(self.remote, cmd)
     return int(out)
示例#4
0
 def reloadZone(self, zone=None):
     """
     reload zone content, one or all zones
     """
     log.info("Reloading zone %s", zone)
     cmd = self.cmd.reload_zone.format(zone=zone)
     cmd = cmd.split(" ")
     return util.runCmd(self.remote, cmd)
示例#5
0
    def move(self, dest):
        """
        Move the file
        todo, make the object invalid, or point to the new path?
        """
        if not isinstance(dest, FileMgr):
            raise ValueError("dest file must be instance of FileUtil")

        if self.remote and not dest.remote:
            raise ValueError(
                "Not implemented, cannot move from local to remote server")
        if not self.remote and dest.remote:
            raise ValueError(
                "Not implemented, cannot move from remote to local server")

        cmd = ["mv", "--force", self.filename, dest.filename]
        ret = util.runCmd(self.remote, cmd, call=True)
        return ret
示例#6
0
    def increaseSoaSerial(self, zoneinfo):
        """
        Increase serial number in a zonefile
        First verifies that the SOA has the format YYYYMMDDxx, with a valid date
        Extra check: new file with updated soa must have same size as old file
        """
        if zoneinfo.typ != "master":
            raise NS_Exception(
                "increaseSoaSerial only makes sense for zone type master")

        self._verifyTmpDir()
        tmpfile = "%s/%s" % (self.tmpdir, zoneinfo.name)

        # Copy file to temp
        fsrc = FileMgr(self.remote, zoneinfo.file)
        fdst = FileMgr(filename=tmpfile, mode="w")
        fsrc.copy(fdst)

        # compare checksums on original and copied file
        if not fsrc.compare(fdst):
            raise NS_Exception("Error, copied file differs in checksum")

        # We now have a verified copy of the file locally, Search for serial number
        f = open(tmpfile)
        fpos = f.tell()
        line = f.readline()
        serial = None
        while line:
            line = line.rstrip()
            if line.lower().endswith("; serial"):
                serial = line
                serialfpos = fpos
            fpos = f.tell()
            line = f.readline()
        f.close()

        if serial is None:
            raise NS_Exception("Can't find serial number in file %s" %
                               zoneinfo.file)

        # search backwards for first digit
        p = len(serial) - len("; Serial")
        while not serial[p].isdigit():
            p -= 1
            if p < 0:
                raise NS_Exception(
                    "Can't find last digit in serial number in file %s" %
                    zoneinfo.file)

        # check all 10 positions, must be digits
        p -= 9  # should be first position in serial number
        if p < 0:
            raise NS_Exception(
                "Can't find all digist in serial number in file %s" %
                zoneinfo.file)
        if not serial[p:p + 10].isdigit():
            raise NS_Exception("Can't find serial number in file %s" %
                               zoneinfo.file)

        # check if serial starts with a valid date
        try:
            dt = datetime.datetime.strptime(serial[p:p + 8], "%Y%m%d").date()
        except ValueError as err:
            raise NS_Exception(
                "Serial number does not start with a valid date, in file %s" %
                zoneinfo.file)

        seq = int(serial[p + 8:p + 10])
        now = datetime.datetime.now().date()
        if now > dt:
            # Serial has old date, replace with todays date and restart sequence
            dt = now
            seq = 0
        else:
            if seq > 98:
                # todo, increase to next day and restart sequence
                dt = dt + datetime.timedelta(days=1)
                seq = 0
            else:
                seq += 1
        serial = dt.strftime("%Y%m%d") + str(seq).zfill(2)

        # Ok, write the new serial to the temp file
        f = open(tmpfile, "r+b")
        f.seek(serialfpos + p)
        f.write(serial.encode())
        f.close()

        # Copy the file with updated serial number to server
        if self.remote:
            self._verifyTmpDir(self.remote)
            fsrc = FileMgr(filename=tmpfile)
            fdst = FileMgr(self.remote, tmpfile, mode="w")
            fsrc.copy(fdst)

            # Compare checksums on local and remote file so copy was ok
            if not fsrc.compare(fdst):
                raise NS_Exception(
                    "Error: Copy of new file failed, incorrect checksum")

        # Verify size between original file and file with updated serial
        # They should be identical, since serial number never changes size
        fsrc = FileMgr(remote=self.remote, filename=tmpfile)
        fdst = FileMgr(remote=self.remote, filename=zoneinfo.file)
        if fsrc.size() != fdst.size():
            raise NS_Exception(
                "Error: Old file and new file has different sizes")

        # Copy file to correct location
        cmd = ["cp", "--force", fsrc.filename, fdst.filename]
        util.runCmd(remote=self.remote, cmd=cmd)

        # Tell bind we have an updated serial no
        self.reloadZone(zoneinfo.name)
示例#7
0
 def sha256sum(self):
     """Calculate sha256 checksum on file"""
     cmd = ["sha256sum", self.filename]
     out = util.runCmd(self.remote, cmd)
     return out.split()[0].decode()
示例#8
0
 def mkdir(self):
     """
     Create the directory
     """
     cmd = ["mkdir", "-p", self.filename]
     return util.runCmd(self.remote, cmd, call=True) == 0
示例#9
0
 def exist(self):
     """
     Returns True if file exist
     """
     cmd = ["test", "-f", self.filename]
     return util.runCmd(self.remote, cmd, call=True) == 0
示例#10
0
 def restart_v6(self):
     log.info("Restart dhcpv6 server")
     cmd = self.config.ipv6.restart
     cmd = cmd.split(" ")
     return util.runCmd(None, cmd)