Package com.cloud.agent.api.storage

Examples of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer


            secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
            installPath = "template/tmpl/" + accountId + "/" + newTemplateId;
            if( !createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
                details = " Filed to create folder " + installPath + " in secondary storage";
                s_logger.warn(details);
                return new CreatePrivateTemplateAnswer(cmd, false, details);
            }
            String templatePath = secondaryStorageMountPath + "/" + installPath;
            // create snapshot SR
            String filename = backedUpSnapshotUuid;
            if ( !filename.startsWith("VHD-") && !filename.endsWith(".vhd")) {
                filename = backedUpSnapshotUuid + ".vhd";
            }
            String snapshotPath = secondaryStorageMountPath + "/snapshots/" + accountId + "/" + volumeId + "/" + filename;
            String results = createTemplateFromSnapshot(conn, templatePath, snapshotPath, wait);
            String[] tmp = results.split("#");
            String tmpltUuid = tmp[1];
            long physicalSize = Long.parseLong(tmp[2]);
            long virtualSize = Long.parseLong(tmp[3]) * 1024 * 1024;
            String tmpltFilename = tmpltUuid + ".vhd";

            // create the template.properties file
            result = postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUuid, userSpecifiedName, null, physicalSize, virtualSize, newTemplateId);
            if (!result) {
                throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + templatePath);
            }
            installPath = installPath + "/" + tmpltFilename;
            return new CreatePrivateTemplateAnswer(cmd, true, null, installPath, virtualSize, physicalSize, tmpltUuid, ImageFormat.VHD);
        } catch (Exception e) {
            if (secondaryStorageMountPath != null) {
                deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
            }
            details = "Creating template from snapshot " + backedUpSnapshotUuid + " failed due to " + e.toString();
            s_logger.error(details, e);
        }
        return new CreatePrivateTemplateAnswer(cmd, result, details);
    }
View Full Code Here


      URI uri;
            uri = new URI(secondaryStorageUrl);
      String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
      String installPath = "template/tmpl/" + accountId + "/" + templateId;
      Map<String, String> res = OvmStoragePool.createTemplateFromVolume(_conn, secondaryStorageMountPath, installPath, volumePath, wait);
      return new CreatePrivateTemplateAnswer(cmd, true, null, res.get("installPath"), Long.valueOf(res.get("virtualSize")), Long.valueOf(res.get("physicalSize")), res.get("templateFileName"), ImageFormat.RAW);
    } catch (Exception e) {
      s_logger.debug("Create template failed", e);
      return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    }
  }
View Full Code Here

      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 = Transaction.open(Transaction.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 = Transaction.open(Transaction.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 = Transaction.open(Transaction.CLOUD_DB);
            txn.close();
    }

    return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(),
        template.getSize(), template.getName(), ImageFormat.QCOW2);
  }
View Full Code Here

    MockSecStorageVO sec = null;
    try {
      txn.start();
      volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
      if (volume == null) {
        return new CreatePrivateTemplateAnswer(cmd, false, "cant' find volume" + cmd.getVolumePath());
      }

      sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
      if (sec == null) {
        return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
      }
      txn.commit();
    } catch (Exception ex) {
      txn.rollback();
      throw new CloudRuntimeException("Error when creating private template from volume");
    } finally {
      txn.close();
            txn = Transaction.open(Transaction.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(volume.getSize());
    template.setStatus(Status.DOWNLOADED);
    template.setType(MockVolumeType.TEMPLATE);
    txn = Transaction.open(Transaction.SIMULATOR_DB);
    try {
      txn.start();
      template = _mockVolumeDao.persist(template);
      txn.commit();
    } catch (Exception ex) {
      txn.rollback();
      throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when persisting template "
          + template.getName(), ex);
    } finally {
      txn.close();
            txn = Transaction.open(Transaction.CLOUD_DB);
            txn.close();
    }

    return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(),
        template.getSize(), template.getName(), ImageFormat.QCOW2);
  }
View Full Code Here

            }
            // FIXME: before sending the command, check if there's enough capacity
            // on the storage server to create the template

            // This can be sent to a KVM host too.
            CreatePrivateTemplateAnswer answer = null;
            if (snapshotId != null) {
                if (!_snapshotDao.lockInLockTable(snapshotId.toString(), 10)) {
                    throw new CloudRuntimeException("Creating template from snapshot failed due to snapshot:" + snapshotId + " is being used, try it later ");
                }
            } else {
                if (!_volsDao.lockInLockTable(volumeId.toString(), 10)) {
                    throw new CloudRuntimeException("Creating template from volume failed due to volume:" + volumeId + " is being used, try it later ");
                }
            }
            try {
                answer = (CreatePrivateTemplateAnswer) _storageMgr.sendToPool(pool, cmd);
            } catch (StorageUnavailableException e) {
            } finally {
                if (snapshotId != null) {
                    _snapshotDao.unlockFromLockTable(snapshotId.toString());
                } else {
                    _volsDao.unlockFromLockTable(volumeId.toString());
                }
            }
            if ((answer != null) && answer.getResult()) {
                privateTemplate = _templateDao.findById(templateId);
                String answerUniqueName = answer.getUniqueName();
                if (answerUniqueName != null) {
                    privateTemplate.setUniqueName(answerUniqueName);
                } else {
                    privateTemplate.setUniqueName(uniqueName);
                }
                ImageFormat format = answer.getImageFormat();
                if (format != null) {
                    privateTemplate.setFormat(format);
                } else {
                    // This never occurs.
                    // Specify RAW format makes it unusable for snapshots.
                    privateTemplate.setFormat(ImageFormat.RAW);
                }

                String checkSum = getChecksum(secondaryStorageHost.getId(), answer.getPath());

                Transaction txn = Transaction.currentTxn();

                txn.start();

                privateTemplate.setChecksum(checkSum);
                _templateDao.update(templateId, privateTemplate);

                // add template zone ref for this template
                _templateDao.addTemplateToZone(privateTemplate, zoneId);
                VMTemplateHostVO templateHostVO = new VMTemplateHostVO(secondaryStorageHost.getId(), templateId);
                templateHostVO.setDownloadPercent(100);
                templateHostVO.setDownloadState(Status.DOWNLOADED);
                templateHostVO.setInstallPath(answer.getPath());
                templateHostVO.setLastUpdated(new Date());
                templateHostVO.setSize(answer.getVirtualSize());
                templateHostVO.setPhysicalSize(answer.getphysicalSize());
                _templateHostDao.persist(templateHostVO);

                UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), secondaryStorageHost.getDataCenterId(), privateTemplate.getId(),
                        privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), templateHostVO.getSize());
                _usageEventDao.persist(usageEvent);
