Commit f4eac542 authored by nilanjandaw's avatar nilanjandaw

added API structure

parent 1dce7e84
"use strict";
const isolateBackend = require('./isolate') const isolateBackend = require('./isolate')
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const libSupport = require('./lib') const libSupport = require('./lib')
const fs = require('fs') 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) { function runIsolate(filename) {
return new Promise( (resolve, reject) => {
let timeStart = Date.now() let timeStart = Date.now()
let {isolate, context} = isolateBackend.createIsolate(); 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) => { fs.readFile(filename, 'utf-8',(err, data) => {
if (err)
reject(err);
isolate.compileScript(data).then(script => { isolate.compileScript(data).then(script => {
script.run(context) script.run(context)
.then(result => { .then(result => {
console.log(result); console.log(result);
let timeDifference = Math.ceil((Date.now() - timeStart)) let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference); console.log("isolate time taken: ", timeDifference);
resolve();
}); });
}) }).catch(err => {reject(err)})
}); });
});
} }
function runProcess(filename) { function runProcess(filename) {
return new Promise((resolve, reject) => {
let timeStart = Date.now() let timeStart = Date.now()
const process = spawn('node', [filename]); const process = spawn('node', [filename]);
...@@ -34,17 +110,33 @@ function runProcess(filename) { ...@@ -34,17 +110,33 @@ function runProcess(filename) {
process.stderr.on('data', (data) => { process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`); console.error(`stderr: ${data}`);
reject(data);
}); });
process.on('close', (code) => { process.on('close', (code) => {
console.log(`child process exited with code ${code}`); console.log(`child process exited with code ${code}`);
resolve();
}); });
})
} }
function runContainer(path) { function deployContainer(path, imageName) {
return new Promise((resolve, reject) => {
let buildStart = Date.now() let buildStart = Date.now()
let imageName = libSupport.makeid(10)
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"]); const process = spawn('docker', ["build", "-t", imageName, path, "-q"]);
process.stdout.on('data', (data) => { process.stdout.on('data', (data) => {
...@@ -60,6 +152,17 @@ function runContainer(path) { ...@@ -60,6 +152,17 @@ function runContainer(path) {
console.log(`child process exited with code ${code}`); console.log(`child process exited with code ${code}`);
let timeDifference = Math.ceil((Date.now() - buildStart)) let timeDifference = Math.ceil((Date.now() - buildStart))
console.log("image build time taken: ", timeDifference); console.log("image build time taken: ", timeDifference);
resolve();
});
}
});
})
}
function runContainer(imageName) {
console.log(imageName);
return new Promise((resolve, reject) => {
let timeStart = Date.now() let timeStart = Date.now()
const process = spawn('docker', ["run", "--name", imageName, imageName]); const process = spawn('docker', ["run", "--name", imageName, imageName]);
...@@ -71,13 +174,14 @@ function runContainer(path) { ...@@ -71,13 +174,14 @@ function runContainer(path) {
process.stderr.on('data', (data) => { process.stderr.on('data', (data) => {
console.error(`stderr: ${data}`); console.error(`stderr: ${data}`);
reject(data);
}); });
process.on('close', (code) => { process.on('close', (code) => {
resolve(code);
}) })
}); })
} }
runIsolate('./test/script.js'); app.listen(port, () => console.log(`Server listening on port ${port}!`))
runProcess('./test/script.js'); \ No newline at end of file
runContainer("test/");
\ No newline at end of file
...@@ -4,12 +4,16 @@ ...@@ -4,12 +4,16 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1", "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