Search in sources :

Example 36 with TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO in project cosmic by MissionCriticalCloud.

the class StorageManagerImpl method cleanupDownloadUrls.

@Override
public void cleanupDownloadUrls() {
    // Cleanup expired volume URLs
    final List<VolumeDataStoreVO> volumesOnImageStoreList = _volumeStoreDao.listVolumeDownloadUrls();
    for (final VolumeDataStoreVO volumeOnImageStore : volumesOnImageStoreList) {
        final long downloadUrlCurrentAgeInSecs = DateUtil.getTimeDifference(DateUtil.now(), volumeOnImageStore.getExtractUrlCreated());
        if (downloadUrlCurrentAgeInSecs < _downloadUrlExpirationInterval) {
            // URL hasnt expired yet
            continue;
        }
        final long volumeId = volumeOnImageStore.getVolumeId();
        s_logger.debug("Removing download url " + volumeOnImageStore.getExtractUrl() + " for volume id " + volumeId);
        // Remove it from image store
        final ImageStoreEntity secStore = (ImageStoreEntity) _dataStoreMgr.getDataStore(volumeOnImageStore.getDataStoreId(), DataStoreRole.Image);
        secStore.deleteExtractUrl(volumeOnImageStore.getInstallPath(), volumeOnImageStore.getExtractUrl(), Upload.Type.VOLUME);
        // Now expunge it from DB since this entry was created only for download purpose
        _volumeStoreDao.expunge(volumeOnImageStore.getId());
        final Volume volume = _volumeDao.findById(volumeId);
        if (volume != null && volume.getState() == Volume.State.Expunged) {
            _volumeDao.remove(volumeId);
        }
    }
    // Cleanup expired template URLs
    final List<TemplateDataStoreVO> templatesOnImageStoreList = _templateStoreDao.listTemplateDownloadUrls();
    for (final TemplateDataStoreVO templateOnImageStore : templatesOnImageStoreList) {
        final long downloadUrlCurrentAgeInSecs = DateUtil.getTimeDifference(DateUtil.now(), templateOnImageStore.getExtractUrlCreated());
        if (downloadUrlCurrentAgeInSecs < _downloadUrlExpirationInterval) {
            // URL hasnt expired yet
            continue;
        }
        s_logger.debug("Removing download url " + templateOnImageStore.getExtractUrl() + " for template id " + templateOnImageStore.getTemplateId());
        // Remove it from image store
        final ImageStoreEntity secStore = (ImageStoreEntity) _dataStoreMgr.getDataStore(templateOnImageStore.getDataStoreId(), DataStoreRole.Image);
        secStore.deleteExtractUrl(templateOnImageStore.getInstallPath(), templateOnImageStore.getExtractUrl(), Upload.Type.TEMPLATE);
        // Now remove download details from DB.
        templateOnImageStore.setExtractUrl(null);
        templateOnImageStore.setExtractUrlCreated(null);
        _templateStoreDao.update(templateOnImageStore.getId(), templateOnImageStore);
    }
}
Also used : VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO)

Example 37 with TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method registerTemplateForPostUpload.

@Override
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating post upload template")
public GetUploadParamsResponse registerTemplateForPostUpload(final GetUploadParamsForTemplateCmd cmd) throws ResourceAllocationException, MalformedURLException {
    final TemplateAdapter adapter = getAdapter(HypervisorType.getType(cmd.getHypervisor()));
    final TemplateProfile profile = adapter.prepare(cmd);
    final List<TemplateOrVolumePostUploadCommand> payload = adapter.createTemplateForPostUpload(profile);
    if (CollectionUtils.isNotEmpty(payload)) {
        final GetUploadParamsResponse response = new GetUploadParamsResponse();
        /*
             * There can be one or more commands depending on the number of secondary stores the template needs to go to. Taking the first one to do the url upload. The
             * template will be propagated to the rest through copy by management server commands.
             */
        final TemplateOrVolumePostUploadCommand firstCommand = payload.get(0);
        final String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
        final String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, firstCommand.getRemoteEndPoint(), firstCommand.getEntityUUID());
        response.setPostURL(new URL(url));
        // set the post url, this is used in the monitoring thread to determine the SSVM
        final TemplateDataStoreVO templateStore = _tmplStoreDao.findByTemplate(firstCommand.getEntityId(), DataStoreRole.getRole(firstCommand.getDataToRole()));
        if (templateStore != null) {
            templateStore.setExtractUrl(url);
            _tmplStoreDao.persist(templateStore);
        }
        response.setId(UUID.fromString(firstCommand.getEntityUUID()));
        final int timeout = ImageStoreUploadMonitorImpl.getUploadOperationTimeout();
        final DateTime currentDateTime = new DateTime(DateTimeZone.UTC);
        final String expires = currentDateTime.plusMinutes(timeout).toString();
        response.setTimeout(expires);
        final String key = _configDao.getValue(Config.SSVMPSK.key());
        /*
             * encoded metadata using the post upload config ssh key
             */
        final Gson gson = new GsonBuilder().create();
        final String metadata = EncryptionUtil.encodeData(gson.toJson(firstCommand), key);
        response.setMetadata(metadata);
        /*
             * signature calculated on the url, expiry, metadata.
             */
        response.setSignature(EncryptionUtil.generateSignature(metadata + url + expires, key));
        return response;
    } else {
        throw new CloudRuntimeException("Unable to register template.");
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) GetUploadParamsResponse(com.cloud.api.response.GetUploadParamsResponse) URL(java.net.URL) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) DateTime(org.joda.time.DateTime) TemplateOrVolumePostUploadCommand(com.cloud.storage.command.TemplateOrVolumePostUploadCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateProfile(com.cloud.storage.TemplateProfile) ActionEvent(com.cloud.event.ActionEvent)

Example 38 with TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method getAbsoluteIsoPath.

@Override
public Pair<String, String> getAbsoluteIsoPath(final long templateId, final long dataCenterId) {
    final TemplateDataStoreVO templateStoreRef = _tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, dataCenterId, VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
    if (templateStoreRef == null) {
        throw new CloudRuntimeException("Template " + templateId + " has not been completely downloaded to zone " + dataCenterId);
    }
    final DataStore store = _dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image);
    final String isoPath = store.getUri() + "/" + templateStoreRef.getInstallPath();
    return new Pair<>(isoPath, store.getUri());
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) Pair(com.cloud.utils.Pair)

Example 39 with TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO in project cosmic by MissionCriticalCloud.

the class DownloadMonitorImpl method initiateTemplateDownload.

private void initiateTemplateDownload(final DataObject template, final AsyncCompletionCallback<DownloadAnswer> callback) {
    boolean downloadJobExists = false;
    TemplateDataStoreVO vmTemplateStore = null;
    final DataStore store = template.getDataStore();
    vmTemplateStore = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
    if (vmTemplateStore == null) {
        vmTemplateStore = new TemplateDataStoreVO(store.getId(), template.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUri());
        vmTemplateStore.setDataStoreRole(store.getRole());
        vmTemplateStore = _vmTemplateStoreDao.persist(vmTemplateStore);
    } else if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
        downloadJobExists = true;
    }
    final Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
    if (vmTemplateStore != null) {
        start();
        final VirtualMachineTemplate tmpl = _templateDao.findById(template.getId());
        DownloadCommand dcmd = new DownloadCommand((TemplateObjectTO) (template.getTO()), maxTemplateSizeInBytes);
        dcmd.setProxy(getHttpProxy());
        if (downloadJobExists) {
            dcmd = new DownloadProgressCommand(dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART);
        }
        if (vmTemplateStore.isCopy()) {
            dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
        }
        final EndPoint ep = _epSelector.select(template);
        if (ep == null) {
            final String errMsg = "There is no secondary storage VM for downloading template to image store " + store.getName();
            s_logger.warn(errMsg);
            throw new CloudRuntimeException(errMsg);
        }
        final DownloadListener dl = new DownloadListener(ep, store, template, _timer, this, dcmd, callback);
        // initialize those auto-wired field in download listener.
        ComponentContext.inject(dl);
        if (downloadJobExists) {
            // due to handling existing download job issues, we still keep
            // downloadState in template_store_ref to avoid big change in
            // DownloadListener to use
            // new ObjectInDataStore.State transition. TODO: fix this later
            // to be able to remove downloadState from template_store_ref.
            s_logger.info("found existing download job");
            dl.setCurrState(vmTemplateStore.getDownloadState());
        }
        try {
            ep.sendMessageAsync(dcmd, new UploadListener.Callback(ep.getId(), dl));
        } catch (final Exception e) {
            s_logger.warn("Unable to start /resume download of template " + template.getId() + " to " + store.getName(), e);
            dl.setDisconnected();
            dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
        }
    }
}
Also used : VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) DownloadCommand(com.cloud.storage.command.DownloadCommand) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) UploadListener(com.cloud.storage.upload.UploadListener) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DownloadProgressCommand(com.cloud.storage.command.DownloadProgressCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore)

Aggregations

TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)39 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)17 VMTemplateVO (com.cloud.storage.VMTemplateVO)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)11 Date (java.util.Date)9 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)7 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)6 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)6 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)5 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)5 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 URISyntaxException (java.net.URISyntaxException)5 ExecutionException (java.util.concurrent.ExecutionException)5 ConfigurationException (javax.naming.ConfigurationException)5 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)4 CreateCmdResult (com.cloud.engine.subsystem.api.storage.CreateCmdResult)3