View Full Code Here

            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) {
                _storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
            }
            if (snapshotPool != null) {
View Full Code Here

                    primary = _storagePoolMgr.createStoragePool(cmd.getPool().getUuid(),
                                      cmd.getPool().getHost(), cmd.getPool().getPort(),
                                      cmd.getPool().getPath(), cmd.getPool().getUserInfo(),
                                      cmd.getPool().getType());
                } else {
                    return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
                }
            }

            KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
            String tmpltPath = secondaryStorage.getLocalPath() + File.separator
                    + templateInstallFolder;
            _storage.mkdirs(tmpltPath);

            if (primary.getType() != StoragePoolType.RBD) {
                Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
                command.add("-f", disk.getPath());
                command.add("-t", tmpltPath);
                command.add("-n", cmd.getUniqueName() + ".qcow2");

                String result = command.execute();

                if (result != null) {
                    s_logger.debug("failed to create template: " + result);
                    return new CreatePrivateTemplateAnswer(cmd, false, result);
                }
            } else {
                s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + cmd.getUniqueName());
                Script.runSimpleBashScript("qemu-img convert"
                        + " -f raw -O qcow2 "
                        + KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(),
                                primary.getSourcePort(),
                                primary.getAuthUserName(),
                                primary.getAuthSecret(),
                                disk.getPath())
                                + " " + tmpltPath + "/" + cmd.getUniqueName() + ".qcow2");
                File templateProp = new File(tmpltPath + "/template.properties");
                if (!templateProp.exists()) {
                    templateProp.createNewFile();
                }

                String templateContent = "filename=" + cmd.getUniqueName() + ".qcow2" + System.getProperty("line.separator");

                DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
                Date date = new Date();
                templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");

                FileOutputStream templFo = new FileOutputStream(templateProp);
                templFo.write(templateContent.getBytes());
                templFo.flush();
                templFo.close();
            }

            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(tmpltPath, null,
                    cmd.getUniqueName());

            TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
            loc.create(1, true, cmd.getUniqueName());
            loc.addFormat(info);
            loc.save();

            return new CreatePrivateTemplateAnswer(cmd, true, null,
                    templateInstallFolder + cmd.getUniqueName() + ".qcow2",
                    info.virtualSize, info.size, cmd.getUniqueName(),
                    ImageFormat.QCOW2);
        } catch (LibvirtException e) {
            s_logger.debug("Failed to get secondary storage pool: "
                    + e.toString());
            return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
        } catch (InternalErrorException e) {
            return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
        } catch (IOException e) {
            return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
        } catch (ConfigurationException e) {
            return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
        } catch (CloudRuntimeException e) {
            return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
        } finally {
            if (secondaryStorage != null) {
                _storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid());
            }
        }
View Full Code Here

            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 = Transaction.open(Transaction.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 = Transaction.open(Transaction.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 = Transaction.open(Transaction.CLOUD_DB);
            txn.close();
        }

        return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(),
                template.getSize(), template.getName(), ImageFormat.QCOW2);
    }
View Full Code Here

        MockSecStorageVO sec = null;
        try {
            txn.start();
            volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
            if (volume == null) {
                return new CreatePrivateTemplateAnswer(cmd, false, "cant' find volume" + cmd.getVolumePath());
            }

            sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
            if (sec == null) {
                return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
            }
            txn.commit();
        } catch (Exception ex) {
            txn.rollback();
            throw new CloudRuntimeException("Error when creating private template from volume");
        } finally {
            txn.close();
            txn = Transaction.open(Transaction.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(volume.getSize());
        template.setStatus(Status.DOWNLOADED);
        template.setType(MockVolumeType.TEMPLATE);
        txn = Transaction.open(Transaction.SIMULATOR_DB);
        try {
            txn.start();
            template = _mockVolumeDao.persist(template);
            txn.commit();
        } catch (Exception ex) {
            txn.rollback();
            throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when persisting template "
                    + template.getName(), ex);
        } finally {
            txn.close();
            txn = Transaction.open(Transaction.CLOUD_DB);
            txn.close();
        }

        return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(),
                template.getSize(), template.getName(), ImageFormat.QCOW2);
    }
View Full Code Here

            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) {
                _storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
            }
            if (snapshotPool != null) {
View Full Code Here

TOP

Related Classes of com.cloud.agent.api.storage.CreatePrivateTemplateAnswer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.