From bdc712928f9395b3a320312343d619d5630c708b Mon Sep 17 00:00:00 2001 From: cavit Date: Sat, 19 Jun 2021 05:22:54 -0700 Subject: [PATCH 1/4] docker options added --- plugins/EpadDB.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++ utils/Docker.js | 2 ++ 2 files changed, 59 insertions(+) diff --git a/plugins/EpadDB.js b/plugins/EpadDB.js index 18f9133b..8101fce0 100644 --- a/plugins/EpadDB.js +++ b/plugins/EpadDB.js @@ -2757,6 +2757,7 @@ async function epaddb(fastify, options, done) { }); const onlyNameValues = []; const foldersToBind = []; + let dockeroptions = {}; for (let i = 0; i < tempPluginParams.length; i += 1) { if ( tempPluginParams[i].format === 'InputFolder' || @@ -2779,10 +2780,66 @@ async function epaddb(fastify, options, done) { onlyNameValues.push(tempPluginParams[i].default_value); } } + + if (tempPluginParams[i].paramid === 'dockeroptions') { + let tmpArray = []; + switch (tempPluginParams[i].format) { + case 'sharedram': + dockeroptions.ShmSize = tempPluginParams[i].default_value; + break; + case 'driver': + if (!dockeroptions.DeviceRequests) { + dockeroptions = { + ...dockeroptions, + DeviceRequests: [{ Driver: tempPluginParams[i].default_value }], + }; + } else { + dockeroptions.DeviceRequests[0].Driver = tempPluginParams[i].default_value; + } + break; + case 'deviceids': + // device ids need to be passed to container as array. Expect coma separated strings and convert those to an array before sending + + if (tempPluginParams[i].default_value.split(',').length >= 1) { + tmpArray = tempPluginParams[i].default_value.split(','); + } else { + tmpArray.push(tempPluginParams[i].default_value); + } + + if (!dockeroptions.DeviceRequests) { + dockeroptions = { + ...dockeroptions, + DeviceRequests: [{ DeviceIDs: tmpArray }], + }; + } else { + dockeroptions.DeviceRequests[0].DeviceIDs = tmpArray; + } + break; + case 'capabilities': + // device ids need to be passed to container as array. Expect coma separated strings and convert those to an array before sending + + if (tempPluginParams[i].default_value.split(',').length >= 1) { + tmpArray = tempPluginParams[i].default_value.split(','); + } else { + tmpArray.push(tempPluginParams[i].default_value); + } + if (!dockeroptions.DeviceRequests) { + dockeroptions = { + ...dockeroptions, + DeviceRequests: [{ Capabilities: [tmpArray] }], + }; + } else { + dockeroptions.DeviceRequests[0].Capabilities = [tmpArray]; + } + break; + default: + } + } } const returnObj = { paramsDocker: onlyNameValues, dockerFoldersToBind: foldersToBind, + dockeroptions: { HostConfig: dockeroptions }, }; return resolve(returnObj); diff --git a/utils/Docker.js b/utils/Docker.js index a19cf86a..4cdd0499 100644 --- a/utils/Docker.js +++ b/utils/Docker.js @@ -130,6 +130,8 @@ class DockerService { StdinOnce: false, HostConfig: { Binds: dockerFoldersToBind, + DeviceRequests: [...params.dockeroptions.HostConfig.DeviceRequests], + ShmSize: parseInt(params.dockeroptions.HostConfig.ShmSize, 10) || 64000000, }, }) // eslint-disable-next-line prefer-arrow-callback From 5f06125db8cf669a35a36dd2a0e02578c8c48c12 Mon Sep 17 00:00:00 2001 From: cavit Date: Mon, 21 Jun 2021 18:52:13 -0700 Subject: [PATCH 2/4] register app remote server call fixed --- config/index.js | 11 +++++++++-- plugins/EpadDB.js | 21 +++++++++++++-------- plugins/Other.js | 3 +++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/config/index.js b/config/index.js index 5991a43b..15b84725 100644 --- a/config/index.js +++ b/config/index.js @@ -48,7 +48,8 @@ config.thickDb = config.thickDb || { }; config.maxConcurrent = config.maxConcurrent || 5; config.disableStats = config.disableStats || false; -config.statsEpad = config.statsEpad || 'https://epad-public.stanford.edu'; +config.statsEpad = + config.statsEpad || process.env.STATSEPAD || 'https://epadlite-public.stanford.edu'; config.limitStudies = process.env.LIMIT_STUDIES || config.limitStudies; config.unassignedProjectID = config.unassignedProjectID || 'nonassigned'; config.XNATUploadProjectID = config.XNATUploadProjectID || 'all'; @@ -70,5 +71,11 @@ if (process.env.CORS_ORIGIN) { } config.noResume = process.env.NO_RESUME === 'true' || config.noResume || false; config.secret = process.env.SECRET || config.secret || undefined; - +// mail relay example. Required to register the app +// config.notificationEmail = { host: 'smtp.gmail.com' }; +// config.notificationEmail = { port: 465 }; // tls 465 +// config.notificationEmail.isTls = true; // use TLS +// config.notificationEmail.auth = {}; +// config.notificationEmail.auth = { user: 'example@gmail.com' }; +// config.notificationEmail.auth = { pass: 'examplepass' }; module.exports = config; diff --git a/plugins/EpadDB.js b/plugins/EpadDB.js index 8101fce0..b80c09fb 100644 --- a/plugins/EpadDB.js +++ b/plugins/EpadDB.js @@ -3415,15 +3415,16 @@ async function epaddb(fastify, options, done) { fastify.nodemailer.sendMail(mailOptions, (err, info) => { if (err) { fastify.log.error(`could not send email to ${paramTo}. Error: ${err.message}`); - reject(new InternalError('Error Happened while senfing an email', err)); + reject(new InternalError('Error Happened while sending an email', err)); } else { fastify.log.info(`Email accepted for ${JSON.stringify(info.accepted)}`); resolve(info); } }); + } else { + reject(new InternalError('Mail relay settings are not found', new Error('334'))); + // Error : 334 means –> Provide SMTP authentication credentials. } - reject(new InternalError('Mail relay settings are not found', new Error('334'))); - // Error : 334 means –> Provide SMTP authentication credentials. }) ); @@ -3483,6 +3484,7 @@ async function epaddb(fastify, options, done) { fastify.decorate('registerServerForAppKey', async (request, reply) => { const requestSenderServerName = request.raw.headers.host.split(':')[0]; const tempBody = request.body; + console.log('................request body', request.body); let tempEpadStatServer = config.statsEpad.split('//')[1]; if ( tempEpadStatServer === '' || @@ -3491,12 +3493,15 @@ async function epaddb(fastify, options, done) { ) { tempEpadStatServer = config.statsEpad; } + // if (config.statsEpad) { if (!requestSenderServerName.includes(tempEpadStatServer)) { - const resultRemoteRegister = await Axios.post(`${config.statsEpad}/register`, { + console.log('calling remote server'); + const resultRemoteRegister = await Axios.post(`${config.statsEpad}/api/register`, { headers: { 'Content-Type': 'application/json', }, - tempBody, + ...tempBody, + username: 'admin', }); fastify.log.info( @@ -3506,7 +3511,7 @@ async function epaddb(fastify, options, done) { reply.code(resultRemoteRegister.code).send(resultRemoteRegister.data); return; } - + console.log('after................request body', request.body); const tempName = request.body.name; const tempEmail = request.body.email; const tempOrganization = request.body.organization; @@ -3540,7 +3545,7 @@ async function epaddb(fastify, options, done) { hostname: tempHostname, email: tempEmail, emailvalidationcode: tempGeneratedEmailValidationCode, - creator: request.epadAuth.username, + creator: 'registercall', createdtime: Date.now(), emailvalidationsent: Date.now(), }); @@ -3571,7 +3576,7 @@ async function epaddb(fastify, options, done) { emailvalidationcode: tempGeneratedEmailValidationCode, updatetime: Date.now(), emailvalidationsent: Date.now(), - updated_by: request.epadAuth.username, + updated_by: 'registercall', }, { where: { diff --git a/plugins/Other.js b/plugins/Other.js index 71160898..fe0737c4 100644 --- a/plugins/Other.js +++ b/plugins/Other.js @@ -1403,6 +1403,7 @@ async function other(fastify) { users: 'user', worklists: 'worklist', ontology: 'ontology', + register: 'register', }; if (urlParts[urlParts.length - 1] === 'download') reqInfo.methodText = 'DOWNLOAD'; if (levels[urlParts[urlParts.length - 1]]) { @@ -1684,6 +1685,7 @@ async function other(fastify) { !req.raw.url.startsWith(`${fastify.getPrefixForRoute()}/epad/statistics`) && // disabling auth for put is dangerous !req.raw.url.startsWith(`${fastify.getPrefixForRoute()}/download`) && !req.raw.url.startsWith(`${fastify.getPrefixForRoute()}/ontology`) && + !req.raw.url.startsWith(`${fastify.getPrefixForRoute()}/register`) && req.method !== 'OPTIONS' ) { // if auth has been given in config, verify authentication @@ -1904,6 +1906,7 @@ async function other(fastify) { break; case 'POST': if ( + reqInfo.level !== 'register' && !fastify.hasCreatePermission(request, reqInfo.level) && !( reqInfo.level === 'worklist' && From b8c0165d032bd1f6fdb1a41d4356377a6b58e3b3 Mon Sep 17 00:00:00 2001 From: cavit Date: Mon, 21 Jun 2021 23:33:19 -0700 Subject: [PATCH 3/4] logs cleaned and email relay example added to config --- config/index.js | 8 ++++---- plugins/EpadDB.js | 3 --- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/config/index.js b/config/index.js index 15b84725..a14d087e 100644 --- a/config/index.js +++ b/config/index.js @@ -73,9 +73,9 @@ config.noResume = process.env.NO_RESUME === 'true' || config.noResume || false; config.secret = process.env.SECRET || config.secret || undefined; // mail relay example. Required to register the app // config.notificationEmail = { host: 'smtp.gmail.com' }; -// config.notificationEmail = { port: 465 }; // tls 465 +// config.notificationEmail = { port: 465 }; // ssl: 465 or 587 // config.notificationEmail.isTls = true; // use TLS -// config.notificationEmail.auth = {}; -// config.notificationEmail.auth = { user: 'example@gmail.com' }; -// config.notificationEmail.auth = { pass: 'examplepass' }; +// config.notificationEmail.auth = {}; +// config.notificationEmail.auth.user: 'example@gmail.com' ; +// config.notificationEmail.auth.pass: 'examplepass' ; module.exports = config; diff --git a/plugins/EpadDB.js b/plugins/EpadDB.js index b80c09fb..28905c32 100644 --- a/plugins/EpadDB.js +++ b/plugins/EpadDB.js @@ -3484,7 +3484,6 @@ async function epaddb(fastify, options, done) { fastify.decorate('registerServerForAppKey', async (request, reply) => { const requestSenderServerName = request.raw.headers.host.split(':')[0]; const tempBody = request.body; - console.log('................request body', request.body); let tempEpadStatServer = config.statsEpad.split('//')[1]; if ( tempEpadStatServer === '' || @@ -3495,7 +3494,6 @@ async function epaddb(fastify, options, done) { } // if (config.statsEpad) { if (!requestSenderServerName.includes(tempEpadStatServer)) { - console.log('calling remote server'); const resultRemoteRegister = await Axios.post(`${config.statsEpad}/api/register`, { headers: { 'Content-Type': 'application/json', @@ -3511,7 +3509,6 @@ async function epaddb(fastify, options, done) { reply.code(resultRemoteRegister.code).send(resultRemoteRegister.data); return; } - console.log('after................request body', request.body); const tempName = request.body.name; const tempEmail = request.body.email; const tempOrganization = request.body.organization; From d16ee41cfc94ecae15f016b3bb2698bd3ea1713c Mon Sep 17 00:00:00 2001 From: cavit Date: Wed, 23 Jun 2021 17:55:46 -0700 Subject: [PATCH 4/4] duplicate codevalue fixed --- plugins/EpadDB.js | 1 - plugins/Ontology.js | 23 +++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/plugins/EpadDB.js b/plugins/EpadDB.js index 28905c32..449fc41b 100644 --- a/plugins/EpadDB.js +++ b/plugins/EpadDB.js @@ -3499,7 +3499,6 @@ async function epaddb(fastify, options, done) { 'Content-Type': 'application/json', }, ...tempBody, - username: 'admin', }); fastify.log.info( diff --git a/plugins/Ontology.js b/plugins/Ontology.js index 1268033a..2981981a 100644 --- a/plugins/Ontology.js +++ b/plugins/Ontology.js @@ -255,7 +255,7 @@ async function Ontology(fastify, options, done) { fastify.decorate( 'insertOntologyItemInternal', async (lexiconObj) => - // this function need to call remote ontology server if no valid ontology apikey + // this function need to call remote ontology server if valid ontology apikey new Promise(async (resolve, reject) => { let returnObj = null; try { @@ -265,7 +265,7 @@ async function Ontology(fastify, options, done) { } if (returnObj.code === 200) { try { - const nextindex = (await fastify.generateCodeValueInternal()) + 1; + // const nextindex = (await fastify.generateCodeValueInternal()) + 1; const { codemeaning: CODE_MEANING, description, @@ -279,19 +279,30 @@ async function Ontology(fastify, options, done) { const retVal = await models.lexicon.create({ CODE_MEANING, - CODE_VALUE: `99EPAD_${nextindex}`, description, SCHEMA_DESIGNATOR, SCHEMA_VERSION, referenceuid, referencename, referencetype, - indexno: nextindex, creator, createdtime: Date.now(), updatetime: Date.now(), }); + await models.lexicon.update( + { + CODE_VALUE: `99EPAD_${retVal.ID}`, + indexno: retVal.indexno, + updatetime: Date.now(), + }, + { + where: { + ID: retVal.ID, + }, + } + ); + const resultInJson = { id: retVal.ID, codevalue: retVal.CODE_VALUE, @@ -417,7 +428,7 @@ async function Ontology(fastify, options, done) { referencename, referencetype, } = request.body; - models.lexicon.update( + await models.lexicon.update( { CODE_MEANING, CODE_VALUE, @@ -453,7 +464,7 @@ async function Ontology(fastify, options, done) { try { await fastify.validateApiKeyInternal(request); const { codevalue: CODE_VALUE } = request.params; - models.lexicon.destroy({ + await models.lexicon.destroy({ where: { CODE_VALUE, },