diff --git a/webServer/server.js b/webServer/server.js index 32ea178..f709385 100644 --- a/webServer/server.js +++ b/webServer/server.js @@ -1,13 +1,13 @@ -const path = require('path'); -const express = require('express'); -const fs = require('fs'); -const childProcess = require('child_process'); -const bodyParser = require('body-parser') -const socketio = require("socket.io"); -const tmp = require('tmp'); -const app = express(); -const port = 8080; -const command = 'docker'; +const path = require('path') +const express = require('express') +const fs = require('fs') +const childProcess = require('child_process') +const bodyParser = require('body-parser') +const socketio = require('socket.io') +const tmp = require('tmp') +const app = express() +const port = 80 +const command = 'docker' const serverTitle = ` ██████╗ ██████╗ █████╗ ███╗ ██╗ ██████╗██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ @@ -16,344 +16,350 @@ const serverTitle = ` ██╔══██╗██╔══██╗██╔══██║██║╚██╗██║██║ ██╔══██║ ██╔══██╗██║ ██║ ██╔██╗ ██████╔╝██║ ██║██║ ██║██║ ╚████║╚██████╗██║ ██║ ██████╔╝╚██████╔╝██╔╝ ██╗ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ - `; + ` // eslint-disable-next-line no-undef -app.use(bodyParser.json()); -app.use(express.static(path.join(__dirname, '../dist'))); -app.get('/', (req, res) => - res.sendFile(path.join(__dirname, '../dist', 'index.html')) -); +app.use(bodyParser.json()) +app.use(express.static(path.join(__dirname, '../dist'))) +app.get('/', (req, res) => res.sendFile(path.join(__dirname, '../dist', 'index.html'))) //API Calls app.get('/api/getContainers', (req, res) => { - getContainerList(res); -}); + getContainerList(res) +}) app.post('/api/createImage', (req, res) => { - createImage(req.body, res); - -}); + createImage(req.body, res) +}) //Docker Container API Calls app.post('/api/startContainer', (req, res) => { - startContainer(req.body, res); - -}); + startContainer(req.body, res) +}) app.post('/api/stopContainer', (req, res) => { - stopContainer(req.body, res); - -}); + stopContainer(req.body, res) +}) app.post('/api/destroyContainer', (req, res) => { - destroyContainer(req.body, res); - -}); + destroyContainer(req.body, res) +}) app.post('connectToContainer', (req, res) => { - let params = req.body; - // connectToContainer(params, res); -}); + let params = req.body + // connectToContainer(params, res); +}) //Docker SSH API Calls app.post('/api/sendCommand', (req, res) => { - let params = req.body; - sendCommand(params, res); - + let params = req.body + sendCommand(params, res) }) //Last Route. Automatically send 404 app.get('*', (req, res) => { - res.redirect('/'); - -}); + res.redirect('/') +}) //Main Web Server -const webServer = app.listen((port), () => { - - console.log(serverTitle); - console.log(`Branch Box Server listening on http://localhost:${port}`); -}); +const webServer = app.listen(port, () => { + console.log(serverTitle) + console.log(`Branch Box Server listening on http://localhost:${port}`) +}) //Socket Server -const io = socketio(webServer); -io.on('connection', (socket) => { -}); -io.on('disconnect', (socket) => { -}); +const io = socketio(webServer) +io.on('connection', (socket) => {}) +io.on('disconnect', (socket) => {}) //Function Definitions function createImage(params, res) { - let branch = params.branch; - let profile = params.profileName; - let clearCache = params.clearCache; - let dockerComposeFile = null; - let dockerPath = path.join(__dirname, '../docker/'); - fs.readdirSync(dockerPath).forEach((file) => { - if (file.indexOf('.yml') > -1) { - dockerComposeFile = file; - } - }); - //Create Tmp Env File to use in command line - let tmpEnvFile = tmp.fileSync(); - fs.writeFileSync(tmpEnvFile.name, 'BRANCH=' + branch); - - let commandArgs = [ - 'compose', - '--env-file', - tmpEnvFile.name, - '--profile', - profile, - '-f', - dockerPath + dockerComposeFile, - 'build', - '--progress=plain', - '--build-arg', - 'BRANCH=' + branch, - (clearCache ? '--no-cache' : '') - ]; - let x = childProcess.spawn(command, commandArgs); - x.stdout.on('data', (data) => { - io.emit('buildProgress', data.toString()); - }); - x.stdout.on('close', (data) => { - io.emit('buildProgress', "Image Build Complete!"); - io.emit('buildComplete', true); - res.send('Image Created!'); - - }); - + let branch = params.branch + let profile = params.profileName + let clearCache = params.clearCache + let dockerComposeFile = null + let dockerPath = path.join(__dirname, '../docker/') + fs.readdirSync(dockerPath).forEach((file) => { + if (file.indexOf('.yml') > -1) { + dockerComposeFile = file + } + }) + //Create Tmp Env File to use in command line + let tmpEnvFile = tmp.fileSync() + fs.writeFileSync(tmpEnvFile.name, 'BRANCH=' + branch) + + let commandArgs = [ + 'compose', + '--env-file', + tmpEnvFile.name, + '--profile', + profile, + '-f', + dockerPath + dockerComposeFile, + 'build', + '--progress=plain', + '--build-arg', + 'BRANCH=' + branch, + clearCache ? '--no-cache' : '' + ] + let x = childProcess.spawn(command, commandArgs) + x.stdout.on('data', (data) => { + io.emit('buildProgress', data.toString()) + }) + x.stdout.on('close', (data) => { + io.emit('buildProgress', 'Image Build Complete!') + io.emit('buildComplete', true) + res.send('Image Created!') + }) } function startContainer(params, res) { - let profile = params.profileName; - let branch = params.branch; - let dockerComposeFile = null; - let dockerPath = path.join(__dirname, '../docker/'); - fs.readdirSync(dockerPath).forEach((file) => { - if (file.indexOf('.yml') > -1) { - dockerComposeFile = file; - } - }); - - //Create Tmp Env File to use in command line - let tmpEnvFile = tmp.fileSync(); - fs.writeFileSync(tmpEnvFile.name, 'BRANCH=' + branch); - - let httpPort = getOpenPort(); - let httpsPort = getOpenPort(); - let sshPort = getOpenPort(); - let mySqlPort = getOpenPort(); - let commandArgs = [ - 'compose', - '--env-file', - tmpEnvFile.name, - '-p', - Math.random().toString(36).slice(2), - '--profile', - profile, - '-f', - dockerPath + dockerComposeFile, - 'up', - - '-d', - ]; - let x = childProcess.spawn(command, commandArgs); - x.stdout.on('data', (data) => { - console.log(data.toString()); - io.emit('buildProgress', data.toString()); - }); - x.stderr.on('data', (data) => { - console.log(data.toString()); - io.emit('buildProgress', data.toString()); - io.emit('buildError', data.toString()); - }); - - x.stdout.on('close', (data) => { - io.emit('buildProgress', "Container Process Completed."); - res.send('Container Started'); - - }) + let profile = params.profileName + let branch = params.branch + let dockerComposeFile = null + let dockerPath = path.join(__dirname, '../docker/') + fs.readdirSync(dockerPath).forEach((file) => { + if (file.indexOf('.yml') > -1) { + dockerComposeFile = file + } + }) + + //Create Tmp Env File to use in command line + let tmpEnvFile = tmp.fileSync() + fs.writeFileSync(tmpEnvFile.name, 'BRANCH=' + branch) + + let httpPort = getOpenPort() + let httpsPort = getOpenPort() + let sshPort = getOpenPort() + let mySqlPort = getOpenPort() + let commandArgs = [ + 'compose', + '--env-file', + tmpEnvFile.name, + '-p', + Math.random().toString(36).slice(2), + '--profile', + profile, + '-f', + dockerPath + dockerComposeFile, + 'up', + + '-d' + ] + let x = childProcess.spawn(command, commandArgs) + x.stdout.on('data', (data) => { + console.log(data.toString()) + io.emit('buildProgress', data.toString()) + }) + x.stderr.on('data', (data) => { + console.log(data.toString()) + io.emit('buildProgress', data.toString()) + io.emit('buildError', data.toString()) + }) + + x.stdout.on('close', (data) => { + io.emit('buildProgress', 'Container Process Completed.') + res.send('Container Started') + }) } function stopContainer(params, res) { - let profile = params.profileName; - let projectName = params.projectName.split('-')[0]; - let dockerComposeFile = null; - let dockerPath = path.join(__dirname, '../docker/'); - fs.readdirSync(dockerPath).forEach((file) => { - if (file.indexOf('.yml') > -1) { - dockerComposeFile = file; - } - }); - - let commandArgs = ['compose', '-p', projectName, '--profile', profile, '-f', dockerPath + dockerComposeFile, 'stop',]; - let x = childProcess.spawn(command, commandArgs); - x.stdout.on('data', (data) => { - console.log(data.toString()); - io.emit('buildProgress', data.toString()); - }); - x.stdout.on('close', (data) => { - io.emit('buildProgress', "Container Stopped!"); - res.send('Container Stopped'); - - }) + let profile = params.profileName + let projectName = params.projectName.split('-')[0] + let dockerComposeFile = null + let dockerPath = path.join(__dirname, '../docker/') + fs.readdirSync(dockerPath).forEach((file) => { + if (file.indexOf('.yml') > -1) { + dockerComposeFile = file + } + }) + + let commandArgs = [ + 'compose', + '-p', + projectName, + '--profile', + profile, + '-f', + dockerPath + dockerComposeFile, + 'stop' + ] + let x = childProcess.spawn(command, commandArgs) + x.stdout.on('data', (data) => { + console.log(data.toString()) + io.emit('buildProgress', data.toString()) + }) + x.stdout.on('close', (data) => { + io.emit('buildProgress', 'Container Stopped!') + res.send('Container Stopped') + }) } function destroyContainer(params, res) { - let profile = params.profileName; - let projectName = params.projectName.split('-')[0]; - let dockerComposeFile = null; - let dockerPath = path.join(__dirname, '../docker/'); - fs.readdirSync(dockerPath).forEach((file) => { - if (file.indexOf('.yml') > -1) { - dockerComposeFile = file; - } - }); - - let commandArgs = ['compose', '-p', projectName, '--profile', profile, '-f', dockerPath + dockerComposeFile, 'down', '-v']; - let x = childProcess.spawn(command, commandArgs); - x.stdout.on('data', (data) => { - console.log(data.toString()); - io.emit('buildProgress', data.toString()); - }); - x.stdout.on('close', (data) => { - io.emit('buildProgress', "Container Destroyed!"); - res.send('Container Destroyed'); - - }) + let profile = params.profileName + let projectName = params.projectName.split('-')[0] + let dockerComposeFile = null + let dockerPath = path.join(__dirname, '../docker/') + fs.readdirSync(dockerPath).forEach((file) => { + if (file.indexOf('.yml') > -1) { + dockerComposeFile = file + } + }) + + let commandArgs = [ + 'compose', + '-p', + projectName, + '--profile', + profile, + '-f', + dockerPath + dockerComposeFile, + 'down', + '-v' + ] + let x = childProcess.spawn(command, commandArgs) + x.stdout.on('data', (data) => { + console.log(data.toString()) + io.emit('buildProgress', data.toString()) + }) + x.stdout.on('close', (data) => { + io.emit('buildProgress', 'Container Destroyed!') + res.send('Container Destroyed') + }) } function getContainerList(res) { - - let commandArgs = ['ps', '-a', '--format', '"{{.ID}}|{{.Names}}|{{.Image}}|{{.Ports}}|{{.Status}}"']; - let x = childProcess.spawn(command, commandArgs); - let headers = [ - { - title: 'ID', - key : 'id', - value: 'id', - }, - { - title: 'Name', - key : 'name', - value: 'name', - }, - { - title: 'Image', - key : 'image', - value: 'image', - }, - { - title: 'Ports', - key : 'ports', - value: 'ports', - }, - { - title: 'Status', - key : 'status', - value: 'status', - }, - { - title: 'Branch', - key : 'branch', - value: 'branch', - } - ]; - let returnData = { - headers : headers, - containers: [], - }; - returnData.headers = headers; - x.stdout.on('data', (data) => { - let output = data.toString(); - let containers = output.split('\n'); - containers.map((container) => { - container = container.replace(/"/g, ''); - let containerArr = container.split('|'); - let containerRow = {}; - containerArr.forEach((item, index) => { - let headerName = headers[index].key; - - if (headerName === 'ports') { - let portsList = item.split(','); - let ports = []; - portsList.forEach((port) => { - let portArr = port.split('->'); - let host = (portArr[0]) ? portArr[0].trim() : null; - let guest = (portArr[1]) ? portArr[1].trim() : null; - if (guest !== null && guest.indexOf('443') > -1) { - host = 'HTTPS'; - } else if (guest !== null && guest.indexOf('80') > -1) { - host = 'HTTP'; - } else if (guest !== null && guest.indexOf('3306') > -1) { - host = 'MySQL: ' + host + ''; - } - ports.push({ - host : host, - guest: guest, - }); - }); - containerRow[headerName] = ports; - return; - } else if (headerName === 'branch') { - let branch = ''; - let git = childProcess.spawn(command, ['exec', '-it,' + containerRow['name'], '/bin/bash', '-c', 'git branch --show-current']); - git.stdout.on('data', (data) => { - branch = data.toString(); - containerRow[headerName] = branch; - - }); - return; - - } - containerRow[headerName] = item.trim(); - }); - if (containerRow.id) { - returnData.containers.push(containerRow); - + let commandArgs = [ + 'ps', + '-a', + '--format', + '"{{.ID}}|{{.Names}}|{{.Image}}|{{.Ports}}|{{.Status}}"' + ] + let x = childProcess.spawn(command, commandArgs) + let headers = [ + { + title: 'ID', + key: 'id', + value: 'id' + }, + { + title: 'Name', + key: 'name', + value: 'name' + }, + { + title: 'Image', + key: 'image', + value: 'image' + }, + { + title: 'Ports', + key: 'ports', + value: 'ports' + }, + { + title: 'Status', + key: 'status', + value: 'status' + }, + { + title: 'Branch', + key: 'branch', + value: 'branch' + } + ] + let returnData = { + headers: headers, + containers: [] + } + returnData.headers = headers + x.stdout.on('data', (data) => { + let output = data.toString() + let containers = output.split('\n') + containers.map((container) => { + container = container.replace(/"/g, '') + let containerArr = container.split('|') + let containerRow = {} + containerArr.forEach((item, index) => { + let headerName = headers[index].key + + if (headerName === 'ports') { + let portsList = item.split(',') + let ports = [] + portsList.forEach((port) => { + let portArr = port.split('->') + let host = portArr[0] ? portArr[0].trim() : null + let guest = portArr[1] ? portArr[1].trim() : null + if (guest !== null && guest.indexOf('443') > -1) { + host = 'HTTPS' + } else if (guest !== null && guest.indexOf('80') > -1) { + host = 'HTTP' + } else if (guest !== null && guest.indexOf('3306') > -1) { + host = 'MySQL: ' + host + '' } - }); - - io.emit('containerListUpdated', returnData); - res.send(returnData); - - }); + ports.push({ + host: host, + guest: guest + }) + }) + containerRow[headerName] = ports + return + } else if (headerName === 'branch') { + let branch = '' + let git = childProcess.spawn(command, [ + 'exec', + '-it,' + containerRow['name'], + '/bin/bash', + '-c', + 'git branch --show-current' + ]) + git.stdout.on('data', (data) => { + branch = data.toString() + containerRow[headerName] = branch + }) + return + } + containerRow[headerName] = item.trim() + }) + if (containerRow.id) { + returnData.containers.push(containerRow) + } + }) + io.emit('containerListUpdated', returnData) + res.send(returnData) + }) } function sendCommand(params, res) { - let containerName = params.containerName; - let sshCommand = params.sshCommand; - console.log(params); - let command = 'docker'; - let commandArgs = ['exec', containerName, 'sh', '-c', sshCommand]; - let x = childProcess.spawn(command, commandArgs); - x.stdout.on('data', (data) => { - console.log(data.toString()); - io.emit('sshOutput', data.toString()); - }); - x.stdout.on('close', (data) => { - io.emit('sshClose', "Connection Closed!"); - res.send(data.toString() + "\r\n" + "Connection Closed!"); - - }) + let containerName = params.containerName + let sshCommand = params.sshCommand + console.log(params) + let command = 'docker' + let commandArgs = ['exec', containerName, 'sh', '-c', sshCommand] + let x = childProcess.spawn(command, commandArgs) + x.stdout.on('data', (data) => { + console.log(data.toString()) + io.emit('sshOutput', data.toString()) + }) + x.stdout.on('close', (data) => { + io.emit('sshClose', 'Connection Closed!') + res.send(data.toString() + '\r\n' + 'Connection Closed!') + }) } function getOpenPort(portNumber = null) { - let lsof = 'lsof'; - let port = (portNumber) ? portNumber : Math.floor(Math.random() * 65535); - let commandArgs = ['-i:', port]; - let x = childProcess.spawn(lsof, commandArgs); - - x.stdout.on('data', (data) => { - if (data.toString().indexOf('LISTEN') > -1) { - port--; - getOpenPort(port); - } else { - console.log('Found open port: ' + port); - return port; - } - }); - return port; - + let lsof = 'lsof' + let port = portNumber ? portNumber : Math.floor(Math.random() * 65535) + let commandArgs = ['-i:', port] + let x = childProcess.spawn(lsof, commandArgs) + + x.stdout.on('data', (data) => { + if (data.toString().indexOf('LISTEN') > -1) { + port-- + getOpenPort(port) + } else { + console.log('Found open port: ' + port) + return port + } + }) + return port }