From 527b062ba983c5e8f26d7327235a70227108e2d0 Mon Sep 17 00:00:00 2001 From: JagadishPujari Date: Tue, 5 Jan 2021 15:47:49 +0530 Subject: [PATCH 1/4] Issue #SB-21295 feat: Menu configuration deploy to blob refresh API to update config --- router/appRest.js | 40 ++++++++++++++++++++++++++++++++++++++++ router/package.json | 2 ++ 2 files changed, 42 insertions(+) diff --git a/router/appRest.js b/router/appRest.js index 3b6f144..06f399e 100644 --- a/router/appRest.js +++ b/router/appRest.js @@ -10,6 +10,9 @@ var LOG = require('./log/logger') var literals = require('./config/literals') var config = require('./config/config') var chatflow = require('./config/chatflow') +var reload = require('require-reload')(require), +chatflow = reload('./config/chatflow'); +literals = reload('./config/literals'); var RasaCoreController = require('./controllers/rasaCoreController') const telemetry = require('./api/telemetry/telemetry.js') var UUIDV4 = require('uuid') @@ -76,6 +79,43 @@ appBot.post('/whatsapp', function (req, res) { }) +// Update latest config from blob +appBot.post('/refresh', function(req, response) { + var url = 'https://sunbirddev.blob.core.windows.net/chatbot/router/config/literals.js' + var dest = 'router/config/literals.js' + // Update config for literals + updateConfigFromBlob(url, dest, function(){ + try { + literals = reload('./config/literals'); + } catch (e) { + //if this threw an error, the api variable is still set to the old, working version + console.error("Failed to reload literals.js! Error: ", e); + } + }) + + updateConfigFromBlob('https://sunbirddev.blob.core.windows.net/chatbot/router/config/chatflow.js', './config/chatflow.js', function(){ + try { + chatflow = reload('./config/chatflow'); + } catch (e) { + //if this threw an error, the api variable is still set to the old, working version + console.error("Failed to reload chatflow.js! Error: ", e); + } + }) +}) + +var updateConfigFromBlob = function(url, dest, cb) { + var file = fs.createWriteStream(dest); + var request = https.get(url, function(response) { + response.pipe(file); + file.on('finish', function() { + file.close(cb); // close() is async, call cb after close completes. + }); + }).on('error', function(err) { // Handle errors + fs.unlink(dest); // Delete the file async. (But we don't check the result) + if (cb) cb(err.message); + }); + }; + function handler(req, res, data) { diff --git a/router/package.json b/router/package.json index 6319ab7..26c8d01 100644 --- a/router/package.json +++ b/router/package.json @@ -14,6 +14,7 @@ "dateformat": "^3.0.3", "elasticsearch": "^16.6.0", "express": "^4.17.1", + "fs": "0.0.1-security", "helmet": "^3.21.2", "lodash": "^4.17.20", "method-override": "^3.0.0", @@ -22,6 +23,7 @@ "sb_config_util": "file:libs/sb-config-util", "redis": "^3.0.2", "request": "^2.88.0", + "require-reload": "^0.2.2", "socket.io": "^2.3.0", "tz-lookup": "^6.1.25", "uuid": "^3.4.0", From 249ca98821655e8e2dedd0316a000152aab0a218 Mon Sep 17 00:00:00 2001 From: JagadishPujari Date: Tue, 5 Jan 2021 17:40:54 +0530 Subject: [PATCH 2/4] Issue #SB-21295 feat: Menu configuration deploy to blob refresh API to update config --- router/appRest.js | 33 +++++++++++++++++++-------------- router/config/config.js | 9 +++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/router/appRest.js b/router/appRest.js index 06f399e..74982ce 100644 --- a/router/appRest.js +++ b/router/appRest.js @@ -81,29 +81,34 @@ appBot.post('/whatsapp', function (req, res) { // Update latest config from blob appBot.post('/refresh', function(req, response) { - var url = 'https://sunbirddev.blob.core.windows.net/chatbot/router/config/literals.js' - var dest = 'router/config/literals.js' - // Update config for literals - updateConfigFromBlob(url, dest, function(){ + var domian = config.ENV_URL[req.headers.host] + var url = domian + 'chatbot/router/config/' + var dest = './config/' + + var literalsStr = 'literals.js'; + var chatflowStr = 'chatflow.js'; + updateConfigFromBlob(url, dest, literalsStr, function(){ try { literals = reload('./config/literals'); + updateConfigFromBlob(url, dest, chatflowStr, function(){ + try { + literals = reload('./config/chatflow'); + response.send({'msg': 'Configuration is updated successfully for chatflow and literals!!!'}) + } catch (e) { + //if this threw an error, the api variable is still set to the old, working version + console.error("Failed to reload literals.js! Error: ", e); + } + }) } catch (e) { //if this threw an error, the api variable is still set to the old, working version console.error("Failed to reload literals.js! Error: ", e); } }) - - updateConfigFromBlob('https://sunbirddev.blob.core.windows.net/chatbot/router/config/chatflow.js', './config/chatflow.js', function(){ - try { - chatflow = reload('./config/chatflow'); - } catch (e) { - //if this threw an error, the api variable is still set to the old, working version - console.error("Failed to reload chatflow.js! Error: ", e); - } - }) }) -var updateConfigFromBlob = function(url, dest, cb) { +var updateConfigFromBlob = function(url, dest, configName, cb) { + url = url + configName; + dest = dest + configName; var file = fs.createWriteStream(dest); var request = https.get(url, function(response) { response.pipe(file); diff --git a/router/config/config.js b/router/config/config.js index 076a1c0..207c8ba 100644 --- a/router/config/config.js +++ b/router/config/config.js @@ -56,4 +56,13 @@ config.UNKNOWN_OPTION_FREEFLOW = 'unknown_option_freeFlow' config.UNKNOWN_OPTION = 'unknown_option' config.TELEGRAM_BOT_ENDPOINT = 'https://api.telegram.org/bot/sendMessage' + +config.ENV_URL = { + 'diksha.gov.in': 'https://ntpproductionall.blob.core.windows.net/', + 'staging.sunbirded.org':'https://ntpstagingall.blob.core.windows.net/', + 'preprod.ntp.net.in':'https://preprodall.blob.core.windows.net/', + 'dev.sunbirded.org':'https://sunbirddev.blob.core.windows.net/', + 'vdn.diksha.gov.in/': '' +} + module.exports = config; From e08fa38bf42d2e3e06c7ba6289bdd8864ece6d52 Mon Sep 17 00:00:00 2001 From: JagadishPujari Date: Tue, 5 Jan 2021 17:46:43 +0530 Subject: [PATCH 3/4] Issue #SB-21295 feat: Menu configuration deploy to blob refresh API to update config --- router/appRest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router/appRest.js b/router/appRest.js index 74982ce..709d609 100644 --- a/router/appRest.js +++ b/router/appRest.js @@ -92,7 +92,7 @@ appBot.post('/refresh', function(req, response) { literals = reload('./config/literals'); updateConfigFromBlob(url, dest, chatflowStr, function(){ try { - literals = reload('./config/chatflow'); + chatflow = reload('./config/chatflow'); response.send({'msg': 'Configuration is updated successfully for chatflow and literals!!!'}) } catch (e) { //if this threw an error, the api variable is still set to the old, working version From 6e642dca3e047b4d64fec7af0e70c12f00afc431 Mon Sep 17 00:00:00 2001 From: JagadishPujari Date: Fri, 8 Jan 2021 22:03:25 +0530 Subject: [PATCH 4/4] Issue #SB-21295 feat: added Blob path env variable --- helm_charts/router/values.j2 | 1 + router/appRest.js | 52 ++++++++++++++++++++---------------- router/config/config.js | 8 +----- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/helm_charts/router/values.j2 b/helm_charts/router/values.j2 index a07937d..6d75c27 100644 --- a/helm_charts/router/values.j2 +++ b/helm_charts/router/values.j2 @@ -30,3 +30,4 @@ routerenv: WHATSAPP_SECRET_KEY: "{{sunbird_whatsapp_secret_key | default("''")}}" WHATSAPP_AUTH_TOKEN: "{{sunbird_whatsapp_auth_token | default("''")}}" WHATSAPP_SOURCE: "{{sunbird_whatsapp_source | default("''")}}" + CONFIG_BLOB_PATH: "{{sunbird_public_storage_account_name}}" \ No newline at end of file diff --git a/router/appRest.js b/router/appRest.js index 709d609..6a18a79 100644 --- a/router/appRest.js +++ b/router/appRest.js @@ -81,29 +81,35 @@ appBot.post('/whatsapp', function (req, res) { // Update latest config from blob appBot.post('/refresh', function(req, response) { - var domian = config.ENV_URL[req.headers.host] - var url = domian + 'chatbot/router/config/' - var dest = './config/' - - var literalsStr = 'literals.js'; - var chatflowStr = 'chatflow.js'; - updateConfigFromBlob(url, dest, literalsStr, function(){ - try { - literals = reload('./config/literals'); - updateConfigFromBlob(url, dest, chatflowStr, function(){ - try { - chatflow = reload('./config/chatflow'); - response.send({'msg': 'Configuration is updated successfully for chatflow and literals!!!'}) - } catch (e) { - //if this threw an error, the api variable is still set to the old, working version - console.error("Failed to reload literals.js! Error: ", e); - } - }) - } catch (e) { - //if this threw an error, the api variable is still set to the old, working version - console.error("Failed to reload literals.js! Error: ", e); - } - }) + if(config.CONFIG_BLOB_PATH) { + var domian = "https://" + config.CONFIG_BLOB_PATH + ".blob.core.windows.net" + var url = domian + '/chatbot/router/config/' + var dest = 'router/config/' + + var literalsStr = 'literals.js'; + var chatflowStr = 'chatflow.js'; + updateConfigFromBlob(url, dest, literalsStr, function(){ + try { + literals = reload('./config/literals'); + updateConfigFromBlob(url, dest, chatflowStr, function(){ + try { + chatflow = reload('./config/chatflow'); + response.send({'msg': 'Configuration is updated successfully for chatflow and literals!!!'}) + } catch (e) { + //if this threw an error, the api variable is still set to the old, working version + console.error("Failed to reload chatflow.js! Error: ", e); + response.send({'msg': e.message}) + } + }) + } catch (e) { + //if this threw an error, the api variable is still set to the old, working version + console.error("Failed to reload literals.js! Error: ", e); + response.send({'msg': e.message}) + } + }) + } else { + response.send({'msg': 'ENV configuration blob path is not defined'}) + } }) var updateConfigFromBlob = function(url, dest, configName, cb) { diff --git a/router/config/config.js b/router/config/config.js index 207c8ba..01ff82c 100644 --- a/router/config/config.js +++ b/router/config/config.js @@ -57,12 +57,6 @@ config.UNKNOWN_OPTION = 'unknown_option' config.TELEGRAM_BOT_ENDPOINT = 'https://api.telegram.org/bot/sendMessage' -config.ENV_URL = { - 'diksha.gov.in': 'https://ntpproductionall.blob.core.windows.net/', - 'staging.sunbirded.org':'https://ntpstagingall.blob.core.windows.net/', - 'preprod.ntp.net.in':'https://preprodall.blob.core.windows.net/', - 'dev.sunbirded.org':'https://sunbirddev.blob.core.windows.net/', - 'vdn.diksha.gov.in/': '' -} +config.CONFIG_BLOB_PATH = env.CONFIG_BLOB_PATH module.exports = config;