Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 3.8.0_Fix #23

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ MAINTAINER "Mahesh Kumar Gangula" "[email protected]"
USER root
COPY src /opt/print-service/
WORKDIR /opt/print-service/
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
#ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
RUN npm install --unsafe-perm
FROM node:8.11-slim
FROM node:node:8-buster-slim
MAINTAINER "Mahesh Kumar Gangula" "[email protected]"
RUN apt-get clean \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt update && apt install fonts-indic -y \
&& apt-get install -y google-chrome-unstable \
&& apt-get install -y google-chrome-unstable gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
RUN groupadd -r sunbird && useradd -r -g sunbird -G audio,video sunbird \
Expand All @@ -21,4 +21,7 @@ RUN fc-cache -f -v
USER sunbird
COPY --from=build --chown=sunbird /opt/print-service/ /home/sunbird/print-service/
WORKDIR /home/sunbird/print-service/
CMD ["node", "app.js", "&"]
# All the downloaded zip will be present inside certs folder.
RUN mkdir /home/sunbird/print-service/certs
ENV NODE_ENV production
CMD ["node","app.js","&"]
53 changes: 53 additions & 0 deletions auto_build_deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@Library('deploy-conf') _
node('build-slave') {
try {
String ANSI_GREEN = "\u001B[32m"
String ANSI_NORMAL = "\u001B[0m"
String ANSI_BOLD = "\u001B[1m"
String ANSI_RED = "\u001B[31m"
String ANSI_YELLOW = "\u001B[33m"

ansiColor('xterm') {
stage('Checkout') {
tag_name = env.JOB_NAME.split("/")[-1]
pre_checks()
if (!env.hub_org) {
println(ANSI_BOLD + ANSI_RED + "Uh Oh! Please set a Jenkins environment variable named hub_org with value as registery/sunbidrded" + ANSI_NORMAL)
error 'Please resolve the errors and rerun..'
}
else
println(ANSI_BOLD + ANSI_GREEN + "Found environment variable named hub_org with value as: " + hub_org + ANSI_NORMAL)
cleanWs()
def scmVars = checkout scm
checkout scm: [$class: 'GitSCM', branches: [[name: "refs/tags/$tag_name"]], userRemoteConfigs: [[url: scmVars.GIT_URL]]]
build_tag = tag_name + "_" + env.BUILD_NUMBER
commit_hash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
artifact_version = tag_name + "_" + commit_hash
echo "build_tag: " + build_tag
}

// stage Build
env.NODE_ENV = "build"
print "Environment will be : ${env.NODE_ENV}"
sh('chmod 777 build.sh')
sh("./build.sh ${build_tag} ${env.NODE_NAME} ${hub_org}")

// stage ArchiveArtifacts
archiveArtifacts "metadata.json"
currentBuild.description = "${build_tag}"

}
currentBuild.result = "SUCCESS"
slack_notify(currentBuild.result, tag_name)
email_notify()
auto_build_deploy()

}
catch (err) {
currentBuild.result = "FAILURE"
slack_notify(currentBuild.result, tag_name)
email_notify()
throw err
}

}
30 changes: 30 additions & 0 deletions src/FileManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs')
const logger = require('./sdk/log4js');



const getAbsolutePath = (path) => {
var fullpath = __dirname +"/"+path;
console.log("absolute path of download zip is:",fullpath )
return fullpath;
}



const deleteFiles = (filePaths)=>{
filePaths.forEach(element => {
fs.unlink(element, deleteFile)
});}


var deleteFile= function (err) {
if (err) {
logger.info("unlink failed", err);
} else {
logger.info("file deleted");
}
}


exports.getAbsolutePath= getAbsolutePath;
exports.deleteFiles = deleteFiles;
8 changes: 3 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express'),
cluster = require('express-cluster'),
cookieParser = require('cookie-parser'),
logger = require('morgan'),
logger = require('./sdk/log4js'),
bodyParser = require('body-parser'),
envVariables = require('./envVariables'),
port = envVariables.port,
Expand All @@ -17,7 +17,6 @@ const express = require('express'),
else next()
})
app.use(bodyParser.json({ limit: '1mb' }));
app.use(logger('dev'));
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
Expand All @@ -32,7 +31,6 @@ const express = require('express'),
return app.listen(port, () => console.log(`print-service cluster is running on port ${port} with ${process.pid} pid`));
}, { count: threads });
} else {
const app = createAppServer();
const app = createAppServer();
app.listen(port, () => console.log(`print-service is running in test env on port ${port} with ${process.pid} pid`));
}

}
21 changes: 13 additions & 8 deletions src/envVariables.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const os = require('os');

const envVariables = {
port: process.env.service_port || 5000,
threads: process.env.service_threads || os.cpus().length,
azureAccountName: process.env.sunbird_azure_account_name,
azureAccountKey: process.env.sunbird_azure_account_key,
azureContainerName: process.env.sunbird_azure_container_name,
level: process.env.service_log_level || 'info',
encodingType: process.env.service_encoding_type,
filename: process.env.service_file_filename || 'print-service-%DATE%.log'
port: process.env.service_port || 5000,
threads: process.env.service_threads || os.cpus().length,
azureAccountName: process.env.sunbird_azure_account_name,
azureAccountKey: process.env.sunbird_azure_account_key,
azureContainerName: process.env.sunbird_azure_container_name,
privateContainer: {
azureAccountName: process.env.sunbird_pvt_azure_account_name,
azureAccountKey: process.env.sunbird_pvt_azure_account_key,
azureContainerName: process.env.sunbird_pvt_azure_container_name
},
level: process.env.service_log_level || 'info',
encodingType: process.env.service_encoding_type,
filename: process.env.service_file_filename || 'print-service-%DATE%.log',
}
module.exports = envVariables;
36 changes: 36 additions & 0 deletions src/generators/HtmlGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');
const Mapper = require('./Mapper')
const encodingType = 'utf8';
const logger = require('../sdk/log4js');


