Switched over to using Docker API
ToDo: Change location to actual location
This commit is contained in:
parent
c77fc44172
commit
a1bf08ddce
4 changed files with 36 additions and 11 deletions
|
@ -32,6 +32,7 @@ export enum Status {
|
||||||
|
|
||||||
export enum ServiceLocation {
|
export enum ServiceLocation {
|
||||||
brr7_4800u = "brr7-4800u",
|
brr7_4800u = "brr7-4800u",
|
||||||
|
tower_0 = "tower-0",
|
||||||
other = ""
|
other = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
interfaces/DockerStatus.ts
Normal file
9
interfaces/DockerStatus.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
export interface DockerInfo {
|
||||||
|
name: string,
|
||||||
|
status: DockerStatus,
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum DockerStatus {
|
||||||
|
running = "running",
|
||||||
|
}
|
|
@ -1,13 +1,27 @@
|
||||||
import Docker from 'dockerode'
|
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) {
|
export default async function ContainersAPI(req: any, res: any) {
|
||||||
|
const token = JSON.parse(JSON.stringify(ApiSecret.token));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const options = {
|
const res1 = await fetch('https://portainer.neshweb.net/api/endpoints/2/docker/containers/json', {
|
||||||
socketPath: '/var/run/docker.sock',
|
method: "GET",
|
||||||
path: '/v1.41/containers/json'
|
headers: {"X-API-Key": token}
|
||||||
};
|
});
|
||||||
var docker = new Docker({ socketPath: options.socketPath });
|
|
||||||
const list = await docker.listContainers({ all: true })
|
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);
|
res.status(200).json(list);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { ReactElement } from 'react'
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import { CardContentService, PageContentBox, PageDescription, PageTitle } from '../components/styles/content';
|
import { CardContentService, PageContentBox, PageDescription, PageTitle } from '../components/styles/content';
|
||||||
import ServiceList from '../public/data/pages.json';
|
import ServiceList from '../public/data/pages.json';
|
||||||
|
import { DockerInfo } from '../interfaces/DockerStatus';
|
||||||
|
|
||||||
const fetcher = (url: string) => fetch(url).then((res) => res.json())
|
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
|
// 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
|
||||||
|
|
||||||
|
@ -93,14 +94,14 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[])
|
||||||
for (let i = 0; i < containers.length; i++) {
|
for (let i = 0; i < containers.length; i++) {
|
||||||
const container = containers[i];
|
const container = containers[i];
|
||||||
// Docker API returns container names with / prepended
|
// 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
|
// so far only "running" is properly implemented, mroe cases to follow as needed
|
||||||
switch (container.State) {
|
switch (container.status) {
|
||||||
case "running":
|
case "running":
|
||||||
entry.status = Status.online;
|
entry.status = Status.online;
|
||||||
break;
|
break;
|
||||||
default:
|
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;
|
entry.status = Status.offline;
|
||||||
}
|
}
|
||||||
found = true;
|
found = true;
|
||||||
|
@ -162,7 +163,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[])
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchFullDataArray = (containerData: Dockerode.ContainerInfo[], dataSet: Service[]) => {
|
const fetchFullDataArray = (containerData: DockerInfo[], dataSet: Service[]) => {
|
||||||
const fetchStatus = (entry: Service) => getStatus(entry, containerData);
|
const fetchStatus = (entry: Service) => getStatus(entry, containerData);
|
||||||
return Promise.all(dataSet.map(fetchStatus));
|
return Promise.all(dataSet.map(fetchStatus));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue