Search in sources :

Example 6 with ImageFormat

use of com.cloud.storage.Storage.ImageFormat 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 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;
    }
    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 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 && requiresHvm == null && sortKey == null && isDynamicallyScalable == null && isRoutingTemplate == null && //update details in every case except this one
    (!cleanupDetails && details == null));
    if (!updateNeeded) {
        return template;
    }
    template = _tmpltDao.createForUpdate(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) {
        long oldGuestOSId = template.getGuestOSId();
        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 (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 {
            template.setTemplateType(TemplateType.USER);
        }
    }
    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) GuestOSVO(com.cloud.storage.GuestOSVO) ImageFormat(com.cloud.storage.Storage.ImageFormat) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with ImageFormat

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

the class DownloadMonitorImpl method downloadVolumeToStorage.

@Override
public void downloadVolumeToStorage(DataObject volume, AsyncCompletionCallback<DownloadAnswer> callback) {
    boolean downloadJobExists = false;
    VolumeDataStoreVO volumeHost = null;
    DataStore store = volume.getDataStore();
    VolumeInfo volInfo = (VolumeInfo) volume;
    RegisterVolumePayload payload = (RegisterVolumePayload) volInfo.getpayload();
    String url = payload.getUrl();
    String checkSum = payload.getChecksum();
    ImageFormat format = ImageFormat.valueOf(payload.getFormat());
    volumeHost = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId());
    if (volumeHost == null) {
        volumeHost = new VolumeDataStoreVO(store.getId(), volume.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url, checkSum);
        _volumeStoreDao.persist(volumeHost);
    } else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) {
        downloadJobExists = true;
    } else {
        // persit url and checksum
        volumeHost.setDownloadUrl(url);
        volumeHost.setChecksum(checkSum);
        _volumeStoreDao.update(volumeHost.getId(), volumeHost);
    }
    Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes();
    if (volumeHost != null) {
        start();
        Volume vol = _volumeDao.findById(volume.getId());
        DownloadCommand dcmd = new DownloadCommand((VolumeObjectTO) (volume.getTO()), maxVolumeSizeInBytes, checkSum, url, format);
        dcmd.setProxy(getHttpProxy());
        if (downloadJobExists) {
            dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
            dcmd.setResourceType(ResourceType.VOLUME);
        }
        EndPoint ep = _epSelector.select(volume);
        if (ep == null) {
            s_logger.warn("There is no secondary storage VM for image store " + store.getName());
            return;
        }
        DownloadListener dl = new DownloadListener(ep, store, volume, _timer, this, dcmd, callback);
        // auto-wired those injected fields in DownloadListener
        ComponentContext.inject(dl);
        if (downloadJobExists) {
            dl.setCurrState(volumeHost.getDownloadState());
        }
        try {
            ep.sendMessageAsync(dcmd, new UploadListener.Callback(ep.getId(), dl));
        } catch (Exception e) {
            s_logger.warn("Unable to start /resume download of volume " + volume.getId() + " to " + store.getName(), e);
            dl.setDisconnected();
            dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
        }
    }
}
Also used : DownloadCommand(org.apache.cloudstack.storage.command.DownloadCommand) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RegisterVolumePayload(com.cloud.storage.RegisterVolumePayload) UploadListener(com.cloud.storage.upload.UploadListener) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ImageFormat(com.cloud.storage.Storage.ImageFormat) DownloadProgressCommand(org.apache.cloudstack.storage.command.DownloadProgressCommand) Volume(com.cloud.storage.Volume) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)

Example 8 with ImageFormat

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

the class NfsSecondaryStorageResource method copyFromNfsToS3.

