Search in sources :

Example 1 with TemplateType

use of com.cloud.storage.Storage.TemplateType in project cloudstack by apache.

the class TemplateServiceImpl method createChildDataDiskTemplate.

private boolean createChildDataDiskTemplate(DatadiskTO dataDiskTemplate, VMTemplateVO template, TemplateInfo parentTemplate, DataStore imageStore, int diskCount) throws ExecutionException, InterruptedException {
    // Make an entry in vm_template table
    Storage.ImageFormat format = dataDiskTemplate.isIso() ? Storage.ImageFormat.ISO : template.getFormat();
    String suffix = dataDiskTemplate.isIso() ? "-IsoDiskTemplate-" : "-DataDiskTemplate-";
    TemplateType ttype = dataDiskTemplate.isIso() ? TemplateType.ISODISK : TemplateType.DATADISK;
    final long templateId = _templateDao.getNextInSequence(Long.class, "id");
    long guestOsId = dataDiskTemplate.isIso() ? 1 : 0;
    String templateName = dataDiskTemplate.isIso() ? dataDiskTemplate.getPath().substring(dataDiskTemplate.getPath().lastIndexOf(File.separator) + 1) : template.getName() + suffix + diskCount;
    VMTemplateVO templateVO = new VMTemplateVO(templateId, templateName, format, false, false, false, ttype, template.getUrl(), template.requiresHvm(), template.getBits(), template.getAccountId(), null, templateName, false, guestOsId, false, template.getHypervisorType(), null, null, false, false, false, false);
    if (dataDiskTemplate.isIso()) {
        templateVO.setUniqueName(templateName);
    }
    templateVO.setParentTemplateId(template.getId());
    templateVO.setSize(dataDiskTemplate.getVirtualSize());
    templateVO = _templateDao.persist(templateVO);
    // Make sync call to create Datadisk templates in image store
    TemplateApiResult result = null;
    TemplateInfo dataDiskTemplateInfo = imageFactory.getTemplate(templateVO.getId(), imageStore);
    AsyncCallFuture<TemplateApiResult> future = createDatadiskTemplateAsync(parentTemplate, dataDiskTemplateInfo, dataDiskTemplate.getPath(), dataDiskTemplate.getDiskId(), dataDiskTemplate.getFileSize(), dataDiskTemplate.isBootable());
    result = future.get();
    if (result.isSuccess()) {
        // Make an entry in template_zone_ref table
        if (imageStore.getScope().getScopeType() == ScopeType.REGION) {
            associateTemplateToZone(templateId, null);
        } else if (imageStore.getScope().getScopeType() == ScopeType.ZONE) {
            Long zoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
            VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
            _vmTemplateZoneDao.persist(templateZone);
        }
        _resourceLimitMgr.incrementResourceCount(template.getAccountId(), ResourceType.secondary_storage, templateVO.getSize());
    } else {
        // Delete the Datadisk templates that were already created as they are now invalid
        s_logger.debug("Since creation of Datadisk template: " + templateVO.getId() + " failed, delete other Datadisk templates that were created as part of parent" + " template download");
        TemplateInfo parentTemplateInfo = imageFactory.getTemplate(templateVO.getParentTemplateId(), imageStore);
        cleanupDatadiskTemplates(parentTemplateInfo);
    }
    return result.isSuccess();
}
Also used : ImageFormat(com.cloud.storage.Storage.ImageFormat) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) Storage(com.cloud.storage.Storage) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateType(com.cloud.storage.Storage.TemplateType) Date(java.util.Date)

Example 2 with TemplateType

use of com.cloud.storage.Storage.TemplateType in project cloudstack by apache.

the class TemplateManagerImpl method updateTemplateOrIso.

