Exemple #1
0
    def _handle_lago_template(
        self, disk_path, template_spec, template_store, template_repo
    ):
        disk_metadata = template_spec.get('metadata', {})
        if template_store is None or template_repo is None:
            raise RuntimeError('No templates directory provided')

        template = template_repo.get_by_name(template_spec['template_name'])
        template_version = template.get_version(
            template_spec.get('template_version', None)
        )
        if template_version not in template_store:
            LOGGER.info(
                log_utils.log_always(
                    "Template %s not in cache, downloading"
                ) % template_version.name,
            )
            template_store.download(template_version)

        template_store.mark_used(template_version, self.paths.uuid())
        disk_metadata.update(
            template_store.get_stored_metadata(
                template_version,
            ),
        )
        base = template_store.get_path(template_version)
        qemu_cmd = ['qemu-img', 'create', '-f', 'qcow2', '-b', base, disk_path]
        return qemu_cmd, disk_metadata
Exemple #2
0
    def _handle_lago_template(
        self, disk_path, template_spec, template_store, template_repo
    ):
        disk_metadata = template_spec.get('metadata', {})
        if template_store is None or template_repo is None:
            raise RuntimeError('No templates directory provided')

        template = template_repo.get_by_name(template_spec['template_name'])
        template_version = template.get_version(
            template_spec.get('template_version', None)
        )
        if template_version not in template_store:
            LOGGER.info(
                log_utils.log_always("Template %s not in cache, downloading") %
                template_version.name,
            )
            template_store.download(template_version)

        template_store.mark_used(template_version, self.paths.uuid())
        disk_metadata.update(
            template_store.get_stored_metadata(template_version, ),
        )
        base = template_store.get_path(template_version)
        qemu_cmd = ['qemu-img', 'create', '-f', 'qcow2', '-b', base, disk_path]
        return qemu_cmd, disk_metadata
Exemple #3
0
    def _create_disk(
        self,
        name,
        spec,
        template_repo=None,
        template_store=None,
    ):
        """
        Creates a disc with the given name from the given repo or store.

        Args:
            name (str): Name of the domain to create the disk for
            spec (dict): Specification of the disk to create
            template_repo (TemplateRepository or None): template repo instance
                to use
            template_store (TemplateStore or None): template store instance to
                use

        Returns:
            Tuple(str, dict): Path with the disk and metadata

        Raises:
            RuntimeError: If the type of the disk is not supported or failed to
                create the disk
        """
        LOGGER.debug("Spec: %s" % spec)
        with LogTask("Create disk %s" % spec['name']):
            disk_metadata = {}

            disk_filename = '%s_%s.%s' % (name, spec['name'], spec['format'])
            disk_path = self.paths.images(disk_filename)

            if spec['type'] == 'template':
                template_type = spec.get('template_type', 'lago')
                if template_type == 'lago':
                    if template_store is None or template_repo is None:
                        raise RuntimeError('No templates directory provided')

                    template = template_repo.get_by_name(spec['template_name'])
                    template_version = template.get_version(
                        spec.get('template_version', None)
                    )

                    if template_version not in template_store:
                        LOGGER.info(
                            log_utils.log_always(
                                "Template %s not in cache, downloading"
                            ) % template_version.name,
                        )
                        template_store.download(template_version)
                    template_store.mark_used(
                        template_version, self.paths.uuid()
                    )

                    disk_metadata.update(
                        template_store.get_stored_metadata(
                            template_version,
                        ),
                    )

                    base = template_store.get_path(template_version)
                    qemu_img_cmd = [
                        'qemu-img', 'create', '-f', 'qcow2', '-b', base,
                        disk_path
                    ]

                elif template_type == 'qcow2':
                    path = spec.get('path', '')
                    if not path:
                        raise RuntimeError('Partial drive spec %s' % str(spec))
                    disk_metadata = spec.get('metadata', {})
                    qemu_img_cmd = [
                        'qemu-img', 'create', '-f', 'qcow2', '-b', path,
                        disk_path
                    ]
                else:
                    raise RuntimeError(
                        'Unsupporte template spec %s' % str(spec)
                    )
                task_message = 'Create disk %s(%s)' % (name, spec['name'])

            elif spec['type'] == 'empty':
                qemu_img_cmd = [
                    'qemu-img', 'create', '-f', spec['format'], disk_path,
                    spec['size']
                ]
                task_message = 'Create empty disk image'

            elif spec['type'] == 'file':
                url = spec.get('url', '')
                path = spec.get('path', '')
                disk_metadata = spec.get('metadata', {})
                if not url and not path:
                    raise RuntimeError('Partial drive spec %s' % str(spec))

                if url:
                    disk_in_prefix = self.fetch_url(url)
                    if path:
                        shutil.move(disk_in_prefix, spec['path'])
                    else:
                        spec['path'] = disk_in_prefix

                # If we're using raw file, return it's path
                return spec['path'], disk_metadata
            else:
                raise RuntimeError('Unknown drive spec %s' % str(spec))

            if os.path.exists(disk_path):
                os.unlink(disk_path)

            with LogTask(task_message):
                ret, _, _ = utils.run_command(qemu_img_cmd)
                if ret != 0:
                    raise RuntimeError(
                        'Failed to create image, qemu-img returned %d' % ret,
                    )
                # To avoid losing access to the file
                os.chmod(disk_path, 0666)

            disk_rel_path = os.path.join(
                '$LAGO_PREFIX_PATH',
                os.path.basename(self.paths.images()),
                os.path.basename(disk_path),
            )
            return disk_rel_path, disk_metadata