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.
 
 
 
 
 
 

3706 lines
114 KiB

var fs = require('fs');
var express = require('express');
//var https = require('https');
var http = require('http');
var app = express();
const cors = require('cors')
const helmet = require('helmet');
const { Client } = require('ssh2');
const sshconnect = new Client();
var allowedOrigins = [
'https://localhost:55543',
'http://localhost:3080',
'http://localhost:3088',
'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);
}
}));
// 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;
}
}
function checkpid(arr, val) {
return arr.some(arrVal => val === arrVal);
}
//app.use(cors)
//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());
*/
app.use(express.json());
var serverPort = 3080;
/*
// ssl enabled
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);
{
upgradeTimeout: 30000 // default value is 10000ms, try changing it to 20k or more
}
socket.on('disconnect', reason => {
console.log(`reason: ${reason}`);
});
*/
var server = http.createServer(app);
const io = require("socket.io")(server, {
//pingInterval: 25000,
reconnectionDelay: 5000,
reconnectionDelayMax: 5000,
pingTimeout: 90000, // default value is 10000ms, try changing it to 20k or more
upgradeTimeout: 50000, // default value is 10000ms, try changing it to 20k or more
cors: {
origin: "http://localhost:8080",
methods: ["GET", "POST"]
}
});
const { spawn, exec, execSync } = require('child_process')
const simpleGit = require('simple-git');
// 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');
const logerribuild = fs.openSync('./logs/build-out.log', 'a');
//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);
})
/*
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);
});
*/
var static_options = {
dotfiles: 'ignore',
etag: false,
extensions: ['html'],
index: false,
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now())
}
}
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))
// ***************************************************
// rest get
// ***************************************************
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)
}
});
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) => {
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];
if(typeof req.query["filter"] !== "undefined" && req.query["filter"] !== ''){
filter = req.query["filter"]
}else{
console.log('yes')
}
RES.type = req.query["type"]
var showexec = `./LabLearningObject-hybrid/index.js`
fs.readFile(showexec, 'utf8', function (err, data) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
var nn = []
var datajson = JSON.parse(data);
var datalenth = datajson.length
var results = []
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
}
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 = ''
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'
}
var greplevel = new RegExp(filterlevel);
var datalenth1 = datajson.length
for (var i=0 ; i < datalenth1 ; i++)
{
if(greplevel.test(datajson[i]['level'])){
resultsl.push(datajson[i]);
}
}
datajson=resultsl
}
// 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);
}
});
}
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;
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},
//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)
});
});
app.get('/dockerservices', (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 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) => {
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]['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
//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)
});
});
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)
});
});
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)
});
});
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)
});
});
app.get('/getservicesinfoall', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var servicepath = './instance/'
fs.readdir(servicepath, { withFileTypes: true }, (error, files) => {
const directoriesInDIrectory = files
.filter((item) => item.isDirectory())
.map((item) => item.name);
//console.log(directoriesInDIrectory);
RES.error = false
RES.error_msg = "ok"
RES.data = directoriesInDIrectory;
res.json(RES)
});
});
app.get('/getservicesinfo', (req, res, next) => {
var RES = new Object();
RES.instance = req.query["instance"]
var services_path = './instance/'+RES.instance
//console.log(services_path)
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)
}
});
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)
}
});
});
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)
}
});
app.get('/getmountinfo', (req, res, next) => {
var mypath1 = process.cwd()
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))
var nypathroot = mypath1+'/instance/'
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.mypath = nypathroot;
//RES.data = found;
res.json(RES)
}else{
var found = 'no'
RES.error = false
RES.error_msg = "ok"
RES.mypath = nypathroot;
//RES.data = found;
res.json(RES)
}
});
});
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)
}
});
});
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
//console.log(JSON.stringify(search+' '+servicename))
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('/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);
//console.log(JSON.stringify(datajson.length))
//console.log(JSON.stringify(datajson))
var extenderror = new RegExp(RES.instance);
var found = 'no';
for(var i = 0; i < datajson.length; i++) {
var servicename = datajson[i].Names
//console.log(JSON.stringify(servicename))
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('/getservicesstatusall', (req, res, next) => {
var RES = new Object();
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);
RES.error = false
RES.error_msg = "ok"
RES.data = datajson;
res.json(RES)
}else{
RES.error = true
RES.error_msg = "no"
RES.data = 'no';
res.json(RES)
}
});
});
app.get('/stopservice', (req, res, next) => {
var RES = new Object();
var service = req.query["instance"]
var services_path = './instance/'+service+'/'+service
var services_pathexec = process.cwd()
var stopeditexec = `/bin/bash ./stop-sidecar-edit-service.sh ${service}`
const stopedit = spawn(stopeditexec, {
stdio: 'pipe',
shell: true,
cwd: services_pathexec
});
stopedit.on('close', function (code) {
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', 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 = {}
n.data = code
io.emit('message_close', n);
RES.error = false
RES.error_msg = "ok"
RES.data = 'ok';
res.json(RES)
});
});
});
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`
//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();
} 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();
var mypath1 = process.cwd()
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
RES.data = "All_services_are_running";
const apptest = require('express')();
var newport1 = ''
const servertest1 = apptest.listen(0, () => {
servertest1.close();
})
newport1 = servertest1.address().port
var showexec = `./swarmlab_editor_service.sh ${RES.instance} ${newport1}`
//var runpath = `.`
//var runpathfile = `./swarmlab_editor_service.sh`
// console.log(runpath)
// console.log(showexec)
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);
io.emit('message_close', n);
//console.log('data')
var filter = `start:${RES.instance}`
var grep = new RegExp(filter);
if(grep.test(n.data)){
n.start = `${RES.instance}`
n.port = `${newport1}`
io.emit('message_exec_editor', n);
io.emit('message_close', n);
//console.log('close')
io.emit('message_err', n);
RES.error = false
RES.error_msg = 'ok'
RES.data = n.data
RES.port = newport1
res.json(RES)
}
});
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)
/*
RES.error = false
RES.error_msg = 'no'
RES.port = newport1
res.json(RES)
*/
});
//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}`
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 = `${mypath1}/swarmlab_editor_service.sh ${RES.instance} ${newport1}`
n.data = buildtmpexec
io.emit('message_out', n);
n.data = 'close!'
io.emit('message_close', n);
n.start = `${RES.instance}`
n.port = `${newport1}`
io.emit('message_exec_editor', 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);
});
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('/chown', (req, res, next) => {
var RES = new Object();
var mypath1 = process.cwd()
var runuser = process.env.USER || ""
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
var showexec = `docker exec swarmlabadmin /bin/bash -c "chown -R ${runuser}:${runuser} /instance/${RES.instance}/${RES.instance}"`
//var showexec = `docker exec swarmlabadmin /bin/bash -c "chown -R root:root /instance/${RES.instance}/${RES.instance}"`
//console.log(showexec)
try {
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);
n.start = `${RES.instance}`
RES.error = false
RES.error_msg = 'ok'
RES.data = n.data
res.json(RES)
});
} catch (error) {
console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
});
app.get('/checkowner', (req, res, next) => {
var RES = new Object();
var mypath1 = process.cwd()
var runuser = process.env.USER || ""
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
var instancedir = `./instance/${RES.instance}/${RES.instance}`
if (fs.existsSync(instancedir)) {
var showexec = `docker exec swarmlabadmin /bin/bash -c "chown -R ${runuser}:${runuser} /instance/${RES.instance}/${RES.instance}"`
try {
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);
n.start = `${RES.instance}`
RES.error = false
RES.error_msg = 'ok'
RES.data = n.data
res.json(RES)
});
} catch (error) {
console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
}else{
RES.error = false
RES.error_msg = 'nodir'
RES.data = 'no'
res.json(RES)
}
});
app.get('/getcustom', (req, res, next) => {
var RES = new Object();
var mypath1 = process.cwd()
var runuser = process.env.USER || ""
RES.instance = req.query["instance"]
RES.error = false
RES.error_msg = "ok"
var instancedir = `./instance/${RES.instance}/${RES.instance}/custom`
//console.log(instancedir)
if (fs.existsSync(instancedir)) {
var extrapackages = `${instancedir}/extrapackages`
var extrash = `${instancedir}/custom.sh`
try {
const extrapackagestext = fs.readFileSync(extrapackages, {encoding:'utf8', flag:'r'});
const extrashtext = fs.readFileSync(extrash, {encoding:'utf8', flag:'r'});
RES.error = false
RES.error_msg = 'ok'
RES.extrapackagestext = extrapackagestext
RES.extrashtext = extrashtext
res.json(RES)
} catch (error) {
//console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
}else{
RES.error = false
RES.error_msg = 'nodir'
RES.data = 'no'
res.json(RES)
}
});
app.get('/savecustom', (req, res, next) => {
var RES = new Object();
var mypath1 = process.cwd()
var runuser = process.env.USER || ""
RES.instance = req.query["instance"]
RES.package = req.query["package"]
RES.sh = req.query["sh"]
RES.error = false
RES.error_msg = "ok"
var instancedir = `./instance/${RES.instance}/${RES.instance}/custom`
if (fs.existsSync(instancedir)) {
var extrapackages = `${instancedir}/extrapackages`
var extrash = `${instancedir}/custom.sh`
try {
const extrapackagestext = fs.writeFileSync(extrapackages, RES.package, {encoding:'utf8'});
const extrashtext = fs.writeFileSync(extrash, RES.sh, {encoding:'utf8'});
RES.error = false
RES.error_msg = 'ok'
RES.extrapackagestext = extrapackagestext
RES.extrashtext = extrapackagestext
res.json(RES)
} catch (error) {
//console.log('Error:', error);
RES.error = true
RES.error_msg = error
RES.data = 'no'
res.json(RES)
}
}else{
RES.error = false
RES.error_msg = 'nodir'
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)
}
});
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)
});
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";
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)
}
});
});
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);
//console.log('logdata')
});
log.stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
io.emit('message_err', n);
//console.log('logdataerr')
});
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)
//console.log('logdataclose')
});
/*
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
*/
});
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)
}
});
});
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-');
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 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)
}
}
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
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
});
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
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)
}
});
});
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
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)
}
});
});
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)
}
});
});
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"]
//console.log('value error '+JSON.stringify(RES.value))
//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 = `
DEV_NAME=swarmlab0
INTERFACE_IP=${INTERFACE_IP}/24
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
wg setconf $DEV_NAME ${mypath}/hybrid/connect/wg0.conf
# 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}
[Peer]
PublicKey = ${swarmlab_public_wgkey}
Endpoint = ${endpoint_tmp}
AllowedIPs = ${allowed_ips}
PersistentKeepalive = 30
`
//fs.writeFileSync('./hybrid/connect/template-wg0.conf', swarmlab_wg0_template);
fs.writeFileSync('./hybrid/connect/wg0.conf', swarmlab_wg0_template);
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}
`
//fs.writeFileSync('./hybrid/connect/template-swarm.sh', swarmlab_swarm_template);
fs.writeFileSync('./hybrid/connect/reload-swarmlab.sh', swarmlab_swarm_template);
var swarmlab_swarm_join_template = `
function swarmlab_ifup() {
swarmlab=$(/bin/bash ${mypath}/hybrid/connect/status.sh swarmlab0 | jq .swarmlab)
tempswarm="\${swarmlab%\\"}"
tempswarm="\${tempswarm#\\"}"
swarmlab=$tempswarm
echo $swarmlab
}
function hybrid_ifup() {
hybrid=$(/bin/bash ${mypath}/hybrid/connect/status.sh swarmlab0 | jq .hybrid)
tempswarm1="\${hybrid%\\"}"
tempswarm1="\${tempswarm1#\\"}"
hybrid=$tempswarm1
echo $hybrid
}
hybrid=$(hybrid_ifup)
if [ $hybrid == 'NotOnline' ]; then
/bin/bash ${mypath}/hybrid/connect/swarmlab-connect.sh
echo "Start swarmlab0 network interface"
sleep 3
hybrid1=$(hybrid_ifup)
if [ $hybrid1 == 'NotOnline' ]; then
echo "Failed to start swarmlab0 network interface"
else
echo "Swarmlab hybrid $hybrid"
fi
else
echo "Swarmlab hybrid $hybrid"
fi
sleep 1
swarmlab=$(swarmlab_ifup)
if [ $swarmlab != 'active' ]; then
/bin/bash ${mypath}/hybrid/connect/reload-swarmlab.sh
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
/bin/bash ${mypath}/hybrid/connect/get-swarmlab-ca
/bin/bash ${mypath}/hybrid/connect/get-base-ca
`
fs.writeFileSync('./hybrid/connect/join.sh', swarmlab_swarm_join_template);
var swarmlab_swarm_leave_template = `
function hybrid_ifup() {
hybrid=$(/bin/bash ${mypath}/hybrid/connect/status.sh swarmlab0 | jq .hybrid)
tempswarm1="\${hybrid%\\"}"
tempswarm1="\${tempswarm1#\\"}"
hybrid=$tempswarm1
echo $hybrid
}
hybrid=$(hybrid_ifup)
if [ $hybrid == 'NotOnline' ]; then
echo "Swarmlab hybrid $hybrid"
else
ip link set swarmlab0 down
ip link del swarmlab0
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)
});
function create_project(swarmlabname,project){
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);
});
});
}
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);
});
}
function myserverStatus(){
var serverStatus = '/bin/bash ./hybrid/connect/status.sh swarmlab0';
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)
let child = [];
var ppid = ''
io.on('connection', function(socket) {
console.log('new connection');
socket.on('disconnect', reason => {
console.log(`reason: ${reason}`);
});
socket.on('start', (value) => {
//console.log('start')
//console.log('value '+JSON.stringify(value))
if(value.swarmlabname){
try {
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
}
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
var ENV_service_file = `
REGISTRY_ADDR=localhost
REGISTRY_PORT=5000
IMAGE_NAME=${service}
SSH_PORT=${service_port}
R_PORT1=${newport1}
R_PORT2=${newport2}
R_PORT3=${newport3}
R_PORT4=${newport4}
R_PORT5=${newport5}
TEST=${service}
`
//console.log('value1 '+JSON.stringify(ENV_service_file_path))
//console.log('value2 '+JSON.stringify(ENV_service_file))
fs.writeFileSync(ENV_service_file_path, ENV_service_file ,{encoding:'utf8',flag:'w'});
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;
//console.log(ppid)
child[0].stdout.on('data', function (data) {
var n = {}
n.data = data.toString()
n.cwdpath = process.cwd()
n.service = service
io.emit('message_out', n);
});
child[0].stderr.on('data', function (data) {
var n = {}
n.data = data.toString()
n.cwdpath = process.cwd()
n.service = service
io.emit('message_err', n);
});
child[0].on('close', function (code) {
var n = {}
n.data = code
io.emit('message_close', n);
});
});
} catch (error) {
console.log('Error:', error);
}
}else{
var n = {}
n.data = 'NoSwarmlabname'
io.emit('message_err', n);
}
});
socket.on('start_storage', (value) => {
if(value.swarmlabname){
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_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);
});
} catch (error) {
console.log('Error:', error);
}
}else{
var n = {}
n.data = 'NoSwarmlabname'
io.emit('message_err_storage', n);
}
});
socket.on('start_poc', (value) => {
if(value.swarmlabname){
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);
});
} catch (error) {
console.log('Error:', error);
}
}else{
var n = {}
n.data = 'NoSwarmlabname'
io.emit('message_err_poc', n);
}
});
socket.on('stop', (service) => {
var services_path = './instance/'+service+'/'+service
var services_pathexec = process.cwd()
var stopeditexec = `/bin/bash ./stop-sidecar-edit-service.sh ${service}`
const stopedit = spawn(stopeditexec, {
stdio: 'pipe',
shell: true,
cwd: services_pathexec
});
stopedit.on('close', function (code) {
// var n = {}
// n.data = code
// io.emit('message_close', n);
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', 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 = {}
n.data = code
io.emit('message_close', n);
});
});
});
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);
});
});
socket.on('install', (value) => {
if(value.swarmlabname){
try {
//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'
var ENV_service_file_path_div = services_path+'/'+service
var nn = {}
nn.data = 'Wait for service(s) to be available before executing a command'
io.emit('message_out', 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', 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);
}
}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}
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', n);
n.data = '...'
io.emit('message_out', n);
create_project(service,service)
if (fs.existsSync(ENV_service_file_path_div)) {
fs.writeFileSync(ENV_service_file_path, ENV_service_file);
}
}).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);
}
}
});
}
} catch (error) {
console.log('Error:', error);
}
}else{
var n = {}
n.data = 'no services'
io.emit('message_err', n);
}
});
socket.on('installstorage', (value) => {
if(value.swarmlabname){
try {
//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'
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);
}
}
});
}
} catch (error) {
console.log('Error:', error);
}
}else{
var n = {}
n.data = 'no services'
io.emit('message_err_storage', n);
}
});
socket.on('container-stop', (service, id) => {
var mypath = process.cwd()
//console.log(mypath);
var containerrm = '/bin/sh ./hybrid/connect/container-stop.sh '+id
const stop = spawn(containerrm, {
stdio: 'pipe',
shell: true,
cwd: mypath
});
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 = {}
n.data = code
io.emit('message_close', n);
});
});
// ------------------------------------
// ----------------- 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}`
try {
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();
} catch (error) {
console.log('Error:', error);
}
/*
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);
io.emit('message_close', n);
}else{
var n = {}
n.data = 'KILL false '
//io.emit('message_out', n);
io.emit('message_close', n);
}
});
// ------------------------------------
// ----------------- ssh connect and run command
// ------------------------------------
socket.on('kill', () => {
//console.log(ppid)
//process.kill(ppid, 'SIGHUP');
child[0].kill('SIGHUP');
//child[0].kill('SIGINT');
//child[0].kill();
});
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 = {}
n.data = 'bash ./connect-server.sh'
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);
}
});
});
server.listen(serverPort, function() {
console.log('server up and running at %s port', serverPort);
});