Search in sources :

Example 21 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class HypervisorTemplateAdapter method create.

@Override
public VMTemplateVO create(TemplateProfile profile) {
    // persist entry in vm_template, vm_template_details and template_zone_ref tables, not that entry at template_store_ref is not created here, and created in createTemplateAsync.
    VMTemplateVO template = persistTemplate(profile, State.Active);
    if (template == null) {
        throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate());
    }
    // find all eligible image stores for this zone scope
    List<DataStore> imageStores = storeMgr.getImageStoresByScope(new ZoneScope(profile.getZoneId()));
    if (imageStores == null || imageStores.size() == 0) {
        throw new CloudRuntimeException("Unable to find image store to download template " + profile.getTemplate());
    }
    Set<Long> zoneSet = new HashSet<Long>();
    // For private templates choose a random store. TODO - Have a better algorithm based on size, no. of objects, load etc.
    Collections.shuffle(imageStores);
    for (DataStore imageStore : imageStores) {
        // skip data stores for a disabled zone
        Long zoneId = imageStore.getScope().getScopeId();
        if (zoneId != null) {
            DataCenterVO zone = _dcDao.findById(zoneId);
            if (zone == null) {
                s_logger.warn("Unable to find zone by id " + zoneId + ", so skip downloading template to its image store " + imageStore.getId());
                continue;
            }
            // Check if zone is disabled
            if (Grouping.AllocationState.Disabled == zone.getAllocationState()) {
                s_logger.info("Zone " + zoneId + " is disabled, so skip downloading template to its image store " + imageStore.getId());
                continue;
            }
            // Check if image store has enough capacity for template
            if (!_statsCollector.imageStoreHasEnoughCapacity(imageStore)) {
                s_logger.info("Image store doesn't has enough capacity, so skip downloading template to this image store " + imageStore.getId());
                continue;
            }
            // We want to download private template to one of the image store in a zone
            if (isPrivateTemplate(template) && zoneSet.contains(zoneId)) {
                continue;
            } else {
                zoneSet.add(zoneId);
            }
        }
        TemplateInfo tmpl = imageFactory.getTemplate(template.getId(), imageStore);
        CreateTemplateContext<TemplateApiResult> context = new CreateTemplateContext<TemplateApiResult>(null, tmpl);
        AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createTemplateAsyncCallBack(null, null));
        caller.setContext(context);
        imageService.createTemplateAsync(tmpl, imageStore, caller);
    }
    _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
    return template;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) HashSet(java.util.HashSet)

Example 22 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class TemplateAdapterBase method prepareDelete.

@Override
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
    Long templateId = cmd.getId();
    Long userId = CallContext.current().getCallingUserId();
    Account account = CallContext.current().getCallingAccount();
    Long zoneId = cmd.getZoneId();
    VMTemplateVO template = _tmpltDao.findById(templateId);
    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);
}
Also used : Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateProfile(com.cloud.storage.TemplateProfile)

Example 23 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class TemplateTest method setUp.

@Test(priority = -1)
public void setUp() {
    ComponentContext.initComponentsLifeCycle();
    // create data center
    DataCenterVO dc = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null, NetworkType.Basic, null, null, true, true, null, null);
    dc = dcDao.persist(dc);
    dcId = dc.getId();
    imageStore = new ImageStoreVO();
    imageStore.setName("test");
    imageStore.setDataCenterId(dcId);
    imageStore.setProviderName(DataStoreProvider.NFS_IMAGE);
    imageStore.setRole(DataStoreRole.Image);
    imageStore.setUrl(this.getSecondaryStorage());
    imageStore.setUuid(UUID.randomUUID().toString());
    imageStore.setProtocol("nfs");
    imageStore = imageStoreDao.persist(imageStore);
    VMTemplateVO image = new VMTemplateVO();
    image.setTemplateType(TemplateType.USER);
    image.setUrl(this.getTemplateUrl());
    image.setUniqueName(UUID.randomUUID().toString());
    image.setName(UUID.randomUUID().toString());
    image.setPublicTemplate(true);
    image.setFeatured(true);
    image.setRequiresHvm(true);
    image.setBits(64);
    image.setFormat(Storage.ImageFormat.VHD);
    image.setEnablePassword(true);
    image.setEnableSshKey(true);
    image.setGuestOSId(1);
    image.setBootable(true);
    image.setPrepopulate(true);
    image.setCrossZones(true);
    image.setExtractable(true);
    // image.setImageDataStoreId(storeId);
    image = templateDao.persist(image);
    templateId = image.getId();
    // inject mockito
    LocalHostEndpoint ep = new LocalHostEndpoint();
    ep.setResource(new MockLocalNfsSecondaryStorageResource());
    Mockito.when(epSelector.select(Matchers.any(DataObject.class))).thenReturn(ep);
    Mockito.when(epSelector.select(Matchers.any(DataStore.class))).thenReturn(ep);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) LocalHostEndpoint(org.apache.cloudstack.storage.LocalHostEndpoint) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) MockLocalNfsSecondaryStorageResource(org.apache.cloudstack.storage.MockLocalNfsSecondaryStorageResource) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) Test(org.testng.annotations.Test)

Example 24 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class VolumeServiceTest method createTemplate.

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;
    }
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) URISyntaxException(java.net.URISyntaxException) ExecutionException(java.util.concurrent.ExecutionException)

Example 25 with VMTemplateVO

use of com.cloud.storage.VMTemplateVO in project cloudstack by apache.

the class VolumeTestVmware method createTemplateInDb.

private VMTemplateVO createTemplateInDb() {
    image = new VMTemplateVO();
    image.setTemplateType(TemplateType.USER);
    image.setUniqueName(UUID.randomUUID().toString());
    image.setName(UUID.randomUUID().toString());
    image.setPublicTemplate(true);
    image.setFeatured(true);
    image.setRequiresHvm(true);
    image.setBits(64);
    image.setFormat(Storage.ImageFormat.VHD);
    image.setEnablePassword(true);
    image.setEnableSshKey(true);
    image.setGuestOSId(1);
    image.setBootable(true);
    image.setPrepopulate(true);
    image.setCrossZones(true);
    image.setExtractable(true);
    image = imageDataDao.persist(image);
    return image;
}
Also used : VMTemplateVO(com.cloud.storage.VMTemplateVO)

Aggregations

VMTemplateVO (com.cloud.storage.VMTemplateVO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)27 Account (com.cloud.user.Account)27 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)24 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)22 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)17 Date (java.util.Date)16 ArrayList (java.util.ArrayList)15 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)13 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 DataCenterVO (com.cloud.dc.DataCenterVO)12 ActionEvent (com.cloud.event.ActionEvent)11 TemplateProfile (com.cloud.storage.TemplateProfile)11 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)11 VolumeVO (com.cloud.storage.VolumeVO)11 Test (org.junit.Test)11 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)10 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)9 DB (com.cloud.utils.db.DB)9