123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- const App = require('../../../models/App');
- const axios = require('axios');
- const Logger = require('../../../utils/Logger');
- const logger = new Logger();
- const loadConfig = require('../../../utils/loadConfig');
- const useDocker = async (apps) => {
- const {
- useOrdering: orderType,
- unpinStoppedApps,
- dockerHost: host,
- } = await loadConfig();
- let containers = null;
- // Get list of containers
- try {
- if (host.includes('localhost')) {
- // Use default host
- let { data } = await axios.get(
- `http://${host}/containers/json?{"status":["running"]}`,
- {
- socketPath: '/var/run/docker.sock',
- }
- );
- containers = data;
- } else {
- // Use custom host
- let { data } = await axios.get(
- `http://${host}/containers/json?{"status":["running"]}`
- );
- containers = data;
- }
- } catch {
- logger.log(`Can't connect to the Docker API on ${host}`, 'ERROR');
- }
- if (containers) {
- apps = await App.findAll({
- order: [[orderType, 'ASC']],
- });
- // Filter out containers without any annotations
- containers = containers.filter((e) => Object.keys(e.Labels).length !== 0);
- const dockerApps = [];
- for (const container of containers) {
- let labels = container.Labels;
- // Traefik labels for URL configuration
- if (!('flame.url' in labels)) {
- for (const label of Object.keys(labels)) {
- if (/^traefik.*.frontend.rule/.test(label)) {
- // Traefik 1.x
- let value = labels[label];
- if (value.indexOf('Host') !== -1) {
- value = value.split('Host:')[1];
- labels['flame.url'] =
- 'https://' + value.split(',').join(';https://');
- }
- } else if (/^traefik.*?\.rule/.test(label)) {
- // Traefik 2.x
- const value = labels[label];
- if (value.indexOf('Host') !== -1) {
- const regex = /\`([a-zA-Z0-9\.\-]+)\`/g;
- const domains = [];
- while ((match = regex.exec(value)) != null) {
- domains.push('http://' + match[1]);
- }
- if (domains.length > 0) {
- labels['flame.url'] = domains.join(';');
- }
- }
- }
- }
- }
- // add each container as flame formatted app
- if (
- 'flame.name' in labels &&
- 'flame.url' in labels &&
- /^app/.test(labels['flame.type'])
- ) {
- for (let i = 0; i < labels['flame.name'].split(';').length; i++) {
- const names = labels['flame.name'].split(';');
- const urls = labels['flame.url'].split(';');
- let icons = '';
- let descriptions = '';
- let visibility = '';
- if ('flame.icon' in labels) {
- icons = labels['flame.icon'].split(';');
- }
- if ('flame.description' in labels) {
- descriptions = labels['flame.description'].split(';');
- }
- if ('flame.visible' in labels) {
- visibility = labels['flame.visible'].split(';');
- }
- dockerApps.push({
- name: names[i] || names[0],
- url: urls[i] || urls[0],
- icon: icons[i] || 'docker',
- // Add description and visbility
- description: descriptions[i] || '',
- isPublic: (visibility[i] && /^true$/.test(visibility[i]) ? visibility[i] : visibility[0]) || null,
- });
- }
- }
- }
- if (unpinStoppedApps) {
- for (const app of apps) {
- await app.update({ isPinned: false });
- }
- }
- for (const item of dockerApps) {
- // If app already exists, update it
- // Find by name or url
- if (apps.some((app) => app.name === item.name || app.url === item.url)) {
- const app = apps.find((a) => a.name === item.name || app.url === item.url);
- if (
- item.icon === 'custom' ||
- (item.icon === 'docker' && app.icon != 'docker')
- ) {
- // update without overriding icon
- await app.update({
- name: item.name,
- url: item.url,
- // Add description and visbility
- description: item.description,
- isPublic: item.isPublic,
- isPinned: true,
- });
- } else {
- await app.update({
- ...item,
- isPinned: true,
- });
- }
- } else {
- // else create new app
- await App.create({
- ...item,
- icon: item.icon === 'custom' ? 'docker' : item.icon,
- isPinned: true,
- });
- }
- }
- }
- };
- module.exports = useDocker;
|