Search in sources :

Example 1 with TemplateOrVolumePostUploadCommand

use of org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand in project cloudstack by apache.

the class TemplateManagerImpl method registerPostUploadInternal.

/**
 * Internal register template or ISO method - post local upload
 * @param adapter
 * @param profile
 */
private GetUploadParamsResponse registerPostUploadInternal(TemplateAdapter adapter, TemplateProfile profile) throws MalformedURLException {
    List<TemplateOrVolumePostUploadCommand> payload = adapter.createTemplateForPostUpload(profile);
    if (CollectionUtils.isNotEmpty(payload)) {
        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.
             */
        TemplateOrVolumePostUploadCommand firstCommand = payload.get(0);
        String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
        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
        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()));
        int timeout = ImageStoreUploadMonitorImpl.getUploadOperationTimeout();
        DateTime currentDateTime = new DateTime(DateTimeZone.UTC);
        String expires = currentDateTime.plusMinutes(timeout).toString();
        response.setTimeout(expires);
        String key = _configDao.getValue(Config.SSVMPSK.key());
        /*
             * encoded metadata using the post upload config ssh key
             */
        Gson gson = new GsonBuilder().create();
        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 : TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) GsonBuilder(com.google.gson.GsonBuilder) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Gson(com.google.gson.Gson) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) GetUploadParamsResponse(org.apache.cloudstack.api.response.GetUploadParamsResponse) URL(java.net.URL) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) DateTime(org.joda.time.DateTime)

Example 2 with TemplateOrVolumePostUploadCommand

use of org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand in project cloudstack by apache.

the class NfsSecondaryStorageResource method getTemplateOrVolumePostUploadCmd.

private TemplateOrVolumePostUploadCommand getTemplateOrVolumePostUploadCmd(String metadata) {
    TemplateOrVolumePostUploadCommand cmd = null;
    try {
        Gson gson = new GsonBuilder().create();
        cmd = gson.fromJson(EncryptionUtil.decodeData(metadata, getPostUploadPSK()), TemplateOrVolumePostUploadCommand.class);
    } catch (Exception ex) {
        s_logger.error("exception while decoding and deserialising metadata", ex);
    }
    return cmd;
}
Also used : TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) 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)

Example 3 with TemplateOrVolumePostUploadCommand

use of org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand in project cloudstack by apache.

the class NfsSecondaryStorageResource method createUploadEntity.

public UploadEntity createUploadEntity(String uuid, String metadata, long contentLength) {
    TemplateOrVolumePostUploadCommand cmd = getTemplateOrVolumePostUploadCmd(metadata);
    UploadEntity uploadEntity = null;
    if (cmd == null) {
        String errorMessage = "unable decode and deserialize metadata.";
        updateStateMapWithError(uuid, errorMessage);
        throw new InvalidParameterValueException(errorMessage);
    } else {
        uuid = cmd.getEntityUUID();
        processTimeout = cmd.getProcessTimeout();
        if (isOneTimePostUrlUsed(cmd)) {
            uploadEntity = uploadEntityStateMap.get(uuid);
            StringBuilder errorMessage = new StringBuilder("The one time post url is already used");
            if (uploadEntity != null) {
                errorMessage.append(" and the upload is in ").append(uploadEntity.getUploadState()).append(" state.");
            }
            throw new InvalidParameterValueException(errorMessage.toString());
        }
        int maxSizeInGB = Integer.parseInt(cmd.getMaxUploadSize());
        int contentLengthInGB = getSizeInGB(contentLength);
        if (contentLengthInGB > maxSizeInGB) {
            String errorMessage = "Maximum file upload size exceeded. Content Length received: " + contentLengthInGB + "GB. Maximum allowed size: " + maxSizeInGB + "GB.";
            updateStateMapWithError(uuid, errorMessage);
            throw new InvalidParameterValueException(errorMessage);
        }
        checkSecondaryStorageResourceLimit(cmd, contentLengthInGB);
        try {
            String absolutePath = cmd.getAbsolutePath();
            uploadEntity = new UploadEntity(uuid, cmd.getEntityId(), UploadEntity.Status.IN_PROGRESS, cmd.getName(), absolutePath);
            uploadEntity.setMetaDataPopulated(true);
            uploadEntity.setResourceType(UploadEntity.ResourceType.valueOf(cmd.getType()));
            uploadEntity.setProcessTimeout(processTimeout);
            uploadEntity.setFormat(Storage.ImageFormat.valueOf(cmd.getImageFormat()));
            // relative path with out ssvm mount info.
            uploadEntity.setTemplatePath(absolutePath);
            String dataStoreUrl = cmd.getDataTo();
            String installPathPrefix = this.getRootDir(dataStoreUrl, cmd.getNfsVersion()) + File.separator + absolutePath;
            uploadEntity.setInstallPathPrefix(installPathPrefix);
            uploadEntity.setHvm(cmd.getRequiresHvm());
            uploadEntity.setChksum(cmd.getChecksum());
            uploadEntity.setMaxSizeInGB(maxSizeInGB);
            uploadEntity.setDescription(cmd.getDescription());
            uploadEntity.setContentLength(contentLength);
            // create a install dir
            if (!_storage.exists(installPathPrefix)) {
                _storage.mkdir(installPathPrefix);
            }
            uploadEntityStateMap.put(uuid, uploadEntity);
        } catch (Exception e) {
            // upload entity will be null incase an exception occurs and the handler will not proceed.
            s_logger.error("exception occurred while creating upload entity ", e);
            updateStateMapWithError(uuid, e.getMessage());
        }
    }
    return uploadEntity;
}
Also used : UploadEntity(org.apache.cloudstack.storage.template.UploadEntity) TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) 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)

