Search in sources :

Example 6 with CreatePrivateTemplateAnswer

use of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
    final String templateInstallFolder = "template/tmpl/" + templateFolder;
    final String tmplName = libvirtUtilitiesHelper.generateUuidName();
    final String tmplFileName = tmplName + ".qcow2";
    KvmStoragePool secondaryPool = null;
    KvmStoragePool snapshotPool = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        String snapshotPath = command.getSnapshotUuid();
        final int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        snapshotPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
        secondaryPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl());
        final KvmPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName());
        final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(templatePath);
        final String tmplPath = templateInstallFolder + File.separator + tmplFileName;
        final String createTmplPath = libvirtComputingResource.createTmplPath();
        final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
        final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
        scriptCommand.add("-t", templatePath);
        scriptCommand.add("-n", tmplFileName);
        scriptCommand.add("-f", snapshot.getPath());
        scriptCommand.execute();
        final Processor qcow2Processor = libvirtUtilitiesHelper.buildQcow2Processor(storage);
        final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
        final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath);
        loc.create(1, true, tmplName);
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } finally {
        if (secondaryPool != null) {
            storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
        if (snapshotPool != null) {
            storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid());
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 7 with CreatePrivateTemplateAnswer

use of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected CreatePrivateTemplateAnswer execute(final CreatePrivateTemplateFromSnapshotCommand cmd) {
    String templateFolder = cmd.getAccountId() + File.separator + cmd.getNewTemplateId();
    String templateInstallFolder = "template/tmpl/" + templateFolder;
    String tmplName = UUID.randomUUID().toString();
    String tmplFileName = tmplName + ".qcow2";
    KVMStoragePool secondaryPool = null;
    KVMStoragePool snapshotPool = null;
    try {
        String snapshotPath = cmd.getSnapshotUuid();
        int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        snapshotPool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl() + snapshotPath);
        KVMPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(cmd.getSnapshotName());
        secondaryPool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl());
        String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
        _storage.mkdirs(templatePath);
        String tmplPath = templateInstallFolder + File.separator + tmplFileName;
        Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
        command.add("-t", templatePath);
        command.add("-n", tmplFileName);
        command.add("-f", snapshot.getPath());
        command.execute();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
        TemplateLocation loc = new TemplateLocation(_storage, templatePath);
        loc.create(1, true, tmplName);
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(cmd, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
    } catch (ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } catch (InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } catch (IOException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } catch (CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } finally {
        if (secondaryPool != null) {
            secondaryPool.delete();
        }
        if (snapshotPool != null) {
            snapshotPool.delete();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 8 with CreatePrivateTemplateAnswer

use of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer in project CloudStack-archive by CloudStack-extras.

the class MockStorageManagerImpl method CreatePrivateTemplateFromVolume.

@Override
public CreatePrivateTemplateAnswer CreatePrivateTemplateFromVolume(CreatePrivateTemplateFromVolumeCommand cmd) {
    MockVolumeVO volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
    if (volume == null) {
        return new CreatePrivateTemplateAnswer(cmd, false, "cant' find volume" + cmd.getVolumePath());
    }
    MockSecStorageVO sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
    if (sec == null) {
        return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
    }
    MockVolumeVO template = new MockVolumeVO();
    String uuid = UUID.randomUUID().toString();
    template.setName(uuid);
    template.setPath(sec.getMountPoint() + uuid);
    template.setPoolId(sec.getId());
    template.setSize(volume.getSize());
    template.setStatus(Status.DOWNLOADED);
    template.setType(MockVolumeType.TEMPLATE);
    template = _mockVolumeDao.persist(template);
    return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(), template.getSize(), template.getName(), ImageFormat.QCOW2);
}
Also used : CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO)

Example 9 with CreatePrivateTemplateAnswer

use of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtCreatePrivateTemplateFromVolumeCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String secondaryStorageUrl = command.getSecondaryStorageUrl();
    KvmStoragePool secondaryStorage = null;
    KvmStoragePool primary = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final String templateFolder = command.getAccountId() + File.separator + command.getTemplateId() + File.separator;
        final String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
        try {
            primary = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primary = storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), command.getPool().getUserInfo(), command.getPool().getType());
            } else {
                return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
            }
        }
        final KvmPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(tmpltPath);
        if (primary.getType() != StoragePoolType.RBD) {
            final String createTmplPath = libvirtComputingResource.createTmplPath();
            final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
            final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
            scriptCommand.add("-f", disk.getPath());
            scriptCommand.add("-t", tmpltPath);
            scriptCommand.add("-n", command.getUniqueName() + ".qcow2");
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("failed to create template: " + result);
                return new CreatePrivateTemplateAnswer(command, false, result);
            }
        } else {
            s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + command.getUniqueName());
            final QemuImgFile srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
            srcFile.setFormat(PhysicalDiskFormat.RAW);
            final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + command.getUniqueName() + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(0);
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException e) {
                s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + command.getUniqueName() + ".qcow2" + System.getProperty("line.separator");
            final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
            final Date date = new Date();
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
            try (FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes("UTF-8"));
                templFo.flush();
            } catch (final IOException ex) {
                s_logger.error("CreatePrivateTemplateAnswer:Exception:" + ex.getMessage());
            }
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, storage);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final FormatInfo info = qcow2Processor.process(tmpltPath, null, command.getUniqueName());
        final TemplateLocation loc = new TemplateLocation(storage, tmpltPath);
        loc.create(1, true, command.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, null, templateInstallFolder + command.getUniqueName() + ".qcow2", info.virtualSize, info.size, command.getUniqueName(), ImageFormat.QCOW2);
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid());
        }
    }
}
Also used : KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) HashMap(java.util.HashMap) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateLocation(com.cloud.storage.template.TemplateLocation) QemuImgException(com.cloud.utils.qemu.QemuImgException) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) Script(com.cloud.utils.script.Script) IOException(java.io.IOException) InternalErrorException(com.cloud.exception.InternalErrorException) Date(java.util.Date) QemuImg(com.cloud.utils.qemu.QemuImg) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) QemuImgFile(com.cloud.utils.qemu.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) QemuImgFile(com.cloud.utils.qemu.QemuImgFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 10 with CreatePrivateTemplateAnswer

use of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer in project cloudstack by apache.

the class MockStorageManagerImpl method CreatePrivateTemplateFromSnapshot.

@Override
public CreatePrivateTemplateAnswer CreatePrivateTemplateFromSnapshot(CreatePrivateTemplateFromSnapshotCommand cmd) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    MockVolumeVO snapshot = null;
    MockSecStorageVO sec = null;
    try {
        txn.start();
        String snapshotUUId = cmd.getSnapshotUuid();
        snapshot = _mockVolumeDao.findByName(snapshotUUId);
        if (snapshot == null) {
            snapshotUUId = cmd.getSnapshotName();
            snapshot = _mockVolumeDao.findByName(snapshotUUId);
            if (snapshot == null) {
                return new CreatePrivateTemplateAnswer(cmd, false, "can't find snapshot:" + snapshotUUId);
            }
        }
        sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
        if (sec == null) {
            return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
        }
        txn.commit();
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    MockVolumeVO template = new MockVolumeVO();
    String uuid = UUID.randomUUID().toString();
    template.setName(uuid);
    template.setPath(sec.getMountPoint() + uuid);
    template.setPoolId(sec.getId());
    template.setSize(snapshot.getSize());
    template.setStatus(Status.DOWNLOADED);
    template.setType(MockVolumeType.TEMPLATE);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        template = _mockVolumeDao.persist(template);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when saving template " + template, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(), template.getSize(), template.getName(), ImageFormat.QCOW2);
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)10 ConfigurationException (javax.naming.ConfigurationException)9 IOException (java.io.IOException)7 InternalErrorException (com.cloud.exception.InternalErrorException)6 Processor (com.cloud.storage.template.Processor)6 FormatInfo (com.cloud.storage.template.Processor.FormatInfo)6 TemplateLocation (com.cloud.storage.template.TemplateLocation)6 Script (com.cloud.utils.script.Script)6 MockSecStorageVO (com.cloud.simulator.MockSecStorageVO)4 MockVolumeVO (com.cloud.simulator.MockVolumeVO)4 StorageLayer (com.cloud.storage.StorageLayer)4 QCOW2Processor (com.cloud.storage.template.QCOW2Processor)4 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)3 KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)2 KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)2 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)2 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)2