protected Answer copyFromNfsToS3(CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    NfsTO srcStore = (NfsTO) srcDataStore;
    DataStoreTO destDataStore = destData.getDataStore();
    final S3TO s3 = (S3TO) destDataStore;
    try {
        final String templatePath = determineStorageTemplatePath(srcStore.getUrl(), srcData.getPath(), _nfsVersion);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Found " + srcData.getObjectType() + " from directory " + templatePath + " to upload to S3.");
        }
        final String bucket = s3.getBucketName();
        File srcFile = findFile(templatePath);
        if (srcFile == null) {
            return new CopyCmdAnswer("Can't find src file:" + templatePath);
        }
        ImageFormat format = getTemplateFormat(srcFile.getName());
        String key = destData.getPath() + S3Utils.SEPARATOR + srcFile.getName();
        putFile(s3, srcFile, bucket, key).waitForCompletion();
        DataTO retObj = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(key);
            newTemplate.setSize(getVirtualSize(srcFile, format));
            newTemplate.setPhysicalSize(srcFile.length());
            newTemplate.setFormat(format);
            retObj = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(key);
            newVol.setSize(srcFile.length());
            retObj = newVol;
        } else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
            SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
            newSnapshot.setPath(key);
            retObj = newSnapshot;
        }
        return new CopyCmdAnswer(retObj);
    } catch (Exception e) {
        s_logger.error("failed to upload" + srcData.getPath(), e);
        return new CopyCmdAnswer("failed to upload" + srcData.getPath() + e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) ImageFormat(com.cloud.storage.Storage.ImageFormat)

Example 9 with ImageFormat

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

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNODisk.

@Test
public void testPrimaryStorageDownloadCommandNOTemplateNODisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
    final String name = "Test";
    final String url = "http://template/";
    final ImageFormat format = ImageFormat.QCOW2;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
    final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
    final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
    final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
    final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) StoragePool(com.cloud.storage.StoragePool) NfsStoragePool(com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) PrimaryStorageDownloadCommand(com.cloud.agent.api.storage.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.storage.Storage.ImageFormat) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) Test(org.junit.Test)

Example 10 with ImageFormat

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

the class TemplateAdapterBase method prepare.

