implement docker service discovery via labels

pull/113/head
Ben Phelps 2 years ago
parent ef1c5dbcc9
commit d66326b41d

@ -28,6 +28,7 @@
"react-i18next": "^11.18.5",
"react-icons": "^4.4.0",
"rutorrent-promise": "^2.0.0",
"shvl": "^3.0.0",
"swr": "^1.3.0"
},
"devDependencies": {

@ -32,6 +32,7 @@ specifiers:
react-i18next: ^11.18.5
react-icons: ^4.4.0
rutorrent-promise: ^2.0.0
shvl: ^3.0.0
swr: ^1.3.0
tailwindcss: ^3.1.8
typescript: ^4.8.2
@ -56,6 +57,7 @@ dependencies:
react-i18next: 11.18.5_4sidbwfhen5r7txudrvpua6nty
react-icons: 4.4.0_react@18.2.0
rutorrent-promise: 2.0.0
shvl: 3.0.0
swr: 1.3.0_react@18.2.0
devDependencies:
@ -2354,6 +2356,10 @@ packages:
engines: {node: '>=8'}
dev: true
/shvl/3.0.0:
resolution: {integrity: sha512-5IomAM3ykE/g9K9L6lhODc+TpCuN03rrhlboegeKyyfh66DDdpRD5JN37DYhNHH+RaYjiIDx64K/Ms/xQYOR5w==}
dev: false
/side-channel/1.0.4:
resolution: {integrity: sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==}
dependencies:

@ -14,7 +14,7 @@ export default async function handler(req, res) {
}
try {
const docker = new Docker(await getDockerArguments(containerServer));
const docker = new Docker(getDockerArguments(containerServer));
const containers = await docker.listContainers({
all: true,
});

@ -13,7 +13,7 @@ export default async function handler(req, res) {
}
try {
const docker = new Docker(await getDockerArguments(containerServer));
const docker = new Docker(getDockerArguments(containerServer));
const containers = await docker.listContainers({
all: true,
});

@ -1,40 +1,26 @@
import { promises as fs } from "fs";
import path from "path";
import yaml from "js-yaml";
import checkAndCopyConfig from "utils/config";
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/service-helpers";
export default async function handler(req, res) {
checkAndCopyConfig("services.yaml");
const discoveredServices = cleanServiceGroups(await servicesFromDocker());
const configuredServices = cleanServiceGroups(await servicesFromConfig());
const servicesYaml = path.join(process.cwd(), "config", "services.yaml");
const fileContents = await fs.readFile(servicesYaml, "utf8");
const services = yaml.load(fileContents);
const mergedGroupsNames = [
...new Set([discoveredServices.map((group) => group.name), configuredServices.map((group) => group.name)].flat()),
];
// map easy to write YAML objects into easy to consume JS arrays
const servicesArray = services.map((group) => ({
name: Object.keys(group)[0],
services: group[Object.keys(group)[0]].map((entries) => {
const { widget, ...service } = entries[Object.keys(entries)[0]];
const result = {
name: Object.keys(entries)[0],
...service,
};
const mergedGroups = [];
if (widget) {
const { type } = widget;
mergedGroupsNames.forEach((groupName) => {
const discoveredGroup = discoveredServices.find((group) => group.name === groupName) || { services: [] };
const configuredGroup = configuredServices.find((group) => group.name === groupName) || { services: [] };
result.widget = {
type,
service_group: Object.keys(group)[0],
service_name: Object.keys(entries)[0],
};
}
const mergedGroup = {
name: groupName,
services: [...discoveredGroup.services, ...configuredGroup.services].filter((service) => service),
};
return result;
}),
}));
mergedGroups.push(mergedGroup);
});
res.send(servicesArray);
res.send(mergedGroups);
}

@ -1,15 +1,15 @@
import path from "path";
import { promises as fs } from "fs";
import { readFileSync } from "fs";
import yaml from "js-yaml";
import checkAndCopyConfig from "utils/config";
export default async function getDockerArguments(server) {
export default function getDockerArguments(server) {
checkAndCopyConfig("docker.yaml");
const configFile = path.join(process.cwd(), "config", "docker.yaml");
const configData = await fs.readFile(configFile, "utf8");
const configData = readFileSync(configFile, "utf8");
const servers = yaml.load(configData);
if (!server) {

@ -11,7 +11,7 @@ i18n
.init({
fallbackLng: "en",
ns: ["common"],
debug: process.env.NODE_ENV === "development",
// debug: process.env.NODE_ENV === "development",
defaultNS: "common",
nonExplicitSupportedLngs: true,
interpolation: {

@ -2,8 +2,15 @@ import { promises as fs } from "fs";
import path from "path";
import yaml from "js-yaml";
import Docker from "dockerode";
import * as shvl from "shvl";
import checkAndCopyConfig from "utils/config";
import getDockerArguments from "utils/docker";
export async function servicesFromConfig() {
checkAndCopyConfig("services.yaml");
export default async function getServiceWidget(group, service) {
const servicesYaml = path.join(process.cwd(), "config", "services.yaml");
const fileContents = await fs.readFile(servicesYaml, "utf8");
const services = yaml.load(fileContents);
@ -17,7 +24,102 @@ export default async function getServiceWidget(group, service) {
})),
}));
const serviceGroup = servicesArray.find((g) => g.name === group);
return servicesArray;
}
export async function servicesFromDocker() {
checkAndCopyConfig("docker.yaml");
const dockerYaml = path.join(process.cwd(), "config", "docker.yaml");
const dockerFileContents = await fs.readFile(dockerYaml, "utf8");
const servers = yaml.load(dockerFileContents);
const serviceServers = await Promise.all(
Object.keys(servers).map(async (serverName) => {
const docker = new Docker(getDockerArguments(serverName));
const containers = await docker.listContainers({
all: true,
});
// 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;
Object.keys(container.Labels).forEach((label) => {
if (label.startsWith("homepage")) {
if (!constructedService) {
constructedService = {
container: container.Names[0].replace(/^\//, ""),
server: serverName,
};
}
shvl.set(constructedService, label.replace("homepage.", ""), container.Labels[label]);
}
});
return constructedService;
});
return { server: serverName, services: discovered.filter((filteredService) => filteredService) };
})
);
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;
}
export function cleanServiceGroups(groups) {
return groups.map((serviceGroup) => ({
name: serviceGroup.name,
services: serviceGroup.services.map((service) => {
const cleanedService = { ...service };
if (cleanedService.widget) {
const { type } = cleanedService.widget;
cleanedService.widget = {
type,
service_name: service.name,
service_group: serviceGroup.name,
};
}
return cleanedService;
}),
}));
}
export default async function getServiceWidget(group, service) {
const configuredServices = await servicesFromConfig();
const serviceGroup = configuredServices.find((g) => g.name === group);
if (serviceGroup) {
const serviceEntry = serviceGroup.services.find((s) => s.name === service);
if (serviceEntry) {
@ -26,5 +128,16 @@ export default async function getServiceWidget(group, service) {
}
}
const discoveredServices = await servicesFromDocker();
const dockerServiceGroup = discoveredServices.find((g) => g.name === group);
if (dockerServiceGroup) {
const dockerServiceEntry = dockerServiceGroup.services.find((s) => s.name === service);
if (dockerServiceEntry) {
const { widget } = dockerServiceEntry;
return widget;
}
}
return false;
}

Loading…
Cancel
Save