From 357bccce0108933cf44cb2d6aa46f39f84f0ac99 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Tue, 16 Nov 2021 05:07:22 +0500 Subject: [PATCH] chore: provide sas executable to docker --- .gitignore | 1 + api/package.json | 3 +-- api/src/app.ts | 3 +-- api/src/controllers/internal/Session.ts | 7 +++-- api/src/routes/api/auth.ts | 27 ------------------- api/src/types/Process.d.ts | 2 +- api/src/utils/connectDB.ts | 36 +++++++++++++++++++++++++ api/src/utils/index.ts | 1 + docker-compose.prod.yml | 1 + startProduction.sh | 23 ++++++++++++++++ 10 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 api/src/utils/connectDB.ts create mode 100755 startProduction.sh diff --git a/.gitignore b/.gitignore index 4fe91fa..0457868 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ build/ certificates/ executables/ .env +sas \ No newline at end of file diff --git a/api/package.json b/api/package.json index bd25b43..4278c44 100644 --- a/api/package.json +++ b/api/package.json @@ -79,7 +79,6 @@ "typescript": "^4.3.2" }, "configuration": { - "sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4/sas", - "sasJsPort": 5000 + "sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4/sas" } } diff --git a/api/src/app.ts b/api/src/app.ts index 1b78f2f..6879eff 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -6,8 +6,7 @@ import cors from 'cors' import webRouter from './routes/web' import apiRouter from './routes/api' -import { getWebBuildFolderPath } from './utils' -import { connectDB } from './routes/api/auth' +import { connectDB, getWebBuildFolderPath } from './utils' dotenv.config() diff --git a/api/src/controllers/internal/Session.ts b/api/src/controllers/internal/Session.ts index c25daf5..56a6e58 100644 --- a/api/src/controllers/internal/Session.ts +++ b/api/src/controllers/internal/Session.ts @@ -1,6 +1,5 @@ import path from 'path' import { Session } from '../../types' -import { configuration } from '../../../package.json' import { promisify } from 'util' import { execFile } from 'child_process' import { getTmpSessionsFolderPath, generateUniqueFileName } from '../../utils' @@ -65,8 +64,8 @@ export class SessionController { // update the session array to say that it is currently running // however we also need a promise so that we can update the // session array to say that it has (eventually) finished. - const sasLoc = process.sasLoc ?? configuration.sasPath - execFilePromise(sasLoc, [ + + execFilePromise(process.sasLoc, [ '-SYSIN', codePath, '-LOG', @@ -84,7 +83,7 @@ export class SessionController { .catch((err) => { session.completed = true session.crashed = err.toString() - console.log('session crashed', session.id, err) + console.log('session crashed', session.id) }) // we have a triggered session - add to array diff --git a/api/src/routes/api/auth.ts b/api/src/routes/api/auth.ts index 32929ef..df359bc 100644 --- a/api/src/routes/api/auth.ts +++ b/api/src/routes/api/auth.ts @@ -1,5 +1,4 @@ import express from 'express' -import mongoose from 'mongoose' import { AuthController } from '../../controllers/' import Client from '../../model/Client' @@ -28,32 +27,6 @@ export const populateClients = async () => { }) } -export const connectDB = async () => { - const { MODE } = process.env - if (MODE?.trim() !== 'server') { - console.log('Running in Destop Mode, no DB to connect.') - - const { sasLoc, driveLoc } = await getDesktopFields() - - process.sasLoc = sasLoc - process.driveLoc = driveLoc - - return - } - - // NOTE: when exporting app.js as agent for supertest - // we should exlcude connecting to the real database - if (process.env.NODE_ENV !== 'test') { - mongoose.connect(process.env.DB_CONNECT as string, async (err) => { - if (err) throw err - - console.log('Connected to db!') - - await populateClients() - }) - } -} - authRouter.post('/authorize', async (req, res) => { const { error, value: body } = authorizeValidation(req.body) if (error) return res.status(400).send(error.details[0].message) diff --git a/api/src/types/Process.d.ts b/api/src/types/Process.d.ts index f90ae02..dbd7f0f 100644 --- a/api/src/types/Process.d.ts +++ b/api/src/types/Process.d.ts @@ -1,6 +1,6 @@ declare namespace NodeJS { export interface Process { - sasLoc?: string + sasLoc: string driveLoc?: string sessionController?: import('../controllers/internal').SessionController } diff --git a/api/src/utils/connectDB.ts b/api/src/utils/connectDB.ts new file mode 100644 index 0000000..660d727 --- /dev/null +++ b/api/src/utils/connectDB.ts @@ -0,0 +1,36 @@ +import path from 'path' +import mongoose from 'mongoose' +import { configuration } from '../../package.json' +import { getDesktopFields } from '.' +import { populateClients } from '../routes/api/auth' + +export const connectDB = async () => { + const { MODE } = process.env + if (MODE?.trim() !== 'server') { + console.log('Running in Destop Mode, no DB to connect.') + + const { sasLoc, driveLoc } = await getDesktopFields() + + process.sasLoc = sasLoc + process.driveLoc = driveLoc + + return + } else { + const { SAS_EXEC } = process.env + process.sasLoc = SAS_EXEC + ? path.join(__dirname, '..', '..', '...', SAS_EXEC) + : configuration.sasPath + } + + // NOTE: when exporting app.js as agent for supertest + // we should exlcude connecting to the real database + if (process.env.NODE_ENV !== 'test') { + mongoose.connect(process.env.DB_CONNECT as string, async (err) => { + if (err) throw err + + console.log('Connected to db!') + + await populateClients() + }) + } +} diff --git a/api/src/utils/index.ts b/api/src/utils/index.ts index ade03b5..3cf1039 100644 --- a/api/src/utils/index.ts +++ b/api/src/utils/index.ts @@ -1,3 +1,4 @@ +export * from './connectDB' export * from './file' export * from './generateAccessToken' export * from './generateAuthCode' diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index c809b32..6934523 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -14,6 +14,7 @@ services: REFRESH_TOKEN_SECRET: ${REFRESH_TOKEN_SECRET} AUTH_CODE_SECRET: ${AUTH_CODE_SECRET} DB_CONNECT: mongodb://mongodb:27017/sasjs + SAS_EXEC: ${SAS_EXEC} expose: - ${PORT_API} ports: diff --git a/startProduction.sh b/startProduction.sh new file mode 100755 index 0000000..bb7848f --- /dev/null +++ b/startProduction.sh @@ -0,0 +1,23 @@ +sasjsPath=$(grep sasPath package.json | sed 's/.*"sasPath": "\(.*\)".*/\1/') + +if [ -z "$sasjsPath" ] +then + echo "Please enter path to SAS executable:" + read sasjsPath + +fi + +if [ -e $sasjsPath ] +then + echo "Using sas executable:" \"$sasjsPath\" +else + echo "No file present at:" \"$sasjsPath\" + exit 1 +fi + +cp $sasjsPath ./ +sasjs=$(basename $sasjsPath) + +echo "SAS Executable name:" $sasjs + +SAS_EXEC=$sasjs docker-compose -f docker-compose.prod.yml up --build -d \ No newline at end of file