@Override
 public void addSystemVMTemplatesToSecondary(DataStore store) {
   long storeId = store.getId();
   List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
   for (VMTemplateVO tmplt : rtngTmplts) {
     TemplateDataStoreVO tmpltStore =
         _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId());
     if (tmpltStore == null) {
       tmpltStore =
           new TemplateDataStoreVO(
               storeId,
               tmplt.getId(),
               new Date(),
               100,
               Status.DOWNLOADED,
               null,
               null,
               null,
               TemplateConstants.DEFAULT_SYSTEM_VM_TEMPLATE_PATH + tmplt.getId() + File.separator,
               tmplt.getUrl());
       tmpltStore.setSize(0L);
       tmpltStore.setPhysicalSize(0); // no size information for
       // pre-seeded system vm templates
       tmpltStore.setDataStoreRole(store.getRole());
       _vmTemplateStoreDao.persist(tmpltStore);
     }
   }
 }
 private void createVmwareToolsIso() {
   String isoName = "vmware-tools.iso";
   VMTemplateVO tmplt = _tmpltDao.findByTemplateName(isoName);
   Long id;
   if (tmplt == null) {
     id = _tmpltDao.getNextInSequence(Long.class, "id");
     VMTemplateVO template =
         new VMTemplateVO(
             id,
             isoName,
             isoName,
             ImageFormat.ISO,
             true,
             true,
             TemplateType.PERHOST,
             null,
             null,
             true,
             64,
             Account.ACCOUNT_ID_SYSTEM,
             null,
             "VMware Tools Installer ISO",
             false,
             1,
             false,
             HypervisorType.VMware);
     _tmpltDao.persist(template);
   } else {
     id = tmplt.getId();
     tmplt.setTemplateType(TemplateType.PERHOST);
     tmplt.setUrl(null);
     _tmpltDao.update(id, tmplt);
   }
 }
  private String generateCopyUrl(TemplateInfo srcTemplate) {
    DataStore srcStore = srcTemplate.getDataStore();
    EndPoint ep = _epSelector.select(srcTemplate);
    if (ep != null) {
      if (ep.getPublicAddr() == null) {
        s_logger.warn("A running secondary storage vm has a null public ip?");
        return null;
      }
      return generateCopyUrl(
          ep.getPublicAddr(),
          ((ImageStoreEntity) srcStore).getMountPoint(),
          srcTemplate.getInstallPath());
    }

    VMTemplateVO tmplt = _templateDao.findById(srcTemplate.getId());
    HypervisorType hyperType = tmplt.getHypervisorType();
    /*No secondary storage vm yet*/
    if (hyperType != null && hyperType == HypervisorType.KVM) {
      return "file://"
          + ((ImageStoreEntity) srcStore).getMountPoint()
          + "/"
          + srcTemplate.getInstallPath();
    }
    return null;
  }
Esempio n. 4
0
  public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
    Long templateId = cmd.getId();
    Long userId = UserContext.current().getCallerUserId();
    Account account = UserContext.current().getCaller();
    Long zoneId = cmd.getZoneId();

    VMTemplateVO template = _tmpltDao.findById(templateId.longValue());
    if (template == null) {
      throw new InvalidParameterValueException("unable to find template with id " + templateId);
    }

    userId =
        accountAndUserValidation(account, userId, null, template, "Unable to delete template ");

    UserVO user = _userDao.findById(userId);
    if (user == null) {
      throw new InvalidParameterValueException("Please specify a valid user.");
    }

    if (template.getFormat() == ImageFormat.ISO) {
      throw new InvalidParameterValueException("Please specify a valid template.");
    }

    return new TemplateProfile(userId, template, zoneId);
  }
 @Override
 public void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateInfo> templateInfos) {
   if (templateInfos == null) {
     return;
   }
   Long hostId = host.getId();
   List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
   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);
     }
   }
 }
  @Test
  public void testCreateTemplateFromVolume() {
    DataStore primaryStore = createPrimaryDataStore();
    primaryStoreId = primaryStore.getId();
    primaryStore = this.dataStoreMgr.getPrimaryDataStore(primaryStoreId);
    VolumeVO volume = createVolume(null, primaryStore.getId());
    VolumeInfo volInfo = this.volFactory.getVolume(volume.getId());
    AsyncCallFuture<VolumeApiResult> future =
        this.volumeService.createVolumeAsync(volInfo, primaryStore);
    try {
      VolumeApiResult result = future.get();

      AssertJUnit.assertTrue(result.isSuccess());
      volInfo = result.getVolume();
      VMTemplateVO templateVO = createTemplateInDb();
      TemplateInfo tmpl = this.templateFactory.getTemplate(templateVO.getId(), DataStoreRole.Image);
      DataStore imageStore = this.dataStoreMgr.getImageStore(this.dcId);

      this.imageService.createTemplateFromVolumeAsync(volInfo, tmpl, imageStore);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  @Override
  public boolean remove(Long id) {
    VMTemplateVO template = createForUpdate();
    template.setRemoved(new Date());

    return update(id, template);
  }
 private void initiateTemplateDownload(Long templateId, HostVO ssHost) {
   VMTemplateVO template = _templateDao.findById(templateId);
   if (template != null && (template.getUrl() != null)) {
     // find all storage hosts and tell them to initiate download
     downloadTemplateToStorage(template, ssHost);
   }
 }
  @Override
  public VMTemplateVO create(TemplateProfile profile) {
    VMTemplateVO template = persistTemplate(profile);
    Long zoneId = profile.getZoneId();

    // create an entry at template_store_ref with store_id = null to represent that this template is
    // ready for use.
    TemplateDataStoreVO vmTemplateHost =
        new TemplateDataStoreVO(
            null,
            template.getId(),
            new Date(),
            100,
            Status.DOWNLOADED,
            null,
            null,
            null,
            null,
            template.getUrl());
    this._tmpltStoreDao.persist(vmTemplateHost);

    if (zoneId == null || zoneId == -1) {
      List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
      if (dcs != null && dcs.size() > 0) {
        templateCreateUsage(template, dcs.get(0).getId());
      }
    } else {
      templateCreateUsage(template, zoneId);
    }

    _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
    return template;
  }
Esempio n. 10
0
 private TemplateInfo createTemplate() {
   try {
     DataStore store = createImageStore();
     VMTemplateVO image = createImageData();
     TemplateInfo template = imageDataFactory.getTemplate(image.getId(), store);
     // AsyncCallFuture<TemplateApiResult> future =
     // imageService.createTemplateAsync(template, store);
     // future.get();
     template = imageDataFactory.getTemplate(image.getId(), store);
     /*
      * imageProviderMgr.configure("image Provider", new HashMap<String,
      * Object>()); VMTemplateVO image = createImageData();
      * ImageDataStoreProvider defaultProvider =
      * imageProviderMgr.getProvider("DefaultProvider");
      * ImageDataStoreLifeCycle lifeCycle =
      * defaultProvider.getLifeCycle(); ImageDataStore store =
      * lifeCycle.registerDataStore("defaultHttpStore", new
      * HashMap<String, String>());
      * imageService.registerTemplate(image.getId(),
      * store.getImageDataStoreId()); TemplateEntity te =
      * imageService.getTemplateEntity(image.getId()); return te;
      */
     return template;
   } catch (Exception e) {
     Assert.fail("failed", e);
     return null;
   }
 }
  protected Void createTemplateAsyncCallback(
      AsyncCallbackDispatcher<? extends BaseImageStoreDriverImpl, DownloadAnswer> callback,
      CreateContext<CreateCmdResult> context) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Performing image store createTemplate async callback");
    }
    DownloadAnswer answer = callback.getResult();
    DataObject obj = context.data;
    DataStore store = obj.getDataStore();

    TemplateDataStoreVO tmpltStoreVO =
        _templateStoreDao.findByStoreTemplate(store.getId(), obj.getId());
    if (tmpltStoreVO != null) {
      if (tmpltStoreVO.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              "Template is already in DOWNLOADED state, ignore further incoming DownloadAnswer");
        }
        return null;
      }
      TemplateDataStoreVO updateBuilder = _templateStoreDao.createForUpdate();
      updateBuilder.setDownloadPercent(answer.getDownloadPct());
      updateBuilder.setDownloadState(answer.getDownloadStatus());
      updateBuilder.setLastUpdated(new Date());
      updateBuilder.setErrorString(answer.getErrorString());
      updateBuilder.setJobId(answer.getJobId());
      updateBuilder.setLocalDownloadPath(answer.getDownloadPath());
      updateBuilder.setInstallPath(answer.getInstallPath());
      updateBuilder.setSize(answer.getTemplateSize());
      updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize());
      _templateStoreDao.update(tmpltStoreVO.getId(), updateBuilder);
      // update size in vm_template table
      VMTemplateVO tmlptUpdater = _templateDao.createForUpdate();
      tmlptUpdater.setSize(answer.getTemplateSize());
      _templateDao.update(obj.getId(), tmlptUpdater);
    }

    AsyncCompletionCallback<CreateCmdResult> caller = context.getParentCallback();

    if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR
        || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.ABANDONED
        || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.UNKNOWN) {
      CreateCmdResult result = new CreateCmdResult(null, null);
      result.setSuccess(false);
      result.setResult(answer.getErrorString());
      caller.complete(result);
    } else if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
      if (answer.getCheckSum() != null) {
        VMTemplateVO templateDaoBuilder = _templateDao.createForUpdate();
        templateDaoBuilder.setChecksum(answer.getCheckSum());
        _templateDao.update(obj.getId(), templateDaoBuilder);
      }

      CreateCmdResult result = new CreateCmdResult(null, null);
      caller.complete(result);
    }
    return null;
  }
  // update template_zone_ref for cross-zone template for newly added zone
  @Override
  public void associateCrosszoneTemplatesToZone(long dcId) {
    VMTemplateZoneVO tmpltZone;

    List<VMTemplateVO> allTemplates = _templateDao.listAll();
    for (VMTemplateVO vt : allTemplates) {
      if (vt.isCrossZones()) {
        tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
        if (tmpltZone == null) {
          VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
          _vmTemplateZoneDao.persist(vmTemplateZone);
        }
      }
    }
  }
 private void templateCreateUsage(VMTemplateVO template, long dcId) {
   if (template.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
     UsageEventVO usageEvent =
         new UsageEventVO(
             EventTypes.EVENT_TEMPLATE_CREATE,
             template.getAccountId(),
             dcId,
             template.getId(),
             template.getName(),
             null,
             template.getSourceTemplateId(),
             0L);
     _usageEventDao.persist(usageEvent);
   }
 }
