Add Location handling

This commit is contained in:
Neshura 2023-03-14 22:32:53 +01:00
parent 26f9ac2099
commit 795609f0a0
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
4 changed files with 48 additions and 75 deletions

View file

@ -1,7 +1,9 @@
import { ServiceLocation } from './CardTypes';
export interface DockerInfo { export interface DockerInfo {
name: string, name: string,
status: DockerStatus, status: DockerStatus,
id: string id: string
location: ServiceLocation,
} }
export enum DockerStatus { export enum DockerStatus {

View file

@ -1,6 +1,7 @@
import Docker from 'dockerode' import Docker from 'dockerode'
import ApiSecret from '../../private/portainer_api_secret.json' import ApiSecret from '../../private/portainer_api_secret.json'
import { DockerInfo } from '../../interfaces/DockerStatus'; import { DockerInfo } from '../../interfaces/DockerStatus';
import { ServiceLocation } from '../../interfaces/CardTypes';
export default async function ContainersAPI(req: any, res: any) { export default async function ContainersAPI(req: any, res: any) {
const token = JSON.parse(JSON.stringify(ApiSecret.token)); 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.name = entry.Names[0].substring(1);
newEntry.status = entry.State; newEntry.status = entry.State;
newEntry.id = entry.Id; newEntry.id = entry.Id;
newEntry.location = ServiceLocation.tower_0;
list.push(newEntry); list.push(newEntry);
}); });

View file

@ -1,7 +1,7 @@
import Head from 'next/head' import Head from 'next/head'
import { Game } from '../interfaces/CardTypes'; import { Game } from '../interfaces/CardTypes';
import { PageContentBox, PageDescription, PageTitle, CardContentGame } from '../components/styles/content' import { PageContentBox, PageDescription, PageTitle, CardContentGame } from '../components/styles/content'
import GameList from '../public/pages.json'; import GameList from '../public/data/pages.json';
function Servers() { function Servers() {
// TODO: unuggly this shit // TODO: unuggly this shit

View file

@ -61,78 +61,8 @@ async function getStatus(entry: Service, containers: DockerInfo[]) {
// Currently the only location supporting different fetching depending on type is brr7-4800u // 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 // Others to follow but low prio as this is currently the only location used
// Location BRR7-4800U // Type APP
if (entry.location === ServiceLocation.brr7_4800u) { if (entry.type === ServiceType.app) {
// 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
await fetch(entry.href) await fetch(entry.href)
.then((response) => { .then((response) => {
if (response.ok) { if (response.ok) {
@ -155,9 +85,48 @@ async function getStatus(entry: Service, containers: DockerInfo[]) {
entry.status = Status.error; 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 { 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; entry.status = Status.error;
} }
return entry; return entry;