/**
* @author Anmol Gupta <[email protected]>
*/
class HtmlGenerator{

constructor(htmlFilePath, request){
this.htmlFilePath=htmlFilePath;
this.request= request;
}


generateTempHtmlFile(){
var startTime = Date.now();
var htmlFile = fs.readFileSync(this.htmlFilePath,encodingType);
var mapper = new Mapper(htmlFile, this.request.getContextMap());
var mappedHtml = mapper.replacePlaceholders();
var mappedHtmlFilePath = this.getReqIdHtmlFilePath();
fs.writeFileSync(mappedHtmlFilePath,mappedHtml)
logger.debug("HtmlGenerator:generateTempHtmlFile:file written successfully in ms:", Date.now()-startTime)
return mappedHtmlFilePath;
}

getReqIdHtmlFilePath(){
var tempHtmlFilePath = this.htmlFilePath.replace("index.html", this.request.getRequestId()+".html")
logger.info("HtmlGenerator:getReqIdHtmlFilePath:the temp filepath formed is", tempHtmlFilePath)
return tempHtmlFilePath;
}

}
module.exports = HtmlGenerator;
23 changes: 23 additions & 0 deletions src/generators/Mapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var velocity = require('velocityjs');

/**
* @author Anmol Gupta <[email protected]>
*/


class Mapper{

constructor(htmlFile,contextMap)
{
this.htmlFile= htmlFile;
this.contextMap= contextMap;
}


replacePlaceholders(){
var asts = velocity.parse(this.htmlFile);
var results = (new velocity.Compile(asts)).render(this.contextMap,null);
return results;
}
}
module.exports= Mapper;
30 changes: 30 additions & 0 deletions src/helpers/DownloadManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const logger = require('../sdk/log4js');
const request = require('superagent');
const fs = require('fs');


/**
* @author Anmol Gupta <[email protected]>
*/
class DownloadManager {

constructor(downloadParams) {
this.downloadParams = downloadParams;
}


downloadFile(callback) {
request
.get(this.downloadParams.getSourceUrl())
.on('error', function (error) {
logger.error("DownloadManager:downloadFile:",error);
})
.pipe(fs.createWriteStream(this.downloadParams.getDownloadPath()))
.on('finish', function () {
logger.info('finished dowloading');
callback(null)
});
}
}

module.exports = DownloadManager
48 changes: 48 additions & 0 deletions src/helpers/DownloadParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const logger = require('../sdk/log4js');

/**
* @author Anmol Gupta <[email protected]>
*/

class DownloadParams {


constructor(sourceUrl) {
this.sourceUrl = sourceUrl;
this.CERT_DOWNLOAD_FOLDER= "certs/"
}

getDownloadPath() {
const fileName = this.getFileName()
const downloadPath = this.CERT_DOWNLOAD_FOLDER.concat(fileName)
logger.info("DownloadParams:getDownloadPath:Download path formed:", downloadPath)
return downloadPath;
}


getSourceUrl() {
return this.sourceUrl;
}

getFileExtractToPath() {
const extactToPath = this.CERT_DOWNLOAD_FOLDER.concat(this.getFileName().replace(".zip", "/"))
logger.info("DownloadParams:getFileExtractToPath got:", extactToPath)
return extactToPath
}


getHtmlPath() {
const htmlPath = this.CERT_DOWNLOAD_FOLDER.concat(this.getFileName().replace(".zip", "/index.html"))
logger.info("DownloadParams:getHtmlPath:index.html file path is:", htmlPath)
return htmlPath;


}

getFileName() {
return this.sourceUrl.substring(this.sourceUrl.lastIndexOf('/') + 1);
}

}

module.exports = DownloadParams;
30 changes: 30 additions & 0 deletions src/helpers/FileExtractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const logger = require('../sdk/log4js');
const admZip = require('adm-zip');
const filemanager = require('../FileManager')


/**
* @author Anmol Gupta <[email protected]>
*/


class FileExtactor {

constructor(downloadParams) {
this.downloadParams = downloadParams
}
/**
* this method will extract the zip file and return the absolute path file uri.
* @param {*} callback
*/
extractZipFile(callback) {
const startTime = Date.now();
var zip = new admZip(this.downloadParams.getDownloadPath());
logger.info('FileExtractor:extractZipFile:start unzip at path', this.downloadParams.getFileExtractToPath());
zip.extractAllTo(this.downloadParams.getFileExtractToPath(), true);
logger.debug('FileExtractor:extractZipFile:finished unzip in secs:', Date.now() - startTime);
callback(null,filemanager.getAbsolutePath(this.downloadParams.getHtmlPath()))
}
}

module.exports = FileExtactor;
37 changes: 37 additions & 0 deletions src/helpers/Request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@




/**
* @author Anmol Gupta <[email protected]>
*/



class Request {

constructor(contextMap, htmlTemplate, requestId, storageParmas) {
this.contextMap = contextMap;
this.htmlTemplate = htmlTemplate;
this.requestId = requestId;
this.storageParmas= storageParmas;
}

getContextMap() {
return this.contextMap;
}

getStorageParams(){
return this.storageParmas;
}

getRequestId() {
return this.requestId;
}
getHtmlTemplate(){
return this.htmlTemplate;
}

}

module.exports = Request;
Loading