Esempio n. 14
0
  // @Test
  public void testCopyBaseImage() {
    DataStore primaryStore = createPrimaryDataStore();
    primaryStoreId = primaryStore.getId();
    primaryStore = this.dataStoreMgr.getPrimaryDataStore(primaryStoreId);
    VolumeVO volume = createVolume(image.getId(), primaryStore.getId());
    VolumeInfo volInfo = this.volFactory.getVolume(volume.getId());
    AsyncCallFuture<VolumeApiResult> future =
        this.volumeService.createVolumeFromTemplateAsync(
            volInfo,
            this.primaryStoreId,
            this.templateFactory.getTemplate(this.image.getId(), DataStoreRole.Image));
    try {
      VolumeApiResult result = future.get();

      AssertJUnit.assertTrue(result.isSuccess());

      VolumeInfo newVol = result.getVolume();
      this.volumeService.destroyVolume(newVol.getId());
      VolumeInfo vol = this.volFactory.getVolume(volume.getId());
      this.volumeService.expungeVolumeAsync(vol);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ConcurrentOperationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Esempio n. 15
0
  @Override
  public long getAllocatedPoolCapacity(StoragePoolVO pool, VMTemplateVO templateForVmCreation) {

    // Get size for all the volumes
    Pair<Long, Long> sizes = _volumeDao.getCountAndTotalByPool(pool.getId());
    long totalAllocatedSize = sizes.second() + sizes.first() * _extraBytesPerVolume;

    // Get size for VM Snapshots
    totalAllocatedSize = totalAllocatedSize + getVMSnapshotAllocatedCapacity(pool);

    // Iterate through all templates on this storage pool
    boolean tmpinstalled = false;
    List<VMTemplateStoragePoolVO> templatePoolVOs;
    templatePoolVOs = _templatePoolDao.listByPoolId(pool.getId());

    for (VMTemplateStoragePoolVO templatePoolVO : templatePoolVOs) {
      if ((templateForVmCreation != null)
          && !tmpinstalled
          && (templatePoolVO.getTemplateId() == templateForVmCreation.getId())) {
        tmpinstalled = true;
      }
      long templateSize = templatePoolVO.getTemplateSize();
      totalAllocatedSize += templateSize + _extraBytesPerVolume;
    }

    // Add the size for the templateForVmCreation if its not already present
    /*if ((templateForVmCreation != null) && !tmpinstalled) {

    }*/

    return totalAllocatedSize;
  }
  public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
    TemplateProfile profile = super.prepareDelete(cmd);
    VMTemplateVO template = profile.getTemplate();
    Long zoneId = profile.getZoneId();

    if (template.getTemplateType() == TemplateType.SYSTEM) {
      throw new InvalidParameterValueException("The DomR template cannot be deleted.");
    }

    if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) {
      throw new InvalidParameterValueException(
          "Failed to find a secondary storage host in the specified zone.");
    }

    return profile;
  }
  public boolean isZoneReady(Map<Long, ZoneHostInfo> zoneHostInfoMap, long dataCenterId) {
    ZoneHostInfo zoneHostInfo = zoneHostInfoMap.get(dataCenterId);
    if (zoneHostInfo != null
        && (zoneHostInfo.getFlags() & RunningHostInfoAgregator.ZoneHostInfo.ROUTING_HOST_MASK)
            != 0) {
      VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId);
      HostVO secHost = _hostDao.findSecondaryStorageHost(dataCenterId);
      if (secHost == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              "No secondary storage available in zone "
                  + dataCenterId
                  + ", wait until it is ready to launch secondary storage vm");
        }
        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);
        if (l != null && l.size() > 0 && l.get(0).second().intValue() > 0) {

          return true;
        } else {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug(
                "Primary storage is not ready, wait until it is ready to launch secondary storage vm");
          }
        }
      } else {
        if (s_logger.isTraceEnabled()) {
          s_logger.trace("Zone host is ready, but secondary storage vm template is not ready");
        }
      }
    }
    return false;
  }
  @Override
  @DB
  public long addTemplateToZone(VMTemplateVO tmplt, long zoneId) {
    Transaction txn = Transaction.currentTxn();
    txn.start();
    VMTemplateVO tmplt2 = findById(tmplt.getId());
    if (tmplt2 == null) {
      if (persist(tmplt) == null) {
        throw new CloudRuntimeException("Failed to persist the template " + tmplt);
      }
      if (tmplt.getDetails() != null) {
        _templateDetailsDao.persist(tmplt.getId(), tmplt.getDetails());
      }
    }
    VMTemplateZoneVO tmpltZoneVO = _templateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
    if (tmpltZoneVO == null) {
      tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
      _templateZoneDao.persist(tmpltZoneVO);
    } else {
      tmpltZoneVO.setRemoved(null);
      tmpltZoneVO.setLastUpdated(new Date());
      _templateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
    }
    txn.commit();

    return tmplt.getId();
  }
  public VMTemplateVO findSystemVMTemplate(long zoneId, HypervisorType hType) {
    SearchCriteria<VMTemplateVO> sc = tmpltTypeHyperSearch.create();
    sc.setParameters("templateType", Storage.TemplateType.SYSTEM);
    sc.setJoinParameters("tmplHyper", "type", Host.Type.Routing);
    sc.setJoinParameters("tmplHyper", "zoneId", zoneId);

    // order by descending order of id
    List<VMTemplateVO> tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, null));

    for (VMTemplateVO tmplt : tmplts) {
      if (tmplt.getHypervisorType() == hType) {
        return tmplt;
      }
    }
    if (tmplts.size() > 0 && hType == HypervisorType.Any) {
      return tmplts.get(0);
    }
    return null;
  }
  private boolean preparePxeInBasicZone(
      VirtualMachineProfile profile,
      NicProfile nic,
      DeployDestination dest,
      ReservationContext context)
      throws AgentUnavailableException, OperationTimedoutException {
    NetworkVO nwVO = _nwDao.findById(nic.getNetworkId());
    QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class);
    sc.and(sc.entity().getDeviceType(), Op.EQ, BaremetalPxeType.KICK_START.toString());
    sc.and(sc.entity().getPhysicalNetworkId(), Op.EQ, nwVO.getPhysicalNetworkId());
    BaremetalPxeVO pxeVo = sc.find();
    if (pxeVo == null) {
      throw new CloudRuntimeException(
          "No kickstart PXE server found in pod: "
              + dest.getPod().getId()
              + ", you need to add it before starting VM");
    }
    VMTemplateVO template = _tmpDao.findById(profile.getTemplateId());
    List<String> tuple = parseKickstartUrl(profile);

    String ks = tuple.get(0);
    String kernel = tuple.get(1);
    String initrd = tuple.get(2);

    PrepareKickstartPxeServerCommand cmd = new PrepareKickstartPxeServerCommand();
    cmd.setKsFile(ks);
    cmd.setInitrd(initrd);
    cmd.setKernel(kernel);
    cmd.setMac(nic.getMacAddress());
    cmd.setTemplateUuid(template.getUuid());
    Answer aws = _agentMgr.send(pxeVo.getHostId(), cmd);
    if (!aws.getResult()) {
      s_logger.warn(
          "Unable to set host: "
              + dest.getHost().getId()
              + " to PXE boot because "
              + aws.getDetails());
      return false;
    }

    return true;
  }
