Switched over to using Docker API

ToDo: Change location to actual location
This commit is contained in:
Neshura 2023-03-14 22:13:49 +01:00
parent c77fc44172
commit a1bf08ddce
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
4 changed files with 36 additions and 11 deletions

View file

@ -32,6 +32,7 @@ export enum Status {
export enum ServiceLocation {
brr7_4800u = "brr7-4800u",
tower_0 = "tower-0",
other = ""
}

View file

@ -0,0 +1,9 @@
export interface DockerInfo {
name: string,
status: DockerStatus,
id: string
}
export enum DockerStatus {
running = "running",
}

View file

@ -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);
}

View file

@ -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));
}