From 795609f0a0672f5f06cbcd4ff86be69f3f60b660 Mon Sep 17 00:00:00 2001 From: Neshura Date: Tue, 14 Mar 2023 22:32:53 +0100 Subject: [PATCH] Add Location handling --- interfaces/DockerStatus.ts | 2 + pages/api/containers.tsx | 2 + pages/servers.tsx | 2 +- pages/services.tsx | 117 ++++++++++++++----------------------- 4 files changed, 48 insertions(+), 75 deletions(-) diff --git a/interfaces/DockerStatus.ts b/interfaces/DockerStatus.ts index b3bc696..910f222 100644 --- a/interfaces/DockerStatus.ts +++ b/interfaces/DockerStatus.ts @@ -1,7 +1,9 @@ +import { ServiceLocation } from './CardTypes'; export interface DockerInfo { name: string, status: DockerStatus, id: string + location: ServiceLocation, } export enum DockerStatus { diff --git a/pages/api/containers.tsx b/pages/api/containers.tsx index d83f405..7f6a6d2 100644 --- a/pages/api/containers.tsx +++ b/pages/api/containers.tsx @@ -1,6 +1,7 @@ import Docker from 'dockerode' import ApiSecret from '../../private/portainer_api_secret.json' import { DockerInfo } from '../../interfaces/DockerStatus'; +import { ServiceLocation } from '../../interfaces/CardTypes'; export default async function ContainersAPI(req: any, res: any) { const token = JSON.parse(JSON.stringify(ApiSecret.token)); @@ -20,6 +21,7 @@ export default async function ContainersAPI(req: any, res: any) { newEntry.name = entry.Names[0].substring(1); newEntry.status = entry.State; newEntry.id = entry.Id; + newEntry.location = ServiceLocation.tower_0; list.push(newEntry); }); diff --git a/pages/servers.tsx b/pages/servers.tsx index 1acfe95..c193a49 100644 --- a/pages/servers.tsx +++ b/pages/servers.tsx @@ -1,7 +1,7 @@ import Head from 'next/head' import { Game } from '../interfaces/CardTypes'; import { PageContentBox, PageDescription, PageTitle, CardContentGame } from '../components/styles/content' -import GameList from '../public/pages.json'; +import GameList from '../public/data/pages.json'; function Servers() { // TODO: unuggly this shit diff --git a/pages/services.tsx b/pages/services.tsx index 8793aba..fce23f6 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -61,78 +61,8 @@ 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 - // Location BRR7-4800U - if (entry.location === ServiceLocation.brr7_4800u) { - // Type APP - if (entry.type === ServiceType.app) { - await fetch(entry.href) - .then((response) => { - if (response.ok) { - switch (response.status) { - case 200: - case 301: - case 302: - entry.status = Status.online; - break; - default: - entry.status = Status.offline; - } - } - else { - entry.status = Status.offline; - } - }) - .catch((error) => { - console.error("Error pinging Website: ", error); - entry.status = Status.error; - }) - } - // Type Docker - else if (entry.type === ServiceType.docker) { - if (entry.name !== null) { - let found = false; - for (let i = 0; i < containers.length; i++) { - const container = containers[i]; - // Docker API returns container names with / prepended - if (container.name === entry.docker_container_name) { - // so far only "running" is properly implemented, mroe cases to follow as needed - switch (container.status) { - case "running": - entry.status = Status.online; - break; - default: - console.log("Container Status " + container.status + " has no case implemented"); - entry.status = Status.offline; - } - found = true; - // cancel the for - break; - } - // If container name is not missing the container is set to offline - else { - entry.status = Status.offline; - } - } - if (!found) { - console.warn("Container for " + entry.name + " could not be found"); - } - } - // if name is null do not enter for loop - else { - console.error("Container Name not specified"); - entry.status = Status.error; - } - } - // If no Type matches - else { - console.warn("Service Type for Service " + entry.name + " not specified or invalid"); - entry.status = Status.error; - } - } - // Location Other - // TODO: implement docker type for other locations - else if (entry.location === ServiceLocation.other) { - // Currently uses the same handling as app type for the other location + // Type APP + if (entry.type === ServiceType.app) { await fetch(entry.href) .then((response) => { if (response.ok) { @@ -155,9 +85,48 @@ async function getStatus(entry: Service, containers: DockerInfo[]) { entry.status = Status.error; }) } - // If no Location matches + // Type Docker + else if (entry.type === ServiceType.docker) { + if (entry.name !== null) { + let found = false; + for (let i = 0; i < containers.length; i++) { + const container = containers[i]; + // Docker API returns container names with / prepended + if (container.name === entry.docker_container_name) { + + if (container.location === entry.location) { + // so far only "running" is properly implemented, mroe cases to follow as needed + switch (container.status) { + case "running": + entry.status = Status.online; + break; + default: + console.log("Container Status " + container.status + " has no case implemented"); + entry.status = Status.offline; + } + found = true; + // cancel the for + break; + } + } + // If container name is not missing the container is set to offline + else { + entry.status = Status.offline; + } + } + if (!found) { + console.warn("Container for " + entry.name + " could not be found"); + } + } + // if name is null do not enter for loop + else { + console.error("Container Name not specified"); + entry.status = Status.error; + } + } + // If no Type matches else { - console.warn("Service Location for Service " + entry.name + " not specified"); + console.warn("Service Type for Service " + entry.name + " not specified or invalid"); entry.status = Status.error; } return entry;