private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
    Long id = cmd.getId();
    String name = cmd.getTemplateName();
    String displayText = cmd.getDisplayText();
    String format = cmd.getFormat();
    Long guestOSId = cmd.getOsTypeId();
    Boolean passwordEnabled = cmd.getPasswordEnabled();
    Boolean sshKeyEnabled = cmd.isSshKeyEnabled();
    Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
    Boolean isRoutingTemplate = cmd.isRoutingType();
    Boolean bootable = cmd.getBootable();
    Boolean requiresHvm = cmd.getRequiresHvm();
    Integer sortKey = cmd.getSortKey();
    Map details = cmd.getDetails();
    Account account = CallContext.current().getCallingAccount();
    boolean cleanupDetails = cmd.isCleanupDetails();
    // verify that template exists
    VMTemplateVO template = _tmpltDao.findById(id);
    if (template == null || template.getRemoved() != null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("unable to find template/iso with specified id");
        ex.addProxyObject(String.valueOf(id), "templateId");
        throw ex;
    }
    long oldGuestOSId = template.getGuestOSId();
    verifyTemplateId(id);
    // do a permission check
    _accountMgr.checkAccess(account, AccessType.OperateEntry, true, template);
    if (cmd.isRoutingType() != null) {
        if (!_accountService.isRootAdmin(account.getId())) {
            throw new PermissionDeniedException("Parameter isrouting can only be specified by a Root Admin, permission denied");
        }
    }
    // update template type
    TemplateType templateType = null;
    if (cmd instanceof UpdateTemplateCmd) {
        String newType = ((UpdateTemplateCmd) cmd).getTemplateType();
        if (newType != null) {
            if (!_accountService.isRootAdmin(account.getId())) {
                throw new PermissionDeniedException("Parameter templatetype can only be specified by a Root Admin, permission denied");
            }
            try {
                templateType = TemplateType.valueOf(newType.toUpperCase());
            } catch (IllegalArgumentException ex) {
                throw new InvalidParameterValueException("Please specify a valid templatetype: ROUTING / SYSTEM / USER / BUILTIN / PERHOST");
            }
        }
        if (templateType != null && cmd.isRoutingType() != null && (TemplateType.ROUTING.equals(templateType) != cmd.isRoutingType())) {
            throw new InvalidParameterValueException("Please specify a valid templatetype (consistent with isrouting parameter).");
        }
        if (templateType != null && (templateType == TemplateType.SYSTEM || templateType == TemplateType.BUILTIN) && !template.isCrossZones()) {
            throw new InvalidParameterValueException("System and Builtin templates must be cross zone");
        }
    }
    // update is needed if any of the fields below got filled by the user
    boolean updateNeeded = !(name == null && displayText == null && format == null && guestOSId == null && passwordEnabled == null && bootable == null && sshKeyEnabled == null && requiresHvm == null && sortKey == null && isDynamicallyScalable == null && isRoutingTemplate == null && templateType == null && // update details in every case except this one
    (!cleanupDetails && details == null));
    if (!updateNeeded) {
        return template;
    }
    template = _tmpltDao.findById(id);
    if (name != null) {
        template.setName(name);
    }
    if (displayText != null) {
        template.setDisplayText(displayText);
    }
    if (sortKey != null) {
        template.setSortKey(sortKey);
    }
    ImageFormat imageFormat = null;
    if (format != null) {
        try {
            imageFormat = ImageFormat.valueOf(format.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new InvalidParameterValueException("Image format: " + format + " is incorrect. Supported formats are " + EnumUtils.listValues(ImageFormat.values()));
        }
        template.setFormat(imageFormat);
    }
    if (guestOSId != null) {
        GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
        if (guestOS == null) {
            throw new InvalidParameterValueException("Please specify a valid guest OS ID.");
        } else {
            template.setGuestOSId(guestOSId);
        }
        if (guestOSId != oldGuestOSId) {
            // vm guest os type need to be updated if template guest os id changes.
            SearchCriteria<VMInstanceVO> sc = _vmInstanceDao.createSearchCriteria();
            sc.addAnd("templateId", SearchCriteria.Op.EQ, id);
            sc.addAnd("state", SearchCriteria.Op.NEQ, State.Expunging);
            List<VMInstanceVO> vms = _vmInstanceDao.search(sc, null);
            if (vms != null && !vms.isEmpty()) {
                for (VMInstanceVO vm : vms) {
                    vm.setGuestOSId(guestOSId);
                    _vmInstanceDao.update(vm.getId(), vm);
                }
            }
        }
    }
    if (passwordEnabled != null) {
        template.setEnablePassword(passwordEnabled);
    }
    if (sshKeyEnabled != null) {
        template.setEnableSshKey(sshKeyEnabled);
    }
    if (bootable != null) {
        template.setBootable(bootable);
    }
    if (requiresHvm != null) {
        template.setRequiresHvm(requiresHvm);
    }
    if (isDynamicallyScalable != null) {
        template.setDynamicallyScalable(isDynamicallyScalable);
    }
    if (isRoutingTemplate != null) {
        if (isRoutingTemplate) {
            template.setTemplateType(TemplateType.ROUTING);
        } else if (templateType != null) {
            template.setTemplateType(templateType);
        } else {
            template.setTemplateType(TemplateType.USER);
        }
    } else if (templateType != null) {
        template.setTemplateType(templateType);
    }
    validateDetails(template, details);
    if (cleanupDetails) {
        template.setDetails(null);
        _tmpltDetailsDao.removeDetails(id);
    } else if (details != null && !details.isEmpty()) {
        template.setDetails(details);
        _tmpltDao.saveDetails(template);
    }
    _tmpltDao.update(id, template);
    return _tmpltDao.findById(id);
}
Also used : Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) TemplateType(com.cloud.storage.Storage.TemplateType) GuestOSVO(com.cloud.storage.GuestOSVO) ImageFormat(com.cloud.storage.Storage.ImageFormat) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UpdateTemplateCmd(org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ImageFormat (com.cloud.storage.Storage.ImageFormat)2 TemplateType (com.cloud.storage.Storage.TemplateType)2 VMTemplateVO (com.cloud.storage.VMTemplateVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 GuestOSVO (com.cloud.storage.GuestOSVO)1 Storage (com.cloud.storage.Storage)1 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)1 Account (com.cloud.user.Account)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UpdateTemplateCmd (org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd)1 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)1