Esempio n. 21
0
  private Long accountAndUserValidation(
      Account account, Long userId, UserVmVO vmInstanceCheck, VMTemplateVO template, String msg)
      throws PermissionDeniedException {

    if (account != null) {
      if (!isAdmin(account.getType())) {
        if ((vmInstanceCheck != null) && (account.getId() != vmInstanceCheck.getAccountId())) {
          throw new PermissionDeniedException(msg + ". Permission denied.");
        }

        if ((template != null)
            && (!template.isPublicTemplate()
                && (account.getId() != template.getAccountId())
                && (template.getTemplateType() != TemplateType.PERHOST))) {
          throw new PermissionDeniedException(msg + ". Permission denied.");
        }

      } else {
        if ((vmInstanceCheck != null)
            && !_domainDao.isChildDomain(account.getDomainId(), vmInstanceCheck.getDomainId())) {
          throw new PermissionDeniedException(msg + ". Permission denied.");
        }
        // FIXME: if template/ISO owner is null we probably need to
        // throw some kind of exception

        if (template != null) {
          Account templateOwner = _accountDao.findById(template.getAccountId());
          if ((templateOwner != null)
              && !_domainDao.isChildDomain(account.getDomainId(), templateOwner.getDomainId())) {
            throw new PermissionDeniedException(msg + ". Permission denied.");
          }
        }
      }
    }
    // If command is executed via 8096 port, set userId to the id of System
    // account (1)
    if (userId == null) {
      userId = new Long(1);
    }

    return userId;
  }
