2022-09-04 21:58:42 +03:00
|
|
|
import { promises as fs } from "fs";
|
|
|
|
import path from "path";
|
2022-09-07 16:53:24 +03:00
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
import yaml from "js-yaml";
|
2022-09-09 21:53:05 +03:00
|
|
|
import Docker from "dockerode";
|
|
|
|
import * as shvl from "shvl";
|
2023-09-18 01:49:03 +02:00
|
|
|
import { CustomObjectsApi, NetworkingV1Api, ApiextensionsV1Api } from "@kubernetes/client-node";
|
2022-09-09 21:53:05 +03:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
import createLogger from "utils/logger";
|
2023-07-10 15:44:43 +01:00
|
|
|
import checkAndCopyConfig, { CONF_DIR, substituteEnvironmentVars } from "utils/config/config";
|
2022-09-26 15:25:10 +03:00
|
|
|
import getDockerArguments from "utils/config/docker";
|
2022-10-24 17:03:35 -05:00
|
|
|
import getKubeConfig from "utils/config/kubernetes";
|
2022-09-09 21:53:05 +03:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
const logger = createLogger("service-helpers");
|
|
|
|
|
2022-09-09 21:53:05 +03:00
|
|
|
export async function servicesFromConfig() {
|
|
|
|
checkAndCopyConfig("services.yaml");
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2023-07-10 15:44:43 +01:00
|
|
|
const servicesYaml = path.join(CONF_DIR, "services.yaml");
|
2023-02-23 08:50:25 -06:00
|
|
|
const rawFileContents = await fs.readFile(servicesYaml, "utf8");
|
|
|
|
const fileContents = substituteEnvironmentVars(rawFileContents);
|
2022-09-04 21:58:42 +03:00
|
|
|
const services = yaml.load(fileContents);
|
|
|
|
|
2022-09-09 22:01:01 +03:00
|
|
|
if (!services) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
// map easy to write YAML objects into easy to consume JS arrays
|
2022-09-07 16:53:24 +03:00
|
|
|
const servicesArray = services.map((servicesGroup) => ({
|
|
|
|
name: Object.keys(servicesGroup)[0],
|
|
|
|
services: servicesGroup[Object.keys(servicesGroup)[0]].map((entries) => ({
|
|
|
|
name: Object.keys(entries)[0],
|
|
|
|
...entries[Object.keys(entries)[0]],
|
2023-09-06 13:53:39 +03:00
|
|
|
type: "service",
|
2022-09-07 16:53:24 +03:00
|
|
|
})),
|
|
|
|
}));
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2023-01-24 12:48:49 -06:00
|
|
|
// add default weight to services based on their position in the configuration
|
|
|
|
servicesArray.forEach((group, groupIndex) => {
|
|
|
|
group.services.forEach((service, serviceIndex) => {
|
2023-09-06 13:53:39 +03:00
|
|
|
if (!service.weight) {
|
2023-01-24 12:48:49 -06:00
|
|
|
servicesArray[groupIndex].services[serviceIndex].weight = (serviceIndex + 1) * 100;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-09 21:53:05 +03:00
|
|
|
return servicesArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function servicesFromDocker() {
|
|
|
|
checkAndCopyConfig("docker.yaml");
|
|
|
|
|
2023-07-10 15:44:43 +01:00
|
|
|
const dockerYaml = path.join(CONF_DIR, "docker.yaml");
|
2023-02-23 08:50:25 -06:00
|
|
|
const rawDockerFileContents = await fs.readFile(dockerYaml, "utf8");
|
|
|
|
const dockerFileContents = substituteEnvironmentVars(rawDockerFileContents);
|
2022-09-09 21:53:05 +03:00
|
|
|
const servers = yaml.load(dockerFileContents);
|
|
|
|
|
2022-09-09 22:01:01 +03:00
|
|
|
if (!servers) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-09-09 21:53:05 +03:00
|
|
|
const serviceServers = await Promise.all(
|
|
|
|
Object.keys(servers).map(async (serverName) => {
|
2022-12-22 21:16:52 -08:00
|
|
|
try {
|
2023-06-07 14:06:23 -07:00
|
|
|
const isSwarm = !!servers[serverName].swarm;
|
2022-12-22 21:16:52 -08:00
|
|
|
const docker = new Docker(getDockerArguments(serverName).conn);
|
2023-06-07 14:06:23 -07:00
|
|
|
const listProperties = { all: true };
|
2023-09-06 13:53:39 +03:00
|
|
|
const containers = await (isSwarm
|
|
|
|
? docker.listServices(listProperties)
|
|
|
|
: docker.listContainers(listProperties));
|
2022-09-09 21:53:05 +03:00
|
|
|
|
2022-12-22 21:16:52 -08:00
|
|
|
// bad docker connections can result in a <Buffer ...> object?
|
|
|
|
// in any case, this ensures the result is the expected array
|
|
|
|
if (!Array.isArray(containers)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const discovered = containers.map((container) => {
|
|
|
|
let constructedService = null;
|
2023-09-06 13:53:39 +03:00
|
|
|
const containerLabels = isSwarm ? shvl.get(container, "Spec.Labels") : container.Labels;
|
|
|
|
const containerName = isSwarm ? shvl.get(container, "Spec.Name") : container.Names[0];
|
2022-12-22 21:16:52 -08:00
|
|
|
|
2023-06-07 14:06:23 -07:00
|
|
|
Object.keys(containerLabels).forEach((label) => {
|
2022-12-22 21:16:52 -08:00
|
|
|
if (label.startsWith("homepage.")) {
|
|
|
|
if (!constructedService) {
|
|
|
|
constructedService = {
|
2023-06-07 14:06:23 -07:00
|
|
|
container: containerName.replace(/^\//, ""),
|
2022-12-22 21:16:52 -08:00
|
|
|
server: serverName,
|
2023-09-06 13:53:39 +03:00
|
|
|
type: "service",
|
2022-12-22 21:16:52 -08:00
|
|
|
};
|
|
|
|
}
|
2023-09-06 13:53:39 +03:00
|
|
|
shvl.set(
|
|
|
|
constructedService,
|
|
|
|
label.replace("homepage.", ""),
|
|
|
|
substituteEnvironmentVars(containerLabels[label])
|
|
|
|
);
|
2022-09-09 21:53:05 +03:00
|
|
|
}
|
2022-12-22 21:16:52 -08:00
|
|
|
});
|
2022-09-09 21:53:05 +03:00
|
|
|
|
2022-12-22 21:16:52 -08:00
|
|
|
return constructedService;
|
|
|
|
});
|
2022-09-09 21:53:05 +03:00
|
|
|
|
2022-12-22 21:16:52 -08:00
|
|
|
return { server: serverName, services: discovered.filter((filteredService) => filteredService) };
|
|
|
|
} catch (e) {
|
|
|
|
// a server failed, but others may succeed
|
|
|
|
return { server: serverName, services: [] };
|
|
|
|
}
|
2022-09-09 21:53:05 +03:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const mappedServiceGroups = [];
|
|
|
|
|
|
|
|
serviceServers.forEach((server) => {
|
|
|
|
server.services.forEach((serverService) => {
|
|
|
|
let serverGroup = mappedServiceGroups.find((searchedGroup) => searchedGroup.name === serverService.group);
|
|
|
|
if (!serverGroup) {
|
|
|
|
mappedServiceGroups.push({
|
|
|
|
name: serverService.group,
|
|
|
|
services: [],
|
|
|
|
});
|
|
|
|
serverGroup = mappedServiceGroups[mappedServiceGroups.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const { name: serviceName, group: serverServiceGroup, ...pushedService } = serverService;
|
|
|
|
const result = {
|
|
|
|
name: serviceName,
|
|
|
|
...pushedService,
|
|
|
|
};
|
|
|
|
|
|
|
|
serverGroup.services.push(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return mappedServiceGroups;
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
function getUrlFromIngress(ingress) {
|
2023-01-09 08:30:50 -06:00
|
|
|
const urlHost = ingress.spec.rules[0].host;
|
|
|
|
const urlPath = ingress.spec.rules[0].http.paths[0].path;
|
2023-09-06 13:53:39 +03:00
|
|
|
const urlSchema = ingress.spec.tls ? "https" : "http";
|
2023-01-09 08:30:50 -06:00
|
|
|
return `${urlSchema}://${urlHost}${urlPath}`;
|
2022-10-26 10:15:25 -05:00
|
|
|
}
|
|
|
|
|
2023-09-18 01:49:03 +02:00
|
|
|
export async function checkCRD(kc, name) {
|
|
|
|
const apiExtensions = kc.makeApiClient(ApiextensionsV1Api);
|
|
|
|
const exist = await apiExtensions
|
|
|
|
.readCustomResourceDefinitionStatus(name)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(async (error) => {
|
|
|
|
if (error.statusCode === 403) {
|
|
|
|
logger.error(
|
|
|
|
"Error checking if CRD %s exists. Make sure to add the following permission to your RBAC: %d %s %s",
|
|
|
|
name,
|
|
|
|
error.statusCode,
|
|
|
|
error.body.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
});
|
|
|
|
|
|
|
|
return exist
|
|
|
|
}
|
|
|
|
|
2022-10-24 17:03:35 -05:00
|
|
|
export async function servicesFromKubernetes() {
|
2023-09-06 13:53:39 +03:00
|
|
|
const ANNOTATION_BASE = "gethomepage.dev";
|
2023-01-09 08:30:50 -06:00
|
|
|
const ANNOTATION_WIDGET_BASE = `${ANNOTATION_BASE}/widget.`;
|
|
|
|
const ANNOTATION_POD_SELECTOR = `${ANNOTATION_BASE}/pod-selector`;
|
|
|
|
|
2022-10-24 17:03:35 -05:00
|
|
|
checkAndCopyConfig("kubernetes.yaml");
|
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
try {
|
|
|
|
const kc = getKubeConfig();
|
2022-10-27 16:53:54 -05:00
|
|
|
if (!kc) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-10-26 10:15:25 -05:00
|
|
|
const networking = kc.makeApiClient(NetworkingV1Api);
|
2023-03-29 23:19:56 +01:00
|
|
|
const crd = kc.makeApiClient(CustomObjectsApi);
|
2022-10-26 10:15:25 -05:00
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
const ingressList = await networking
|
|
|
|
.listIngressForAllNamespaces(null, null, null, null)
|
2022-10-26 10:15:25 -05:00
|
|
|
.then((response) => response.body)
|
|
|
|
.catch((error) => {
|
|
|
|
logger.error("Error getting ingresses: %d %s %s", error.statusCode, error.body, error.response);
|
|
|
|
return null;
|
|
|
|
});
|
2023-03-29 23:19:56 +01:00
|
|
|
|
2023-09-18 01:49:03 +02:00
|
|
|
const traefikContainoExists = await checkCRD(kc, "ingressroutes.traefik.containo.us");
|
|
|
|
const traefikExists = await checkCRD(kc, "ingressroutes.traefik.io");
|
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
const traefikIngressListContaino = await crd
|
|
|
|
.listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes")
|
2023-09-19 13:12:03 -07:00
|
|
|
.then((response) => response.body)
|
2023-06-13 22:04:56 +02:00
|
|
|
.catch(async (error) => {
|
2023-09-18 01:49:03 +02:00
|
|
|
if (traefikContainoExists) {
|
2023-09-06 13:53:39 +03:00
|
|
|
logger.error(
|
|
|
|
"Error getting traefik ingresses from traefik.containo.us: %d %s %s",
|
|
|
|
error.statusCode,
|
|
|
|
error.body,
|
|
|
|
error.response
|
|
|
|
);
|
2023-08-28 19:00:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
});
|
2023-06-13 22:04:56 +02:00
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
const traefikIngressListIo = await crd
|
|
|
|
.listClusterCustomObject("traefik.io", "v1alpha1", "ingressroutes")
|
2023-09-19 13:12:03 -07:00
|
|
|
.then((response) => response.body)
|
2023-08-28 19:00:39 +01:00
|
|
|
.catch(async (error) => {
|
2023-09-18 01:49:03 +02:00
|
|
|
if (traefikExists) {
|
2023-09-06 13:53:39 +03:00
|
|
|
logger.error(
|
|
|
|
"Error getting traefik ingresses from traefik.io: %d %s %s",
|
|
|
|
error.statusCode,
|
|
|
|
error.body,
|
|
|
|
error.response
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-28 19:00:39 +01:00
|
|
|
return [];
|
2023-03-29 23:19:56 +01:00
|
|
|
});
|
2023-09-06 13:53:39 +03:00
|
|
|
|
2023-09-18 01:49:03 +02:00
|
|
|
const traefikIngressList = [...traefikIngressListContaino?.items ?? [], ...traefikIngressListIo?.items ?? []];
|
2023-03-29 23:19:56 +01:00
|
|
|
|
2023-09-18 01:49:03 +02:00
|
|
|
if (traefikIngressList.length > 0) {
|
2023-09-19 15:00:22 -07:00
|
|
|
const traefikServices = traefikIngressList.filter(
|
2023-09-06 13:53:39 +03:00
|
|
|
(ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/href`]
|
|
|
|
);
|
2023-03-29 23:19:56 +01:00
|
|
|
ingressList.items.push(...traefikServices);
|
|
|
|
}
|
2023-06-06 01:14:10 +02:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
if (!ingressList) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-01-03 16:15:08 -06:00
|
|
|
const services = ingressList.items
|
2023-09-06 13:53:39 +03:00
|
|
|
.filter(
|
|
|
|
(ingress) =>
|
|
|
|
ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/enabled`] === "true"
|
|
|
|
)
|
2023-01-03 16:15:08 -06:00
|
|
|
.map((ingress) => {
|
2023-09-06 13:53:39 +03:00
|
|
|
let constructedService = {
|
|
|
|
app: ingress.metadata.name,
|
|
|
|
namespace: ingress.metadata.namespace,
|
|
|
|
href: ingress.metadata.annotations[`${ANNOTATION_BASE}/href`] || getUrlFromIngress(ingress),
|
|
|
|
name: ingress.metadata.annotations[`${ANNOTATION_BASE}/name`] || ingress.metadata.name,
|
|
|
|
group: ingress.metadata.annotations[`${ANNOTATION_BASE}/group`] || "Kubernetes",
|
|
|
|
weight: ingress.metadata.annotations[`${ANNOTATION_BASE}/weight`] || "0",
|
|
|
|
icon: ingress.metadata.annotations[`${ANNOTATION_BASE}/icon`] || "",
|
|
|
|
description: ingress.metadata.annotations[`${ANNOTATION_BASE}/description`] || "",
|
|
|
|
external: false,
|
|
|
|
type: "service",
|
|
|
|
};
|
|
|
|
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]) {
|
|
|
|
constructedService.external =
|
|
|
|
String(ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]).toLowerCase() === "true";
|
2022-10-26 10:15:25 -05:00
|
|
|
}
|
2023-09-06 13:53:39 +03:00
|
|
|
if (ingress.metadata.annotations[ANNOTATION_POD_SELECTOR]) {
|
|
|
|
constructedService.podSelector = ingress.metadata.annotations[ANNOTATION_POD_SELECTOR];
|
|
|
|
}
|
|
|
|
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`]) {
|
|
|
|
constructedService.ping = ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`];
|
|
|
|
}
|
|
|
|
Object.keys(ingress.metadata.annotations).forEach((annotation) => {
|
|
|
|
if (annotation.startsWith(ANNOTATION_WIDGET_BASE)) {
|
|
|
|
shvl.set(
|
|
|
|
constructedService,
|
|
|
|
annotation.replace(`${ANNOTATION_BASE}/`, ""),
|
|
|
|
ingress.metadata.annotations[annotation]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2022-10-26 10:15:25 -05:00
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
try {
|
|
|
|
constructedService = JSON.parse(substituteEnvironmentVars(JSON.stringify(constructedService)));
|
|
|
|
} catch (e) {
|
|
|
|
logger.error("Error attempting k8s environment variable substitution.");
|
|
|
|
}
|
2023-05-28 22:23:11 -07:00
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
return constructedService;
|
|
|
|
});
|
2022-10-24 17:03:35 -05:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
const mappedServiceGroups = [];
|
2022-10-24 17:03:35 -05:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
services.forEach((serverService) => {
|
|
|
|
let serverGroup = mappedServiceGroups.find((searchedGroup) => searchedGroup.name === serverService.group);
|
|
|
|
if (!serverGroup) {
|
|
|
|
mappedServiceGroups.push({
|
|
|
|
name: serverService.group,
|
|
|
|
services: [],
|
|
|
|
});
|
|
|
|
serverGroup = mappedServiceGroups[mappedServiceGroups.length - 1];
|
|
|
|
}
|
2022-10-24 17:03:35 -05:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
const { name: serviceName, group: serverServiceGroup, ...pushedService } = serverService;
|
|
|
|
const result = {
|
|
|
|
name: serviceName,
|
|
|
|
...pushedService,
|
|
|
|
};
|
2022-10-24 17:03:35 -05:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
serverGroup.services.push(result);
|
|
|
|
});
|
2022-10-24 17:03:35 -05:00
|
|
|
|
2022-10-26 10:15:25 -05:00
|
|
|
return mappedServiceGroups;
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
2022-10-24 17:03:35 -05:00
|
|
|
}
|
|
|
|
|
2022-09-09 21:53:05 +03:00
|
|
|
export function cleanServiceGroups(groups) {
|
|
|
|
return groups.map((serviceGroup) => ({
|
|
|
|
name: serviceGroup.name,
|
|
|
|
services: serviceGroup.services.map((service) => {
|
|
|
|
const cleanedService = { ...service };
|
2023-04-07 20:16:35 -07:00
|
|
|
if (cleanedService.showStats !== undefined) cleanedService.showStats = JSON.parse(cleanedService.showStats);
|
2023-09-06 13:53:39 +03:00
|
|
|
if (typeof service.weight === "string") {
|
2023-01-24 12:48:49 -06:00
|
|
|
const weight = parseInt(service.weight, 10);
|
|
|
|
if (Number.isNaN(weight)) {
|
|
|
|
cleanedService.weight = 0;
|
|
|
|
} else {
|
|
|
|
cleanedService.weight = weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof cleanedService.weight !== "number") {
|
|
|
|
cleanedService.weight = 0;
|
|
|
|
}
|
2022-09-09 21:53:05 +03:00
|
|
|
|
|
|
|
if (cleanedService.widget) {
|
2022-09-12 12:38:50 +03:00
|
|
|
// whitelisted set of keys to pass to the frontend
|
|
|
|
const {
|
|
|
|
type, // all widgets
|
2022-09-29 21:15:25 -07:00
|
|
|
fields,
|
2023-04-30 19:09:37 -04:00
|
|
|
hideErrors,
|
2022-09-12 12:38:50 +03:00
|
|
|
server, // docker widget
|
|
|
|
container,
|
|
|
|
currency, // coinmarketcap widget
|
|
|
|
symbols,
|
2023-07-13 21:39:45 -07:00
|
|
|
slugs,
|
2022-12-08 16:03:29 -06:00
|
|
|
defaultinterval,
|
2023-02-03 01:12:52 -08:00
|
|
|
site, // unifi widget
|
2022-10-24 17:03:35 -05:00
|
|
|
namespace, // kubernetes widget
|
2022-12-08 16:03:29 -06:00
|
|
|
app,
|
2023-01-22 09:03:56 -08:00
|
|
|
podSelector,
|
2023-05-14 06:44:33 +10:00
|
|
|
wan, // opnsense widget, pfsense widget
|
2023-03-12 16:50:28 -07:00
|
|
|
enableBlocks, // emby/jellyfin
|
2023-04-01 01:10:00 +02:00
|
|
|
enableNowPlaying,
|
2023-06-06 01:14:10 +02:00
|
|
|
volume, // diskstation widget,
|
|
|
|
enableQueue, // sonarr/radarr
|
2023-07-13 18:39:11 +02:00
|
|
|
node, // Proxmox
|
2023-07-24 12:28:03 +07:00
|
|
|
snapshotHost, // kopia
|
|
|
|
snapshotPath,
|
2023-07-30 08:17:30 +03:00
|
|
|
userEmail, // azuredevops
|
2023-08-01 03:54:19 +03:00
|
|
|
repositoryId,
|
|
|
|
metric, // glances
|
2023-09-06 13:53:39 +03:00
|
|
|
chart, // glances
|
2023-08-06 14:21:01 +03:00
|
|
|
stream, // mjpeg
|
2023-08-06 07:33:08 -07:00
|
|
|
fit,
|
2023-08-12 14:44:54 +07:00
|
|
|
method, // openmediavault widget
|
2023-08-26 07:39:15 +01:00
|
|
|
mappings, // customapi widget
|
|
|
|
refreshInterval,
|
2023-09-28 19:23:44 +01:00
|
|
|
integrations, // calendar widget
|
2023-09-28 23:24:37 +01:00
|
|
|
firstDayInWeek,
|
2022-09-12 12:38:50 +03:00
|
|
|
} = cleanedService.widget;
|
2022-09-09 21:53:05 +03:00
|
|
|
|
2023-06-28 08:47:55 -07:00
|
|
|
let fieldsList = fields;
|
2023-09-06 13:53:39 +03:00
|
|
|
if (typeof fields === "string") {
|
|
|
|
try {
|
|
|
|
JSON.parse(fields);
|
|
|
|
} catch (e) {
|
2023-06-28 08:47:55 -07:00
|
|
|
logger.error("Invalid fields list detected in config for service '%s'", service.name);
|
|
|
|
fieldsList = null;
|
|
|
|
}
|
|
|
|
}
|
2023-07-13 18:39:11 +02:00
|
|
|
|
2022-09-09 21:53:05 +03:00
|
|
|
cleanedService.widget = {
|
|
|
|
type,
|
2023-01-24 09:31:20 -06:00
|
|
|
fields: fieldsList || null,
|
2023-04-30 19:09:37 -04:00
|
|
|
hide_errors: hideErrors || false,
|
2022-09-09 21:53:05 +03:00
|
|
|
service_name: service.name,
|
|
|
|
service_group: serviceGroup.name,
|
|
|
|
};
|
2022-09-12 06:18:51 +03:00
|
|
|
|
2023-07-30 08:17:30 +03:00
|
|
|
if (type === "azuredevops") {
|
|
|
|
if (userEmail) cleanedService.widget.userEmail = userEmail;
|
|
|
|
if (repositoryId) cleanedService.widget.repositoryId = repositoryId;
|
|
|
|
}
|
|
|
|
|
2023-07-13 21:39:45 -07:00
|
|
|
if (type === "coinmarketcap") {
|
|
|
|
if (currency) cleanedService.widget.currency = currency;
|
|
|
|
if (symbols) cleanedService.widget.symbols = symbols;
|
|
|
|
if (slugs) cleanedService.widget.slugs = slugs;
|
|
|
|
if (defaultinterval) cleanedService.widget.defaultinterval = defaultinterval;
|
|
|
|
}
|
2022-09-26 12:04:37 +03:00
|
|
|
|
2022-09-12 06:18:51 +03:00
|
|
|
if (type === "docker") {
|
2022-09-26 12:04:37 +03:00
|
|
|
if (server) cleanedService.widget.server = server;
|
|
|
|
if (container) cleanedService.widget.container = container;
|
2022-09-12 06:18:51 +03:00
|
|
|
}
|
2023-02-03 01:12:52 -08:00
|
|
|
if (type === "unifi") {
|
|
|
|
if (site) cleanedService.widget.site = site;
|
|
|
|
}
|
2023-07-13 18:39:11 +02:00
|
|
|
if (type === "proxmox") {
|
|
|
|
if (node) cleanedService.widget.node = node;
|
|
|
|
}
|
2022-10-24 17:03:35 -05:00
|
|
|
if (type === "kubernetes") {
|
|
|
|
if (namespace) cleanedService.widget.namespace = namespace;
|
|
|
|
if (app) cleanedService.widget.app = app;
|
2022-12-08 16:03:29 -06:00
|
|
|
if (podSelector) cleanedService.widget.podSelector = podSelector;
|
2022-10-24 17:03:35 -05:00
|
|
|
}
|
2023-05-14 06:44:33 +10:00
|
|
|
if (["opnsense", "pfsense"].includes(type)) {
|
2023-01-22 09:03:56 -08:00
|
|
|
if (wan) cleanedService.widget.wan = wan;
|
|
|
|
}
|
2023-05-14 06:44:33 +10:00
|
|
|
if (["emby", "jellyfin"].includes(type)) {
|
2023-04-06 20:18:06 -07:00
|
|
|
if (enableBlocks !== undefined) cleanedService.widget.enableBlocks = JSON.parse(enableBlocks);
|
|
|
|
if (enableNowPlaying !== undefined) cleanedService.widget.enableNowPlaying = JSON.parse(enableNowPlaying);
|
2023-03-12 16:50:28 -07:00
|
|
|
}
|
2023-06-06 01:14:10 +02:00
|
|
|
if (["sonarr", "radarr"].includes(type)) {
|
|
|
|
if (enableQueue !== undefined) cleanedService.widget.enableQueue = JSON.parse(enableQueue);
|
|
|
|
}
|
2023-05-22 01:13:40 +10:00
|
|
|
if (["diskstation", "qnap"].includes(type)) {
|
2023-04-01 01:10:00 +02:00
|
|
|
if (volume) cleanedService.widget.volume = volume;
|
|
|
|
}
|
2023-07-24 12:28:03 +07:00
|
|
|
if (type === "kopia") {
|
|
|
|
if (snapshotHost) cleanedService.widget.snapshotHost = snapshotHost;
|
|
|
|
if (snapshotPath) cleanedService.widget.snapshotPath = snapshotPath;
|
|
|
|
}
|
2023-08-01 03:54:19 +03:00
|
|
|
if (type === "glances") {
|
|
|
|
if (metric) cleanedService.widget.metric = metric;
|
2023-09-06 13:53:39 +03:00
|
|
|
if (chart !== undefined) {
|
|
|
|
cleanedService.widget.chart = chart;
|
|
|
|
} else {
|
|
|
|
cleanedService.widget.chart = true;
|
|
|
|
}
|
2023-08-01 03:54:19 +03:00
|
|
|
}
|
2023-08-06 14:21:01 +03:00
|
|
|
if (type === "mjpeg") {
|
|
|
|
if (stream) cleanedService.widget.stream = stream;
|
2023-08-06 07:33:08 -07:00
|
|
|
if (fit) cleanedService.widget.fit = fit;
|
2023-08-06 14:21:01 +03:00
|
|
|
}
|
2023-08-12 14:44:54 +07:00
|
|
|
if (type === "openmediavault") {
|
|
|
|
if (method) cleanedService.widget.method = method;
|
|
|
|
}
|
2023-08-26 07:39:15 +01:00
|
|
|
if (type === "customapi") {
|
|
|
|
if (mappings) cleanedService.widget.mappings = mappings;
|
|
|
|
if (refreshInterval) cleanedService.widget.refreshInterval = refreshInterval;
|
|
|
|
}
|
2023-09-28 19:23:44 +01:00
|
|
|
if (type === "calendar") {
|
|
|
|
if (integrations) cleanedService.widget.integrations = integrations;
|
2023-09-28 23:24:37 +01:00
|
|
|
if (firstDayInWeek) cleanedService.widget.firstDayInWeek = firstDayInWeek;
|
2023-09-28 19:23:44 +01:00
|
|
|
}
|
2022-09-09 21:53:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return cleanedService;
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-06-11 09:50:41 -07:00
|
|
|
export async function getServiceItem(group, service) {
|
2022-09-09 21:53:05 +03:00
|
|
|
const configuredServices = await servicesFromConfig();
|
|
|
|
|
|
|
|
const serviceGroup = configuredServices.find((g) => g.name === group);
|
2022-09-04 21:58:42 +03:00
|
|
|
if (serviceGroup) {
|
|
|
|
const serviceEntry = serviceGroup.services.find((s) => s.name === service);
|
2023-06-11 09:50:41 -07:00
|
|
|
if (serviceEntry) return serviceEntry;
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
|
2022-09-09 21:53:05 +03:00
|
|
|
const discoveredServices = await servicesFromDocker();
|
|
|
|
|
|
|
|
const dockerServiceGroup = discoveredServices.find((g) => g.name === group);
|
|
|
|
if (dockerServiceGroup) {
|
|
|
|
const dockerServiceEntry = dockerServiceGroup.services.find((s) => s.name === service);
|
2023-06-11 09:50:41 -07:00
|
|
|
if (dockerServiceEntry) return dockerServiceEntry;
|
2022-09-09 21:53:05 +03:00
|
|
|
}
|
|
|
|
|
2022-10-24 17:03:35 -05:00
|
|
|
const kubernetesServices = await servicesFromKubernetes();
|
|
|
|
const kubernetesServiceGroup = kubernetesServices.find((g) => g.name === group);
|
|
|
|
if (kubernetesServiceGroup) {
|
|
|
|
const kubernetesServiceEntry = kubernetesServiceGroup.services.find((s) => s.name === service);
|
2023-06-11 09:50:41 -07:00
|
|
|
if (kubernetesServiceEntry) return kubernetesServiceEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function getServiceWidget(group, service) {
|
|
|
|
const serviceItem = await getServiceItem(group, service);
|
|
|
|
if (serviceItem) {
|
|
|
|
const { widget } = serviceItem;
|
|
|
|
return widget;
|
2022-10-24 17:03:35 -05:00
|
|
|
}
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
return false;
|
2022-12-08 16:03:29 -06:00
|
|
|
}
|