Commit f4eac542 authored by nilanjandaw's avatar nilanjandaw

added API structure

parent 1dce7e84
"use strict";
const isolateBackend = require('./isolate')
const { spawn } = require('child_process');
const libSupport = require('./lib')
const fs = require('fs')
const express = require('express')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload');
const redis = require('redis')
let client = redis.createClient();
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(fileUpload())
const port = 8080
app.post('/serverless/deploy', (req, res) => {
let runtime = req.body.runtime
let file = req.files.serverless
let functionHash = file.md5
console.log("sa");
file.mv(__dirname + "/test/" + functionHash, function (err) {
if (err) {
console.log(err);
res.send("error").status(400)
}
else {
if (runtime === "container") {
deployContainer('./test/', functionHash)
.then(() => {
res.json({
status: "success",
function_id: functionHash
})
})
.catch(err => {
res.json({
status: "error",
reason: err}).status(400)
})
} else {
res.json({
status: "success",
function_id: functionHash
})
}
}
})
})
app.post('/serverless/execute/:id', (req, res) => {
let runtime = req.body.runtime
let functionHash = req.params.id
if (runtime === "isolate")
runIsolate('./test/' + functionHash).then(() => res.json({status: "success"}))
else if (runtime === "process")
runProcess('./test/' + functionHash).then(() => res.json({ status: "success" }))
else if (runtime === "container")
runContainer(functionHash).then(() => res.json({ status: "success" }))
else {
res.send("unknown")
return
}
})
function runIsolate(filename) {
let timeStart = Date.now()
let {isolate, context} = isolateBackend.createIsolate();
// Let's see what happens when we try to blow the isolate's memory
fs.readFile(filename, 'utf-8',(err, data) => {
return new Promise( (resolve, reject) => {
let timeStart = Date.now()
let {isolate, context} = isolateBackend.createIsolate();
isolate.compileScript(data).then(script => {
script.run(context)
.then(result => {
console.log(result);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference);
fs.readFile(filename, 'utf-8',(err, data) => {
if (err)
reject(err);
isolate.compileScript(data).then(script => {
script.run(context)
.then(result => {
console.log(result);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference);
resolve();
});
}).catch(err => {reject(err)})
});
})
});
}
function runProcess(filename) {
let timeStart = Date.now()
const process = spawn('node', [filename]);
return new Promise((resolve, reject) => {
let timeStart = Date.now()
const process = spawn('node', [filename]);
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("process time taken: ", timeDifference);
});
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("process time taken: ", timeDifference);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
reject(data);
});
process.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
process.on('close', (code) => {
console.log(`child process exited with code ${code}`);
resolve();
});
})
}
function runContainer(path) {
let buildStart = Date.now()
let imageName = libSupport.makeid(10)
const process = spawn('docker', ["build", "-t", imageName, path, "-q"]);
function deployContainer(path, imageName) {
return new Promise((resolve, reject) => {
let buildStart = Date.now()
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
fs.writeFile(__dirname + '/test/Dockerfile',
`FROM node:latest
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node ${imageName}`
, function (err) {
if (err) reject(err);
else {
console.log('Dockerfile created');
const process = spawn('docker', ["build", "-t", imageName, path, "-q"]);
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
process.on('close', (code) => {
console.log(`child process exited with code ${code}`);
let timeDifference = Math.ceil((Date.now() - buildStart))
console.log("image build time taken: ", timeDifference);
process.on('close', (code) => {
console.log(`child process exited with code ${code}`);
let timeDifference = Math.ceil((Date.now() - buildStart))
console.log("image build time taken: ", timeDifference);
resolve();
});
}
});
})
}
function runContainer(imageName) {
console.log(imageName);
return new Promise((resolve, reject) => {
let timeStart = Date.now()
const process = spawn('docker', ["run", "--name", imageName, imageName]);
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("container run time taken: ", timeDifference);
console.log("container run time taken: ", timeDifference);
});
process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
reject(data);
});
process.on('close', (code) => {
resolve(code);
})
});
})
}
runIsolate('./test/script.js');
runProcess('./test/script.js');
runContainer("test/");
\ No newline at end of file
app.listen(port, () => console.log(`Server listening on port ${port}!`))
\ No newline at end of file
......@@ -4,12 +4,16 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"isolated-vm": "^3.0.0"
"express-fileupload": "^1.1.6",
"isolated-vm": "^3.0.0",
"redis": "^2.8.0"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment