Package com.cloud.storage

Examples of com.cloud.storage.VMTemplateHostVO


            throw new CloudRuntimeException(
                    format("Attempt to sync template id %1$s that is not attached to a host.",
                            templateId));
        }

        final VMTemplateHostVO templateHostRef = templateHostRefs.get(0);

        if (!isS3Enabled()) {
            return;
        }

        final S3TO s3 = getS3TO();
        if (s3 == null) {
            LOGGER.warn("S3 Template Sync Failed: Attempt to sync templates with S3, but no S3 instance defined.");
            return;
        }

        final HostVO secondaryHost = this.hostDao.findById(templateHostRef
                .getHostId());
        if (secondaryHost == null) {
            throw new CloudRuntimeException(format(
                    "Unable to find secondary storage host id %1$s.",
                    templateHostRef.getHostId()));
        }

        final Long dataCenterId = secondaryHost.getDataCenterId();
        final Long accountId = template.getAccountId();

        try {

            executeWithNoWaitLock(determineLockId(accountId, templateId),
                    new Callable<Void>() {

                @Override
                public Void call() throws Exception {

                    final UploadTemplateToS3FromSecondaryStorageCommand cmd = new UploadTemplateToS3FromSecondaryStorageCommand(
                            s3, secondaryHost.getStorageUrl(),
                            dataCenterId, accountId, templateId);

                    final Answer answer = agentManager.sendToSSVM(
                            dataCenterId, cmd);
                    if (answer == null || !answer.getResult()) {

                        final String reason = answer != null ? answer
                                .getDetails()
                                : "S3 template sync failed due to an unspecified error.";
                                throw new CloudRuntimeException(
                                        format("Failed to upload template id %1$s to S3 from secondary storage due to %2$s.",
                                                templateId, reason));

                    }

                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format(
                                "Creating VMTemplateS3VO instance using template id %1s.",
                                templateId));
                    }

                    final VMTemplateS3VO vmTemplateS3VO = new VMTemplateS3VO(
                            s3.getId(), templateId, now(),
                            templateHostRef.getSize(), templateHostRef
                            .getPhysicalSize());

                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Persisting %1$s",
                                vmTemplateS3VO));
View Full Code Here


  }

  @Override
  public boolean templateAvailable(long templateId, long hostId) {
    VMTemplateHostVO tmpltHost = findByHostTemplate(hostId, templateId);
    if (tmpltHost == null)
      return false;
   
    return tmpltHost.getDownloadState()==Status.DOWNLOADED;
  }
View Full Code Here

    for (HostVO secondaryStorageHost : secondaryStorageHosts) {
      long hostId = secondaryStorageHost.getId();
      long sZoneId = secondaryStorageHost.getDataCenterId();
      List<VMTemplateHostVO> templateHostVOs = _tmpltHostDao.listByHostTemplate(hostId, templateId);
      for (VMTemplateHostVO templateHostVO : templateHostVOs) {
        VMTemplateHostVO lock = _tmpltHostDao.acquireInLockTable(templateHostVO.getId());
        try {
          if (lock == null) {
            s_logger.debug("Failed to acquire lock when deleting templateHostVO with ID: " + templateHostVO.getId());
            success = false;
            break;
          }
          UsageEventUtils.publishUsageEvent(eventType, account.getId(), sZoneId, templateId, null, null, null);
                    templateHostVO.setDestroyed(true);
          _tmpltHostDao.update(templateHostVO.getId(), templateHostVO);
                    String installPath = templateHostVO.getInstallPath();
                    if (installPath != null) {
                        Answer answer = _agentMgr.sendToSecStorage(secondaryStorageHost, new DeleteTemplateCommand(secondaryStorageHost.getStorageUrl(), installPath));

                        if (answer == null || !answer.getResult()) {
                            s_logger.debug("Failed to delete " + templateHostVO + " due to " + ((answer == null) ? "answer is null" : answer.getDetails()));
                        } else {
                            _tmpltHostDao.remove(templateHostVO.getId());
                            s_logger.debug("Deleted template at: " + installPath);
                        }
                    } else {
                        _tmpltHostDao.remove(templateHostVO.getId());
                    }
          VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(sZoneId, templateId);
         
          if (templateZone != null) {
            _tmpltZoneDao.remove(templateZone.getId());
          }
        } finally {
          if (lock != null) {
            _tmpltHostDao.releaseFromLockTable(lock.getId());
          }
        }
      }
     
      if (!success) {
        break;
      }
    }
   
    s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
   
    // If there are no more non-destroyed template host entries for this template, delete it
    if (success && (_tmpltHostDao.listByTemplateId(templateId).size() == 0)) {
      long accountId = template.getAccountId();
     
      VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId);

      try {
        if (lock == null) {
          s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId);
          success = false;
        } else if (_tmpltDao.remove(templateId)) {
          // Decrement the number of templates
            _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
        }

      } finally {
        if (lock != null) {
          _tmpltDao.releaseFromLockTable(lock.getId());
        }
      }
     
      s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed.");
    }
