Search in sources :

Example 1 with Type

use of com.cloud.storage.Upload.Type in project cloudstack by apache.

the class UploadMonitorImpl method extractTemplate.

@Override
public Long extractTemplate(VMTemplateVO template, String url, TemplateDataStoreVO vmTemplateHost, Long dataCenterId, long eventId, long asyncJobId, AsyncJobManager asyncMgr) {
    Type type = (template.getFormat() == ImageFormat.ISO) ? Type.ISO : Type.TEMPLATE;
    DataStore secStore = storeMgr.getImageStore(dataCenterId);
    UploadVO uploadTemplateObj = new UploadVO(secStore.getId(), template.getId(), new Date(), Upload.Status.NOT_UPLOADED, type, url, Mode.FTP_UPLOAD);
    _uploadDao.persist(uploadTemplateObj);
    if (vmTemplateHost != null) {
        start();
        UploadCommand ucmd = new UploadCommand(template, url, vmTemplateHost.getInstallPath(), vmTemplateHost.getSize());
        UploadListener ul = new UploadListener(secStore, _timer, _uploadDao, uploadTemplateObj, this, ucmd, template.getAccountId(), template.getName(), type, eventId, asyncJobId, asyncMgr);
        _listenerMap.put(uploadTemplateObj, ul);
        try {
            EndPoint ep = _epSelector.select(secStore);
            if (ep == null) {
                String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                return null;
            }
            ep.sendMessageAsync(ucmd, new UploadListener.Callback(ep.getId(), ul));
        } catch (Exception e) {
            s_logger.warn("Unable to start upload of " + template.getUniqueName() + " from " + secStore.getName() + " to " + url, e);
            ul.setDisconnected();
            ul.scheduleStatusCheck(RequestType.GET_OR_RESTART);
        }
        return uploadTemplateObj.getId();
    }
    return null;
}
Also used : Type(com.cloud.storage.Upload.Type) RequestType(com.cloud.agent.api.storage.UploadProgressCommand.RequestType) UploadCommand(com.cloud.agent.api.storage.UploadCommand) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) UploadVO(com.cloud.storage.UploadVO) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) Date(java.util.Date) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 2 with Type

use of com.cloud.storage.Upload.Type in project cloudstack by apache.

the class UploadMonitorImpl method createEntityDownloadURL.

@Override
public UploadVO createEntityDownloadURL(VMTemplateVO template, TemplateDataStoreVO vmTemplateHost, Long dataCenterId, long eventId) {
    String errorString = "";
    boolean success = false;
    Type type = (template.getFormat() == ImageFormat.ISO) ? Type.ISO : Type.TEMPLATE;
    // find an endpoint to send command
    DataStore store = storeMgr.getDataStore(vmTemplateHost.getDataStoreId(), DataStoreRole.Image);
    EndPoint ep = _epSelector.select(store);
    if (ep == null) {
        String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
        s_logger.error(errMsg);
        return null;
    }
    //Check if it already exists.
    List<UploadVO> extractURLList = _uploadDao.listByTypeUploadStatus(template.getId(), type, UploadVO.Status.DOWNLOAD_URL_CREATED);
    if (extractURLList.size() > 0) {
        // do some check here
        UploadVO upload = extractURLList.get(0);
        String uploadUrl = extractURLList.get(0).getUploadUrl();
        String[] token = uploadUrl.split("/");
        // example: uploadUrl = https://10-11-101-112.realhostip.com/userdata/2fdd9a70-9c4a-4a04-b1d5-1e41c221a1f9.iso
        // then token[2] = 10-11-101-112.realhostip.com, token[4] = 2fdd9a70-9c4a-4a04-b1d5-1e41c221a1f9.iso
        String hostname = ep.getPublicAddr().replace(".", "-") + ".";
        if (// ssvm publicip and domain suffix not changed
        (token != null) && (token.length == 5) && (token[2].equals(hostname + _ssvmUrlDomain)))
            return extractURLList.get(0);
        else if ((token != null) && (token.length == 5) && (token[2].startsWith(hostname))) {
            // domain suffix changed
            String uuid = token[4];
            uploadUrl = generateCopyUrl(ep.getPublicAddr(), uuid);
            UploadVO vo = _uploadDao.createForUpdate();
            vo.setLastUpdated(new Date());
            vo.setUploadUrl(uploadUrl);
            _uploadDao.update(upload.getId(), vo);
            return _uploadDao.findById(upload.getId(), true);
        } else {
            // ssvm publicip changed
            return null;
        }
    }
    // It doesn't exist so create a DB entry.
    UploadVO uploadTemplateObj = new UploadVO(vmTemplateHost.getDataStoreId(), template.getId(), new Date(), Status.DOWNLOAD_URL_NOT_CREATED, 0, type, Mode.HTTP_DOWNLOAD);
    uploadTemplateObj.setInstallPath(vmTemplateHost.getInstallPath());
    _uploadDao.persist(uploadTemplateObj);
    try {
        // Create Symlink at ssvm
        String path = vmTemplateHost.getInstallPath();
        // adding "." + vhd/ova... etc.
        String uuid = UUID.randomUUID().toString() + "." + template.getFormat().getFileExtension();
        CreateEntityDownloadURLCommand cmd = new CreateEntityDownloadURLCommand(((ImageStoreEntity) store).getMountPoint(), path, uuid, null);
        Answer ans = ep.sendMessage(cmd);
        if (ans == null || !ans.getResult()) {
            errorString = "Unable to create a link for " + type + " id:" + template.getId() + "," + (ans == null ? "" : ans.getDetails());
            s_logger.error(errorString);
            throw new CloudRuntimeException(errorString);
        }
        //Construct actual URL locally now that the symlink exists at SSVM
        String extractURL = generateCopyUrl(ep.getPublicAddr(), uuid);
        UploadVO vo = _uploadDao.createForUpdate();
        vo.setLastUpdated(new Date());
        vo.setUploadUrl(extractURL);
        vo.setUploadState(Status.DOWNLOAD_URL_CREATED);
        _uploadDao.update(uploadTemplateObj.getId(), vo);
        success = true;
        return _uploadDao.findById(uploadTemplateObj.getId(), true);
    } finally {
        if (!success) {
            UploadVO uploadJob = _uploadDao.createForUpdate(uploadTemplateObj.getId());
            uploadJob.setLastUpdated(new Date());
            uploadJob.setErrorString(errorString);
            uploadJob.setUploadState(Status.ERROR);
            _uploadDao.update(uploadTemplateObj.getId(), uploadJob);
        }
    }
}
Also used : CreateEntityDownloadURLCommand(com.cloud.agent.api.storage.CreateEntityDownloadURLCommand) Answer(com.cloud.agent.api.Answer) Type(com.cloud.storage.Upload.Type) RequestType(com.cloud.agent.api.storage.UploadProgressCommand.RequestType) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) UploadVO(com.cloud.storage.UploadVO) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) Date(java.util.Date)

Aggregations

RequestType (com.cloud.agent.api.storage.UploadProgressCommand.RequestType)2 Type (com.cloud.storage.Upload.Type)2 UploadVO (com.cloud.storage.UploadVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 Date (java.util.Date)2 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)2 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)2 Answer (com.cloud.agent.api.Answer)1 CreateEntityDownloadURLCommand (com.cloud.agent.api.storage.CreateEntityDownloadURLCommand)1 UploadCommand (com.cloud.agent.api.storage.UploadCommand)1 ConfigurationException (javax.naming.ConfigurationException)1