def test_image_info(tmpdir): cmdlib.run_verbose( ["qemu-img", "create", "-f", "qcow2", f"{tmpdir}/test.qcow2", "10M"]) assert cmdlib.image_info(f"{tmpdir}/test.qcow2").get('format') == "qcow2" cmdlib.run_verbose([ "qemu-img", "create", "-f", "vpc", '-o', 'force_size,subformat=fixed', f"{tmpdir}/test.vpc", "10M" ]) assert cmdlib.image_info(f"{tmpdir}/test.vpc").get('format') == "vpc"
def generate_ovf_parameters(self, vmdk, cpu=2, memory=4096, system_type="vmx-13", os_type="rhel7_64Guest", scsi="VirtualSCSI", network="VmxNet3"): """ Returns a dictionary with the parameters needed to create an OVF file based on the qemu, vmdk, and info from the build metadata """ disk_info = image_info(vmdk) vmdk_size = os.stat(vmdk).st_size image = self.summary product = f'{self.meta["name"]} {self.summary}' vendor = self.meta['name'] version = self.meta['ostree-version'] params = { 'ovf_cpu_count': cpu, 'ovf_memory_mb': memory, 'vsphere_image_name': image, 'vsphere_product_name': product, 'vsphere_product_vendor_name': vendor, 'vsphere_product_version': version, 'vsphere_virtual_system_type': system_type, 'vsphere_os_type': os_type, 'vsphere_scsi_controller_type': scsi, 'vsphere_network_controller_type': network, 'vmdk_capacity': disk_info.get("virtual-size"), 'vmdk_size': str(vmdk_size), } return params
def generate_ovf_parameters(self, vmdk, cpu=2, memory=4096): """ Returns a dictionary with the parameters needed to create an OVF file based on the qemu, vmdk, image.yaml, and info from the build metadata """ image_yaml = flatten_image_yaml( '/usr/lib/coreos-assembler/image-default.yaml', flatten_image_yaml('src/config/image.yaml')) system_type = 'vmx-{}'.format(image_yaml['vmware-hw-version']) os_type = image_yaml['vmware-os-type'] disk_info = image_info(vmdk) vmdk_size = os.stat(vmdk).st_size image = self.summary product = f'{self.meta["name"]} {self.summary}' vendor = self.meta['name'] version = self.meta['ostree-version'] params = { 'ovf_cpu_count': cpu, 'ovf_memory_mb': memory, 'vsphere_image_name': image, 'vsphere_product_name': product, 'vsphere_product_vendor_name': vendor, 'vsphere_product_version': version, 'vsphere_virtual_system_type': system_type, 'vsphere_os_type': os_type, 'vmdk_capacity': disk_info.get("virtual-size"), 'vmdk_size': str(vmdk_size), } return params
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) log.info(f"Staging temp image: {work_img}") self.set_platform() 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(f"Processing work image callback") self.mutate_callback(work_img) 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)