From 2a8f29b3ec58aae48a44616510e4e76513564111 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 20 May 2018 22:50:01 +0200 Subject: [PATCH] Server Side Code Reorganized --- Dashboard.js | 1 - NewBot.js | 5 + Project.njsproj | 13 + Server/ConfigReader.js | 940 ++++++++++++++++++++++++++++++++++ Server/Github.js | 108 ++++ Server/Storage.js | 102 ++++ index.html | 2 +- server.js | 1093 ++-------------------------------------- 8 files changed, 1198 insertions(+), 1066 deletions(-) create mode 100644 NewBot.js create mode 100644 Server/ConfigReader.js create mode 100644 Server/Github.js create mode 100644 Server/Storage.js diff --git a/Dashboard.js b/Dashboard.js index e1924efa4..5b388adde 100644 --- a/Dashboard.js +++ b/Dashboard.js @@ -33,7 +33,6 @@ let canvas; let markets; let ecosystem = newEcosystem(); - let marketFilesPeriods = '[' + '[' + 24 * 60 * 60 * 1000 + ',' + '"24-hs"' + ']' + ',' + diff --git a/NewBot.js b/NewBot.js new file mode 100644 index 000000000..a532ecbe6 --- /dev/null +++ b/NewBot.js @@ -0,0 +1,5 @@ +function newBot() { + + console.log("Hello World"); + +} diff --git a/Project.njsproj b/Project.njsproj index 220a41955..0295f4eb0 100644 --- a/Project.njsproj +++ b/Project.njsproj @@ -60,6 +60,18 @@ Code + + Code + + + Code + + + Code + + + Code + Code @@ -129,6 +141,7 @@ + diff --git a/Server/ConfigReader.js b/Server/ConfigReader.js new file mode 100644 index 000000000..3bf5339cb --- /dev/null +++ b/Server/ConfigReader.js @@ -0,0 +1,940 @@ +exports.newConfigReader = function newConfigReader() { + + let thisObject = { + loadConfigs: loadConfigs, + initialize: initialize + } + + const GITHUB = require('../Server/Github'); + let github = GITHUB.newGithub(); + + const STORAGE = require('../Server/Storage'); + let storage = STORAGE.newStorage(); + + let serverConfig; + let githubData; + let storageData; + let ecosystem; + let ecosystemObject; + + return thisObject; + + function initialize(pEcosystem, pEcosystemObject, pServerConfig, pGithubData, pStorageData, callBackFunction) { + + serverConfig = pServerConfig; + githubData = pGithubData; + storageData = pStorageData; + ecosystem = pEcosystem; + ecosystemObject = pEcosystemObject; + + readServerConfig(); + + function readServerConfig() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readServerConfig -> Entering function."); } + + let fs = require('fs'); + + let fileName = './this.server.config.json'; + fs.readFile(fileName, onFileRead); + + function onFileRead(err, file) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readServerConfig -> onFileRead -> Entering function."); } + + let fileText; + + fileText = file.toString(); + fileText = fileText.trim(); // remove first byte with some encoding. + + serverConfig = JSON.parse(fileText); + + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readServerConfig -> onFileRead -> fileText = " + fileText); } + + CONSOLE_LOG = serverConfig.webServerLog.console; + LOG_FILE_CONTENT = serverConfig.webServerLog.fileContent; + + /* Finalize initializations. */ + + github.initialize(githubData); + storage.initialize(storageData, serverConfig); + + callBackFunction(serverConfig); + } + catch (err) { + console.log("[ERROR] initialize -> readServerConfig -> onFileRead -> File = " + fileName + " Error = " + err); + } + + } + } + catch (err) { + console.log("[ERROR] initialize -> readServerConfig -> Error = " + err); + } + } + } + + function loadConfigs(callBackFunction) { + + readEcosystemConfig(); + + function readEcosystemConfig() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Entering function."); } + + /* + + This configuration file is the backbone of the system. The first file we are going to get is a template where other configurations are + injected and the files ends up inflated with all these configs in one single JSON object that in turn is later injected into a + javascript module with an object that is going to instantiate it at run-time. + + */ + + switch (serverConfig.configAndPlugins.Location) { + + case 'Cloud': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Cloud -> Entering Case."); } + + storage.getStorageData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Cloud -> onDataArrived -> Entering function."); } + + ecosystem = pData.toString(); + ecosystem = ecosystem.trim(); // remove first byte with some encoding. + + ecosystemObject = JSON.parse(ecosystem); + readHostsConfigs(); + } + catch (err) { + console.log("[ERROR] initialize -> readEcosystemConfig -> Cloud -> onDataArrived -> Error = " + err); + } + } + + break; + } + + case 'File System': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> File System -> Entering Case."); } + + let fs = require('fs'); + try { + let fileName = '../AAPlatform/ecosystem.json'; + fs.readFile(fileName, onFileRead); + + function onFileRead(err, file) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> File System -> onFileRead -> Entering function."); } + + ecosystem = file.toString(); + ecosystem = ecosystem.trim(); // remove first byte with some encoding. + + ecosystemObject = JSON.parse(ecosystem); + readHostsConfigs(); + } + catch (err) { + console.log("[ERROR] initialize -> readEcosystemConfig -> File System -> onFileRead -> File = " + fileName + " Error = " + err); + } + + } + } + catch (err) { + console.log("[ERROR] initialize -> readEcosystemConfig -> File System -> File = " + fileName + " Error = " + err); + } + break; + } + + case 'Github': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Github -> Entering Case."); } + + github.getGithubData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Github -> onDataArrived -> Entering function."); } + + ecosystem = pData.toString(); + ecosystem = ecosystem.trim(); // remove first byte with some encoding. + + ecosystemObject = JSON.parse(ecosystem); + readHostsConfigs(); + } + catch (err) { + console.log("[ERROR] initialize -> readEcosystemConfig -> Github -> onDataArrived -> Error = " + err); + } + } + break; + } + } + } + catch (err) { + console.log("[ERROR] initialize -> readEcosystemConfig -> Error = " + err); + } + } + + function readHostsConfigs() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> Entering function."); } + + let requestsSent = 0; + let responsesReceived = 0; + + for (let i = 0; i < ecosystemObject.hosts.length; i++) { + + let host = ecosystemObject.hosts[i]; + + getCompetitions(); + getPlotters(); + + function getCompetitions() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Entering function."); } + + /* In this first section we will load the competitions configurations. */ + + for (let j = 0; j < host.competitions.length; j++) { + + let competition = host.competitions[j]; + + requestsSent++; + + switch (serverConfig.configAndPlugins.Location) { + + case 'Cloud': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> Entering Case."); } + + storage.getStorageData(host.codeName, competition.repo, competition.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> host.codeName = " + host.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> competition.repo = " + competition.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = competition.repo; + configObj.configFile = competition.configFile; + + host.competitions[j] = configObj; + + if (requestsSent === responsesReceived) { + + readDevTeamsConfigs(); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> Error = " + err); + } + } + break; + } + + case 'File System': { + + try { + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> Entering Case."); } + + let fs = require('fs'); + + let fileName = '../Competitions/' + host.codeName + '/' + competition.repo + '/' + competition.configFile; + fs.readFile(fileName, onFileRead); + + function onFileRead(err, pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> onFileRead -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> onFileRead -> fileName = " + fileName); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = competition.repo; + configObj.configFile = competition.configFile; + + host.competitions[j] = configObj; + + if (requestsSent === responsesReceived) { + + readDevTeamsConfigs(); + + } + } + catch (err) { + console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> onFileRead -> File = " + fileName + " Error = " + err); + } + } + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> File System -> File = " + fileName + " Error = " + err); + } + + break; + } + + case 'Github': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> Entering Case."); } + + github.getGithubData(host.codeName, competition.repo, competition.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> host.codeName = " + host.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> competition.repo = " + competition.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = competition.repo; + configObj.configFile = competition.configFile; + + host.competitions[j] = configObj; + + if (requestsSent === responsesReceived) { + + readDevTeamsConfigs(); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> Error = " + err); + } + } + + break; + } + } + } + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> getCompetitions -> Error = " + err); + } + } + + function getPlotters() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> getPlotters -> Entering function."); } + + /* In this second section we will load the competition plotters configurations. */ + + for (let j = 0; j < host.plotters.length; j++) { + + let plotter = host.plotters[j]; + + requestsSent++; + + switch (serverConfig.configAndPlugins.Location) { + + case 'Cloud': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> Entering Case."); } + + storage.getStorageData(host.codeName, plotter.repo, plotter.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> host.codeName = " + host.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> plotter.repo = " + plotter.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = plotter.repo; + configObj.configFile = plotter.configFile; + + host.plotters[j] = configObj; + + if (requestsSent === responsesReceived) { + + readDevTeamsConfigs(); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> Error = " + err); + } + } + + break; + } + + case 'File System': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> File System -> Entering Case."); } + + try { + + let fs = require('fs'); + + let fileName = '../Plotters/' + host.codeName + '/' + plotter.repo + '/' + plotter.configFile; + fs.readFile(fileName, onFileRead); + + function onFileRead(err, pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> File System -> onFileRead -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> File System -> onFileRead -> fileName = " + fileName); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = plotter.repo; + configObj.configFile = plotter.configFile; + + host.plotters[j] = configObj; + + if (requestsSent === responsesReceived) { + + readDevTeamsConfigs(); + + } + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> File System -> onFileRead -> File = " + fileName + " Error = " + err); + } + + } + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> File System -> File = " + fileName + " Error = " + err); + } + + break; + } + + case 'Github': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> Entering Case."); } + + github.getGithubData(host.codeName, plotter.repo, plotter.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> host.codeName = " + host.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> plotter.repo = " + plotter.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = plotter.repo; + configObj.configFile = plotter.configFile; + + host.plotters[j] = configObj; + + if (requestsSent === responsesReceived) { + + readDevTeamsConfigs(); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> Error = " + err); + } + } + break; + } + } + } + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> getCompetitions -> Error = " + err); + } + } + } + } + catch (err) { + console.log("[ERROR] initialize -> readHostsConfigs -> Error = " + err); + } + } + + function readDevTeamsConfigs() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> Entering function."); } + + /* + + Each bot has its configuration at its own repo since each team must be able to change it at will. + So what we do here is to use the master config at the AAPlatform repo that we already have on + memory and inject into it the config of each bot. + + Inmediatelly after that, we also load the Plotters configs using the same technique. + + */ + + let requestsSent = 0; + let responsesReceived = 0; + + for (let i = 0; i < ecosystemObject.devTeams.length; i++) { + + let devTeam = ecosystemObject.devTeams[i]; + + getBots(); + getPlotters(); + + function getBots() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Entering function."); } + + /* In the next section we are loading the bots configurations. */ + + for (let j = 0; j < devTeam.bots.length; j++) { + + let bot = devTeam.bots[j]; + + requestsSent++; + + switch (serverConfig.configAndPlugins.Location) { + + case 'Cloud': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> Entering Case."); } + + storage.getStorageData(devTeam.codeName, bot.repo, bot.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> bot.repo = " + bot.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + if (pData !== "{}") { // If for any reason we could not tet this config, then we just exclude this bot to improve resiliency. + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = bot.repo; + configObj.configFile = bot.configFile; + + addStoragePermissions(configObj); + + devTeam.bots[j] = configObj; + + } + + if (requestsSent === responsesReceived) { + + callBackFunction(ecosystem, ecosystemObject); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> Error = " + err); + } + } + break; + } + + case 'File System': { + + try { + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> File System -> Entering Case."); } + + let fs = require('fs'); + + let fileName = '../Bots/' + devTeam.codeName + '/' + bot.repo + '/' + bot.configFile; + fs.readFile(fileName, onFileRead); + + function onFileRead(err, pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> File System -> onFileRead -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> File System -> onFileRead -> fileName = " + fileName); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = bot.repo; + configObj.configFile = bot.configFile; + + addStoragePermissions(configObj); + + devTeam.bots[j] = configObj; + + if (requestsSent === responsesReceived) { + + callBackFunction(ecosystem, ecosystemObject); + + } + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> File System -> onFileRead -> File = " + fileName + " Error = " + err); + } + + } + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> File System -> File = " + fileName + " Error = " + err); + } + + break; + } + + case 'Github': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> Entering Case."); } + + github.getGithubData(devTeam.codeName, bot.repo, bot.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> bot.repo = " + bot.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ + + configObj.repo = bot.repo; + configObj.configFile = bot.configFile; + + addStoragePermissions(configObj); + + devTeam.bots[j] = configObj; + + if (requestsSent === responsesReceived) { + + callBackFunction(ecosystem, ecosystemObject); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> Error = " + err); + } + } + break; + } + } + } + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> Error = " + err); + } + } + + function getPlotters() { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Entering Function."); } + + /* In the next section we are loading the plotters configurations. */ + + for (let j = 0; j < devTeam.plotters.length; j++) { + + let plotter = devTeam.plotters[j]; + + requestsSent++; + + switch (serverConfig.configAndPlugins.Location) { + + case 'Cloud': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> Entering Case."); } + + storage.getStorageData(devTeam.codeName, plotter.repo, plotter.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> plotter.repo = " + plotter.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + if (pData !== "{}") { // If for any reason we could not tet this config, then we just exclude this bot to improve resiliency. + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full plotter object and we dont want to lose these two properties, we do this: */ + + configObj.repo = plotter.repo; + configObj.configFile = plotter.configFile; + + devTeam.plotters[j] = configObj; + + } + + if (requestsSent === responsesReceived) { + + callBackFunction(ecosystem, ecosystemObject); + + } + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> Error = " + err); + } + } + break; + } + + case 'File System': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> Entering Case."); } + + try { + + let fs = require('fs'); + + let fileName = '../Plotters/' + devTeam.codeName + '/' + plotter.repo + '/' + plotter.configFile; + fs.readFile(fileName, onFileRead); + + function onFileRead(err, pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> onFileRead -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> onFileRead -> fileName = " + fileName); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full plotter object and we dont want to lose these two properties, we do this: */ + + configObj.repo = plotter.repo; + configObj.configFile = plotter.configFile; + + devTeam.plotters[j] = configObj; + + if (requestsSent === responsesReceived) { + + callBackFunction(ecosystem, ecosystemObject); + + } + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> onFileRead -> File = " + fileName + " Error = " + err); + } + } + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> File = " + fileName + " Error = " + err); + } + break; + } + + case 'Github': { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> Entering Case."); } + + github.getGithubData(devTeam.codeName, plotter.repo, plotter.configFile, onDataArrived) + + function onDataArrived(pData) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> plotter.repo = " + plotter.repo); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> pData = " + pData); } + + responsesReceived++; + + pData = pData.toString(); + pData = pData.trim(); // remove first byte with some encoding. + + let configObj = JSON.parse(pData); + + /* Since we are going to replace the full plotter object and we dont want to lose these two properties, we do this: */ + + configObj.repo = plotter.repo; + configObj.configFile = plotter.configFile; + + devTeam.plotters[j] = configObj; + + if (requestsSent === responsesReceived) { + + callBackFunction(ecosystem, ecosystemObject); + + } + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> Error = " + err); + } + } + break; + } + } + } + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> Error = " + err); + } + } + } + + function addStoragePermissions(pConfigObj) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> addStoragePermissions -> Entering function."); } + + if (pConfigObj.storage !== undefined) { return; } // If this information is at the config file, then we take it, otherwise, we define it here. + + let fileUri; + let sas; + + switch (serverConfig.environment) { + + case "Develop": { + + fileUri = serverConfig.productsStorage.Develop.fileUri; + sas = serverConfig.productsStorage.Develop.sas; + + break; + } + + case "Production": { + + fileUri = serverConfig.productsStorage.Production.fileUri; + sas = serverConfig.productsStorage.Production.sas; + + break; + } + } + + pConfigObj.storage = { + sas: sas, + fileUri: fileUri + }; + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> addStoragePermissions -> Error = " + err); + } + } + + } + catch (err) { + console.log("[ERROR] initialize -> readDevTeamsConfigs -> Error = " + err); + } + } + } + + +} \ No newline at end of file diff --git a/Server/Github.js b/Server/Github.js new file mode 100644 index 000000000..063abcc55 --- /dev/null +++ b/Server/Github.js @@ -0,0 +1,108 @@ +exports.newGithub = function newGithub() { + + let thisObject = { + getGithubData: getGithubData, + initialize: initialize + } + + let githubData; + + return thisObject; + + function initialize(pGithubData) { + + githubData = pGithubData; + + } + + function getGithubData(pOrg, pRepo, pPath, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> Entering function."); } + + let cacheVersion = githubData.get(pOrg + '.' + pRepo + '.' + pPath) + + if (cacheVersion !== undefined) { + + if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> " + pOrg + '.' + pRepo + '.' + pPath + " found at cache."); } + + callBackFunction(cacheVersion); + + } else { + + if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> " + pOrg + '.' + pRepo + '.' + pPath + " NOT found at cache."); } + + const octokit = require('@octokit/rest')() + global.atob = require("atob"); + + let owner = pOrg; + let repo = pRepo; + let branch = "master"; + let page = 1; + let per_page = 100; + let ref = "master"; + let path = pPath; + + octokit.repos.getContent({ owner, repo, path, ref }, onContent); + + function onContent(error, result) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> onContent -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> onContent -> error = " + error); } + if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> onContent -> Github.com responded to request " + pOrg + '.' + pRepo + '.' + pPath + " with result = " + result.toString().substring(0, 100)); } + + if (error !== null) { console.log("[ERROR] getGithubData -> onContent -> " + error); } + + let decoded = atob(result.data.content); + + /* + + This method usually brings up to 3 characters of encoding info at the begining of the JSON string which destroys the JSON format. + We will run the following code with the intention to eliminate this problem. + + */ + + let cleanString = decoded; + let jsonTest; + + try { + jsonTest = JSON.parse(cleanString); + } catch (err) { + cleanString = decoded.substring(1); + try { + jsonTest = JSON.parse(cleanString); + } catch (err) { + cleanString = decoded.substring(2); + try { + jsonTest = JSON.parse(cleanString); + } catch (err) { + cleanString = decoded.substring(3); + try { + jsonTest = JSON.parse(cleanString); + } catch (err) { + console.log("[INFO] getGithubData -> onContent -> Could not clean the data received -> Data = " + decoded.substring(0, 50)); + } + } + } + } + + githubData.set(pOrg + '.' + pRepo + '.' + pPath, cleanString); + + callBackFunction(cleanString); + + } catch (err) { + console.log("[ERROR] getGithubData -> onContent -> err.message = " + err.message); + callBackFunction("{}"); + } + } + } + + } catch (err) { + console.log("[ERROR] getGithubData -> err.message = " + err.message); + callBackFunction("{}"); + } + } +} \ No newline at end of file diff --git a/Server/Storage.js b/Server/Storage.js new file mode 100644 index 000000000..eda2a80ef --- /dev/null +++ b/Server/Storage.js @@ -0,0 +1,102 @@ +exports.newStorage = function newStorage() { + + let thisObject = { + getStorageData: getStorageData, + initialize: initialize + } + + let storageData; + let serverConfig; + + return thisObject; + + function initialize(pStorageData, pServerConfig) { + + storageData = pStorageData; + serverConfig = pServerConfig; + + } + + function getStorageData(pOrg, pRepo, pPath, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> pRepo = " + pRepo); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> pPath = " + pPath); } + + let cacheVersion = storageData.get(pOrg + '.' + pRepo + '.' + pPath) + + if (cacheVersion !== undefined) { + + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> " + pOrg + '.' + pRepo + '.' + pPath + " found at cache."); } + + callBackFunction(cacheVersion); + + } else { + + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> " + pOrg + '.' + pRepo + '.' + pPath + " NOT found at cache."); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + + blobService.getBlobToText('aaplatform', pOrg + "/" + pRepo + "/" + pPath, onFileReceived); + + + function onFileReceived(err, text, response) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> err = " + JSON.stringify(err)); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] getStorageData -> onFileReceived -> response = " + JSON.stringify(response)); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> pRepo = " + pRepo); } + if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> pPath = " + pPath); } + + storageData.set(pOrg + '.' + pRepo + '.' + pPath, text); + + if (err !== null || text === null) { + + console.log("[ERROR] getStorageData -> onFileReceived -> Error Received from Storage Library. "); + console.log("[ERROR] getStorageData -> onFileReceived -> err = " + JSON.stringify(err)); + console.log("[ERROR] getStorageData -> onFileReceived -> Returning an empty JSON object string. "); + + callBackFunction("{}"); + return; + + } + + callBackFunction(text); + + } catch (err) { + console.log("[ERROR] getStorageData -> onFileReceived -> err.message = " + err.message); + callBackFunction("{}"); + } + } + } + + } catch (err) { + console.log("[ERROR] getStorageData -> err.message = " + err.message); + callBackFunction("{}"); + } + } +} \ No newline at end of file diff --git a/index.html b/index.html index 74d92a05a..4e6b1e70b 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,7 @@ - + diff --git a/server.js b/server.js index d4a941aa9..594b24b01 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,7 @@ -let CONSOLE_LOG = false; -let LOG_FILE_CONTENT = false; +/* These 2 are global variables, that is why they do not have a let or var */ + +CONSOLE_LOG = false; +LOG_FILE_CONTENT = false; if (CONSOLE_LOG === true) { console.log("[INFO] Node Server Starting."); } @@ -16,6 +18,12 @@ let port = process.env.PORT || 1337; let isHttpServerStarted = false; +const GITHUB = require('./Server/Github'); +let github = GITHUB.newGithub(); + +const STORAGE = require('./Server/Storage'); +let storage = STORAGE.newStorage(); + initialize(); function initialize() { @@ -27,905 +35,33 @@ function initialize() { githubData = new Map; storageData = new Map; - readServerConfig(); + const CONFIG_READER = require('./Server/ConfigReader'); + let configReader = CONFIG_READER.newConfigReader(); - function readServerConfig() { + configReader.initialize(ecosystem, ecosystemObject, serverConfig, githubData, storageData, onInitialized); - try { + function onInitialized(pServerConfig) { - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readServerConfig -> Entering function."); } + serverConfig = pServerConfig; - let fs = require('fs'); - - let fileName = './this.server.config.json'; - fs.readFile(fileName, onFileRead); + configReader.loadConfigs(onConfigsLoaded); - function onFileRead(err, file) { + function onConfigsLoaded(pEcosystem, pEcosystemObject) { - try { + ecosystem = pEcosystem; + ecosystemObject = pEcosystemObject; + + github.initialize(githubData); + storage.initialize(storageData, serverConfig) - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readServerConfig -> onFileRead -> Entering function."); } + startHtttpServer(); - let fileText; - - fileText = file.toString(); - fileText = fileText.trim(); // remove first byte with some encoding. - - serverConfig = JSON.parse(fileText); - - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readServerConfig -> onFileRead -> fileText = " + fileText); } - - CONSOLE_LOG = serverConfig.webServerLog.console; - LOG_FILE_CONTENT = serverConfig.webServerLog.fileContent; - - readEcosystemConfig(); - } - catch (err) { - console.log("[ERROR] initialize -> readServerConfig -> onFileRead -> File = " + fileName + " Error = " + err); - } - - } - } - catch (err) { - console.log("[ERROR] initialize -> readServerConfig -> Error = " + err); - } - } - - function readEcosystemConfig() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Entering function."); } - - /* - - This configuration file is the backbone of the system. The first file we are going to get is a template where other configurations are - injected and the files ends up inflated with all these configs in one single JSON object that in turn is later injected into a - javascript module with an object that is going to instantiate it at run-time. - - */ - - switch (serverConfig.configAndPlugins.Location) { - - case 'Cloud': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Cloud -> Entering Case."); } - - getStorageData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Cloud -> onDataArrived -> Entering function."); } - - ecosystem = pData.toString(); - ecosystem = ecosystem.trim(); // remove first byte with some encoding. - - ecosystemObject = JSON.parse(ecosystem); - readHostsConfigs(); - } - catch (err) { - console.log("[ERROR] initialize -> readEcosystemConfig -> Cloud -> onDataArrived -> Error = " + err); - } - } - - break; - } - - case 'File System': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> File System -> Entering Case."); } - - let fs = require('fs'); - try { - let fileName = '../AAPlatform/ecosystem.json'; - fs.readFile(fileName, onFileRead); - - function onFileRead(err, file) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> File System -> onFileRead -> Entering function."); } - - ecosystem = file.toString(); - ecosystem = ecosystem.trim(); // remove first byte with some encoding. - - ecosystemObject = JSON.parse(ecosystem); - readHostsConfigs(); - } - catch (err) { - console.log("[ERROR] initialize -> readEcosystemConfig -> File System -> onFileRead -> File = " + fileName + " Error = " + err); - } - - } - } - catch (err) { - console.log("[ERROR] initialize -> readEcosystemConfig -> File System -> File = " + fileName + " Error = " + err); - } - break; - } - - case 'Github': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Github -> Entering Case."); } - - getGithubData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readEcosystemConfig -> Github -> onDataArrived -> Entering function."); } - - ecosystem = pData.toString(); - ecosystem = ecosystem.trim(); // remove first byte with some encoding. - - ecosystemObject = JSON.parse(ecosystem); - readHostsConfigs(); - } - catch (err) { - console.log("[ERROR] initialize -> readEcosystemConfig -> Github -> onDataArrived -> Error = " + err); - } - } - break; - } - } - } - catch (err) { - console.log("[ERROR] initialize -> readEcosystemConfig -> Error = " + err); - } - } - - function readHostsConfigs() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> Entering function."); } - - let requestsSent = 0; - let responsesReceived = 0; - - for (let i = 0; i < ecosystemObject.hosts.length; i++) { - - let host = ecosystemObject.hosts[i]; - - getCompetitions(); - getPlotters(); - - function getCompetitions() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Entering function."); } - - /* In this first section we will load the competitions configurations. */ - - for (let j = 0; j < host.competitions.length; j++) { - - let competition = host.competitions[j]; - - requestsSent++; - - switch (serverConfig.configAndPlugins.Location) { - - case 'Cloud': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> Entering Case."); } - - getStorageData(host.codeName, competition.repo, competition.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> host.codeName = " + host.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> competition.repo = " + competition.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = competition.repo; - configObj.configFile = competition.configFile; - - host.competitions[j] = configObj; - - if (requestsSent === responsesReceived) { - - readDevTeamsConfigs(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> Error = " + err); - } - } - break; - } - - case 'File System': { - - try { - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> Entering Case."); } - - let fs = require('fs'); - - let fileName = '../Competitions/' + host.codeName + '/' + competition.repo + '/' + competition.configFile; - fs.readFile(fileName, onFileRead); - - function onFileRead(err, pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> onFileRead -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> onFileRead -> fileName = " + fileName); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = competition.repo; - configObj.configFile = competition.configFile; - - host.competitions[j] = configObj; - - if (requestsSent === responsesReceived) { - - readDevTeamsConfigs(); - - } - } - catch (err) { - console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> File System -> onFileRead -> File = " + fileName + " Error = " + err); - } - } - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> File System -> File = " + fileName + " Error = " + err); - } - - break; - } - - case 'Github': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> Entering Case."); } - - getGithubData(host.codeName, competition.repo, competition.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> host.codeName = " + host.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> competition.repo = " + competition.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = competition.repo; - configObj.configFile = competition.configFile; - - host.competitions[j] = configObj; - - if (requestsSent === responsesReceived) { - - readDevTeamsConfigs(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> Github -> onDataArrived -> Error = " + err); - } - } - - break; - } - } - } - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getCompetitions -> getCompetitions -> Error = " + err); - } - } - - function getPlotters() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> getPlotters -> Entering function."); } - - /* In this second section we will load the competition plotters configurations. */ - - for (let j = 0; j < host.plotters.length; j++) { - - let plotter = host.plotters[j]; - - requestsSent++; - - switch (serverConfig.configAndPlugins.Location) { - - case 'Cloud': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> Entering Case."); } - - getStorageData(host.codeName, plotter.repo, plotter.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> host.codeName = " + host.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> plotter.repo = " + plotter.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = plotter.repo; - configObj.configFile = plotter.configFile; - - host.plotters[j] = configObj; - - if (requestsSent === responsesReceived) { - - readDevTeamsConfigs(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> Error = " + err); - } - } - - break; - } - - case 'File System': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> File System -> Entering Case."); } - - try { - - let fs = require('fs'); - - let fileName = '../Plotters/' + host.codeName + '/' + plotter.repo + '/' + plotter.configFile; - fs.readFile(fileName, onFileRead); - - function onFileRead(err, pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> File System -> onFileRead -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> File System -> onFileRead -> fileName = " + fileName); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = plotter.repo; - configObj.configFile = plotter.configFile; - - host.plotters[j] = configObj; - - if (requestsSent === responsesReceived) { - - readDevTeamsConfigs(); - - } - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> File System -> onFileRead -> File = " + fileName + " Error = " + err); - } - - } - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> File System -> File = " + fileName + " Error = " + err); - } - - break; - } - - case 'Github': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> Entering Case."); } - - getGithubData(host.codeName, plotter.repo, plotter.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> host.codeName = " + host.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> plotter.repo = " + plotter.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = plotter.repo; - configObj.configFile = plotter.configFile; - - host.plotters[j] = configObj; - - if (requestsSent === responsesReceived) { - - readDevTeamsConfigs(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> Github -> onDataArrived -> Error = " + err); - } - } - break; - } - } - } - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> getPlotters -> getCompetitions -> Error = " + err); - } - } - } - } - catch (err) { - console.log("[ERROR] initialize -> readHostsConfigs -> Error = " + err); - } - } - - function readDevTeamsConfigs() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> Entering function."); } - - /* - - Each bot has its configuration at its own repo since each team must be able to change it at will. - So what we do here is to use the master config at the AAPlatform repo that we already have on - memory and inject into it the config of each bot. - - Inmediatelly after that, we also load the Plotters configs using the same technique. - - */ - - let requestsSent = 0; - let responsesReceived = 0; - - for (let i = 0; i < ecosystemObject.devTeams.length; i++) { - - let devTeam = ecosystemObject.devTeams[i]; - - getBots(); - getPlotters(); - - function getBots() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Entering function."); } - - /* In the next section we are loading the bots configurations. */ - - for (let j = 0; j < devTeam.bots.length; j++) { - - let bot = devTeam.bots[j]; - - requestsSent++; - - switch (serverConfig.configAndPlugins.Location) { - - case 'Cloud': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> Entering Case."); } - - getStorageData(devTeam.codeName, bot.repo, bot.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> bot.repo = " + bot.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - if (pData !== "{}") { // If for any reason we could not tet this config, then we just exclude this bot to improve resiliency. - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = bot.repo; - configObj.configFile = bot.configFile; - - addStoragePermissions(configObj); - - devTeam.bots[j] = configObj; - - } - - if (requestsSent === responsesReceived) { - - startHtttpServer(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> Error = " + err); - } - } - break; - } - - case 'File System': { - - try { - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> File System -> Entering Case."); } - - let fs = require('fs'); - - let fileName = '../Bots/' + devTeam.codeName + '/' + bot.repo + '/' + bot.configFile; - fs.readFile(fileName, onFileRead); - - function onFileRead(err, pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> File System -> onFileRead -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> File System -> onFileRead -> fileName = " + fileName); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = bot.repo; - configObj.configFile = bot.configFile; - - addStoragePermissions(configObj); - - devTeam.bots[j] = configObj; - - if (requestsSent === responsesReceived) { - - startHtttpServer(); - - } - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> File System -> onFileRead -> File = " + fileName + " Error = " + err); - } - - } - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> File System -> File = " + fileName + " Error = " + err); - } - - break; - } - - case 'Github': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> Entering Case."); } - - getGithubData(devTeam.codeName, bot.repo, bot.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> bot.repo = " + bot.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full bot object and we dont want to lose these two properties, we do this: */ - - configObj.repo = bot.repo; - configObj.configFile = bot.configFile; - - addStoragePermissions(configObj); - - devTeam.bots[j] = configObj; - - if (requestsSent === responsesReceived) { - - startHtttpServer(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> Github -> onDataArrived -> Error = " + err); - } - } - break; - } - } - } - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getBots -> Error = " + err); - } - } - - function getPlotters() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Entering Function."); } - - /* In the next section we are loading the plotters configurations. */ - - for (let j = 0; j < devTeam.plotters.length; j++) { - - let plotter = devTeam.plotters[j]; - - requestsSent++; - - switch (serverConfig.configAndPlugins.Location) { - - case 'Cloud': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> Entering Case."); } - - getStorageData(devTeam.codeName, plotter.repo, plotter.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> plotter.repo = " + plotter.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - if (pData !== "{}") { // If for any reason we could not tet this config, then we just exclude this bot to improve resiliency. - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full plotter object and we dont want to lose these two properties, we do this: */ - - configObj.repo = plotter.repo; - configObj.configFile = plotter.configFile; - - devTeam.plotters[j] = configObj; - - } - - if (requestsSent === responsesReceived) { - - startHtttpServer(); - - } - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> Error = " + err); - } - } - break; - } - - case 'File System': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> Entering Case."); } - - try { - - let fs = require('fs'); - - let fileName = '../Plotters/' + devTeam.codeName + '/' + plotter.repo + '/' + plotter.configFile; - fs.readFile(fileName, onFileRead); - - function onFileRead(err, pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> onFileRead -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> onFileRead -> fileName = " + fileName); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full plotter object and we dont want to lose these two properties, we do this: */ - - configObj.repo = plotter.repo; - configObj.configFile = plotter.configFile; - - devTeam.plotters[j] = configObj; - - if (requestsSent === responsesReceived) { - - startHtttpServer(); - - } - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> File System -> onFileRead -> File = " + fileName + " Error = " + err); - } - } - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> File = " + fileName + " Error = " + err); - } - break; - } - - case 'Github': { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> Entering Case."); } - - getGithubData(devTeam.codeName, plotter.repo, plotter.configFile, onDataArrived) - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> devTeam.codeName = " + devTeam.codeName); } - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> plotter.repo = " + plotter.repo); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> pData = " + pData); } - - responsesReceived++; - - pData = pData.toString(); - pData = pData.trim(); // remove first byte with some encoding. - - let configObj = JSON.parse(pData); - - /* Since we are going to replace the full plotter object and we dont want to lose these two properties, we do this: */ - - configObj.repo = plotter.repo; - configObj.configFile = plotter.configFile; - - devTeam.plotters[j] = configObj; - - if (requestsSent === responsesReceived) { - - startHtttpServer(); - - } - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> Github -> onDataArrived -> Error = " + err); - } - } - break; - } - } - } - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> getPlotters -> Error = " + err); - } - } - } - - function addStoragePermissions(pConfigObj) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] initialize -> readDevTeamsConfigs -> addStoragePermissions -> Entering function."); } - - if (pConfigObj.storage !== undefined) { return; } // If this information is at the config file, then we take it, otherwise, we define it here. - - let fileUri; - let sas; - - switch (serverConfig.environment) { - - case "Develop": { - - fileUri = serverConfig.productsStorage.Develop.fileUri; - sas = serverConfig.productsStorage.Develop.sas; - - break; - } - - case "Production": { - - fileUri = serverConfig.productsStorage.Production.fileUri; - sas = serverConfig.productsStorage.Production.sas; - - break; - } - } - - pConfigObj.storage = { - sas: sas, - fileUri: fileUri - }; - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> addStoragePermissions -> Error = " + err); - } - } - - } - catch (err) { - console.log("[ERROR] initialize -> readDevTeamsConfigs -> Error = " + err); } } } + + function startHtttpServer() { if (CONSOLE_LOG === true) { console.log("[INFO] startHtttpServer -> Entering function."); } @@ -1192,7 +328,7 @@ function onBrowserRequest(request, response) { if (CONSOLE_LOG === true) { console.log("[INFO] onBrowserRequest -> readEcosystemConfig -> Cloud -> Entering Case."); } - getStorageData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) + storage.getStorageData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) function onDataArrived(pData) { @@ -1216,7 +352,7 @@ function onBrowserRequest(request, response) { if (CONSOLE_LOG === true) { console.log("[INFO] onBrowserRequest -> readEcosystemConfig -> Github -> Entering Case."); } - getGithubData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) + github.getGithubData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) function onDataArrived(pData) { @@ -1239,7 +375,7 @@ function onBrowserRequest(request, response) { if (CONSOLE_LOG === true) { console.log("[INFO] onBrowserRequest -> readEcosystemConfig -> Cloud -> Entering Case."); } - getStorageData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) + storage.getStorageData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) function onDataArrived(pData) { @@ -1263,7 +399,7 @@ function onBrowserRequest(request, response) { if (CONSOLE_LOG === true) { console.log("[INFO] onBrowserRequest -> readEcosystemConfig -> Github -> Entering Case."); } - getGithubData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) + github.getGithubData(requestParameters[2], requestParameters[3], requestParameters[4], onDataArrived) function onDataArrived(pData) { @@ -1594,176 +730,5 @@ function respondWithFile(fileName, response) { } } -function getGithubData(pOrg, pRepo, pPath, callBackFunction) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> Entering function."); } - - let cacheVersion = githubData.get(pOrg + '.' + pRepo + '.' + pPath) - - if (cacheVersion !== undefined) { - - if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> " + pOrg + '.' + pRepo + '.' + pPath + " found at cache."); } - - callBackFunction(cacheVersion); - - } else { - - if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> " + pOrg + '.' + pRepo + '.' + pPath + " NOT found at cache."); } - - const octokit = require('@octokit/rest')() - global.atob = require("atob"); - - let owner = pOrg; - let repo = pRepo; - let branch = "master"; - let page = 1; - let per_page = 100; - let ref = "master"; - let path = pPath; - - octokit.repos.getContent({ owner, repo, path, ref }, onContent); - - function onContent(error, result) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> onContent -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> onContent -> error = " + error); } - if (CONSOLE_LOG === true) { console.log("[INFO] getGithubData -> onContent -> Github.com responded to request " + pOrg + '.' + pRepo + '.' + pPath + " with result = " + result.toString().substring(0, 100)); } - - if (error !== null) { console.log("[ERROR] getGithubData -> onContent -> " + error); } - - let decoded = atob(result.data.content); - - /* - - This method usually brings up to 3 characters of encoding info at the begining of the JSON string which destroys the JSON format. - We will run the following code with the intention to eliminate this problem. - - */ - - let cleanString = decoded; - let jsonTest; - - try { - jsonTest = JSON.parse(cleanString); - } catch (err) { - cleanString = decoded.substring(1); - try { - jsonTest = JSON.parse(cleanString); - } catch (err) { - cleanString = decoded.substring(2); - try { - jsonTest = JSON.parse(cleanString); - } catch (err) { - cleanString = decoded.substring(3); - try { - jsonTest = JSON.parse(cleanString); - } catch (err) { - console.log("[INFO] getGithubData -> onContent -> Could not clean the data received -> Data = " + decoded.substring(0, 50)); - } - } - } - } - - githubData.set(pOrg + '.' + pRepo + '.' + pPath, cleanString); - - callBackFunction(cleanString); - - } catch (err) { - console.log("[ERROR] getGithubData -> onContent -> err.message = " + err.message); - callBackFunction("{}"); - } - } - } - - } catch (err) { - console.log("[ERROR] getGithubData -> err.message = " + err.message); - callBackFunction("{}"); - } -} - -function getStorageData(pOrg, pRepo, pPath, callBackFunction) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> pRepo = " + pRepo); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> pPath = " + pPath); } - - let cacheVersion = storageData.get(pOrg + '.' + pRepo + '.' + pPath) - - if (cacheVersion !== undefined) { - - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> " + pOrg + '.' + pRepo + '.' + pPath + " found at cache."); } - - callBackFunction(cacheVersion); - - } else { - - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> " + pOrg + '.' + pRepo + '.' + pPath + " NOT found at cache."); } - - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - - blobService.getBlobToText('aaplatform', pOrg + "/" + pRepo + "/" + pPath, onFileReceived); - function onFileReceived(err, text, response) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> err = " + JSON.stringify(err)); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] getStorageData -> onFileReceived -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> pRepo = " + pRepo); } - if (CONSOLE_LOG === true) { console.log("[INFO] getStorageData -> onFileReceived -> pPath = " + pPath); } - - storageData.set(pOrg + '.' + pRepo + '.' + pPath, text); - - if (err !== null || text === null) { - - console.log("[ERROR] getStorageData -> onFileReceived -> Error Received from Storage Library. "); - console.log("[ERROR] getStorageData -> onFileReceived -> err = " + JSON.stringify(err)); - console.log("[ERROR] getStorageData -> onFileReceived -> Returning an empty JSON object string. "); - - callBackFunction("{}"); - return; - - } - - callBackFunction(text); - - } catch (err) { - console.log("[ERROR] getStorageData -> onFileReceived -> err.message = " + err.message); - callBackFunction("{}"); - } - } - } - - } catch (err) { - console.log("[ERROR] getStorageData -> err.message = " + err.message); - callBackFunction("{}"); - } -} \ No newline at end of file