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();
}
}
}
Aggregations