Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cli/diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions src/controllers/diagnostic-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
27 changes: 12 additions & 15 deletions src/routes/diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},
{
Expand Down
50 changes: 38 additions & 12 deletions src/services/diagnostic-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down