def squash_tarcmd_reverse(self, imagedir=None): if not imagedir: imagedir = self.tmpdir rootfsdir = imagedir + "/rootfs" if os.path.exists(imagedir + "/squashed.tar"): return (True) if not self.anchore_layers: return (False) if not os.path.exists(rootfsdir): os.makedirs(rootfsdir) revlayer = list(self.anchore_layers) revlayer.reverse() excludesfile = '/'.join([imagedir, 'tarexcludes']) anchore_utils.touch_file(excludesfile) #open(excludesfile, 'w').close() for l in revlayer: layertar = imagedir + "/" + l + "/layer.tar" self._logger.debug("layer to squash: " + layertar) # washere tarcmd = ["tar", "-C", rootfsdir, "-t", "-f", layertar] self._logger.debug("cmd: " + ' '.join(tarcmd)) allfiles = subprocess.check_output(tarcmd) with open(excludesfile, 'a') as OFH: for f in allfiles.splitlines(): if re.match('.*\.wh\..*', f): fsub = re.sub(r"\.wh\.", "", f) OFH.write(f + "\n") OFH.write(fsub + "\n") tarcmd = ["tar", "-C", rootfsdir, "-X", excludesfile, "-x", "-v", "-f", layertar] self._logger.debug("cmd: " + ' '.join(tarcmd)) try: allfiles = subprocess.check_output(tarcmd) except Exception as err: self._logger.warn("Warn: Untar of unpacked image layer failed - proceeding but not all files in image are present.") self._logger.warn("Command: " + ' '.join(tarcmd)) self._logger.warn("Info: " + str(err)) with open(excludesfile, 'a') as OFH: for f in allfiles.splitlines(): OFH.write(f + "\n") newfile = excludesfile + "." + l shutil.copy(excludesfile, newfile) self._logger.debug("EXCLUDES: " + newfile) self.squashtar = imagedir + "/squashed.tar" self.squashed_allfiles = subprocess.check_output(["tar", "-C", rootfsdir, "-c", "-v", "-f", self.squashtar, "."]) return (True)
def squash_tarcmd_reverse(self, imagedir=None): if not imagedir: imagedir = self.tmpdir rootfsdir = imagedir + "/rootfs" if os.path.exists(imagedir + "/squashed.tar"): return (True) if not self.anchore_layers: return (False) if not os.path.exists(rootfsdir): os.makedirs(rootfsdir) revlayer = list(self.anchore_layers) revlayer.reverse() excludesfile = '/'.join([imagedir, 'tarexcludes']) anchore_utils.touch_file(excludesfile) for l in revlayer: layertar = imagedir + "/" + l + "/layer.tar" self._logger.debug("layer to squash: " + layertar) tarcmd = ["tar", "-C", rootfsdir, "-t", "-f", layertar] self._logger.debug("cmd: " + ' '.join(tarcmd)) allfiles = subprocess.check_output(tarcmd) with open(excludesfile, 'a') as OFH: for f in allfiles.splitlines(): if re.match('.*\.wh\..*', f): fsub = re.sub(r"\.wh\.", "", f) OFH.write(f + "\n") OFH.write(fsub + "\n") tarcmd = ["tar", "-C", rootfsdir, "-X", excludesfile, "-x", "-v", "-f", layertar] self._logger.debug("cmd: " + ' '.join(tarcmd)) try: allfiles = subprocess.check_output(tarcmd) except Exception as err: self._logger.warn("Warn: Untar of unpacked image layer failed - proceeding but not all files in image are present.") self._logger.warn("Command: " + ' '.join(tarcmd)) self._logger.warn("Info: " + str(err)) with open(excludesfile, 'a') as OFH: for f in allfiles.splitlines(): OFH.write(f + "\n") newfile = excludesfile + "." + l shutil.copy(excludesfile, newfile) self._logger.debug("EXCLUDES: " + newfile) self.squashtar = imagedir + "/squashed.tar" self.squashed_allfiles = subprocess.check_output(["tar", "-C", rootfsdir, "-c", "-v", "-f", self.squashtar, "."]) return (True)