@Override
public TemplateProfile prepare(boolean isIso, long userId, String name, String displayText, Integer bits, Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured, Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType, String chksum, Boolean bootable, String templateTag, Account templateOwner, Map details, Boolean sshkeyEnabled, String imageStoreUuid, Boolean isDynamicallyScalable, TemplateType templateType) throws ResourceAllocationException {
    if (isPublic == null) {
        isPublic = Boolean.FALSE;
    }
    if (zoneId.longValue() == -1) {
        zoneId = null;
    }
    if (isIso) {
        if (bootable == null) {
            bootable = Boolean.TRUE;
        }
        GuestOS noneGuestOs = ApiDBUtils.findGuestOSByDisplayName(ApiConstants.ISO_GUEST_OS_NONE);
        if ((guestOSId == null || guestOSId == noneGuestOs.getId()) && bootable == true) {
            throw new InvalidParameterValueException("Please pass a valid GuestOS Id");
        }
        if (bootable == false) {
            //Guest os id of None.
            guestOSId = noneGuestOs.getId();
        }
    } else {
        if (bits == null) {
            bits = Integer.valueOf(64);
        }
        if (passwordEnabled == null) {
            passwordEnabled = false;
        }
        if (requiresHVM == null) {
            requiresHVM = true;
        }
    }
    if (isExtractable == null) {
        isExtractable = Boolean.FALSE;
    }
    if (sshkeyEnabled == null) {
        sshkeyEnabled = Boolean.FALSE;
    }
    boolean isAdmin = _accountMgr.isRootAdmin(templateOwner.getId());
    boolean isRegionStore = false;
    List<ImageStoreVO> stores = _imgStoreDao.findRegionImageStores();
    if (stores != null && stores.size() > 0) {
        isRegionStore = true;
    }
    if (!isAdmin && zoneId == null && !isRegionStore) {
        // domain admin and user should also be able to register template on a region store
        throw new InvalidParameterValueException("Please specify a valid zone Id. Only admins can create templates in all zones.");
    }
    // check for the url format only when url is not null. url can be null incase of form based upload
    if (url != null && url.toLowerCase().contains("file://")) {
        throw new InvalidParameterValueException("File:// type urls are currently unsupported");
    }
    // check whether owner can create public templates
    boolean allowPublicUserTemplates = TemplateManager.AllowPublicUserTemplates.valueIn(templateOwner.getId());
    if (!isAdmin && !allowPublicUserTemplates && isPublic) {
        throw new InvalidParameterValueException("Only private templates/ISO can be created.");
    }
    if (!isAdmin || featured == null) {
        featured = Boolean.FALSE;
    }
    ImageFormat imgfmt;
    try {
        imgfmt = ImageFormat.valueOf(format.toUpperCase());
    } catch (IllegalArgumentException e) {
        s_logger.debug("ImageFormat IllegalArgumentException: " + e.getMessage());
        throw new IllegalArgumentException("Image format: " + format + " is incorrect. Supported formats are " + EnumUtils.listValues(ImageFormat.values()));
    }
    // Check that the resource limit for templates/ISOs won't be exceeded
    UserVO user = _userDao.findById(userId);
    if (user == null) {
        throw new IllegalArgumentException("Unable to find user with id " + userId);
    }
    _resourceLimitMgr.checkResourceLimit(templateOwner, ResourceType.template);
    // If a zoneId is specified, make sure it is valid
    if (zoneId != null) {
        DataCenterVO zone = _dcDao.findById(zoneId);
        if (zone == null) {
            throw new IllegalArgumentException("Please specify a valid zone.");
        }
        Account caller = CallContext.current().getCallingAccount();
        if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getId())) {
            throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId);
        }
    }
    List<VMTemplateVO> systemvmTmplts = _tmpltDao.listAllSystemVMTemplates();
    for (VMTemplateVO template : systemvmTmplts) {
        if (template.getName().equalsIgnoreCase(name) || template.getDisplayText().equalsIgnoreCase(displayText)) {
            throw new IllegalArgumentException("Cannot use reserved names for templates");
        }
    }
    if (hypervisorType.equals(Hypervisor.HypervisorType.XenServer)) {
        if (details == null || !details.containsKey("hypervisortoolsversion") || details.get("hypervisortoolsversion") == null || ((String) details.get("hypervisortoolsversion")).equalsIgnoreCase("none")) {
            String hpvs = _configDao.getValue(Config.XenServerPVdriverVersion.key());
            if (hpvs != null) {
                if (details == null) {
                    details = new HashMap<String, String>();
                }
                details.put("hypervisortoolsversion", hpvs);
            }
        }
    }
    Long id = _tmpltDao.getNextInSequence(Long.class, "id");
    CallContext.current().setEventDetails("Id: " + id + " name: " + name);
    return new TemplateProfile(id, userId, name, displayText, bits, passwordEnabled, requiresHVM, url, isPublic, featured, isExtractable, imgfmt, guestOSId, zoneId, hypervisorType, templateOwner.getAccountName(), templateOwner.getDomainId(), templateOwner.getAccountId(), chksum, bootable, templateTag, details, sshkeyEnabled, null, isDynamicallyScalable, templateType);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) ImageFormat(com.cloud.storage.Storage.ImageFormat) UserVO(com.cloud.user.UserVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) GuestOS(com.cloud.storage.GuestOS) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) TemplateProfile(com.cloud.storage.TemplateProfile)

Aggregations

ImageFormat (com.cloud.storage.Storage.ImageFormat)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 Answer (com.cloud.agent.api.Answer)4 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)4 PrimaryStorageDownloadCommand (com.cloud.agent.api.storage.PrimaryStorageDownloadCommand)4 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool)4 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)4 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)4 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)4 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)4 StoragePool (com.cloud.storage.StoragePool)4 ArrayList (java.util.ArrayList)4 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)3 DataTO (com.cloud.agent.api.to.DataTO)3 NfsTO (com.cloud.agent.api.to.NfsTO)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)3 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)3 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)3