It is now possible to save the results to an AWS S3 bucket

This commit is contained in:
Gaël Métais 2021-03-14 01:49:23 +00:00
parent 2da4371847
commit 9211074b32
3 changed files with 123 additions and 10 deletions

View file

@ -5,10 +5,11 @@ var ylt = require('../../index');
var ScreenshotHandler = require('../../screenshotHandler');
var RunsQueue = require('../datastores/runsQueue');
var RunsDatastore = require('../datastores/runsDatastore');
var ResultsDatastore = require('../datastores/resultsDatastore');
var serverSettings = (process.env.IS_TEST) ? require('../../../test/fixtures/settings.json') : require('../../../server_config/settings.json');
var ResultsDatastore = (serverSettings.awsHosting) ? require('../datastores/awsResultsDatastore') : require('../datastores/resultsDatastore');
var ApiController = function(app) {
'use strict';
@ -134,9 +135,6 @@ var ApiController = function(app) {
.then(function() {
// Remove uneeded temp screenshot path
delete data.params.options.screenshot;
// Here we can remove tools results if not needed
delete data.toolsResults.phantomas.offenders.requests;
return resultsDatastore.saveResult(data);
})

View file

@ -0,0 +1,119 @@
const Q = require('q');
const debug = require('debug')('ylt:resultsDatastore');
const path = require('path');
const AWS = require('aws-sdk');
function ResultsDatastore() {
'use strict';
const serverSettings = require('../../../server_config/settings.json');
const s3 = new AWS.S3();
const resultFileName = 'results.json';
const resultScreenshotName = 'screenshot.jpg';
const resultsFolderName = 'results';
this.saveResult = function(testResults) {
const resultFilePath = path.join(resultsFolderName, testResults.runId, resultFileName);
const screenshotFilePath = path.join(resultsFolderName, testResults.runId, resultScreenshotName);
debug('Starting to save screenshot then results.json file on s3...');
return saveScreenshotIfExists(testResults, screenshotFilePath)
.then(function() {
debug('Saving results file to s3, destination is %s', resultFilePath);
return s3PutObject(resultFilePath, JSON.stringify(testResults, null, 2));
});
};
this.getResult = function(runId) {
const resultFilePath = path.join(resultsFolderName, runId, resultFileName);
debug('Reading results (runID = %s) from AWS s3...', runId);
return s3GetObject(resultFilePath).then(function(bodyBuffer) {
return JSON.parse(bodyBuffer.toString('utf-8'));
});
};
// If there is a screenshot, save it as screenshot.jpg in the same folder as the results
function saveScreenshotIfExists(testResults, imagePath) {
var deferred = Q.defer();
if (testResults.screenshotBuffer) {
s3PutObject(imagePath, testResults.screenshotBuffer)
.fail(function() {
debug('Image %s could not be saved on s3. Ignoring.', imagePath);
})
.finally(function() {
delete testResults.screenshotBuffer;
deferred.resolve();
});
} else {
debug('Screenshot not found');
deferred.resolve();
}
return deferred.promise;
}
this.getScreenshot = function(runId) {
const screenshotFilePath = path.join(resultsFolderName, runId, resultScreenshotName);
debug('Retrieving screenshot (runID = %s) from s3...', runId);
return s3GetObject(screenshotFilePath);
};
function s3PutObject(path, body, ignoreError) {
var deferred = Q.defer();
s3.putObject({
Bucket: serverSettings.awsHosting.s3.bucket,
Key: path,
Body: body
}, function(err, data) {
if (err) {
debug('Could not save file %s on s3', path);
debug(err);
deferred.reject('File saving failed on s3');
} else {
debug('File %s saved on s3', path);
deferred.resolve();
}
});
return deferred.promise;
}
function s3GetObject(path) {
var deferred = Q.defer();
s3.getObject({
Bucket: serverSettings.awsHosting.s3.bucket,
Key: path
}, function(err, data) {
if (err) {
debug('Failed retrieving object %s from s3', path);
debug(err);
deferred.reject(err);
} else {
debug('Response for %s received from s3...', path);
deferred.resolve(data.Body);
}
});
return deferred.promise;
}
}
module.exports = ResultsDatastore;

View file

@ -49,13 +49,13 @@ function ResultsDatastore() {
};
this.deleteResult = function(runId) {
/*this.deleteResult = function(runId) {
var folder = path.join(resultsDir, runId);
debug('Deleting results (runID = %s) from disk...', runId);
return Q.nfcall(rimraf, folder);
};
};*/
// The folder /results/folderName/
@ -92,10 +92,6 @@ function ResultsDatastore() {
return deferred.promise;
}
this.getResultFolder = function(runId) {
return path.join(resultsDir, runId);
};
// If there is a screenshot, save it as screenshot.jpg in the same folder as the results
function saveScreenshotIfExists(testResults, path) {
var deferred = Q.defer();