diff --git a/src/cli/diagnostics.js b/src/cli/diagnostics.js index 2640d70a2..434b54ddd 100644 --- a/src/cli/diagnostics.js +++ b/src/cli/diagnostics.js @@ -143,15 +143,15 @@ const _postMicroserviceStraceDataToFtp = async function (obj) { const _postMicroserviceImageSnapshotCreate = async function (obj) { logger.info(JSON.stringify(obj)); - await DiagnosticService.postMicroserviceImageSnapshotCreate(obj.microserviceId, obj, {}, true); + await DiagnosticService.postMicroserviceImageSnapshotCreate(obj.microserviceId, {}, true); logger.info('Microservice image snapshot has been created successfully'); }; const _getMicroserviceImageSnapshot = async function (obj) { logger.info(JSON.stringify(obj)); - await DiagnosticService.getMicroserviceImageSnapshot(obj.microserviceId, obj, {}, true); - logger.info('Microservice images snapshot has been downloaded successfully'); + const filePath = await DiagnosticService.getMicroserviceImageSnapshot(obj.microserviceId, {}, true); + logger.info('Microservice images path = ' + filePath); }; module.exports = new Diagnostics(); \ No newline at end of file diff --git a/src/controllers/diagnostic-controller.js b/src/controllers/diagnostic-controller.js index 2e70050fd..699a6c382 100644 --- a/src/controllers/diagnostic-controller.js +++ b/src/controllers/diagnostic-controller.js @@ -36,13 +36,13 @@ const postMicroserviceStraceDataToFtpEndPoint = async function (req, user) { const createMicroserviceImageSnapshotEndPoint = async function (req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); logger.info("Microservice id: " + req.params.id); - return await DiagnosticService.postMicroserviceImageSnapshotCreate(req.params.id, req.body, user, false); + return await DiagnosticService.postMicroserviceImageSnapshotCreate(req.params.id, user, false); }; const getMicroserviceImageSnapshotEndPoint = async function (req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); logger.info("Microservice id: " + req.params.id); - return await DiagnosticService.getMicroserviceImageSnapshot(req.params.id, req.body, user, false); + return await DiagnosticService.getMicroserviceImageSnapshot(req.params.id, user, false); }; module.exports = { diff --git a/src/routes/diagnostics.js b/src/routes/diagnostics.js index ebefa101c..372828670 100644 --- a/src/routes/diagnostics.js +++ b/src/routes/diagnostics.js @@ -69,21 +69,18 @@ module.exports = [ errorCodes ); const responseObject = await getMicroserviceImageSnapshotEndPoint(req); - - fs.exists(responseObject.body.filePath, function(exists){ - if (exists) { - res.writeHead(200, { - "Content-Length": responseObject.body['Content-Length'], - "Content-Type": responseObject.body['Content-Type'], - "Content-Disposition": "attachment; filename=" + responseObject.body.fileName - }); - fs.createReadStream(responseObject.body.filePath).pipe(res); - } else { - res.writeHead(400, {"Content-Type": "text/plain"}); - res.end("ERROR File does not exist"); - res.end(ErrorMessages.FILE_DOES_NOT_EXIST); - } - }); + if (responseObject.code !== successCode) { + res + .status(responseObject.code) + .send(responseObject.body) + } else { + res.writeHead(successCode, { + "Content-Length": responseObject.body['Content-Length'], + "Content-Type": responseObject.body['Content-Type'], + "Content-Disposition": "attachment; filename=" + responseObject.body.fileName + }); + fs.createReadStream(responseObject.body.filePath).pipe(res); + } } }, { diff --git a/src/services/diagnostic-service.js b/src/services/diagnostic-service.js index b9b20efb6..2b82a1a67 100644 --- a/src/services/diagnostic-service.js +++ b/src/services/diagnostic-service.js @@ -76,8 +76,20 @@ const postMicroserviceStraceDatatoFtp = async function (id, data, user, isCLI, t _deleteFile(filePath); }; -const postMicroserviceImageSnapshotCreate = async function (id, data, user, isCLI, transaction) { - const microservice = await MicroserviceService.getMicroserviceWithTransaction(id, user, isCLI, transaction); +const postMicroserviceImageSnapshotCreate = async function (microserviceUuid, user, isCLI, transaction) { + const where = isCLI ? + { + uuid: microserviceUuid + } + : + { + uuid: microserviceUuid, + updatedBy: user.id + }; + + + const microservice = await MicroserviceManager.findOneWithDependencies(where, {}, transaction); + if (microservice.iofogUuid === null) { throw new Errors.ValidationError(ErrorMessages.IMAGE_SNAPSHOT_WITHOUT_FOG); } @@ -92,31 +104,45 @@ const postMicroserviceImageSnapshotCreate = async function (id, data, user, isCL await ChangeTrackingManager.update({iofogUuid: microservice.iofogUuid}, {isImageSnapshot: true}, transaction); }; -const getMicroserviceImageSnapshot = async function (id, data, user, isCLI, transaction) { - const microservice = await MicroserviceService.getMicroserviceWithTransaction(id, user, isCLI, transaction); +const getMicroserviceImageSnapshot = async function (microserviceUuid, user, isCLI, transaction) { + const where = isCLI ? + { + uuid: microserviceUuid + } + : + { + uuid: microserviceUuid, + updatedBy: user.id + }; + const microservice = await MicroserviceManager.findOneWithDependencies(where, {}, transaction); if (microservice.iofogUuid === null) { throw new Errors.ValidationError(ErrorMessages.IMAGE_SNAPSHOT_WITHOUT_FOG); } const microserviceToUpdate = { - imageSnapshot: '' - }; + imageSnapshot: '' + }; - if (microservice.imageSnapshot){ + if (!microservice.imageSnapshot) { + throw new Errors.ValidationError(ErrorMessages.IMAGE_SNAPSHOT_NOT_AVAILABLE) + } + let _path = microservice.imageSnapshot; + logger.info('successfully deleted ' + microservice.imageSnapshot); + await MicroserviceManager.update({uuid: microservice.uuid}, microserviceToUpdate, transaction); + if (isCLI) { + return _path + } else { let mimetype = mime.lookup(microservice.imageSnapshot); - let _path = microservice.imageSnapshot; let stat = fs.statSync(_path); let fileSize = stat.size; - logger.info('successfully deleted ' + microservice.imageSnapshot); - await MicroserviceManager.update({uuid: microservice.uuid}, microserviceToUpdate, transaction); - return { 'Content-Length': fileSize, - 'Content-Type' : mimetype, + 'Content-Type': mimetype, fileName: _path.split(new RegExp('/'))[1], filePath: _path }; } + }; const _sendFileToFtp = async function (data, filePath) {