Search in sources :

Example 6 with DownloadingInfo

use of com.emc.storageos.coordinator.client.model.DownloadingInfo in project coprhd-controller by CoprHD.

the class UpgradeService method uploadImage.

/**
 * Upload the image file given.
 * Consumes MediaType.APPLICATION_OCTET_STREAM.
 * This is an asynchronous operation.
 *
 * @brief Upload the specified image file
 * @prereq Cluster state should be STABLE
 * @return Cluster information.
 */
@POST
@Path("image/upload")
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN })
@Consumes({ MediaType.APPLICATION_OCTET_STREAM })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response uploadImage(@Context HttpServletRequest request) {
    File file = null;
    String svcId = _coordinator.getMySvcId();
    _log.info("uploadImage to {} start", svcId);
    // validate
    if (!_coordinator.isClusterUpgradable()) {
        throw APIException.serviceUnavailable.clusterStateNotStable();
    }
    // maximal install number check
    RepositoryInfo targetInfo = null;
    try {
        targetInfo = _coordinator.getTargetInfo(RepositoryInfo.class);
    } catch (Exception e) {
        throw APIException.internalServerErrors.getObjectFromError("target repository info", "coordinator", e);
    }
    if (targetInfo.getVersions().size() > SyncInfoBuilder.MAX_SOFTWARE_VERSIONS) {
        throw APIException.badRequests.numberOfInstalledExceedsMax();
    }
    // length check
    String contentLength = request.getHeader("Content-Length");
    if (Long.parseLong(contentLength) <= 0 || Long.parseLong(contentLength) > MAX_UPLOAD_SIZE) {
        throw APIException.badRequests.fileSizeExceedsLimit(MAX_UPLOAD_SIZE);
    }
    try {
        // remove previous and upload to a temp file
        UpgradeImageUploader uploader = UpgradeImageUploader.getInstance(_upgradeManager);
        uploader.cleanUploadFiles();
        long versionSize = Long.valueOf(contentLength);
        _log.info("The size of the image is:" + versionSize);
        String version = VIPR_UNKNOWN_IMAGE_VERSION;
        initializeDownloadProgress(version, versionSize);
        file = uploader.startUpload(request.getInputStream(), version);
        // install image
        if (file == null || file != null && !file.exists()) {
            throw APIException.internalServerErrors.targetIsNullOrEmpty("Uploaded file");
        }
        version = _upgradeManager.getLocalRepository().installImage(file);
        // set target
        List<SoftwareVersion> newList = new ArrayList<SoftwareVersion>(targetInfo.getVersions());
        SoftwareVersion newVersion = new SoftwareVersion(version);
        if (newList.contains(newVersion)) {
            _log.info("Version has already been installed");
        } else {
            newList.add(newVersion);
            _coordinator.setTargetInfo(new RepositoryInfo(targetInfo.getCurrentVersion(), newList));
            DownloadingInfo temp = _coordinator.getNodeGlobalScopeInfo(DownloadingInfo.class, DOWNLOADINFO_KIND, svcId);
            _coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(version, versionSize, versionSize, DownloadStatus.COMPLETED, temp._errorCounter), DOWNLOADINFO_KIND, svcId);
            _coordinator.setTargetInfo(new DownloadingInfo(version, versionSize), false);
        }
        _log.info("uploadImage to {} end", svcId);
        auditUpgrade(OperationTypeEnum.UPLOAD_IMAGE, AuditLogManager.AUDITLOG_SUCCESS, null, targetInfo.getCurrentVersion().toString(), svcId);
        // return cluster status
        ClusterInfo clusterInfo = _coordinator.getClusterInfo();
        if (clusterInfo == null) {
            throw APIException.internalServerErrors.targetIsNullOrEmpty("Cluster info");
        }
        return toClusterResponse(clusterInfo);
    } catch (APIException ae) {
        throw ae;
    } catch (Exception e) {
        throw APIException.internalServerErrors.uploadInstallError(e);
    } finally {
        if (file != null && file.exists()) {
            file.delete();
        }
    }
}
Also used : DownloadingInfo(com.emc.storageos.coordinator.client.model.DownloadingInfo) ClusterInfo(com.emc.vipr.model.sys.ClusterInfo) SoftwareVersion(com.emc.storageos.coordinator.client.model.SoftwareVersion) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) RepositoryInfo(com.emc.storageos.coordinator.client.model.RepositoryInfo) ArrayList(java.util.ArrayList) File(java.io.File) RemoteRepositoryException(com.emc.storageos.systemservices.exceptions.RemoteRepositoryException) ServiceUnavailableException(com.emc.storageos.svcs.errorhandling.resources.ServiceUnavailableException) InvalidSoftwareVersionException(com.emc.storageos.coordinator.exceptions.InvalidSoftwareVersionException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) LocalRepositoryException(com.emc.storageos.systemservices.exceptions.LocalRepositoryException) IOException(java.io.IOException) CoordinatorClientException(com.emc.storageos.systemservices.exceptions.CoordinatorClientException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Aggregations

DownloadingInfo (com.emc.storageos.coordinator.client.model.DownloadingInfo)6 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)4 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)3 File (java.io.File)3 Path (javax.ws.rs.Path)3 InvalidSoftwareVersionException (com.emc.storageos.coordinator.exceptions.InvalidSoftwareVersionException)2 ServiceUnavailableException (com.emc.storageos.svcs.errorhandling.resources.ServiceUnavailableException)2 CoordinatorClientException (com.emc.storageos.systemservices.exceptions.CoordinatorClientException)2 LocalRepositoryException (com.emc.storageos.systemservices.exceptions.LocalRepositoryException)2 RemoteRepositoryException (com.emc.storageos.systemservices.exceptions.RemoteRepositoryException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 RepositoryInfo (com.emc.storageos.coordinator.client.model.RepositoryInfo)1 SoftwareVersion (com.emc.storageos.coordinator.client.model.SoftwareVersion)1 ClusterInfo (com.emc.vipr.model.sys.ClusterInfo)1 DownloadProgress (com.emc.vipr.model.sys.DownloadProgress)1 NodeProgress (com.emc.vipr.model.sys.NodeProgress)1