Search in sources :

Example 11 with VMTemplateZoneVO

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

the class SecondaryStorageDiscoverer method associateTemplatesToZone.

private void associateTemplatesToZone(long hostId, long dcId) {
    VMTemplateZoneVO tmpltZone;
    List<VMTemplateVO> allTemplates = _vmTemplateDao.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);
            }
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.util.Date)

Example 12 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cosmic by MissionCriticalCloud.

the class TemplateServiceImpl method associateTemplateToZone.

// persist entry in template_zone_ref table. zoneId can be empty for
// region-wide image store, in that case,
// we will associate the template to all the zones.
@Override
public void associateTemplateToZone(final long templateId, final Long zoneId) {
    final List<Long> dcs = new ArrayList<>();
    if (zoneId != null) {
        dcs.add(zoneId);
    } else {
        final List<Zone> zones = zoneRepository.findByRemovedIsNull();
        for (final Zone zone : zones) {
            dcs.add(zone.getId());
        }
    }
    for (final Long id : dcs) {
        VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, templateId);
        if (tmpltZoneVO == null) {
            tmpltZoneVO = new VMTemplateZoneVO(id, templateId, new Date());
            _vmTemplateZoneDao.persist(tmpltZoneVO);
        } else {
            tmpltZoneVO.setLastUpdated(new Date());
            _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 13 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method createPrivateTemplate.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command) throws CloudRuntimeException {
    final long templateId = command.getEntityId();
    final Long volumeId = command.getVolumeId();
    final Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    try {
        final TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
        long zoneId = 0;
        if (snapshotId != null) {
            snapshot = _snapshotDao.findById(snapshotId);
            zoneId = snapshot.getDataCenterId();
        } else if (volumeId != null) {
            volume = _volumeDao.findById(volumeId);
            zoneId = volume.getDataCenterId();
        }
        DataStore store = _dataStoreMgr.getImageStore(zoneId);
        if (store == null) {
            throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
        }
        final AsyncCallFuture<TemplateApiResult> future;
        if (snapshotId != null) {
            final DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
            SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
            if (dataStoreRole == DataStoreRole.Image) {
                if (snapInfo == null) {
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
                    }
                    // We need to copy the snapshot onto secondary.
                    final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
                    snapshotStrategy.backupSnapshot(snapInfo);
                    // Attempt to grab it again.
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
                    }
                }
                final DataStore snapStore = snapInfo.getDataStore();
                if (snapStore != null) {
                    // pick snapshot image store to create template
                    store = snapStore;
                }
            }
            future = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
        } else if (volumeId != null) {
            final VolumeInfo volInfo = _volFactory.getVolume(volumeId);
            future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        final CommandResult result;
        try {
            result = future.get();
            if (result.isFailed()) {
                privateTemplate = null;
                s_logger.debug("Failed to create template" + result.getResult());
                throw new CloudRuntimeException("Failed to create template" + result.getResult());
            }
            // create entries in template_zone_ref table
            if (_dataStoreMgr.isRegionStore(store)) {
                // template created on region store
                _tmpltSvr.associateTemplateToZone(templateId, null);
            } else {
                final VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                _tmpltZoneDao.persist(templateZone);
            }
            privateTemplate = _tmpltDao.findById(templateId);
        } catch (final InterruptedException | ExecutionException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        }
    } finally {
        if (privateTemplate == null) {
            final VolumeVO volumeFinal = volume;
            final SnapshotVO snapshotFinal = snapshot;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    // template_store_ref entries should have been removed using our
                    // DataObject.processEvent command in case of failure, but clean
                    // it up here to avoid
                    // some leftovers which will cause removing template from
                    // vm_template table fail.
                    _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template_zone_ref record
                    _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template record
                    _tmpltDao.expunge(templateId);
                    // decrement resource count
                    if (accountId != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
                    }
                }
            });
        }
    }
    if (privateTemplate != null) {
        return privateTemplate;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(com.cloud.storage.command.CommandResult) DataStoreRole(com.cloud.storage.DataStoreRole) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 14 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project CloudStack-archive by CloudStack-extras.

the class SimulatorDiscoverer method associateTemplatesToZone.

private void associateTemplatesToZone(long hostId, long dcId) {
    VMTemplateZoneVO tmpltZone;
    List<VMTemplateVO> allTemplates = _vmTemplateDao.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);
            }
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.util.Date)

Example 15 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cosmic by MissionCriticalCloud.

the class TemplateServiceImpl method associateCrosszoneTemplatesToZone.

// update template_zone_ref for cross-zone template for newly added zone
@Override
public void associateCrosszoneTemplatesToZone(final long dcId) {
    VMTemplateZoneVO tmpltZone;
    final List<VMTemplateVO> allTemplates = _templateDao.listAll();
    for (final VMTemplateVO vt : allTemplates) {
        if (vt.isCrossZones()) {
            tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
            if (tmpltZone == null) {
                final VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
                _vmTemplateZoneDao.persist(vmTemplateZone);
            }
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.util.Date)

Aggregations

VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)25 VMTemplateVO (com.cloud.storage.VMTemplateVO)18 Date (java.util.Date)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 DB (com.cloud.utils.db.DB)10 ArrayList (java.util.ArrayList)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 Account (com.cloud.user.Account)4 Pair (com.cloud.utils.Pair)4 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)4 ExecutionException (java.util.concurrent.ExecutionException)4 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)3 DomainVO (com.cloud.domain.DomainVO)3 ActionEvent (com.cloud.event.ActionEvent)3 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 CloudException (com.cloud.exception.CloudException)3 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)3 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)3 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)3