Skip to content

Commit

Permalink
Version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jslightham committed Mar 20, 2022
1 parent 7e278d0 commit 0ce45c9
Show file tree
Hide file tree
Showing 10 changed files with 1,026 additions and 54 deletions.
24 changes: 24 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var config = {};

config.languages = {
'java': {
'name': 'java',
'file': 'Main.java',
'script': 'java.sh',
'docker': 'openjdk:11'
},
'python': {
'name': 'python',
'file': 'Main.py',
'script': 'python.sh',
'docker': 'python'
},
'c': {
'name': 'c',
'file': 'Main.c',
'script': 'c.sh',
'docker': 'gcc'
}
}

module.exports = config;
53 changes: 21 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
const Docker = require('dockerode');
const config = require('./config');

const express = require("express");
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');

const util = require('util');
const exec = util.promisify(require('child_process').exec);

var docker = new Docker({ socketPath: '/var/run/docker.sock' });
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const run = require('./routes.js');
app.use('/run', run);

run();
var docker = new Docker({ socketPath: '/var/run/docker.sock' });

async function run() {
docker.createContainer({ Image: 'myimage1:1.0', name: 'test1' }, (err, container) => {
// Pull all images on startup & make sure all scripts are executable
for (const property in config.languages) {
exec("chmod +x ./languages/" + config.languages[property].script);
docker.pull(config.languages[property].docker, (err, stream) => {
if (err) {
console.log(err);
} else {
exec(`docker cp ./temp/abc.java ${container.id}:Main.java && docker cp ./languages/scripts/java.sh ${container.id}:java.sh`).then((stdout, stderr) => {
container.start((err, data) => {
if (err) {
console.log(err);
} else {
container.attach({ stream: true, stdout: true, stderr: true }, function (err, stream) {
stream.pipe(process.stdout);
sleep(5000).then(() => {
container.kill((err, data) => {
container.remove((err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
});
});
});
}
});
});
}
stream.pipe(process.stdout);
});
}

function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
app.listen(8000, () => {
console.log("Express listening on port 8000");
})
5 changes: 5 additions & 0 deletions languages/c.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
echo "Compiling with gcc..."
gcc -Wall Main.c -o Main
echo "Running c program..."
./Main
5 changes: 0 additions & 5 deletions languages/dockerfiles/java

This file was deleted.

1 change: 0 additions & 1 deletion languages/scripts/java.sh → languages/java.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash
ls -l
echo "Compiling java program..."
javac Main.java
echo "Running java program..."
Expand Down
3 changes: 3 additions & 0 deletions languages/python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "Running python file..."
python3 Main.py
Loading

0 comments on commit 0ce45c9

Please sign in to comment.