Example 4 with TemplateOrVolumePostUploadCommand

use of org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand in project cloudstack by apache.

the class VolumeApiServiceImpl method uploadVolume.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPLOAD, eventDescription = "uploading volume for post upload", async = true)
public GetUploadParamsResponse uploadVolume(final GetUploadParamsForVolumeCmd cmd) throws ResourceAllocationException, MalformedURLException {
    Account caller = CallContext.current().getCallingAccount();
    long ownerId = cmd.getEntityOwnerId();
    final Account owner = _entityMgr.findById(Account.class, ownerId);
    final Long zoneId = cmd.getZoneId();
    final String volumeName = cmd.getName();
    String format = cmd.getFormat();
    final Long diskOfferingId = cmd.getDiskOfferingId();
    String imageStoreUuid = cmd.getImageStoreUuid();
    final DataStore store = _tmpltMgr.getImageStore(imageStoreUuid, zoneId);
    validateVolume(caller, ownerId, zoneId, volumeName, null, format, diskOfferingId);
    return Transaction.execute(new TransactionCallbackWithException<GetUploadParamsResponse, MalformedURLException>() {

        @Override
        public GetUploadParamsResponse doInTransaction(TransactionStatus status) throws MalformedURLException {
            VolumeVO volume = persistVolume(owner, zoneId, volumeName, null, cmd.getFormat(), diskOfferingId, Volume.State.NotUploaded);
            VolumeInfo vol = volFactory.getVolume(volume.getId());
            RegisterVolumePayload payload = new RegisterVolumePayload(null, cmd.getChecksum(), cmd.getFormat());
            vol.addPayload(payload);
            Pair<EndPoint, DataObject> pair = volService.registerVolumeForPostUpload(vol, store);
            EndPoint ep = pair.first();
            DataObject dataObject = pair.second();
            GetUploadParamsResponse response = new GetUploadParamsResponse();
            String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
            String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, ep.getPublicAddr(), vol.getUuid());
            response.setPostURL(new URL(url));
            // set the post url, this is used in the monitoring thread to determine the SSVM
            VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(vol.getId());
            assert (volumeStore != null) : "sincle volume is registered, volumestore cannot be null at this stage";
            volumeStore.setExtractUrl(url);
            _volumeStoreDao.persist(volumeStore);
            response.setId(UUID.fromString(vol.getUuid()));
            int timeout = ImageStoreUploadMonitorImpl.getUploadOperationTimeout();
            DateTime currentDateTime = new DateTime(DateTimeZone.UTC);
            String expires = currentDateTime.plusMinutes(timeout).toString();
            response.setTimeout(expires);
            String key = _configDao.getValue(Config.SSVMPSK.key());
            /*
                 * encoded metadata using the post upload config key
                 */
            TemplateOrVolumePostUploadCommand command = new TemplateOrVolumePostUploadCommand(vol.getId(), vol.getUuid(), volumeStore.getInstallPath(), cmd.getChecksum(), vol.getType().toString(), vol.getName(), vol.getFormat().toString(), dataObject.getDataStore().getUri(), dataObject.getDataStore().getRole().toString());
            command.setLocalPath(volumeStore.getLocalDownloadPath());
            // using the existing max upload size configuration
            command.setProcessTimeout(NumbersUtil.parseLong(_configDao.getValue("vmware.package.ova.timeout"), 3600));
            command.setMaxUploadSize(_configDao.getValue(Config.MaxUploadVolumeSize.key()));
            command.setDefaultMaxAccountSecondaryStorage(_configDao.getValue(Config.DefaultMaxAccountSecondaryStorage.key()));
            command.setAccountId(vol.getAccountId());
            Gson gson = new GsonBuilder().create();
            String metadata = EncryptionUtil.encodeData(gson.toJson(command), key);
            response.setMetadata(metadata);
            /*
                 * signature calculated on the url, expiry, metadata.
                 */
            response.setSignature(EncryptionUtil.generateSignature(metadata + url + expires, key));
            return response;
        }
    });
}
Also used : Account(com.cloud.user.Account) MalformedURLException(java.net.MalformedURLException) GsonBuilder(com.google.gson.GsonBuilder) TransactionStatus(com.cloud.utils.db.TransactionStatus) Gson(com.google.gson.Gson) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) GetUploadParamsResponse(org.apache.cloudstack.api.response.GetUploadParamsResponse) URL(java.net.URL) DateTime(org.joda.time.DateTime) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) Pair(com.cloud.utils.Pair) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

TemplateOrVolumePostUploadCommand (org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 InternalErrorException (com.cloud.exception.InternalErrorException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ConfigurationException (javax.naming.ConfigurationException)2 GetUploadParamsResponse (org.apache.cloudstack.api.response.GetUploadParamsResponse)2 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)2 DateTime (org.joda.time.DateTime)2 ActionEvent (com.cloud.event.ActionEvent)1 Account (com.cloud.user.Account)1 Pair (com.cloud.utils.Pair)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 MalformedURLException (java.net.MalformedURLException)1