Esempio n. 22
0
  private void checksumSync(long hostId) {
    SearchCriteria<VMTemplateHostVO> sc = ReadyTemplateStatesSearch.create();
    sc.setParameters(
        "download_state", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
    sc.setParameters("host_id", hostId);

    List<VMTemplateHostVO> templateHostRefList = _vmTemplateHostDao.search(sc, null);
    s_logger.debug(
        "Found "
            + templateHostRefList.size()
            + " templates with no checksum. Will ask for computation");
    for (VMTemplateHostVO templateHostRef : templateHostRefList) {
      s_logger.debug("Getting checksum for template - " + templateHostRef.getTemplateId());
      String checksum = this.templateMgr.getChecksum(hostId, templateHostRef.getInstallPath());
      VMTemplateVO template = _templateDao.findById(templateHostRef.getTemplateId());
      s_logger.debug("Setting checksum " + checksum + " for template - " + template.getName());
      template.setChecksum(checksum);
      _templateDao.update(template.getId(), template);
    }
  }
Esempio n. 23
0
  @Override
  @DB
  public boolean remove(Long id) {
    Transaction txn = Transaction.currentTxn();
    txn.start();
    VMTemplateVO template = createForUpdate();
    template.setRemoved(new Date());

    VMTemplateVO vo = findById(id);
    if (vo != null) {
      if (vo.getFormat() == ImageFormat.ISO) {
        _tagsDao.removeByIdAndType(id, TaggedResourceType.ISO);
      } else {
        _tagsDao.removeByIdAndType(id, TaggedResourceType.Template);
      }
    }

    boolean result = update(id, template);
    txn.commit();
    return result;
  }
Esempio n. 24
0
 @Override
 public boolean downloadTemplateToStorage(VMTemplateVO template, Long zoneId) {
   List<DataCenterVO> dcs = new ArrayList<DataCenterVO>();
   if (zoneId == null) {
     dcs.addAll(_dcDao.listAll());
   } else {
     dcs.add(_dcDao.findById(zoneId));
   }
   long templateId = template.getId();
   boolean isPublic = template.isFeatured() || template.isPublicTemplate();
   for (DataCenterVO dc : dcs) {
     List<HostVO> ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(dc.getId());
     for (HostVO ssHost : ssHosts) {
       if (isTemplateUpdateable(templateId, ssHost.getId())) {
         initiateTemplateDownload(templateId, ssHost);
         if (!isPublic) {
           break;
         }
       }
     }
   }
   return true;
 }
  @Override
  public void downloadBootstrapSysTemplate(DataStore store) {
    Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();

    List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();

    for (VMTemplateVO rtngTmplt : rtngTmplts) {
      toBeDownloaded.add(rtngTmplt);
    }

    List<HypervisorType> availHypers =
        _clusterDao.getAvailableHypervisorInZone(store.getScope().getScopeId());
    if (availHypers.isEmpty()) {
      /*
       * This is for cloudzone, local secondary storage resource started
       * before cluster created
       */
      availHypers.add(HypervisorType.KVM);
    }
    /* Baremetal need not to download any template */
    availHypers.remove(HypervisorType.BareMetal);
    availHypers.add(HypervisorType.None); // bug 9809: resume ISO
    // download.

    for (VMTemplateVO template : toBeDownloaded) {
      if (availHypers.contains(template.getHypervisorType())) {
        // only download sys template applicable for current hypervisor
        TemplateDataStoreVO tmpltHost =
            _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
        if (tmpltHost == null
            || tmpltHost.getState() != ObjectInDataStoreStateMachine.State.Ready) {
          TemplateInfo tmplt = _templateFactory.getTemplate(template.getId(), DataStoreRole.Image);
          createTemplateAsync(tmplt, store, null);
        }
      }
    }
  }
  @Override
  public void handleSysTemplateDownload(HypervisorType hostHyper, Long dcId) {
    Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
    List<DataStore> stores = _storeMgr.getImageStoresByScope(new ZoneScope(dcId));
    if (stores == null || stores.isEmpty()) {
      return;
    }

    /* Download all the templates in zone with the same hypervisortype */
    for (DataStore store : stores) {
      List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
      List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();

      for (VMTemplateVO rtngTmplt : rtngTmplts) {
        if (rtngTmplt.getHypervisorType() == hostHyper) {
          toBeDownloaded.add(rtngTmplt);
        }
      }

      for (VMTemplateVO builtinTmplt : defaultBuiltin) {
        if (builtinTmplt.getHypervisorType() == hostHyper) {
          toBeDownloaded.add(builtinTmplt);
        }
      }

      for (VMTemplateVO template : toBeDownloaded) {
        TemplateDataStoreVO tmpltHost =
            _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
        if (tmpltHost == null
            || tmpltHost.getState() != ObjectInDataStoreStateMachine.State.Ready) {
          associateTemplateToZone(template.getId(), dcId);
          s_logger.info(
              "Downloading builtin template "
                  + template.getUniqueName()
                  + " to data center: "
                  + dcId);
          TemplateInfo tmplt = _templateFactory.getTemplate(template.getId(), DataStoreRole.Image);
          createTemplateAsync(tmplt, store, null);
        }
      }
    }
  }
Esempio n. 27
0
  protected VMTemplateVO persistTemplate(TemplateProfile profile) {
    Long zoneId = profile.getZoneId();
    VMTemplateVO template =
        new VMTemplateVO(
            profile.getTemplateId(),
            profile.getName(),
            profile.getFormat(),
            profile.getIsPublic(),
            profile.getFeatured(),
            profile.getIsExtractable(),
            TemplateType.USER,
            profile.getUrl(),
            profile.getRequiresHVM(),
            profile.getBits(),
            profile.getAccountId(),
            profile.getCheckSum(),
            profile.getDisplayText(),
            profile.getPasswordEnabled(),
            profile.getGuestOsId(),
            profile.getBootable(),
            profile.getHypervisorType(),
            profile.getTemplateTag(),
            profile.getDetails());

    if (zoneId == null || zoneId == -1) {
      List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();

      for (DataCenterVO dc : dcs) {
        _tmpltDao.addTemplateToZone(template, dc.getId());
      }
      template.setCrossZones(true);
    } else {
      _tmpltDao.addTemplateToZone(template, zoneId);
    }
    return template;
  }
Esempio n. 28
0
  private String generateCopyUrl(HostVO sourceServer, VMTemplateHostVO srcTmpltHost) {
    List<SecondaryStorageVmVO> ssVms =
        _secStorageVmDao.getSecStorageVmListInStates(
            SecondaryStorageVm.Role.templateProcessor,
            sourceServer.getDataCenterId(),
            State.Running);
    if (ssVms.size() > 0) {
      SecondaryStorageVmVO ssVm = ssVms.get(0);
      if (ssVm.getPublicIpAddress() == null) {
        s_logger.warn("A running secondary storage vm has a null public ip?");
        return null;
      }
      return generateCopyUrl(
          ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltHost.getInstallPath());
    }

    VMTemplateVO tmplt = _templateDao.findById(srcTmpltHost.getTemplateId());
    HypervisorType hyperType = tmplt.getHypervisorType();
    /*No secondary storage vm yet*/
    if (hyperType != null && hyperType == HypervisorType.KVM) {
      return "file://" + sourceServer.getParent() + "/" + srcTmpltHost.getInstallPath();
    }
    return null;
  }
Esempio n. 29
0
  @Override
  public void handleSysTemplateDownload(HostVO host) {
    List<HypervisorType> hypers =
        _resourceMgr.listAvailHypervisorInZone(host.getId(), host.getDataCenterId());
    HypervisorType hostHyper = host.getHypervisorType();
    if (hypers.contains(hostHyper)) {
      return;
    }

    Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
    List<HostVO> ssHosts =
        _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(
            Host.Type.SecondaryStorage, host.getDataCenterId());
    if (ssHosts == null || ssHosts.isEmpty()) {
      return;
    }
    /*Download all the templates in zone with the same hypervisortype*/
    for (HostVO ssHost : ssHosts) {
      List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
      List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();

      for (VMTemplateVO rtngTmplt : rtngTmplts) {
        if (rtngTmplt.getHypervisorType() == hostHyper) {
          toBeDownloaded.add(rtngTmplt);
        }
      }

      for (VMTemplateVO builtinTmplt : defaultBuiltin) {
        if (builtinTmplt.getHypervisorType() == hostHyper) {
          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);
        }
      }
    }
  }
  @Override
  @DB
  public boolean delete(TemplateProfile profile) {
    boolean success = true;

    VMTemplateVO template = profile.getTemplate();
    Long zoneId = profile.getZoneId();
    Long templateId = template.getId();

    String zoneName;
    List<HostVO> secondaryStorageHosts;
    if (!template.isCrossZones() && zoneId != null) {
      DataCenterVO zone = _dcDao.findById(zoneId);
      zoneName = zone.getName();
      secondaryStorageHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(zoneId);
    } else {
      zoneName = "(all zones)";
      secondaryStorageHosts = _ssvmMgr.listSecondaryStorageHostsInAllZones();
    }

    s_logger.debug(
        "Attempting to mark template host refs for template: "
            + template.getName()
            + " as destroyed in zone: "
            + zoneName);

    // Make sure the template is downloaded to all the necessary secondary storage hosts
    for (HostVO secondaryStorageHost : secondaryStorageHosts) {
      long hostId = secondaryStorageHost.getId();
      List<VMTemplateHostVO> templateHostVOs = _tmpltHostDao.listByHostTemplate(hostId, templateId);
      for (VMTemplateHostVO templateHostVO : templateHostVOs) {
        if (templateHostVO.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
          String errorMsg = "Please specify a template that is not currently being downloaded.";
          s_logger.debug(
              "Template: "
                  + template.getName()
                  + " is currently being downloaded to secondary storage host: "
                  + secondaryStorageHost.getName()
                  + "; cant' delete it.");
          throw new CloudRuntimeException(errorMsg);
        }
      }
    }

    Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
    String eventType = "";

    if (template.getFormat().equals(ImageFormat.ISO)) {
      eventType = EventTypes.EVENT_ISO_DELETE;
    } else {
      eventType = EventTypes.EVENT_TEMPLATE_DELETE;
    }

    // Iterate through all necessary secondary storage hosts and mark the template on each host as
    // destroyed
    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;
          }
          UsageEventVO usageEvent =
              new UsageEventVO(eventType, account.getId(), sZoneId, templateId, null);
          _usageEventDao.persist(usageEvent);
          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.");
    }

    return success;
  }