Example #1
0
    def build(cls,
              image_md_filename,
              image_filename,
              destdir='',
              progressbar=None):
        pack = ImagePack()
        pack.image_md = ImageMetadata.from_file(image_md_filename)
        pack.pack_md = ImagePackMetadata()
        if destdir != '' and not os.path.isdir(destdir):
            raise ValueError('"{0}" is not a directory'.format(destdir))
        pack.filename = os.path.join(
            destdir, '{0}.euimage'.format(pack.image_md.get_nvra()))
        with open(image_md_filename) as image_md_file:
            digest = hashlib.sha256(image_md_file.read())
            pack.pack_md.image_md_sha256sum = digest.hexdigest()
        # Since we have to know the size of the compressed image ahead
        # of time to write tarinfo headers we have to spool the whole
        # thing to disk.  :-\
        with tempfile.NamedTemporaryFile() as compressed_image:
            # Feed stuff to a subprocess to checksum and compress in one pass
            digest = hashlib.sha256()
            bytes_read = 0
            with open(image_filename, 'rb') as original_image:
                xz_proc = subprocess.Popen(('xz', '-c'),
                                           stdin=subprocess.PIPE,
                                           stdout=compressed_image)
                if progressbar:
                    progressbar.start()
                while True:
                    chunk = original_image.read(euca2ools.BUFSIZE)
                    if not chunk:
                        break
                    digest.update(chunk)
                    xz_proc.stdin.write(chunk)
                    bytes_read += len(chunk)
                    if progressbar:
                        progressbar.update(bytes_read)
                xz_proc.stdin.close()
                xz_proc.wait()
            if progressbar:
                progressbar.finish()
            pack.pack_md.image_sha256sum = digest.hexdigest()
            pack.pack_md.image_size = bytes_read

            # Write metadata and pack everything up
            with contextlib.closing(
                    tarfile.open(pack.filename, 'w',
                                 dereference=True)) as tarball:
                with tempfile.NamedTemporaryFile() as pack_md_file:
                    pack.pack_md.dump_to_fileobj(pack_md_file)
                    tarball.add(pack_md_file.name, arcname=PACK_MD_ARCNAME)
                tarball.add(image_md_filename, arcname=IMAGE_MD_ARCNAME)
                tarball.add(compressed_image.name, arcname=IMAGE_ARCNAME)
        return pack
Example #2
0
    def build(cls, image_md_filename, image_filename,
              destdir='', progressbar=None):
        pack = ImagePack()
        pack.image_md = ImageMetadata.from_file(image_md_filename)
        pack.pack_md = ImagePackMetadata()
        if destdir != '' and not os.path.isdir(destdir):
            raise ValueError('"{0}" is not a directory'.format(destdir))
        pack.filename = os.path.join(destdir, '{0}.euimage'.format(
            pack.image_md.get_nvra()))
        with open(image_md_filename) as image_md_file:
            digest = hashlib.sha256(image_md_file.read())
            pack.pack_md.image_md_sha256sum = digest.hexdigest()
        # Since we have to know the size of the compressed image ahead
        # of time to write tarinfo headers we have to spool the whole
        # thing to disk.  :-\
        with tempfile.NamedTemporaryFile() as compressed_image:
            # Feed stuff to a subprocess to checksum and compress in one pass
            digest = hashlib.sha256()
            bytes_read = 0
            with open(image_filename, 'rb') as original_image:
                xz_proc = subprocess.Popen(('xz', '-c'), stdin=subprocess.PIPE,
                                           stdout=compressed_image)
                if progressbar:
                    progressbar.start()
                while True:
                    chunk = original_image.read(euca2ools.BUFSIZE)
                    if not chunk:
                        break
                    digest.update(chunk)
                    xz_proc.stdin.write(chunk)
                    bytes_read += len(chunk)
                    if progressbar:
                        progressbar.update(bytes_read)
                xz_proc.stdin.close()
                xz_proc.wait()
            if progressbar:
                progressbar.finish()
            pack.pack_md.image_sha256sum = digest.hexdigest()
            pack.pack_md.image_size = bytes_read

            # Write metadata and pack everything up
            with contextlib.closing(tarfile.open(pack.filename, 'w',
                                                 dereference=True)) as tarball:
                with tempfile.NamedTemporaryFile() as pack_md_file:
                    pack.pack_md.dump_to_fileobj(pack_md_file)
                    tarball.add(pack_md_file.name, arcname=PACK_MD_ARCNAME)
                tarball.add(image_md_filename, arcname=IMAGE_MD_ARCNAME)
                tarball.add(compressed_image.name, arcname=IMAGE_ARCNAME)
        return pack
Example #3
0
 def open(cls, filename):
     with ImagePack(filename=filename) as pack:
         member = pack.__tarball.getmember(PACK_MD_ARCNAME)
         with contextlib.closing(pack.__tarball.extractfile(member)) \
                 as md_file:
             pack.pack_md = ImagePackMetadata.from_fileobj(md_file)
         member = pack.__tarball.getmember(IMAGE_MD_ARCNAME)
         with contextlib.closing(pack.__tarball.extractfile(member)) \
                 as md_file:
             pack.image_md = ImageMetadata.from_fileobj(md_file)
             md_file.seek(0)
             image_md_sha256sum = hashlib.sha256(md_file.read()).hexdigest()
         if image_md_sha256sum != pack.pack_md.image_md_sha256sum:
             raise RuntimeError(
                 'image metadata appears to be corrupt '
                 '(expected SHA256: {0}, actual: {1})',
                 pack.pack_md.image_md_sha256sum, image_md_sha256sum)
     return pack
Example #4
0
 def open(cls, filename):
     with ImagePack(filename=filename) as pack:
         member = pack.__tarball.getmember(PACK_MD_ARCNAME)
         with contextlib.closing(pack.__tarball.extractfile(member)) \
                 as md_file:
             pack.pack_md = ImagePackMetadata.from_fileobj(md_file)
         member = pack.__tarball.getmember(IMAGE_MD_ARCNAME)
         with contextlib.closing(pack.__tarball.extractfile(member)) \
                 as md_file:
             pack.image_md = ImageMetadata.from_fileobj(md_file)
             md_file.seek(0)
             image_md_sha256sum = hashlib.sha256(md_file.read()).hexdigest()
         if image_md_sha256sum != pack.pack_md.image_md_sha256sum:
             raise RuntimeError('image metadata appears to be corrupt '
                                '(expected SHA256: {0}, actual: {1})',
                                pack.pack_md.image_md_sha256sum,
                                image_md_sha256sum)
     return pack