From a1bf08ddced692a5ee866b095f596248273d63b5 Mon Sep 17 00:00:00 2001 From: Neshura Date: Tue, 14 Mar 2023 22:13:49 +0100 Subject: [PATCH] Switched over to using Docker API ToDo: Change location to actual location --- interfaces/CardTypes.ts | 1 + interfaces/DockerStatus.ts | 9 +++++++++ pages/api/containers.tsx | 26 ++++++++++++++++++++------ pages/services.tsx | 11 ++++++----- 4 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 interfaces/DockerStatus.ts diff --git a/interfaces/CardTypes.ts b/interfaces/CardTypes.ts index aabe411..bae386c 100644 --- a/interfaces/CardTypes.ts +++ b/interfaces/CardTypes.ts @@ -32,6 +32,7 @@ export enum Status { export enum ServiceLocation { brr7_4800u = "brr7-4800u", + tower_0 = "tower-0", other = "" } diff --git a/interfaces/DockerStatus.ts b/interfaces/DockerStatus.ts new file mode 100644 index 0000000..b3bc696 --- /dev/null +++ b/interfaces/DockerStatus.ts @@ -0,0 +1,9 @@ +export interface DockerInfo { + name: string, + status: DockerStatus, + id: string +} + +export enum DockerStatus { + running = "running", +} diff --git a/pages/api/containers.tsx b/pages/api/containers.tsx index 6f75e6e..d83f405 100644 --- a/pages/api/containers.tsx +++ b/pages/api/containers.tsx @@ -1,13 +1,27 @@ import Docker from 'dockerode' +import ApiSecret from '../../private/portainer_api_secret.json' +import { DockerInfo } from '../../interfaces/DockerStatus'; export default async function ContainersAPI(req: any, res: any) { + const token = JSON.parse(JSON.stringify(ApiSecret.token)); + try { - const options = { - socketPath: '/var/run/docker.sock', - path: '/v1.41/containers/json' - }; - var docker = new Docker({ socketPath: options.socketPath }); - const list = await docker.listContainers({ all: true }) + const res1 = await fetch('https://portainer.neshweb.net/api/endpoints/2/docker/containers/json', { + method: "GET", + headers: {"X-API-Key": token} + }); + + const unparsed = await res1.json(); + let list: DockerInfo[] = []; + + unparsed.forEach((entry: any) => { + let newEntry = {} as DockerInfo; + + newEntry.name = entry.Names[0].substring(1); + newEntry.status = entry.State; + newEntry.id = entry.Id; + list.push(newEntry); + }); res.status(200).json(list); } diff --git a/pages/services.tsx b/pages/services.tsx index a42efab..8793aba 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -5,6 +5,7 @@ import { ReactElement } from 'react' import useSWR from 'swr'; import { CardContentService, PageContentBox, PageDescription, PageTitle } from '../components/styles/content'; import ServiceList from '../public/data/pages.json'; +import { DockerInfo } from '../interfaces/DockerStatus'; const fetcher = (url: string) => fetch(url).then((res) => res.json()) @@ -56,7 +57,7 @@ function Services() { ) } -async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) { +async function getStatus(entry: Service, containers: DockerInfo[]) { // Currently the only location supporting different fetching depending on type is brr7-4800u // Others to follow but low prio as this is currently the only location used @@ -93,14 +94,14 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) for (let i = 0; i < containers.length; i++) { const container = containers[i]; // Docker API returns container names with / prepended - if (containers[i].Names.includes("/" + entry.docker_container_name)) { + if (container.name === entry.docker_container_name) { // so far only "running" is properly implemented, mroe cases to follow as needed - switch (container.State) { + switch (container.status) { case "running": entry.status = Status.online; break; default: - console.log("Container Status " + container.State + " has no case implemented"); + console.log("Container Status " + container.status + " has no case implemented"); entry.status = Status.offline; } found = true; @@ -162,7 +163,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) return entry; } -const fetchFullDataArray = (containerData: Dockerode.ContainerInfo[], dataSet: Service[]) => { +const fetchFullDataArray = (containerData: DockerInfo[], dataSet: Service[]) => { const fetchStatus = (entry: Service) => getStatus(entry, containerData); return Promise.all(dataSet.map(fetchStatus)); }