From 19a8f4a47116192dd597c817c06157eff8952347 Mon Sep 17 00:00:00 2001 From: alexandershpak Date: Tue, 6 Nov 2018 16:19:01 +0300 Subject: [PATCH 1/3] fix cli image snapshot --- src/cli/diagnostics.js | 6 +-- src/controllers/diagnostic-controller.js | 4 +- src/routes/diagnostics.js | 27 ++++++------- src/services/diagnostic-service.js | 50 ++++++++++++++++++------ 4 files changed, 55 insertions(+), 32 deletions(-) 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..7f39f5549 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(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); + } } }, { 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) { From 46d7c970f9773bb5ad09e6d586db67fe1ccfa4c2 Mon Sep 17 00:00:00 2001 From: alexandershpak Date: Tue, 6 Nov 2018 16:21:59 +0300 Subject: [PATCH 2/3] fix --- src/routes/diagnostics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/diagnostics.js b/src/routes/diagnostics.js index 7f39f5549..372828670 100644 --- a/src/routes/diagnostics.js +++ b/src/routes/diagnostics.js @@ -74,7 +74,7 @@ module.exports = [ .status(responseObject.code) .send(responseObject.body) } else { - res.writeHead(200, { + res.writeHead(successCode, { "Content-Length": responseObject.body['Content-Length'], "Content-Type": responseObject.body['Content-Type'], "Content-Disposition": "attachment; filename=" + responseObject.body.fileName From 0fc9f37438dc9e18c6f8bed4eec07ec39862a437 Mon Sep 17 00:00:00 2001 From: alexandershpak Date: Tue, 6 Nov 2018 17:10:28 +0300 Subject: [PATCH 3/3] fix --- src/helpers/error-messages.js | 1 + src/services/diagnostic-service.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/helpers/error-messages.js b/src/helpers/error-messages.js index 6d6e3ab9f..1e82c7de2 100644 --- a/src/helpers/error-messages.js +++ b/src/helpers/error-messages.js @@ -48,6 +48,7 @@ module.exports = { INVALID_MICROSERVICE_USER: 'Invalid microservice user or UUID', ROUTE_NOT_FOUND: 'Route not found', IMAGE_SNAPSHOT_WITHOUT_FOG: 'Can not run image snapshot for microservice without ioFog.', + IMAGE_SNAPSHOT_NOT_AVAILABLE: 'Image snapshot is not available for this microservice.', FILE_DOES_NOT_EXIST: 'File does not exist.', RESTRICTED_PUBLISHER: "You are not allowed to add catalog item as 'Eclipse ioFog' publisher", REQUIRED_FOG_NODE: 'ioFog node is required.', diff --git a/src/services/diagnostic-service.js b/src/services/diagnostic-service.js index 2b82a1a67..f3ac70836 100644 --- a/src/services/diagnostic-service.js +++ b/src/services/diagnostic-service.js @@ -123,7 +123,7 @@ const getMicroserviceImageSnapshot = async function (microserviceUuid, user, isC imageSnapshot: '' }; - if (!microservice.imageSnapshot) { + if (!microservice.imageSnapshot || microservice.imageSnapshot === 'get_image') { throw new Errors.ValidationError(ErrorMessages.IMAGE_SNAPSHOT_NOT_AVAILABLE) } let _path = microservice.imageSnapshot;