View Full Code Here

       
        // Add the size for the templateForVmCreation if its not already present
        if ((templateForVmCreation != null) && !tmpinstalled) {
            // If the template that was passed into this allocator is not installed in the storage pool,
            // add 3 * (template size on secondary storage) to the running total
            VMTemplateHostVO templateHostVO = _storageMgr.findVmTemplateHost(templateForVmCreation.getId(), pool);

            if (templateHostVO == null) {
                VMTemplateSwiftVO templateSwiftVO = _swiftMgr.findByTmpltId(templateForVmCreation.getId());
                if (templateSwiftVO != null) {                                   
                    long templateSize = templateSwiftVO.getPhysicalSize();
                    if (templateSize == 0) {
                        templateSize = templateSwiftVO.getSize();
                    }
                    totalAllocatedSize += (templateSize + _extraBytesPerVolume);
                }
            } else {
                long templateSize = templateHostVO.getPhysicalSize();
                if ( templateSize == 0 ){
                    templateSize = templateHostVO.getSize();
                }
                totalAllocatedSize +=  (templateSize + _extraBytesPerVolume);
            }
        }
       
View Full Code Here

                return false;
            }

            boolean templateReady = false;
            if (template != null) {
                VMTemplateHostVO templateHostRef = _vmTemplateHostDao.findByHostTemplate(secHost.getId(), template.getId());
                templateReady = (templateHostRef != null) && (templateHostRef.getDownloadState() == Status.DOWNLOADED);
            }

            if (templateReady) {

                List<Pair<Long, Integer>> l = _storagePoolHostDao.getDatacenterStoragePoolHostInfo(dataCenterId, !_useLocalStorage);
View Full Code Here

 
  @Override
    public boolean copyTemplate(VMTemplateVO template, HostVO sourceServer, HostVO destServer) throws StorageUnavailableException{

    boolean downloadJobExists = false;
        VMTemplateHostVO destTmpltHost = null;
        VMTemplateHostVO srcTmpltHost = null;

        srcTmpltHost = _vmTemplateHostDao.findByHostTemplate(sourceServer.getId(), template.getId());
        if (srcTmpltHost == null) {
          throw new InvalidParameterValueException("Template " + template.getName() + " not associated with " + sourceServer.getName());
        }

        String url = generateCopyUrl(sourceServer, srcTmpltHost);
      if (url == null) {
      s_logger.warn("Unable to start/resume copy of template " + template.getUniqueName() + " to " + destServer.getName() + ", no secondary storage vm in running state in source zone");
      throw new CloudRuntimeException("No secondary VM in running state in zone " + sourceServer.getDataCenterId());
      }
        destTmpltHost = _vmTemplateHostDao.findByHostTemplate(destServer.getId(), template.getId());
        if (destTmpltHost == null) {
            destTmpltHost = new VMTemplateHostVO(destServer.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url);
            destTmpltHost.setCopy(true);
            destTmpltHost.setPhysicalSize(srcTmpltHost.getPhysicalSize());
            _vmTemplateHostDao.persist(destTmpltHost);
        } else if ((destTmpltHost.getJobId() != null) && (destTmpltHost.getJobId().length() > 2)) {
            downloadJobExists = true;
        }

        Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
        if (srcTmpltHost.getSize() > maxTemplateSizeInBytes){
          throw new CloudRuntimeException("Cant copy the template as the template's size " +srcTmpltHost.getSize()+
              " is greater than max.template.iso.size " + maxTemplateSizeInBytes);
        }
       
    if(destTmpltHost != null) {
        start();
            String sourceChecksum = _vmMgr.getChecksum(srcTmpltHost.getHostId(), srcTmpltHost.getInstallPath());
      DownloadCommand dcmd = 
              new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes);
      if (downloadJobExists) {
        dcmd = new DownloadProgressCommand(dcmd, destTmpltHost.getJobId(), RequestType.GET_OR_RESTART);
       }
View Full Code Here

    return null;
  }

  private void downloadTemplateToStorage(VMTemplateVO template, HostVO sserver) {
    boolean downloadJobExists = false;
        VMTemplateHostVO vmTemplateHost = null;

        vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(sserver.getId(), template.getId());
        if (vmTemplateHost == null) {
            vmTemplateHost = new VMTemplateHostVO(sserver.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUrl());
            _vmTemplateHostDao.persist(vmTemplateHost);
        } else if ((vmTemplateHost.getJobId() != null) && (vmTemplateHost.getJobId().length() > 2)) {
            downloadJobExists = true;
        }
               
        Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
        String secUrl = sserver.getStorageUrl();
    if(vmTemplateHost != null) {
        start();
      DownloadCommand dcmd =
             new DownloadCommand(secUrl, template, maxTemplateSizeInBytes);
          if (downloadJobExists) {
              dcmd = new DownloadProgressCommand(dcmd, vmTemplateHost.getJobId(), RequestType.GET_OR_RESTART);
          }
            dcmd.setProxy(getHttpProxy());

      if (vmTemplateHost.isCopy()) {
        dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
      }
      HostVO ssAhost = _ssvmMgr.pickSsvmHost(sserver);
      if( ssAhost == null ) {
               s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName());
               return;
      }
      DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd, _templateDao);
      if (downloadJobExists) {
        dl.setCurrState(vmTemplateHost.getDownloadState());
       }
            DownloadListener old = null;
            synchronized (_listenerMap) {
                old = _listenerMap.put(vmTemplateHost, dl);
            }
View Full Code Here

  }

  @DB
  public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) {
    if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus==Status.ABANDONED)){
      VMTemplateHostVO vmTemplateHost = new VMTemplateHostVO(host.getId(), template.getId());
          synchronized (_listenerMap) {
              _listenerMap.remove(vmTemplateHost);
          }
    }
   
    VMTemplateHostVO vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(host.getId(), template.getId());
   
    Transaction txn = Transaction.currentTxn();
        txn.start();   

        if (dnldStatus == Status.DOWNLOADED) {
            long size = -1;
            if(vmTemplateHost!=null){
                size = vmTemplateHost.getPhysicalSize();
            }
            else{
                s_logger.warn("Failed to get size for template" + template.getName());
            }
            String eventType = EventTypes.EVENT_TEMPLATE_CREATE;
View Full Code Here

                  toBeDownloaded.add(builtinTmplt);
              }
          }
   
          for (VMTemplateVO template: toBeDownloaded) {
              VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(ssHost.getId(), template.getId());
              if (tmpltHost == null || tmpltHost.getDownloadState() != Status.DOWNLOADED) {
                  downloadTemplateToStorage(template, ssHost);
              }
          }
        }
  }
View Full Code Here

      for ( VMTemplateVO tmplt : rtngTmplts ) {
          TemplateInfo tmpltInfo = templateInfos.get(tmplt.getUniqueName());
          if ( tmpltInfo == null ) {
              continue;
          }
          VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(hostId, tmplt.getId());
          if ( tmpltHost == null ) {
                tmpltHost = new VMTemplateHostVO(hostId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl());
              tmpltHost.setSize(tmpltInfo.getSize());
              tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
              _vmTemplateHostDao.persist(tmpltHost);
          }
      }
  }
View Full Code Here

TOP

Related Classes of com.cloud.storage.VMTemplateHostVO

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.