@ -75,7 +75,7 @@ const io = require("socket.io")(server, {
} ) ;
} ) ;
const { spawn , exec } = require ( 'child_process' )
const { spawn , exec , execSync } = require ( 'child_process' )
const simpleGit = require ( 'simple-git' ) ;
const simpleGit = require ( 'simple-git' ) ;
@ -811,22 +811,169 @@ app.get('/getstoragestatus', (req, res, next) => {
} ) ;
} ) ;
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 )
}
}
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 ) => {
app . get ( '/rm_swarmlab' , ( req , res , next ) => {
var mypath = process . cwd ( )
var mypath = process . cwd ( )
var RES = new Object ( ) ;
var RES = new Object ( ) ;
RES . instance = req . query [ "instance" ]
RES . instance = req . query [ "instance" ]
var services_path = './instance/' + RES . instance
var services_path = './instance/' + RES . instance
docker_image_rm ( mypath , RES . instance ) ;
fs . rm ( services_path , {
fs . rm ( services_path , {
recursive : true ,
recursive : true ,
force : true
force : true
} , ( error ) => {
} , ( error ) => {
if ( error ) {
if ( error ) {
RES . error = true
RES . error = true
RES . path = mypath
RES . error_msg = "no"
RES . error_msg = "no"
res . json ( RES )
res . json ( RES )
} else {
} else {
RES . error = false
RES . error = false
RES . path = mypath
RES . error_msg = "ok"
RES . error_msg = "ok"
res . json ( RES )
res . json ( RES )
}
}
@ -839,16 +986,19 @@ app.get('/rm_swarmlabstorage', (req, res, next) => {
var RES = new Object ( ) ;
var RES = new Object ( ) ;
RES . instance = req . query [ "instance" ]
RES . instance = req . query [ "instance" ]
var services_path = './instance/' + RES . instance
var services_path = './instance/' + RES . instance
docker_image_rm ( mypath , RES . instance ) ;
fs . rm ( services_path , {
fs . rm ( services_path , {
recursive : true ,
recursive : true ,
force : true
force : true
} , ( error ) => {
} , ( error ) => {
if ( error ) {
if ( error ) {
RES . error = true
RES . error = true
RES . path = mypath
RES . error_msg = "no"
RES . error_msg = "no"
res . json ( RES )
res . json ( RES )
} else {
} else {
RES . error = false
RES . error = false
RES . path = mypath
RES . error_msg = "ok"
RES . error_msg = "ok"
res . json ( RES )
res . json ( RES )
}
}
@ -1090,6 +1240,37 @@ function create_project(swarmlabname,project){
} ) ;
} ) ;
}
}
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 ( ) {
function myserverStatus ( ) {
var serverStatus = '/bin/bash ./hybrid/connect/status.sh swarmlab0' ;
var serverStatus = '/bin/bash ./hybrid/connect/status.sh swarmlab0' ;
exec ( serverStatus , ( err , stdout , stderr ) => {
exec ( serverStatus , ( err , stdout , stderr ) => {