-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspawnServer.js
executable file
·108 lines (85 loc) · 2.31 KB
/
spawnServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const spawn = require('child_process').spawn;
const express = require('express');
const ls = spawn('ls', ['-lh', '/usr']);
const exec = require('child_process').exec;
var fs = require('fs');
// Constants
const PORT = 3000;
writePortNum = 5000;
var extPort = 49160;
var imageNum = 1;
// App
const app = express();
app.get('/', function (req, res) {
resData = launchContainer();
function writePorts(portNum){
fs.truncate("port.txt", 0, function() {
fs.writeFile("port.txt", portNum , function (err) {
if (err) {
return console.log("Error writing file: " + err);
}
writeDockerFile(portNum);
});
});
}
function writeDockerFile(portNum){
fs.truncate("Dockerfile", 0, function() {
content = "FROM node:boron \n" +
"# Create app directory\n"+
"RUN mkdir -p /usr/src/app\n"+
"WORKDIR /usr/src/app\n"+
"# Install app dependencies\n"+
"COPY package.json /usr/src/app/\n"+
"COPY port.txt /usr/src/app/\n"+
"RUN npm install\n"+
"# Bundle app source\n"+
"COPY . /usr/src/app\n"+
"EXPOSE " + portNum+ "\n"+
"CMD [ \"npm\", \"start\" ]\n"
fs.writeFile("Dockerfile", content , function (err) {
if (err) {
return console.log("Error writing file: " + err);
}
buildAndRunImage();
});
});
}
function buildAndRunImage(){
exec('docker build -t ' + 'helloworld'+imageNum + ' .', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
exec('docker run -p '+extPort+':' + writePortNum + ' -d ' + 'helloworld'+imageNum , (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
console.log("ERROR");
return;
}
else{
console.log("NO ERROR \n")
//extPort++;
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
res.send({
'status' : 'Success',
'msg' : 'New Container launched',
'port' : extPort
});
writePortNum+=100
extPort++;
imageNum++;
return (stdout);
}
});
});
}
function launchContainer(){
writePorts(writePortNum);
//console.log('\n\n\nlaunching on port '+extPort+'\n');
//console.log("INSODE REQUEST\n");
//console.log("incremented port :" + extPort+"\n");
}
});
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);