You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3327 lines
102 KiB

4 years ago
4 years ago
4 years ago
var fs = require('fs');
var express = require('express');
4 years ago
//var https = require('https');
var http = require('http');
4 years ago
var app = express();
4 years ago
const cors = require('cors')
const helmet = require('helmet');
4 years ago
const { Client } = require('ssh2');
const sshconnect = new Client();
4 years ago
var allowedOrigins = [
'https://localhost:55543',
4 years ago
'http://localhost:3080',
'http://localhost:3088',
4 years ago
'http://localhost:8080'
];
app.use(cors({
origin: function(origin, callback){ // allow requests with no origin
// (like mobile apps or curl requests)
if(!origin)
return callback(null, true);
if(allowedOrigins.indexOf(origin) === -1){
var msg = 'The CORS policy for this site does not ';
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
4 years ago
// GLOBAL
global.SSHPID = []; // running pid ssh
function killProcess(pid) {
console.log("KILL "+pid);
// kill proccess gia ssh pid
//check pid se poion aniki
var conn = global.SSHPID[pid]
console.log(conn);
conn.end();
}
function killBuildProcess(pid) {
try {
process.kill(-pid);
return true;
} catch(e) {
return false;
}
}
4 years ago
4 years ago
function checkpid(arr, val) {
return arr.some(arrVal => val === arrVal);
}
4 years ago
//app.use(cors)
4 years ago
//app.use(helmet());
app.use(
helmet({
frameguard: false,
contentSecurityPolicy: false
})
);
/*
app.use(helmet.contentSecurityPolicy());
app.use(helmet.dnsPrefetchControl());
app.use(helmet.expectCt());
app.use(helmet.frameguard());
app.use(helmet.hidePoweredBy());
app.use(helmet.hsts());
app.use(helmet.ieNoOpen());
app.use(helmet.noSniff());
app.use(helmet.permittedCrossDomainPolicies());
app.use(helmet.referrerPolicy());
app.use(helmet.xssFilter());
*/
4 years ago
app.use(express.json());
4 years ago
4 years ago
var serverPort = 3080;
/*
// ssl enabled
4 years ago
var options = {
key: fs.readFileSync('./llo/swarmlab.key'),
cert: fs.readFileSync('./llo/swarmlab.crt')
};
var server = https.createServer(options, app);
var io = require('socket.io')(server);
4 years ago
*/
var server = http.createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: "http://localhost:8080",
methods: ["GET", "POST"]
}
});
4 years ago
4 years ago
const { spawn, exec, execSync } = require('child_process')
4 years ago
const simpleGit = require('simple-git');
4 years ago
// const { exec } = require('child_process');
//const chokidar = require('chokidar');
//const logout = fs.openSync('./logs/out.log', 'a');
//const logerr = fs.openSync('./logs/out.log', 'a');
3 years ago
const logerribuild = fs.openSync('./logs/build-out.log', 'a');
4 years ago
//const watcher = chokidar.watch('./logs/build-out.log', { persistent: true });
const Tail = require('nodejs-tail');
const tail = new Tail('./logs/build-out.log');
tail.watch();
tail.on('line', (line) => {
var n = {}
n.data = line
io.emit('message_log', n);
})
4 years ago
/*
watcher
.on('add', path => {
var n = {}
n.data = path.toString()
})
.on('change', async path => {
//var updateContent = await readLastLines.read(path, 1);
tail.on('line', (line) => {
var n = {}
n.data = line
io.emit('message_out', n);
})
})
.on('unlink', path => {
var n = {}
n.data = path.toString()
io.emit('message_out', n);
});
*/
4 years ago
var static_options = {
dotfiles: 'ignore',
etag: false,
extensions: ['html'],
index: false,
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now())
}
}
3 years ago
4 years ago
app.use('/player', express.static('LabLearningObject-hybrid', static_options))
app.use('/asciidoc', express.static('LabLearningObject-hybrid-tmp', static_options))
app.use('/images', express.static('LabLearningObject-hybrid/images', static_options))
4 years ago
3 years ago
4 years ago
// ***************************************************
// rest get
// ***************************************************
4 years ago
app.post('/save_course_ascii', (req, res, next) => {
const code = req.body["code"]
let file = Math.random().toString(36).slice(2);
var RES = new Object();
var showexec = `./LabLearningObject-hybrid-tmp/${file}.html`
//console.log('file '+showexec)
//console.log(req.body)
fs.writeFile(showexec, code, (err) => {
if (err) {
console.error(`exec error: ${err}`);
RES.error = true
RES.error_msg = "no"
RES.data = 'nofile'
res.json(RES)
}else{
RES.error = false
RES.error_msg = "ok"
RES.data = file
res.json(RES)
}
});
});
app.post('/check_storage_sudorun', (req, res, next) => {
const service = req.body["storagename"]
var mypath = process.cwd()
if(service){
var services_path = './instance/'+service
var ENV_service_file_sudorun = services_path+'/swarmlabconfig/sudorun-storage-run'
var ENV_service_file_sudorundest = services_path+'/sudorun-storage-run'
//console.log(ENV_service_file_sudorun)
if (fs.existsSync(ENV_service_file_sudorun)) {
fs.copyFile(ENV_service_file_sudorun, ENV_service_file_sudorundest, (err) => {
if (err){
var n = {}
n.data = 'yes'
n.path = mypath+'/instance/'+service
n.error = 'yes'
res.json(n)
}else{
var n = {}
n.data = 'yes'
n.path = mypath+'/instance/'+service
n.error = 'no'
res.json(n)
}
});
}else{
var n = {}
n.data = 'error'
n.path = mypath+'/instance/'+service
n.error = 'yes'
res.json(n)
}
}else{
var n = {}
n.data = 'nosudo'
n.path = mypath
res.json(n)
}
});
app.post('/check_storage_sudostop', (req, res, next) => {
const service = req.body["storagename"]
var mypath = process.cwd()
if(service){
var services_path = './instance/'+service
var ENV_service_file_sudostop = services_path+'/swarmlabconfig/sudorun-storage-stop'
var ENV_service_file_sudostopdest = services_path+'/sudorun-storage-stop'
//console.log(ENV_service_file_sudorun)
if (fs.existsSync(ENV_service_file_sudostop)) {
fs.copyFile(ENV_service_file_sudostop, ENV_service_file_sudostopdest, (err) => {
if (err){
var n = {}
n.data = 'yes'
n.path = mypath+'/instance/'+service
n.error = 'yes'
res.json(n)
}else{
var n = {}
n.data = 'yes'
n.path = mypath+'/instance/'+service
n.error = 'no'
res.json(n)
}
});
}else{
var n = {}
n.data = 'error'
n.path = mypath+'/instance/'+service
n.error = 'yes'
res.json(n)
}
}else{
var n = {}
n.data = 'nosudo'
n.path = mypath
res.json(n)
}
});
4 years ago
app.get('/get_course_ascii', (req, res, next) => {
const dir = req.query["dir"]
const file = req.query["file"]
var showexec = `./LabLearningObject-hybrid/${dir}/${file}`
fs.readFile(showexec, 'utf8', function (err, data) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
//console.log(data)
var nn = []
//var datajson = JSON.parse(data);
//console.log(datajson)
var RES = new Object();
RES.error = false
RES.error_msg = "ok"
RES.data = data;
res.json(RES)
});
});
app.get('/get_course', (req, res, next) => {
const course = req.query["course"]
var showexec = `./LabLearningObject-hybrid/${course}/index.js`
fs.readFile(showexec, 'utf8', function (err, data) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
//console.log(data)
var nn = []
var datajson = JSON.parse(data);
//console.log(datajson)
var RES = new Object();
RES.code = req.query["action"]
RES.token = req.query["token"]
RES.error = false
RES.error_msg = "ok"
//RES.data = data;
RES.data = datajson;
res.json(RES.data)
});
});
app.get('/get_llo', (req, res, next) => {
4 years ago
var RES = new Object();
const page = req.query["page"]
const per_page = req.query["per_page"]
var level = req.query["level"]
var lang = req.query["lang"]
var filter = 'NULL'
var sort = req.query["sort"]
var sorttmp1 = sort.split('|');
var sortname = sorttmp1[0];
var sortorder = sorttmp1[1];
4 years ago
if(typeof req.query["filter"] !== "undefined" && req.query["filter"] !== ''){
4 years ago
filter = req.query["filter"]
4 years ago
}else{
4 years ago
console.log('yes')
4 years ago
}
RES.type = req.query["type"]
4 years ago
var showexec = `./LabLearningObject-hybrid/index.js`
fs.readFile(showexec, 'utf8', function (err, data) {
4 years ago
if (err) {
console.error(`exec error: ${err}`);
return;
}
4 years ago
var nn = []
var datajson = JSON.parse(data);
var datalenth = datajson.length
var results = []
4 years ago
if(filter !== 'NULL'){
var grep = new RegExp(filter);
for (var i=0 ; i < datalenth ; i++)
{
if(grep.test(datajson[i]['title'])){
results.push(datajson[i]);
}
}
datajson=results
}
4 years ago
var resultslang = []
var filterlang = ''
if(lang !== 0){
if (lang == 1){
filterlang = 'en'
} else if (lang == 2){
filterlang = 'el'
}
var greplang = new RegExp(filterlang);
var datalenth1 = datajson.length
for (var i=0 ; i < datalenth1 ; i++)
{
if(greplang.test(datajson[i]['lang'])){
resultslang.push(datajson[i]);
}
}
datajson=resultslang
}
var resultsl = []
var filterlevel = ''
4 years ago
if(level !== 0){
if (level == 1){
filterlevel = 'Elementary'
} else if (level == 2){
filterlevel = 'Intermediate'
} else if (level == 3){
filterlevel = 'Advanced'
} else if (level == 4){
filterlevel = 'Expert'
} else if (level == 5){
filterlevel = 'Guru'
}
4 years ago
var greplevel = new RegExp(filterlevel);
var datalenth1 = datajson.length
4 years ago
for (var i=0 ; i < datalenth1 ; i++)
{
if(greplevel.test(datajson[i]['level'])){
resultsl.push(datajson[i]);
}
}
datajson=resultsl
}
4 years ago
// sort by title
if(sortname == 'title'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.title.localeCompare(b.title);
}else{
return b.title.localeCompare(a.title);
}
});
}else if(sortname == 'lang'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.lang.localeCompare(b.lang);
}else{
return b.lang.localeCompare(a.lang);
}
});
}
4 years ago
4 years ago
var resultid = []
var datalenth2 = datajson.length
for (var i=0 ; i < datalenth2 ; i++)
{
datajson[i].id=i
datajson[i]._id=i
resultid.push(datajson[i]);
}
datajson=resultid
var total = datajson.length;
var perpage = per_page
//var page = 1
//var lastpage = Math.trunc(total/5);
var lastpage = total/perpage;
4 years ago
if(lastpage <= 1) {
lastpage=1
}else{
lastpage++
}
4 years ago
lastpage = Math.trunc(lastpage)
4 years ago
4 years ago
var next=(page+1);
4 years ago
if(next >= lastpage){
next=lastpage;
}
4 years ago
var prev=(page-1);
4 years ago
if(prev == 0){
prev=1;
}
4 years ago
var from=((page-1)*perpage)+1;
var to=(perpage*page)
4 years ago
4 years ago
var myplaybooks = new Object();
4 years ago
var links = `
{
"pagination": {
"total": ${total},
"per_page": ${perpage},
"current_page": ${page},
"last_page": ${lastpage},
"next_page_url": "?page=${next}",
"prev_page_url": "?page=${prev}",
"from": ${from},
"to": ${to},
"frommongo": ${from},
"tomongo": ${to}
}
}
3 years ago
`
4 years ago
//"next_page_url": "?page="+${next},
//"prev_page_url": "?page="+${prev},
//myplaybooks.links = links
myplaybooks.links = JSON.parse(links);
4 years ago
from--
myplaybooks.data = datajson.slice(from,to);
//myplaybooks.data = datajson;
4 years ago
var RES = new Object();
RES.code = req.query["action"]
RES.token = req.query["token"]
RES.error = false
RES.error_msg = "ok"
RES.data = myplaybooks;
res.json(RES.data)
});
});
4 years ago
app.get('/dockerservices', (req, res, next) => {
4 years ago
var RES = new Object();
const page = req.query["page"]
const per_page = req.query["per_page"]
4 years ago
var sort = req.query["sort"]
var filter = req.query["filter"]
var type = req.query["type"]
var sort = req.query["sort"]
var sorttmp1 = sort.split('|');
var sortname = sorttmp1[0];
var sortorder = sorttmp1[1];
4 years ago
4 years ago
var showexec = `docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}", "Ports":"{{.Ports}}", "Networks":"{{.Networks}}", "Status":"{{.Status}}","RunningFor":"{{.RunningFor}}","CreatedAt":"{{.CreatedAt}}"}' | jq . -s `
exec(showexec, (err, stdout, stderr) => {
4 years ago
if (err) {
console.error(`exec error: ${err}`);
return;
}
4 years ago
var nn = []
var string = stdout.toString()
var datajson = JSON.parse(string);
var results = []
if(filter !== 'NULL'){
var grep = new RegExp(filter);
var datalenth = datajson.length
for (var i=0 ; i < datalenth ; i++)
{
if(grep.test(datajson[i]['Names'])){
results.push(datajson[i]);
}
}
datajson=results
}
// sort by title
if(sortname == 'Names'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.Names.localeCompare(b.Names);
}else{
return b.Names.localeCompare(a.Names);
}
});
}else if(sortname == 'Status'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.Status.localeCompare(b.Status);
}else{
return b.Status.localeCompare(a.Status);
}
});
}
var resultid = []
var datalenth2 = datajson.length
for (var i=0 ; i < datalenth2 ; i++)
{
datajson[i].id=i
datajson[i]._id=i
resultid.push(datajson[i]);
}
datajson=resultid
4 years ago
4 years ago
//console.log('Number of files ' + stdout);
//console.log('Number string ' + string);
//console.log('stringify ' + JSON.stringify(datajson));
4 years ago
var total = datajson.length;
var perpage = per_page
//var page = 1
4 years ago
var lastpage = total/perpage;
4 years ago
if(lastpage <= 1) {
lastpage=1
}else{
lastpage++
}
4 years ago
lastpage = Math.trunc(lastpage);
4 years ago
var next=(page+1);
if(next >= lastpage){
next=lastpage;
}
var prev=(page-1);
if(prev == 0){
prev=1;
}
var from=((page-1)*perpage)+1;
var to=(perpage*page)
4 years ago
var myplaybooks = new Object();
var links = `
{
"pagination": {
"total": ${total},
"per_page": ${perpage},
"current_page": ${page},
"last_page": ${lastpage},
4 years ago
"next_page_url": "?page=${next}",
"prev_page_url": "?page=${prev}",
4 years ago
"from": ${from},
"to": ${to},
4 years ago
"frommongo": ${from},
"tomongo": ${to}
4 years ago
}
}
`
4 years ago
//"next_page_url": "?page="+${next},
//"prev_page_url": "?page="+${prev},
//console.log(JSON.stringify(links))
4 years ago
//myplaybooks.links = links
myplaybooks.links = JSON.parse(links);
4 years ago
from--
myplaybooks.data = datajson.slice(from,to);
//myplaybooks.data = datajson;
4 years ago
var RES = new Object();
RES.code = req.query["action"]
RES.token = req.query["token"]
RES.error = false
RES.error_msg = "ok"
RES.data = myplaybooks;
res.json(RES.data)
});
});
app.get('/dockerimages', (req, res, next) => {
var RES = new Object();
const page = req.query["page"]
const per_page = req.query["per_page"]
var sort = req.query["sort"]
var filter = req.query["filter"]
var type = req.query["type"]
var sort = req.query["sort"]
var sorttmp1 = sort.split('|');
var sortname = sorttmp1[0];
var sortorder = sorttmp1[1];
var showexec = `docker image ls --format "{{json . }}" | jq . -s `
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
var nn = []
var string = stdout.toString()
var datajson = JSON.parse(string);
var results = []
if(filter !== 'NULL'){
var grep = new RegExp(filter);
var datalenth = datajson.length
for (var i=0 ; i < datalenth ; i++)
{
if(grep.test(datajson[i]['Repository'])){
results.push(datajson[i]);
}
}
datajson=results
}
// sort by title
if(sortname == 'Repository'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.Repository.localeCompare(b.Repository);
}else{
return b.Repository.localeCompare(a.Repository);
}
});
}else if(sortname == 'Size'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.Size.localeCompare(b.Size);
}else{
return b.Size.localeCompare(a.Size);
}
});
}
var resultid = []
var datalenth2 = datajson.length
for (var i=0 ; i < datalenth2 ; i++)
{
datajson[i].id=i
datajson[i]._id=i
resultid.push(datajson[i]);
}
datajson=resultid
//console.log('Number of files ' + stdout);
//console.log('Number string ' + string);
//console.log('stringify ' + JSON.stringify(datajson));
var total = datajson.length;
var perpage = per_page
//var page = 1
var lastpage = total/perpage;
if(lastpage <= 1) {
lastpage=1
}else{
lastpage++
}
lastpage = Math.trunc(lastpage);
var next=(page+1);
if(next >= lastpage){
next=lastpage;
}
var prev=(page-1);
if(prev == 0){
prev=1;
}
var from=((page-1)*perpage)+1;
var to=(perpage*page)
var myplaybooks = new Object();
var links = `
{
"pagination": {
"total": ${total},
"per_page": ${perpage},
"current_page": ${page},
"last_page": ${lastpage},
"next_page_url": "?page=${next}",
"prev_page_url": "?page=${prev}",
"from": ${from},
"to": ${to},
"frommongo": ${from},
"tomongo": ${to}
}
}
`
//"next_page_url": "?page="+${next},
//"prev_page_url": "?page="+${prev},
//console.log(JSON.stringify(links))
//myplaybooks.links = links
myplaybooks.links = JSON.parse(links);
from--
myplaybooks.data = datajson.slice(from,to);
//myplaybooks.data = datajson;
var RES = new Object();
RES.code = req.query["action"]
RES.token = req.query["token"]
RES.error = false
RES.error_msg = "ok"
RES.data = myplaybooks;
res.json(RES.data)
});
});
4 years ago
3 years ago
app.get('/dockernetworks', (req, res, next) => {
var RES = new Object();
const page = req.query["page"]
const per_page = req.query["per_page"]
var sort = req.query["sort"]
var filter = req.query["filter"]
var type = req.query["type"]
var sort = req.query["sort"]
var sorttmp1 = sort.split('|');
var sortname = sorttmp1[0];
var sortorder = sorttmp1[1];
var showexec = `docker network ls --format "{{json . }}" | jq . -s `
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
var nn = []
var string = stdout.toString()
var datajson = JSON.parse(string);
var results = []
if(filter !== 'NULL'){
var grep = new RegExp(filter);
var datalenth = datajson.length
for (var i=0 ; i < datalenth ; i++)
{
if(grep.test(datajson[i]['Name'])){
results.push(datajson[i]);
}
}
datajson=results
}
// sort by title
if(sortname == 'Repository'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.Repository.localeCompare(b.Repository);
}else{
return b.Repository.localeCompare(a.Repository);
}
});
}else if(sortname == 'Size'){
datajson.sort(function (a, b) {
if(sortorder == 'asc'){
return a.Size.localeCompare(b.Size);
}else{
return b.Size.localeCompare(a.Size);
}
});
}
var resultid = []
var datalenth2 = datajson.length
for (var i=0 ; i < datalenth2 ; i++)
{
datajson[i].id=i
datajson[i]._id=i
resultid.push(datajson[i]);
}
datajson=resultid
//console.log('Number of files ' + stdout);
//console.log('Number string ' + string);
//console.log('stringify ' + JSON.stringify(datajson));
var total = datajson.length;
var perpage = per_page
//var page = 1
var lastpage = total/perpage;
if(lastpage <= 1) {
lastpage=1
}else{
lastpage++
}
lastpage = Math.trunc(lastpage);
var next=(page+1);
if(next >= lastpage){
next=lastpage;
}
var prev=(page-1);
if(prev == 0){
prev=1;
}
var from=((page-1)*perpage)+1;
var to=(perpage*page)
var myplaybooks = new Object();
var links = `
{
"pagination": {
"total": ${total},
"per_page": ${perpage},
"current_page": ${page},
"last_page": ${lastpage},
"next_page_url": "?page=${next}",
"prev_page_url": "?page=${prev}",
"from": ${from},
"to": ${to},
"frommongo": ${from},
"tomongo": ${to}
}
}
`
//"next_page_url": "?page="+${next},
//"prev_page_url": "?page="+${prev},
//console.log(JSON.stringify(links))
//myplaybooks.links = links
myplaybooks.links = JSON.parse(links);
from--
myplaybooks.data = datajson.slice(from,to);
//myplaybooks.data = datajson;
var RES = new Object();
RES.code = req.query["action"]
RES.token = req.query["token"]
RES.error = false
RES.error_msg = "ok"
RES.data = myplaybooks;
res.json(RES.data)
});
});
4 years ago
app.get('/buildservices', (req, res, next) => {
var mypath = process.cwd()
var RES = new Object();
const page = req.query["page"]
const per_page = req.query["per_page"]
var sort = req.query["sort"]
var filter = req.query["filter"]
var type = req.query["type"]
var sort = req.query["sort"]
var sorttmp1 = sort.split('|');
var sortname = sorttmp1[0];
var sortorder = sorttmp1[1];
//var showexec = `jq . -s ${mypath}/builders/bento-swarmlab/builds.json`
var showexec = `jq -s '.[] .Images' ${mypath}/builders/bento-swarmlab/builds.json`
//var showexec = `jq -s '.[]' ${mypath}/builders/bento-swarmlab/builds.json`
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
var nn = []
var string = stdout.toString()
var datajson = JSON.parse(string);
var results = []
if(filter !== 'NULL'){
var grep = new RegExp(filter);
var datalenth = datajson.length
for (var i=0 ; i < datalenth ; i++)
{
if(grep.test(datajson[i]['name'])){
results.push(datajson[i]);
}
}
datajson=results
}
var total = datajson.length;
var perpage = per_page
var lastpage = total/perpage;
if(lastpage <= 1) {
lastpage=1
}else{
lastpage++
}
lastpage = Math.trunc(lastpage);
var next=(page+1);
if(next >= lastpage){
next=lastpage;
}
var prev=(page-1);
if(prev == 0){
prev=1;
}
var from=((page-1)*perpage)+1;
var to=(perpage*page)
var myplaybooks = new Object();
var links = `
{
"pagination": {
"total": ${total},
"per_page": ${perpage},
"current_page": ${page},
"last_page": ${lastpage},
"next_page_url": "?page=${next}",
"prev_page_url": "?page=${prev}",
"from": ${from},
"to": ${to},
"frommongo": ${from},
"tomongo": ${to}
}
}
`
myplaybooks.links = JSON.parse(links);
from--
myplaybooks.data = datajson.slice(from,to);
var RES = new Object();
RES.code = req.query["action"]
RES.token = req.query["token"]
RES.error = false
RES.error_msg = "ok"
RES.data = myplaybooks;
res.json(RES.data)
});
});
4 years ago
app.get('/getservicesinfo', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var services_path = './instance/'+RES.instance
4 years ago
//console.log(services_path)
4 years ago
if (fs.existsSync(services_path)) {
var found = 'yes'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
4 years ago
app.get('/getjupyterinfo', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker exec ${RES.instance} /bin/sh -c "jupyter notebook list"`
//console.log(JSON.stringify(showexec))
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
RES.error = false
RES.error_msg = "ok"
RES.test = string;
//RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.test = 'no';
//RES.data = found;
res.json(RES)
}
});
});
app.get('/hybrid_rmimage', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker rmi ${RES.instance} -f`
//console.log(JSON.stringify(showexec))
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
var string = err.toString()
RES.error = false
RES.error_msg = "ok"
RES.test = string;
//RES.data = found;
res.json(RES)
return;
}
if (stdout) {
var string = stdout.toString()
RES.error = false
RES.error_msg = "ok"
RES.test = 'ok';
RES.out = string;
//RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.test = 'ok';
RES.out = string;
//RES.data = found;
res.json(RES)
}
});
});
3 years ago
app.get('/hybrid_rmnetworks', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker network rm ${RES.instance}`
//console.log(JSON.stringify(showexec))
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
var string = err.toString()
RES.error = false
RES.error_msg = "ok"
RES.test = string;
//RES.data = found;
res.json(RES)
return;
}
if (stdout) {
var string = stdout.toString()
RES.error = false
RES.error_msg = "ok"
RES.test = 'ok';
RES.out = string;
//RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.test = 'ok';
RES.out = string;
//RES.data = found;
res.json(RES)
}
});
});
app.get('/hybrid_rmall', (req, res, next) => {
// docker rmi $(docker image ls -q) -f; docker container prune -f; docker image prune -f; docker volume prune -f
var RES = new Object();
RES.action = req.query["action"]
var showexec = ''
if( RES.action == 'images'){
showexec = 'docker rmi $(docker image ls -q) -f; docker image prune -f'
} else if( RES.action == 'containers'){
showexec = ' docker container prune -f'
} else if( RES.action == 'volumes'){
showexec = ' docker volume prune -f'
}
if(showexec != ''){
//console.log(JSON.stringify(showexec))
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
var string = err.toString()
RES.error = 'yes'
RES.error_msg = string
RES.out = string;
RES.out = 'no';
//RES.data = found;
res.json(RES)
return;
}
if (stdout) {
var string = stdout.toString()
RES.error = 'no'
RES.error_msg = "ok"
RES.out = string;
res.json(RES)
}else{
RES.error = 'no'
RES.error_msg = "ok"
RES.out = string;
res.json(RES)
}
});
}else{
RES.error = 'no'
RES.error_msg = "ok"
RES.out = 'no action';
res.json(RES)
}
});
4 years ago
app.get('/getmountinfo', (req, res, next) => {
3 years ago
var mypath1 = process.cwd()
4 years ago
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker inspect --format='{{json .Mounts}}' ${RES.instance} | jq . -s`
//console.log(JSON.stringify(showexec))
3 years ago
var nypathroot = mypath1+'/instance/'
4 years ago
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
3 years ago
RES.mypath = nypathroot;
4 years ago
//RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
3 years ago
RES.mypath = nypathroot;
4 years ago
//RES.data = found;
res.json(RES)
}
});
});
3 years ago
app.get('/hybrid_inspectnetworks', (req, res, next) => {
var mypath1 = process.cwd()
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker network inspect --format='{{json .}}' ${RES.instance} | jq . -s`
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.test = 'no info';
res.json(RES)
}
});
});
app.get('/hybrid_inspectcontainer', (req, res, next) => {
var mypath1 = process.cwd()
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker inspect --format='{{json .}}' ${RES.instance} | jq . -s`
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.test = 'no info';
res.json(RES)
}
});
});
app.get('/hybrid_inspectimages', (req, res, next) => {
var mypath1 = process.cwd()
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker image inspect --format='{{json .}}' ${RES.instance} | jq . -s`
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.test = 'no info';
res.json(RES)
}
});
});
4 years ago
app.get('/getserviceshybridstatus', (req, res, next) => {
var RES = new Object();
RES.bootstrapnameid = req.query["bootstrapnameid"]
RES.bootstrapstackname = req.query["bootstrapstackname"]
var showexec = `docker ps --format '{"Names":"{{ .Names }}", "Status":"{{.Status}}"}' | jq . -s `
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
var search = RES.bootstrapstackname+'_master'+RES.bootstrapnameid
var extenderror = new RegExp(search);
var found = 'no';
for(var i = 0; i < datajson.length; i++) {
var servicename = datajson[i].Names
4 years ago
//console.log(JSON.stringify(search+' '+servicename))
4 years ago
if(extenderror.test(servicename)){
found = 'yes';
}
}
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
});
4 years ago
app.get('/getservicesstatus', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showexec = `docker ps --format '{"Names":"{{ .Names }}", "Status":"{{.Status}}"}' | jq . -s `
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
4 years ago
//console.log(JSON.stringify(datajson.length))
//console.log(JSON.stringify(datajson))
4 years ago
var extenderror = new RegExp(RES.instance);
var found = 'no';
for(var i = 0; i < datajson.length; i++) {
var servicename = datajson[i].Names
4 years ago
//console.log(JSON.stringify(servicename))
4 years ago
if(extenderror.test(servicename)){
found = 'yes';
}
}
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
});
4 years ago
app.get('/rmbuildstatus', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showfile = `./builders/bento-swarmlab/builds/${RES.instance}.virtualbox.box`
//console.log(showfile)
var found = 'no';
try {
found = 'yes';
fs.unlinkSync(showfile)
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
} catch (e) {
found = 'no';
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
app.get('/rungui', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
RES.error_msg0 = ""
RES.data = "All_services_are_running";
var showexec = `./run-gui.sh`
var runpath = `./instance/${RES.instance}/${RES.instance}`
var runpathfile = `./instance/${RES.instance}/${RES.instance}/run-gui.sh`
//console.log(runpath)
//console.log(showexec)
var buildoutdir = `${runpath}/logs/build-out.log`
var builderrdir = `${runpath}/logs/build-out.log`
3 years ago
//const buildout = fs.openSync(buildoutdir, 'a');
//const builderr = fs.openSync(builderrdir, 'a');
try {
const buildout = fs.openSync(buildoutdir, 'a');
const builderr = fs.openSync(builderrdir, 'a');
3 years ago
var buildimage = `echo "EXEC PID: $$"; ${showexec}`
//var buildimage = `echo "EXEC PID: $$"; docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix microservice-firefox /bin/sh -c "firefox"`
const run_buildimage = spawn(buildimage, {
detached: true,
shell: true,
stdio: [ 'ignore', buildout, builderr ],
cwd: runpath
});
var n = {}
n.pid = run_buildimage.pid
io.emit('build_set_buildpid', n);
n.data = 'start ...'
io.emit('message_out', n);
n.data = 'i am trying to automatically start the application'
io.emit('message_out', n);
n.data = 'If this fails, try:'
io.emit('message_out', n);
var buildtmpexec = ""
buildtmpexec = fs.readFileSync(runpathfile, {encoding:'utf8', flag:'r'});
//var buildtmpexec = `docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix microservice-firefox /bin/sh -c "firefox"`
n.data = buildtmpexec
io.emit('message_out', n);
run_buildimage.on('exit', (data) => {
//console.log('exit '+data);
var n = {}
n.data = 'exit!'
io.emit('message_close', n);
});
run_buildimage.on('close', (data) => {
//console.log('close '+data);
var n = {}
n.data = 'close!'
io.emit('message_close', n);
n.name = `${RES.instance}`
n.dir = `${runpath}`
io.emit('message_close_build', n);
3 years ago
RES.error = false
RES.error_msg = 'no'
RES.data = 'no'
res.json(RES)
});
console.log(run_buildimage.pid);
run_buildimage.unref();
} catch (error) {
console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
});
app.get('/runeditor', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
RES.error_msg0 = ""
RES.data = "All_services_are_running";
var showexec = `./swarmlab_editor_service.sh ${RES.instance}`
var runpath = `.`
var runpathfile = `./swarmlab_editor_service.sh`
3 years ago
// console.log(runpath)
console.log(showexec)
3 years ago
var buildoutdir = `${runpath}/logs/build-out.log`
var builderrdir = `${runpath}/logs/build-out.log`
//const buildout = fs.openSync(buildoutdir, 'a');
//const builderr = fs.openSync(builderrdir, 'a');
try {
const buildout = fs.openSync(buildoutdir, 'a');
const builderr = fs.openSync(builderrdir, 'a');
var buildimage = `echo "EXEC PID: $$"; ${showexec}`
//var buildimage = `echo "EXEC PID: $$"; docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix microservice-firefox /bin/sh -c "firefox"`
const run_buildimage = spawn(buildimage, {
detached: true,
shell: true,
stdio: [ 'ignore', buildout, builderr ],
cwd: runpath
});
var n = {}
n.pid = run_buildimage.pid
io.emit('build_set_buildpid', n);
n.data = 'start ...'
io.emit('message_out', n);
n.data = 'i am trying to automatically start the application'
io.emit('message_out', n);
n.data = 'If this fails, try:'
io.emit('message_out', n);
var buildtmpexec = ""
buildtmpexec = fs.readFileSync(runpathfile, {encoding:'utf8', flag:'r'});
//var buildtmpexec = `docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix microservice-firefox /bin/sh -c "firefox"`
n.data = buildtmpexec
io.emit('message_out', n);
run_buildimage.on('exit', (data) => {
//console.log('exit '+data);
var n = {}
n.data = 'exit!'
io.emit('message_close', n);
});
run_buildimage.on('close', (data) => {
//console.log('close '+data);
var n = {}
n.data = 'close!'
io.emit('message_close', n);
n.name = `${RES.instance}`
n.dir = `${runpath}`
io.emit('message_close_build', n);
RES.error = false
RES.error_msg = 'no'
RES.data = 'no'
res.json(RES)
});
console.log(run_buildimage.pid);
run_buildimage.unref();
3 years ago
} catch (error) {
console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
});
app.get('/runbackup', (req, res, next) => {
var RES = new Object();
var mypath = process.cwd()
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
RES.error_msg0 = ""
RES.data = "All_services_are_running";
var showexec = `./swarmlab_backup_service.sh ${RES.instance}`
console.log('exec tar :', showexec);
var runpath = `${mypath}`
var runpathfile = `${mypath}/swarmlab_backup_service.sh`
var buildoutdir = `${runpath}/logs/tar-backup-out.log`
var builderrdir = `${runpath}/logs/tar-backup-out.log`
try {
const buildout = fs.openSync(buildoutdir, 'a');
const builderr = fs.openSync(builderrdir, 'a');
console.log('buildout :', buildout);
var buildimage = `echo "EXEC PID: $$"; ${showexec}`
const run_buildimage = spawn(buildimage, {
detached: true,
shell: true,
stdio: [ 'ignore', buildout, builderr ],
cwd: runpath
});
var n = {}
n.pid = run_buildimage.pid
io.emit('build_set_buildpid', n);
n.data = 'start ...'
io.emit('message_out', n);
n.data = 'Your backup might take a long time, depending on the number of files to back up. '
io.emit('message_out', n);
run_buildimage.on('exit', (data) => {
var n = {}
n.data = 'exit!'
io.emit('message_close', n);
});
run_buildimage.on('close', (data) => {
var n = {}
n.data = 'close!'
io.emit('message_close', n);
n.name = `${RES.instance}`
n.dir = `${runpath}`
io.emit('message_close_build', n);
});
console.log(run_buildimage.pid);
run_buildimage.unref();
} catch (error) {
console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
});
4 years ago
app.get('/getbuildstatus', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showfile = `./builders/bento-swarmlab/builds/${RES.instance}.virtualbox.box`
//console.log(showfile)
var found = 'no';
if (fs.existsSync(showfile)) {
found = 'yes';
}else{
found = 'no';
}
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
});
4 years ago
app.get('/getstoragestatus', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var showfile = `./instance/${RES.instance}/swarmlabconfig/instance`
//console.log(JSON.stringify(showfile))
fs.readFile(showfile, 'utf8', function (err, data) {
if (err) {
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = "no instance file found";
4 years ago
console.log(JSON.stringify(err))
res.json(RES)
}else{
data = data.trim();
var showexec = `docker ps --format '{"Names":"{{ .Names }}", "Status":"{{.Status}}"}' | jq . -s `
exec(showexec, (err, stdout, stderr) => {
if (err) {
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
var extenderror = new RegExp(data);
var found = 'no';
for(var i = 0; i < datajson.length; i++) {
var servicename = datajson[i].Names
if(extenderror.test(servicename)){
found = 'yes';
}
}
RES.error = false
RES.error_msg = "ok"
RES.test = datajson;
RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
}
});
});
app.get('/getpocstatus', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var runpath = `./instance/${RES.instance}`
var found = 'no'
RES.error = false
RES.error_msg = "ok"
//RES.errorm = "Following_services_are_not_running:";
RES.data = "All_services_are_running";
var showexec = `./check-services.sh`
exec(showexec, {cwd: runpath}, (err, stdout, stderr) => {
if (err) {
return;
}
if (stdout) {
var string = stdout.toString()
//var datajson = JSON.parse(string);
var extenderror = new RegExp(RES.data);
var found = 'no';
var servicename = string
if(extenderror.test(servicename)){
found = 'yes';
}
RES.error = false
RES.error_msg = "ok"
RES.test = string;
RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
});
4 years ago
app.get('/getlogs', (req, res, next) => {
var RES = new Object();
var instancejson = JSON.parse(req.query["container"])
var instance = instancejson.container
//console.log(JSON.stringify(instancejson))
var showexec = `docker logs ${instance}`
log = spawn(showexec, {
shell: true,
//cwd: services_path,
detached: false,
stdio: 'pipe'
});
log.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out', n);
});
log.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err', n);
});
log.on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close', n);
RES.error = false
RES.error_msg = "ok"
RES.data = code;
res.json(RES)
});
/*
exec(showexec, (err, stdout, stderr) => {
if (err) {
return;
}
if (stdout) {
var string = stdout.toString().split('\n');
var datajson1 = ''
for(var i = 0; i < string.length; i++) {
datajson1 += JSON.parse(JSON.stringify(string[i]));
}
//var datajson = JSON.parse(JSON.stringify(datajson1));
var datajson = datajson1
//var datajson = JSON.parse(string);
var n = {}
//n.data = `${string}`
n.data = `${datajson}`
io.emit('message_close', n);
RES.error = false
RES.error_msg = "ok"
RES.data = datajson;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = 'no network';
res.json(RES)
}
}); // exec(showexec1
*/
});
4 years ago
app.get('/getshareinfo', (req, res, next) => {
var RES = new Object();
var container = req.query["instance"]
console.log(container)
var showexec = `docker inspect ${container} --format '{"network":"{{ .HostConfig.NetworkMode }}","container":"{{ .Id }}"}' | jq . -s`
exec(showexec, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
try {
var string = stdout.toString()
var datajson = JSON.parse(string);
var showexec1 = `docker inspect ${datajson[0].network} --format '{{json .Containers}}' | jq . -s`
exec(showexec1, (err1, stdout1, stderr1) => {
if (err1) {
console.error(`exec error1: ${err1}`);
return;
}
if (stdout1) {
var string1 = stdout1.toString()
var datajson1 = JSON.parse(string1);
var logdatatest = datajson1.length
for(var iii=0; iii < logdatatest; iii++){
for (var prop in datajson1[iii]) {
if(prop == datajson[0].container){
var IP = datajson1[iii][prop]
break;
}
}
}
console.log(datajson1)
RES.error = false
RES.error_msg = "ok"
RES.container = datajson;
RES.network = IP;
res.json(RES)
}
})
} catch (error) {
console.error(`exec error: ${error}`);
}
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
res.json(RES)
}
});
});
4 years ago
app.get('/getnetworks', (req, res, next) => {
var RES = new Object();
var instancejson = JSON.parse(req.query["instance"])
var instance = instancejson.instance
var datajson1 = ''
//console.log(JSON.stringify(instancejson))
var showexec1 = `docker inspect ${instance} --format '{{json .HostConfig.NetworkMode}}' | jq . -s`
exec(showexec1, (err1, stdout1, stderr1) => {
if (err1) {
return;
}
if (stdout1) {
var string1 = stdout1.toString()
datajson1 = JSON.parse(string1);
//console.log(JSON.stringify(datajson1))
var showexec = `docker network ls --format '{{json .}}' | jq . -s`
exec(showexec, (err, stdout, stderr) => {
if (err) {
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
var networks = []
var extenderror = new RegExp('^hybrid-');
for(var i = 0; i < datajson.length; i++) {
var NET = {}
var servicename = datajson[i].Name
if(extenderror.test(servicename)){
NET.ID = datajson[i].ID
NET.Name = datajson[i].Name
NET.NetworkMode = datajson1
NET.Scope = datajson[i].Scope
NET.Internal = datajson[i].Internal
NET.IPv6 = datajson[i].IPv6
networks.push(NET)
}
}
var extenderror1 = new RegExp('^storage-');
for(var i = 0; i < datajson.length; i++) {
var NET = {}
var servicename1 = datajson[i].Name
if(extenderror1.test(servicename1)){
NET.ID = datajson[i].ID
NET.Name = datajson[i].Name
NET.NetworkMode = datajson1
NET.Scope = datajson[i].Scope
NET.Internal = datajson[i].Internal
NET.IPv6 = datajson[i].IPv6
networks.push(NET)
}
}
var extenderror1 = new RegExp('^microservice-');
4 years ago
for(var i = 0; i < datajson.length; i++) {
var NET = {}
var servicename1 = datajson[i].Name
if(extenderror1.test(servicename1)){
NET.ID = datajson[i].ID
NET.Name = datajson[i].Name
NET.NetworkMode = datajson1
3 years ago
NET.Scope = datajson[i].Scope
NET.Internal = datajson[i].Internal
NET.IPv6 = datajson[i].IPv6
networks.push(NET)
}
}
3 years ago
var extenderror1poc = new RegExp('^poc-');
for(var i = 0; i < datajson.length; i++) {
var NET = {}
var servicename1 = datajson[i].Name
if(extenderror1poc.test(servicename1)){
NET.ID = datajson[i].ID
NET.Name = datajson[i].Name
NET.NetworkMode = datajson1
NET.Scope = datajson[i].Scope
NET.Internal = datajson[i].Internal
NET.IPv6 = datajson[i].IPv6
networks.push(NET)
}
}
3 years ago
var extenderrorm1 = new RegExp('^swlab');
for(var i = 0; i < datajson.length; i++) {
var NET = {}
var servicename1 = datajson[i].Name
if(extenderrorm1.test(servicename1)){
NET.ID = datajson[i].ID
NET.Name = datajson[i].Name
NET.NetworkMode = datajson1
4 years ago
NET.Scope = datajson[i].Scope
NET.Internal = datajson[i].Internal
NET.IPv6 = datajson[i].IPv6
networks.push(NET)
}
}
RES.error = false
RES.error_msg = "ok"
RES.data = networks;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = 'no network';
res.json(RES)
}
}); //var showexec = `docker network ls --format '{{json .}}' | jq . -s
} // if exec(showexec1
}); // exec(showexec1
});
app.post('/updatenetworks', (req, res, next) => {
var RES = new Object();
const network = req.body["network"]
const container = req.body["container"]
var defaultnetwork = req.body["defaultnetwork"]
var execerror = 0
var execerror1 = 0
//console.log(JSON.stringify(defaultnetwork))
var showexec = `docker inspect ${container} --format '{{json .NetworkSettings.Networks}}' | jq . -s`
exec(showexec, (err, stdout, stderr) => {
if (err) {
return;
}
if (stdout) {
var string = stdout.toString()
var datajson = JSON.parse(string);
var NET1 = []
for (const property in datajson[0]) {
var NET = {}
var name = property
NET1.push(NET)
if(property.trim() !== defaultnetwork.trim()){
try {
var showexecrm = `docker network disconnect ${property} ${container} -f`
//console.log(`${showexecrm}`);
execSync(showexecrm);
execerror = 1
var n = {}
n.data = `rm ${property} from ${container}`
io.emit('message_close', n);
} catch (error) {
execerror = 2
var n = {}
n.data = `rm ${error}`
io.emit('message_close', n);
}
}
//console.log(`${property}`);
}
for(var i = 0; i < network.length; i++) {
try {
var showexecadd = `docker network connect ${network[i]} ${container}`
execSync(showexecadd);
var n = {}
n.data = `add ${network[i]} to ${container}`
io.emit('message_close', n);
execerror1 = 1
} catch (error) {
var n = {}
n.data = `add ${error}`
io.emit('message_close', n);
execerror1 = 2
}
}
RES.error = false
RES.error_msg = "ok"
RES.network = NET1
RES.datarm = execerror
RES.dataadd = execerror1
res.json(RES)
}else{
RES.error = false
RES.error_msg = "ok"
RES.data = 'no network';
res.json(RES)
}
}); // exec(showexec1
});
4 years ago
app.get('/rm_swarmlab', (req, res, next) => {
var mypath = process.cwd()
var RES = new Object();
RES.instance = req.query["instance"]
var services_path = './instance/'+RES.instance
4 years ago
docker_image_rm(mypath, RES.instance);
4 years ago
fs.rm(services_path, {
recursive: true,
force: true
}, (error) => {
if (error) {
RES.error = true
4 years ago
RES.path = mypath
4 years ago
RES.error_msg = "no"
res.json(RES)
} else {
RES.error = false
4 years ago
RES.path = mypath
4 years ago
RES.error_msg = "ok"
res.json(RES)
}
});
4 years ago
});
app.get('/rm_swarmlabstorage', (req, res, next) => {
var mypath = process.cwd()
var RES = new Object();
RES.instance = req.query["instance"]
var services_path = './instance/'+RES.instance
4 years ago
docker_image_rm(mypath, RES.instance);
4 years ago
fs.rm(services_path, {
recursive: true,
force: true
}, (error) => {
if (error) {
RES.error = true
4 years ago
RES.path = mypath
4 years ago
RES.error_msg = "no"
res.json(RES)
} else {
RES.error = false
4 years ago
RES.path = mypath
4 years ago
RES.error_msg = "ok"
res.json(RES)
}
});
4 years ago
});
app.get('/rm_swarmlabpoc', (req, res, next) => {
var mypath = process.cwd()
var RES = new Object();
RES.instance = req.query["instance"]
var services_path = './instance/'+RES.instance
docker_image_rm(mypath, RES.instance);
fs.rm(services_path, {
recursive: true,
force: true
}, (error) => {
if (error) {
RES.error = true
RES.path = mypath
RES.error_msg = "no"
res.json(RES)
} else {
RES.error = false
RES.path = mypath
RES.error_msg = "ok"
res.json(RES)
}
});
});
4 years ago
app.get('/get_config', (req, res, next) => {
var mypath = process.cwd()
var RES = new Object();
var found = 'yes'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
RES.data = mypath
res.json(RES)
/*
exec('pwd', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
console.log(JSON.stringify(string))
var found = 'yes'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
RES.data = string
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.data = found;
res.json(RES)
}
});
*/
});
app.get('/create_config', (req, res, next) => {
var RES = new Object();
RES.value = req.query["value"]
4 years ago
//console.log('value error '+JSON.stringify(RES.value))
4 years ago
//var showexec = `cd ./hybrid; /bin/bash ./create-keys.sh`
fs.writeFileSync('./hybrid/keys.json', JSON.stringify(RES.value));
RES.value = JSON.parse(RES.value);
var INTERFACE_IP=RES.value.wg_ip
var endpoint_tmp=RES.value.server_ip
var fields = endpoint_tmp.split(':');
var endpoint = fields[0];
var mypath = process.cwd()
var swarmlab_config_template = `
4 years ago
DEV_NAME=swarmlab0
4 years ago
INTERFACE_IP=${INTERFACE_IP}/24
4 years ago
INTERFACE_IProute=${INTERFACE_IP}
ENDPOINT_IP=${endpoint}
ip link set $DEV_NAME down
ip link del $DEV_NAME
# add wireguard interface
ip link add $DEV_NAME type wireguard
# set wireguard conf
4 years ago
wg setconf $DEV_NAME ${mypath}/hybrid/connect/wg0.conf
4 years ago
# assign ip to wireguard interface
ip addr add $INTERFACE_IP dev $DEV_NAME
# set sysctl
sysctl -w net.ipv4.conf.all.rp_filter=2
# set mtu for wireguard interface
ip link set mtu 1420 up dev $DEV_NAME
# bring wireguard interface up
ip link set up dev $DEV_NAME
# add route
ip route add default via $INTERFACE_IProute metric 2 table 200
ip route show table 200 | grep -w $INTERFACE_IProute
# add local lan route
ip rule add table main suppress_prefixlength 0
ip rule show | grep -w "suppress_prefixlength"
`
fs.writeFileSync('./hybrid/connect/swarmlab-connect.sh', swarmlab_config_template);
var allowed_ips=RES.value.allowed_ips
var swarmlab_public_wgkey=RES.value.swarmlab_public_wgkey
var swarmlab_privatekey=RES.value.privatekey
var swarmlab_wg_port=RES.value.wg_port
var swarmlab_wg0_template = `[Interface]
PrivateKey = ${swarmlab_privatekey}
ListenPort = ${swarmlab_wg_port}
4 years ago
[Peer]
4 years ago
PublicKey = ${swarmlab_public_wgkey}
Endpoint = ${endpoint_tmp}
AllowedIPs = ${allowed_ips}
PersistentKeepalive = 30
`
4 years ago
//fs.writeFileSync('./hybrid/connect/template-wg0.conf', swarmlab_wg0_template);
4 years ago
fs.writeFileSync('./hybrid/connect/wg0.conf', swarmlab_wg0_template);
4 years ago
var swarmlab_swarm=RES.value.workerkey
var swarmlab_swarm_ip=RES.value.swarm_ip
var swarmlab_swarm_template = `
docker swarm join --token ${swarmlab_swarm} ${swarmlab_swarm_ip}
`
4 years ago
//fs.writeFileSync('./hybrid/connect/template-swarm.sh', swarmlab_swarm_template);
4 years ago
fs.writeFileSync('./hybrid/connect/reload-swarmlab.sh', swarmlab_swarm_template);
4 years ago
var swarmlab_swarm_join_template = `
function swarmlab_ifup() {
4 years ago
swarmlab=$(/bin/bash ${mypath}/hybrid/connect/status.sh swarmlab0 | jq .swarmlab)
4 years ago
tempswarm="\${swarmlab%\\"}"
tempswarm="\${tempswarm#\\"}"
swarmlab=$tempswarm
echo $swarmlab
}
function hybrid_ifup() {
4 years ago
hybrid=$(/bin/bash ${mypath}/hybrid/connect/status.sh swarmlab0 | jq .hybrid)
4 years ago
tempswarm1="\${hybrid%\\"}"
tempswarm1="\${tempswarm1#\\"}"
hybrid=$tempswarm1
echo $hybrid
}
hybrid=$(hybrid_ifup)
if [ $hybrid == 'NotOnline' ]; then
/bin/bash ${mypath}/hybrid/connect/swarmlab-connect.sh
4 years ago
echo "Start swarmlab0 network interface"
4 years ago
sleep 3
hybrid1=$(hybrid_ifup)
if [ $hybrid1 == 'NotOnline' ]; then
4 years ago
echo "Failed to start swarmlab0 network interface"
4 years ago
else
echo "Swarmlab hybrid $hybrid"
fi
else
echo "Swarmlab hybrid $hybrid"
fi
sleep 1
swarmlab=$(swarmlab_ifup)
if [ $swarmlab != 'active' ]; then
4 years ago
/bin/bash ${mypath}/hybrid/connect/reload-swarmlab.sh
4 years ago
sleep 5
swarmlab1=$(swarmlab_ifup)
if [ $swarmlab1 != 'active' ]; then
echo "Failed to connect on swarmlab master"
echo "Exec "
echo "/bin/bash ${mypath}/hybrid/connect/reload-swarmlab.sh"
echo "Please try again"
fi
else
echo "Swarmlab master $swarmlab"
fi
4 years ago
/bin/bash ${mypath}/hybrid/connect/get-swarmlab-ca
/bin/bash ${mypath}/hybrid/connect/get-base-ca
4 years ago
`
fs.writeFileSync('./hybrid/connect/join.sh', swarmlab_swarm_join_template);
var swarmlab_swarm_leave_template = `
function hybrid_ifup() {
4 years ago
hybrid=$(/bin/bash ${mypath}/hybrid/connect/status.sh swarmlab0 | jq .hybrid)
4 years ago
tempswarm1="\${hybrid%\\"}"
tempswarm1="\${tempswarm1#\\"}"
hybrid=$tempswarm1
echo $hybrid
}
hybrid=$(hybrid_ifup)
if [ $hybrid == 'NotOnline' ]; then
echo "Swarmlab hybrid $hybrid"
else
4 years ago
ip link set swarmlab0 down
ip link del swarmlab0
4 years ago
sleep 3
hybrid1=$(hybrid_ifup)
if [ $hybrid1 == 'NotOnline' ]; then
echo "Swarmlab hybrid $hybrid"
else
echo "Swarmlab hybrid $hybrid"
fi
fi
`
fs.writeFileSync('./hybrid/connect/leave.sh', swarmlab_swarm_leave_template);
RES.error = false
RES.error_msg = "ok"
res.json(RES)
});
4 years ago
function create_project(swarmlabname,project){
4 years ago
4 years ago
var services_path = './instance/'+swarmlabname+'/'+project
fs.mkdir(services_path, { recursive: true }, (err) => {
var service_up = '/bin/bash ../install/usr/share/swarmlab.io/sec/swarmlab-sec create'
create = spawn(service_up, {
shell: true,
cwd: services_path,
//detached: true,
detached: false,
//stdio: [ 'ignore', logout, logerr ]
stdio: 'pipe'
});
create.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out', n);
});
create.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err', n);
});
create.on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close', n);
});
});
}
4 years ago
4 years ago
function docker_image_rm(swarmlabname,project){
var services_path = swarmlabname
var service_rm = 'docker rmi $(docker images --format "{{.Repository}}#{{ .ID }}" | grep "/'+project+'#" | cut -d"#" -f2)'
console.log('docker rmi '+service_rm)
remove = spawn(service_rm, {
shell: true,
cwd: services_path,
detached: false,
stdio: 'pipe'
});
remove.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out', n);
});
remove.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err', n);
});
remove.on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close', n);
});
}
4 years ago
function myserverStatus(){
4 years ago
var serverStatus = '/bin/bash ./hybrid/connect/status.sh swarmlab0';
4 years ago
exec(serverStatus, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stdout) {
var string = stdout.toString()
string = JSON.parse(string);
//var stringstr = string.replace(/^"(.*)"$/, '$1');
var n = {}
n.data = string
io.emit('message_hybrid_status', n);
}
});
}
myserverStatus();
setInterval(function(){
myserverStatus()
}, 30000)
4 years ago
let child = [];
var ppid = ''
4 years ago
io.on('connection', function(socket) {
console.log('new connection');
4 years ago
socket.on('start', (value) => {
4 years ago
//console.log('start')
//console.log('value '+JSON.stringify(value))
4 years ago
if(value.swarmlabname){
3 years ago
try {
4 years ago
var service = value.swarmlabname
var services_path = './instance/'+service+'/'+service
var ENV_service_file_path = services_path+'/.env'
var service_port = ''
if(value.swarmlabname_size){
var service_size = value.swarmlabname_size
}else{
var service_size = 3
}
const apptest = require('express')();
const servertest = apptest.listen(0, () => {
var newport = servertest.address().port
servertest.close();
if(value.swarmlabname_port){
service_port = value.swarmlabname_port
}else{
service_port = newport
}
3 years ago
var newport1 = ''
const servertest1 = apptest.listen(0, () => {
servertest1.close();
})
newport1 = servertest1.address().port
var newport2 = ''
const servertest2 = apptest.listen(0, () => {
servertest2.close();
})
newport2 = servertest2.address().port
var newport3 = ''
const servertest3 = apptest.listen(0, () => {
servertest3.close();
})
newport3 = servertest3.address().port
var newport4 = ''
const servertest4 = apptest.listen(0, () => {
servertest4.close();
})
newport4 = servertest4.address().port
var newport5 = ''
const servertest5 = apptest.listen(0, () => {
servertest5.close();
})
newport5 = servertest5.address().port
4 years ago
var ENV_service_file = `
REGISTRY_ADDR=localhost
REGISTRY_PORT=5000
IMAGE_NAME=${service}
SSH_PORT=${service_port}
3 years ago
R_PORT1=${newport1}
R_PORT2=${newport2}
R_PORT3=${newport3}
R_PORT4=${newport4}
R_PORT5=${newport5}
4 years ago
TEST=${service}
4 years ago
`
4 years ago
//console.log('value1 '+JSON.stringify(ENV_service_file_path))
//console.log('value2 '+JSON.stringify(ENV_service_file))
4 years ago
fs.writeFileSync(ENV_service_file_path, ENV_service_file ,{encoding:'utf8',flag:'w'});
4 years ago
var service_up = '/bin/bash ../install/usr/share/swarmlab.io/sec/swarmlab-sec up size='+service_size
child[0] = spawn(service_up, {
shell: true,
cwd: services_path,
//detached: true,
detached: false,
//stdio: [ 'ignore', logout, logerr ]
stdio: 'pipe'
});
ppid = child[0].pid;
4 years ago
//console.log(ppid)
4 years ago
child[0].stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
3 years ago
n.cwdpath = process.cwd()
n.service = service
4 years ago
io.emit('message_out', n);
});
child[0].stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
3 years ago
n.cwdpath = process.cwd()
n.service = service
4 years ago
io.emit('message_err', n);
});
child[0].on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close', n);
});
});
3 years ago
} catch (error) {
console.log('Error:', error);
}
4 years ago
}else{
var n = {}
n.data = 'NoSwarmlabname'
io.emit('message_err', n);
}
});
4 years ago
socket.on('start_storage', (value) => {
if(value.swarmlabname){
3 years ago
try {
4 years ago
var service = value.swarmlabname
var services_path = './instance/'+service
var ENV_service_file_path = services_path+'/.env'
var service_password = ''
if(value.password){
service_password = value.password
}else{
service_password = Math.random().toString(36).substring(7);
}
var ENV_service_file = `
SWARMLAB_STORAGE_PASSWORD=${service_password}
`
fs.writeFileSync(ENV_service_file_path, ENV_service_file ,{encoding:'utf8',flag:'a'});
var service_up = '/bin/bash ./run.sh'
child[0] = spawn(service_up, {
shell: true,
cwd: services_path,
//detached: true,
detached: false,
//stdio: [ 'ignore', logout, logerr ]
stdio: 'pipe'
});
ppid = child[0].pid;
//console.log(ppid)
child[0].stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out_storage', n);
});
child[0].stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err_storage', n);
});
child[0].on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close_storage', n);
});
3 years ago
} catch (error) {
console.log('Error:', error);
}
4 years ago
}else{
var n = {}
n.data = 'NoSwarmlabname'
io.emit('message_err_storage', n);
}
});
socket.on('start_poc', (value) => {
if(value.swarmlabname){
3 years ago
try {
var service = value.swarmlabname
var services_path = './instance/'+service
var ENV_service_file_path = services_path+'/.env'
var service_password = ''
if(value.password){
service_password = value.password
}else{
service_password = Math.random().toString(36).substring(7);
}
var ENV_service_file = `
SWARMLAB_STORAGE_PASSWORD=${service_password}
`
fs.writeFileSync(ENV_service_file_path, ENV_service_file ,{encoding:'utf8',flag:'a'});
var service_up = '/bin/bash ./run.sh'
child[0] = spawn(service_up, {
shell: true,
cwd: services_path,
//detached: true,
detached: false,
//stdio: [ 'ignore', logout, logerr ]
stdio: 'pipe'
});
ppid = child[0].pid;
//console.log(ppid)
child[0].stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out_poc', n);
});
child[0].stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err_poc', n);
});
child[0].on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close_poc', n);
});
3 years ago
} catch (error) {
console.log('Error:', error);
}
}else{
var n = {}
n.data = 'NoSwarmlabname'
io.emit('message_err_poc', n);
}
});
4 years ago
4 years ago
socket.on('stop', (service) => {
var services_path = './instance/'+service+'/'+service
const stop = spawn('/bin/sh ./stop.sh', {
stdio: 'pipe',
4 years ago
shell: true,
4 years ago
cwd: services_path
4 years ago
});
4 years ago
stop.stdout.on('data', function (data) {
4 years ago
var n = {}
n.data = data.toString()
io.emit('message_out', n);
});
4 years ago
stop.stderr.on('data', function (data) {
4 years ago
var n = {}
n.data = data.toString()
io.emit('message_err', n);
});
4 years ago
stop.on('close', function (code) {
4 years ago
var n = {}
n.data = code
io.emit('message_close', n);
});
});
4 years ago
socket.on('stop_storage', (service) => {
var services_path = './instance/'+service
//console.log(services_path)
const stop = spawn('/bin/sh ./stop.sh', {
stdio: 'pipe',
shell: true,
cwd: services_path
});
stop.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out_storage', n);
});
stop.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err_storage', n);
});
stop.on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close_storage', n);
});
});
socket.on('stop_poc', (service) => {
var services_path = './instance/'+service
//console.log(services_path)
const stop = spawn('/bin/sh ./stop.sh', {
stdio: 'pipe',
shell: true,
cwd: services_path
});
stop.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out_poc', n);
});
stop.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err_poc', n);
});
stop.on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close_poc', n);
});
});
4 years ago
socket.on('install', (value) => {
if(value.swarmlabname){
3 years ago
try {
4 years ago
//console.log('install')
4 years ago
var service = value.swarmlabname
var service_git = value.git
var services_path = './instance/'+service
var ENV_service_dir = services_path+'/'+service
var ENV_service_file_path = services_path+'/'+service+'/.env'
4 years ago
var ENV_service_file_path_div = services_path+'/'+service
4 years ago
var nn = {}
nn.data = 'Wait for service(s) to be available before executing a command'
io.emit('message_out', nn);
4 years ago
if (fs.existsSync(services_path)) {
4 years ago
const git = simpleGit(
{
baseDir: services_path,
maxConcurrentProcesses: 10
});
try {
git.pull()
.then((status) => {
var n = {}
n.data = 'Updated'
io.emit('message_out', n);
n.data = '...'
io.emit('message_close', n);
create_project(service,service)
}).catch((err) => {
var n = {}
n.data = err.toString()
io.emit('message_err', n);
console.log('error ' + JSON.stringify(err))
});
} catch (e) {
var n = {}
n.data = e.toString()
io.emit('message_err', n);
}
4 years ago
}else{
fs.mkdir(services_path, { recursive: true }, (err) => {
if (err){
console.log('error ' + JSON.stringify(err))
}else{
var ENV_service_file = `
REGISTRY_ADDR=localhost
REGISTRY_PORT=5000
IMAGE_NAME=${service}
4 years ago
SSH_PORT=
4 years ago
`
4 years ago
const git = simpleGit(
{
baseDir: '.',
maxConcurrentProcesses: 10
});
4 years ago
try {
4 years ago
git.clone(service_git, services_path)
.then((status) => {
var n = {}
n.data = 'Installed'
io.emit('message_out', n);
n.data = '...'
io.emit('message_out', n);
create_project(service,service)
4 years ago
if (fs.existsSync(ENV_service_file_path_div)) {
fs.writeFileSync(ENV_service_file_path, ENV_service_file);
}
4 years ago
}).catch((err) => {
var n = {}
n.data = err.toString()
io.emit('message_err', n);
console.log('error ' + JSON.stringify(err))
});
4 years ago
} catch (e) {
var n = {}
n.data = e.toString()
io.emit('message_err', n);
}
}
});
}
3 years ago
} catch (error) {
console.log('Error:', error);
}
4 years ago
}else{
4 years ago
var n = {}
n.data = 'no services'
io.emit('message_err', n);
4 years ago
}
});
4 years ago
4 years ago
socket.on('installstorage', (value) => {
if(value.swarmlabname){
3 years ago
try {
4 years ago
//console.log('install')
var service = value.swarmlabname
var service_git = value.git
var services_path = './instance/'+service
var ENV_service_dir = services_path+'/'+service
var ENV_service_file_path = services_path+'/'+service+'/.env'
4 years ago
var nn = {}
nn.data = 'Wait for service(s) to be available before executing a command'
io.emit('message_out_storage', nn);
if (fs.existsSync(services_path)) {
const git = simpleGit(
{
baseDir: services_path,
maxConcurrentProcesses: 10
});
try {
git.pull()
.then((status) => {
var n = {}
n.data = 'Updated'
io.emit('message_out_storage', n);
n.data = '...'
io.emit('message_out_storage', n);
n.data = 'ok'
io.emit('message_close_storage', n);
//create_project_storage(service,service)
}).catch((err) => {
var n = {}
n.data = err.toString()
io.emit('message_err_storage', n);
console.log('error ' + JSON.stringify(err))
});
} catch (e) {
var n = {}
n.data = e.toString()
io.emit('message_err_storage', n);
}
}else{
fs.mkdir(services_path, { recursive: true }, (err) => {
if (err){
console.log('error ' + JSON.stringify(err))
}else{
// var ENV_service_file = `
//IMAGE_NAME=${service}
//SSH_PORT=
//`
const git = simpleGit(
{
baseDir: '.',
maxConcurrentProcesses: 10
});
try {
git.clone(service_git, services_path)
.then((status) => {
var n = {}
n.data = 'Installed'
io.emit('message_out_storage', n);
n.data = '...'
io.emit('message_out_storage', n);
n.data = 'ok'
io.emit('message_close_storage', n);
//fs.writeFileSync(ENV_service_file_path, ENV_service_file);
//create_project_storage(service,service)
}).catch((err) => {
var n = {}
n.data = err.toString()
io.emit('message_err_storage', n);
console.log('error ' + JSON.stringify(err))
});
} catch (e) {
var n = {}
n.data = e.toString()
io.emit('message_err_storage', n);
}
}
});
}
3 years ago
} catch (error) {
console.log('Error:', error);
}
4 years ago
}else{
var n = {}
n.data = 'no services'
io.emit('message_err_storage', n);
}
});
4 years ago
socket.on('container-stop', (service, id) => {
4 years ago
var mypath = process.cwd()
4 years ago
//console.log(mypath);
4 years ago
var containerrm = '/bin/sh ./hybrid/connect/container-stop.sh '+id
4 years ago
const stop = spawn(containerrm, {
4 years ago
stdio: 'pipe',
shell: true,
4 years ago
cwd: mypath
4 years ago
});
stop.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out', n);
});
stop.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err', n);
});
stop.on('close', function (code) {
var n = {}
4 years ago
n.data = code
4 years ago
io.emit('message_close', n);
});
4 years ago
});
4 years ago
// ------------------------------------
// ----------------- build buildimage build_image command
// ------------------------------------
socket.on('build_image', (option) => {
console.log("run_buildimage")
console.log(JSON.stringify(option))
var mypath = process.cwd()
var buildoutdir = `${mypath}/logs/build-out.log`
var builderrdir = `${mypath}/logs/build-out.log`
var image_name = option.name
var image_dir = option.dir
var image_config = option.config
var image_file_vbox = image_config.slice(0, -5);
var virtualbox = `${mypath}/builders/bento-swarmlab/builds/${image_name}.virtualbox.box`
var virtualboxhome = `${process.env.HOME}/VirtualBox VMs/${image_file_vbox}/${image_file_vbox}.vbox`
var virtualboxhome1 = `'${process.env.HOME}/VirtualBox VMs/${image_file_vbox}/${image_file_vbox}.vbox'`
//"homefile":"/home/zeus/VirtualBox VMs/ubuntu-/ubuntu-.vbox"
mypath = `${mypath}/builders/bento-swarmlab/${image_dir}`
3 years ago
try {
4 years ago
const buildout = fs.openSync(buildoutdir, 'a');
const builderr = fs.openSync(builderrdir, 'a');
var buildimage = `echo "EXEC PID: $$"; packer build -force -only=virtualbox-iso ${image_config}`
const run_buildimage = spawn(buildimage, {
detached: true,
//stdio: 'pipe',
shell: true,
stdio: [ 'ignore', buildout, builderr ],
cwd: mypath
});
var n = {}
n.pid = run_buildimage.pid
//set pid on client for later use e.g. kill
io.emit('build_set_buildpid', n);
n.data = 'start ...'
io.emit('message_out', n);
run_buildimage.on('exit', (data) => {
console.log('exit '+data);
//set pid on server for later use e.g. kill
//var n = {}
//n.pid = line.substr(10).trim()
//set pid on client for later use e.g. kill
//io.emit('build_set_sshpid', n);
var n = {}
n.data = 'exit!'
io.emit('message_close', n);
});
run_buildimage.on('close', (data) => {
console.log('close '+data);
//set pid on server for later use e.g. kill
//var n = {}
//n.pid = line.substr(10).trim()
//set pid on client for later use e.g. kill
//io.emit('build_set_sshpid', n);
var n = {}
n.data = 'close!'
io.emit('message_close', n);
n.name = image_name
n.dir = image_dir
if (fs.existsSync(virtualbox)) {
n.build = 'ok'
n.file = virtualbox
}else{
n.build = 'no'
n.file = 'false'
}
io.emit('message_close_build', n);
// if file exists
if (fs.existsSync(virtualboxhome)) {
n.homefile = virtualboxhome1
n.homefile_exist = 'yes'
io.emit('message_close_build', n);
}else{
n.homefile = 'false'
n.homefile_exist = 'no'
io.emit('message_close_build', n);
}
});
console.log(run_buildimage.pid);
run_buildimage.unref();
3 years ago
} catch (error) {
console.log('Error:', error);
}
4 years ago
/*
var n1 = {}
n1.data = 'exit!'
n1.build = 'ok'
n1.name = image_name
n1.dir = image_dir
n1.homefile = virtualboxhome
n1.file = 'filetest'
io.emit('message_close_build', n1);
*/
//var spawn = require('child_process').spawn;
//var child = spawn('my-command', {detached: true});
//process.kill(-child.pid);
/*
run_buildimage.stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_out', n);
});
run_buildimage.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err', n);
//io.end();
});
run_buildimage.on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close', n);
});
*/
});
// ------------------------------------
// ----------------- ssh connect and run command
// ------------------------------------
socket.on('build_vmlist', (option) => {
console.log("list")
var mypath = process.cwd()
sshconnect.on('connect', function() {
console.log('Connection :: connect');
});
sshconnect.on('ready', function() {
console.log('Connection :: ready');
sshconnect.exec('echo "EXEC PID: $$";/mytools/test-ping.sh', (err, stream) => {
if (err) throw err;
stream.on('close', (code, signal) => {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
var n = {}
n.data = 'close'
io.emit('message_close', n);
sshconnect.end();
}).on('data', (data) => {
var line = '' + data; // unbeautiful ;)
console.log(line);
if(line.substr(0, 10) === 'EXEC PID: ') {
console.log('PID1: ' + line.substr(10));
var sshpid1 = line.substr(10).trim();
//set pid on server for later use e.g. kill
global.SSHPID[sshpid1] = sshconnect
var n = {}
n.pid = line.substr(10).trim()
//set pid on client for later use e.g. kill
io.emit('build_set_sshpid', n);
}
console.log('STDOUT: ' + data);
var n = {}
n.data = data.toString('utf8')
io.emit('message_out', n);
}).stderr.on('data', (data) => {
console.log('STDERR: ' + data);
var n = {}
n.data = 'close'
io.emit('message_out', n);
io.emit('message_err', n);
io.emit('message_close', n);
});
});
});
sshconnect.on('error', function(err) {
console.log('Connection :: error :: ' + err);
var n = {}
n.data = 'close'
io.emit('message_out', n);
io.emit('message_err', n);
io.emit('message_close', n);
});
sshconnect.on('end', function() {
console.log('Connection :: end');
var n = {}
n.data = 'close'
io.emit('message_out', n);
io.emit('message_close', n);
});
sshconnect.on('close', function(had_error) {
console.log('Connection :: close');
var n = {}
n.data = 'close'
io.emit('message_out', n);
io.emit('message_close', n);
});
sshconnect.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
console.log('Connection :: keyboard-interactive');
finish(['my_password_on_remote_machine']);
});
sshconnect.connect({
host: '195.130.109.82',
port: 22,
username: 'zeus',
tryKeyboard: true,
privateKey: fs.readFileSync('./hybrid/connect/zeus-esxikey', 'utf8')
});
});
// ------------------------------------
// ----------------- ssh connect and kill ssh command
// ------------------------------------
/*
// for ssh withoud detach
socket.on('build_kill', (option) => {
var KILL = option.kill.trim()
killProcess(KILL);
var n = {}
n.data = 'KILL '+KILL
io.emit('message_out', n);
io.emit('message_close', n);
});
*/
// for build with detach
socket.on('build_kill', (option) => {
var KILL = option.kill
if(option.kill){
if(KILL == process.pid || KILL == 0){
}else{
killBuildProcess(KILL)
}
//process.kill(-KILL);
var n = {}
n.data = 'KILL '+KILL
//io.emit('message_out', n);
4 years ago
io.emit('message_close', n);
}else{
var n = {}
n.data = 'KILL false '
//io.emit('message_out', n);
4 years ago
io.emit('message_close', n);
}
});
// ------------------------------------
// ----------------- ssh connect and run command
// ------------------------------------
4 years ago
socket.on('kill', () => {
4 years ago
//console.log(ppid)
4 years ago
//process.kill(ppid, 'SIGHUP');
child[0].kill('SIGHUP');
//child[0].kill('SIGINT');
//child[0].kill();
4 years ago
});
4 years ago
socket.on('create-wirequard', (value) => {
let rawdatafile = './hybrid/config.json'
var workerkey = value.workerkey
var privatekey = value.privatekey
var publickey = value.publickey
var server_ip = value.server_ip // 83.212.77.133:51820
var allowed_ips = value.allowed_ips // 10.13.13.0/16, 0.0.0.0/0, ::/0
var services_json = {
workerkey: workerkey,
privatekey: privatekey,
publickey: publickey,
server_ip: server_ip,
allowed_ips: allowed_ips
};
let datajson = JSON.stringify(services_json);
fs.writeFileSync(rawdatafile, datajson);
var services_path = ` ----------------
create a file
/etc/wireguard/wg0.conf
-----------------------
`
var ENV_service_file = `
create a file connect-server.sh
[Interface]
PrivateKey = ${privatekey}
ListenPort = 51820
[Peer]
PublicKey = ${publickey}
Endpoint = ${server_ip}
AllowedIPs = ${allowedOrigins}
PersistentKeepalive = 30
`
var n = {}
n.data = services_path
io.emit('message_out', n);
n.data = ENV_service_file
io.emit('message_out', n);
var n = {}
4 years ago
n.data = 'bash ./connect-server.sh'
4 years ago
io.emit('message_close', n);
});
socket.on('get-serverconfig', () => {
let rawdatafile = './hybrid/keys.json'
if (fs.existsSync(rawdatafile)) {
let rawdata = fs.readFileSync(rawdatafile);
let config = JSON.parse(rawdata);
var n = {}
n.file = 'ok'
n.data = config
io.emit('message_config', n);
}else{
var n = {}
n.file = 'nodata'
n.data = 'nodata'
io.emit('message_config', n);
}
});
4 years ago
});
server.listen(serverPort, function() {
console.log('server up and running at %s port', serverPort);
});