def get_artifact_meta(self): """Get the artifact's metadata""" log.info(f"Calculating metadata for {self.image_name}") return { "path": self.image_name, "sha256": sha256sum_file(self.image_path), "size": os.stat(self.image_path).st_size }
def get_artifact_meta(self, fname=None): fsize = '{}'.format(os.stat(self.image_path).st_size) if fname is None: fname = self.image_name fpath = os.path.join(self.build_dir, fname) log.info(f"Calculating metadata for {fname}") return { "path": fname, "sha256": sha256sum_file(fpath), "size": int(fsize) }
def test_sha256sum_file(tmpdir): """ Verify we get the proper sha256 sum """ test_file = os.path.join(tmpdir, 'testfile') with open(test_file, 'w') as f: f.write('test') # $ sha256sum testfile # 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 e = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08' shasum = cmdlib.sha256sum_file(test_file) assert shasum == e
def get_artifact_meta(self, fname=None): """ Get the articat's meta-data :param fname: name of file to get meta-data for :type fname: str """ fsize = '{}'.format(os.stat(self.image_path).st_size) if fname is None: fname = self.image_name fpath = os.path.join(self.build_dir, fname) log.info(f"Calculating metadata for {fname}") return { "path": fname, "sha256": sha256sum_file(fpath), "size": int(fsize) }
def mutate_image(self): """ mutate_image is broken out seperately to allow other Classes to override the behavor. The callback parameter used to do post-processing on the working image before commiting it to the final location. To see how this is done, look at cosalib.vmware.VMwareOVA.mutate_image. :param callback: callback function for extra processing image :type callback: function """ work_img = os.path.join(self._tmpdir, f"{self.image_name_base}.{self.image_format}") final_img = os.path.join(os.path.abspath(self.build_dir), self.image_name) meta_patch = {} log.info(f"Staging temp image: {work_img}") self.set_platform() # Resizing if requested if self.virtual_size is not None: resize_cmd = [ 'qemu-img', 'resize', self.tmp_image, self.virtual_size ] run_verbose(resize_cmd) cmd = [ 'qemu-img', 'convert', '-f', 'qcow2', '-O', self.image_format, self.tmp_image ] for k, v in self.convert_options.items(): cmd.extend([k, v]) cmd.extend([work_img]) run_verbose(cmd) img_info = image_info(work_img) if self.image_format != img_info.get("format"): raise ImageError((f"{work_img} format mismatch" f" expected: '{self.image_format}'" f" found: '{img_info.get('format')}'")) if self.mutate_callback: log.info("Processing work image callback") meta_patch.update(self.mutate_callback(work_img) or {}) if self.tar_members: # Python does not create sparse Tarfiles, so we have do it via # the CLI here. tarlist = [] for member in self.tar_members: member_name = os.path.basename(member) # In the case of several clouds, the disk is named # `disk.raw` or `disk.vmdk`. When creating a tarball, we # rename the disk to the in-tar name if the name does not # match the default naming. if member_name.endswith(('.raw', '.vmdk')): if member_name != os.path.basename(work_img): shutil.move(work_img, os.path.join(self._tmpdir, member_name)) tarlist.append(member_name) tar_cmd = ['tar', '--owner=0', '--group=0', '-C', self._tmpdir] tar_cmd.extend(self.tar_flags) tar_cmd.extend(['-f', final_img]) tar_cmd.extend(tarlist) run_verbose(tar_cmd) else: log.info(f"Moving {work_img} to {final_img}") shutil.move(work_img, final_img) if self.gzip: sha256 = sha256sum_file(final_img) size = os.stat(final_img).st_size temp_path = f"{final_img}.tmp" with open(temp_path, "wb") as fh: run_verbose(['gzip', '-9c', final_img], stdout=fh) shutil.move(temp_path, final_img) meta_patch.update({ 'skip-compression': True, 'uncompressed-sha256': sha256, 'uncompressed-size': size, }) return meta_patch