From 0f995ffe97c2ee63f18a4ebc8d8364c05cf5cb0f Mon Sep 17 00:00:00 2001 From: Peter8234 Date: Thu, 28 Oct 2021 16:18:45 -0400 Subject: [PATCH 1/5] minor test edit --- server/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server/index.js b/server/index.js index 20b4138..82ef5d6 100644 --- a/server/index.js +++ b/server/index.js @@ -6,6 +6,7 @@ const path = require('path'); const Docker = require('dockerode'); const { app } = expressWs; + const FRONT_END_PATH = path.join(__dirname, '..', 'front-end', 'build') app.use(express.json()); From 0a5c6c0509d65224f9491eb4b65ed7f120f5c6e0 Mon Sep 17 00:00:00 2001 From: Peter8234 Date: Thu, 28 Oct 2021 16:29:58 -0400 Subject: [PATCH 2/5] test edit 2? --- server/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/index.js b/server/index.js index 82ef5d6..20b4138 100644 --- a/server/index.js +++ b/server/index.js @@ -6,7 +6,6 @@ const path = require('path'); const Docker = require('dockerode'); const { app } = expressWs; - const FRONT_END_PATH = path.join(__dirname, '..', 'front-end', 'build') app.use(express.json()); From a8a76833d4278072a0b710aa3c33b89b5cd63d5a Mon Sep 17 00:00:00 2001 From: Peter8234 Date: Tue, 2 Nov 2021 11:54:31 -0400 Subject: [PATCH 3/5] test edit 3 --- front-end/src/dashboard/Container.js | 169 ++++++++++++++------------- 1 file changed, 87 insertions(+), 82 deletions(-) diff --git a/front-end/src/dashboard/Container.js b/front-end/src/dashboard/Container.js index a034406..01b4af0 100644 --- a/front-end/src/dashboard/Container.js +++ b/front-end/src/dashboard/Container.js @@ -1,92 +1,97 @@ -import React, {useState, useEffect} from 'react'; -import {useParams} from "react-router"; -import { w3cwebsocket as W3CWebSocket } from "websocket"; -import {Button, Typography} from "@material-ui/core"; -import {Terminal} from 'xterm'; -import 'xterm/css/xterm.css' -import Box from "@material-ui/core/Box"; -import ChevronLeftIcon from '@mui/icons-material/ChevronLeft'; -function Container() { - const [container, setContainer] = useState({}); - const { jobUuid, id } = useParams(); + +const express = require('express') +const expressApp = express(); +const expressWs = require('express-ws')(expressApp); +const port = 3001; +const path = require('path'); +const Docker = require('dockerode'); +const { app } = expressWs; - useEffect(() => { - const protocol = window.location.protocol.replace('http', 'ws'); - const host = window.location.host - const client = new W3CWebSocket(`${protocol}//${host}/${jobUuid}/container/${id}/logs`); - const term = new Terminal(); +const FRONT_END_PATH = path.join(__dirname, '..', 'front-end', 'build') - function streamLogs() { - term.open(document.getElementById('terminal')); - client.onopen = () => client.send('logs'); - client.onmessage = function (event) { - term.writeln(event.data) - } - client.onerror = (error) => { - console.error(error) - } +app.use(express.json()); + +app.use(express.static(FRONT_END_PATH)); + +const setDocker = async function (req, res, next) { + let docker = new Docker({ protocol: 'ssh', host: `${req.params.jobUuid}.lan`, password: 'password', username: 'root'}); + //ping docker to see if connection is working + try { + await docker.ping(); + req.docker = docker; + next() + } catch (err) { + next(err) + } +} + + +app.get('/:jobUuid/containers', setDocker, (req, res, next) => { + req.docker.listContainers({}, (err, containers) => { + if (err) { + console.error(err); + next(err); + } else { + res.send(containers); } + }) +}); - streamLogs() - }, [id, jobUuid]); +app.get('/:jobUuid/container/:id', setDocker, (req, res, next) => { + req.docker.getContainer(req.params.id).inspect( {},(err, container) => { + if (err) { + console.error(err); + next(err); + } else { + res.send(container); + } + }) +}); + +app.ws('/:jobUuid/container/:id/logs', async (ws, req) => { + let docker = new Docker({ protocol: 'ssh', host: `${req.params.jobUuid}.lan`, password: 'password', username: 'root'}); + + //ping docker to see if connection is working + try { + await docker.ping(); + } catch (err) { + ws.Close(); + } - useEffect(() => { - async function fetchContainer() { - try { - const res = await fetch(`/${jobUuid}/container/${id}`) - const result = await res.json() - setContainer(result) - } catch (reason) { - console.error(reason) - } + let container = docker.getContainer(req.params.id) + if(!container) { + ws.send('Could not find container.'); + } + + container.logs({ + follow: true, + stdout: true, + stderr: true + }, (err, logs) => { + if (err) { + console.error(err); + ws.Close(); + } else { + logs.on('data', chunk => { + let encodedLogs = Buffer.from(chunk, 'utf-8').toString(); + ws.send(encodedLogs); + }) } - fetchContainer() - }, [id, jobUuid]) + }) +}); - return( - - - - - {(container && container.Id)? `Container: ${container.Id.substring(0, 11)}` : null} - {(container && container.Id)? container.Name: null} - - - - - { - (container && container.Id)? <> - - - Status: {container.State.Status} - - - - - Created at: {container.Created} - - - - - Started at: {container.State.StartedAt} - - - - : null - } +app.get('*', function(req, res) { + res.sendFile('index.html', {root: FRONT_END_PATH}); +}); - - -
- - - - ); -} +app.use(function (err, req, res) { + console.error(err.stack) + res.status(500).send({ + err : err.stack + }) +}) -export default Container; \ No newline at end of file +app.listen(port, () => { + console.log(`Docker plugin dashboard listening on http://localhost:${port}`) +}); \ No newline at end of file From c8c78f30949f23e9a71325d24b6216857739b55c Mon Sep 17 00:00:00 2001 From: Peter8234 Date: Wed, 3 Nov 2021 11:08:54 -0400 Subject: [PATCH 4/5] test edit 5 --- front-end/src/dashboard/Container.js | 1 + 1 file changed, 1 insertion(+) diff --git a/front-end/src/dashboard/Container.js b/front-end/src/dashboard/Container.js index 01b4af0..ba50d9a 100644 --- a/front-end/src/dashboard/Container.js +++ b/front-end/src/dashboard/Container.js @@ -14,6 +14,7 @@ app.use(express.json()); app.use(express.static(FRONT_END_PATH)); + const setDocker = async function (req, res, next) { let docker = new Docker({ protocol: 'ssh', host: `${req.params.jobUuid}.lan`, password: 'password', username: 'root'}); //ping docker to see if connection is working From 40c2f24a36d1ab2130be771bf5a65e059078e990 Mon Sep 17 00:00:00 2001 From: Peter8234 Date: Fri, 5 Nov 2021 22:14:00 -0400 Subject: [PATCH 5/5] test edit 8 --- front-end/src/dashboard/Dashboard.js | 1 + 1 file changed, 1 insertion(+) diff --git a/front-end/src/dashboard/Dashboard.js b/front-end/src/dashboard/Dashboard.js index 41317dc..75418ea 100644 --- a/front-end/src/dashboard/Dashboard.js +++ b/front-end/src/dashboard/Dashboard.js @@ -11,6 +11,7 @@ import {Link} from "react-router-dom"; import Box from '@material-ui/core/Box'; import {useParams} from "react-router-dom"; + function Dashboard() { const [containers, setContainers] = useState([]); const [page, setPage] = useState(0);