mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-24 13:39:35 +02:00
Merge branch 'master' of https://github.com/pawelmalak/flame into merge_upstream_2020-12-06
This commit is contained in:
commit
021bd4e85a
266 changed files with 13470 additions and 7067 deletions
33
controllers/apps/createApp.js
Normal file
33
controllers/apps/createApp.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Create new app
|
||||
// @route POST /api/apps
|
||||
// @access Public
|
||||
const createApp = asyncWrapper(async (req, res, next) => {
|
||||
const { pinAppsByDefault } = await loadConfig();
|
||||
|
||||
let body = { ...req.body };
|
||||
|
||||
if (body.icon) {
|
||||
body.icon = body.icon.trim();
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
const app = await App.create({
|
||||
...body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
isPinned: pinAppsByDefault,
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = createApp;
|
18
controllers/apps/deleteApp.js
Normal file
18
controllers/apps/deleteApp.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
|
||||
// @desc Delete app
|
||||
// @route DELETE /api/apps/:id
|
||||
// @access Public
|
||||
const deleteApp = asyncWrapper(async (req, res, next) => {
|
||||
await App.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = deleteApp;
|
4
controllers/apps/docker/index.js
Normal file
4
controllers/apps/docker/index.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
useKubernetes: require('./useKubernetes'),
|
||||
useDocker: require('./useDocker'),
|
||||
};
|
176
controllers/apps/docker/useDocker.js
Normal file
176
controllers/apps/docker/useDocker.js
Normal file
|
@ -0,0 +1,176 @@
|
|||
const App = require('../../../models/App');
|
||||
const axios = require('axios');
|
||||
const Logger = require('../../../utils/Logger');
|
||||
const logger = new Logger();
|
||||
const loadConfig = require('../../../utils/loadConfig');
|
||||
|
||||
dockerDefaultCategory = {
|
||||
id: -2,
|
||||
name: 'Docker',
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: 998,
|
||||
};
|
||||
|
||||
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'])
|
||||
) {
|
||||
const names = labels['flame.name'].split(';');
|
||||
const urls = labels['flame.url'].split(';');
|
||||
const categoriesLabels = labels['flame.category'] ? labels['flame.category'].split(';') : [];
|
||||
const orders = labels['flame.order'] ? labels['flame.order'].split(';') : [];
|
||||
const icons = labels['flame.icon'] ? labels['flame.icon'].split(';') : [];
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory;
|
||||
if (!category) {
|
||||
category = await createNewCategory(categoriesLabels[i]);
|
||||
if (category) {
|
||||
categories.push(category);
|
||||
} else {
|
||||
category = dockerDefaultCategory;
|
||||
}
|
||||
}
|
||||
|
||||
dockerApps.push({
|
||||
name: names[i] || names[0],
|
||||
url: urls[i] || urls[0],
|
||||
icon: icons[i] || 'docker',
|
||||
category: category.id,
|
||||
orderId: orders[i] || 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unpinStoppedApps) {
|
||||
for (const app of apps) {
|
||||
await app.update({ isPinned: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of dockerApps) {
|
||||
// If app already exists, update it
|
||||
if (apps.some((app) => app.name === item.name)) {
|
||||
const app = apps.find((a) => a.name === item.name);
|
||||
|
||||
if (
|
||||
item.icon === 'custom' ||
|
||||
(item.icon === 'docker' && app.icon != 'docker')
|
||||
) {
|
||||
// update without overriding icon
|
||||
await app.update({
|
||||
name: item.name,
|
||||
url: item.url,
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// TODO : Move somewhere else ?
|
||||
async function createNewCategory(newCategoryName) {
|
||||
return await Category.create({
|
||||
name: newCategoryName,
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = useDocker;
|
115
controllers/apps/docker/useKubernetes.js
Normal file
115
controllers/apps/docker/useKubernetes.js
Normal file
|
@ -0,0 +1,115 @@
|
|||
const App = require('../../../models/App');
|
||||
const k8s = require('@kubernetes/client-node');
|
||||
const Logger = require('../../../utils/Logger');
|
||||
const logger = new Logger();
|
||||
const loadConfig = require('../../../utils/loadConfig');
|
||||
|
||||
const kubernetesDefaultCategory = {
|
||||
id: -3,
|
||||
name: 'Kubernetes',
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: 999,
|
||||
};
|
||||
|
||||
const useKubernetes = async (apps) => {
|
||||
const { useOrdering: orderType, unpinStoppedApps } = await loadConfig();
|
||||
|
||||
let ingresses = null;
|
||||
|
||||
try {
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.loadFromCluster();
|
||||
const k8sNetworkingV1Api = kc.makeApiClient(k8s.NetworkingV1Api);
|
||||
await k8sNetworkingV1Api.listIngressForAllNamespaces().then((res) => {
|
||||
ingresses = res.body.items;
|
||||
});
|
||||
} catch {
|
||||
logger.log("Can't connect to the Kubernetes API", 'ERROR');
|
||||
}
|
||||
|
||||
if (ingresses) {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']],
|
||||
});
|
||||
|
||||
ingresses = ingresses.filter(
|
||||
(e) => Object.keys(e.metadata.annotations).length !== 0
|
||||
);
|
||||
|
||||
const kubernetesApps = [];
|
||||
|
||||
for (const ingress of ingresses) {
|
||||
const annotations = ingress.metadata.annotations;
|
||||
|
||||
if (
|
||||
'flame.pawelmalak/name' in annotations &&
|
||||
'flame.pawelmalak/url' in annotations &&
|
||||
/^app/.test(annotations['flame.pawelmalak/type'])
|
||||
) {
|
||||
|
||||
const names = annotations['flame.pawelmalak/.name'].split(';');
|
||||
const urls = annotations['flame.pawelmalak/url'].split(';');
|
||||
const categoriesLabels = annotations['flame.pawelmalak/category'] ? annotations['flame.pawelmalak/category'].split(';') : [];
|
||||
const orders = annotations['flame.pawelmalak/order'] ? annotations['flame.pawelmalak/order'].split(';') : [];
|
||||
const icons = annotations['flame.pawelmalak/icon'] ? annotations['flame.pawelmalak/icon'].split(';') : [];
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory;
|
||||
if (!category) {
|
||||
category = await createNewCategory(categoriesLabels[i]);
|
||||
if (category) {
|
||||
categories.push(category);
|
||||
} else {
|
||||
category = kubernetesDefaultCategory;
|
||||
}
|
||||
}
|
||||
|
||||
kubernetesApps.push({
|
||||
name: names[i] || names[0],
|
||||
url: urls[i] || urls[0],
|
||||
icon: icons[i] || 'kubernetes',
|
||||
category: category.id,
|
||||
orderId: orders[i] || 500,
|
||||
});
|
||||
}
|
||||
|
||||
kubernetesApps.push({
|
||||
name: annotations['flame.pawelmalak/name'],
|
||||
url: annotations['flame.pawelmalak/url'],
|
||||
icon: annotations['flame.pawelmalak/icon'] || 'kubernetes',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (unpinStoppedApps) {
|
||||
for (const app of apps) {
|
||||
await app.update({ isPinned: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of kubernetesApps) {
|
||||
if (apps.some((app) => app.name === item.name)) {
|
||||
const app = apps.find((a) => a.name === item.name);
|
||||
await app.update({ ...item, isPinned: true });
|
||||
} else {
|
||||
await App.create({
|
||||
...item,
|
||||
isPinned: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// TODO : Move somewhere else ?
|
||||
async function createNewCategory(newCategoryName) {
|
||||
return await Category.create({
|
||||
name: newCategoryName,
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = useKubernetes;
|
55
controllers/apps/getAllApps.js
Normal file
55
controllers/apps/getAllApps.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
const { useKubernetes, useDocker } = require('./docker');
|
||||
|
||||
// @desc Get all apps
|
||||
// @route GET /api/apps
|
||||
// @access Public
|
||||
const getAllApps = asyncWrapper(async (req, res, next) => {
|
||||
const {
|
||||
useOrdering: orderType,
|
||||
dockerApps: useDockerAPI,
|
||||
kubernetesApps: useKubernetesAPI,
|
||||
} = await loadConfig();
|
||||
|
||||
let apps;
|
||||
|
||||
if (useDockerAPI) {
|
||||
await useDocker(apps);
|
||||
}
|
||||
|
||||
if (useKubernetesAPI) {
|
||||
await useKubernetes(apps);
|
||||
}
|
||||
|
||||
// apps visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||
: [[orderType, 'ASC']];
|
||||
|
||||
apps = await App.findAll({
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
// Set header to fetch containers info every time
|
||||
return res.status(200).setHeader('Cache-Control', 'no-store').json({
|
||||
success: true,
|
||||
data: apps,
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: apps,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getAllApps;
|
30
controllers/apps/getSingleApp.js
Normal file
30
controllers/apps/getSingleApp.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
|
||||
// @desc Get single app
|
||||
// @route GET /api/apps/:id
|
||||
// @access Public
|
||||
const getSingleApp = asyncWrapper(async (req, res, next) => {
|
||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const app = await App.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`App with the id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getSingleApp;
|
8
controllers/apps/index.js
Normal file
8
controllers/apps/index.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
createApp: require('./createApp'),
|
||||
getSingleApp: require('./getSingleApp'),
|
||||
deleteApp: require('./deleteApp'),
|
||||
updateApp: require('./updateApp'),
|
||||
reorderApps: require('./reorderApps'),
|
||||
getAllApps: require('./getAllApps'),
|
||||
};
|
23
controllers/apps/reorderApps.js
Normal file
23
controllers/apps/reorderApps.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
|
||||
// @desc Reorder apps
|
||||
// @route PUT /api/apps/0/reorder
|
||||
// @access Public
|
||||
const reorderApps = asyncWrapper(async (req, res, next) => {
|
||||
req.body.apps.forEach(async ({ id, orderId }) => {
|
||||
await App.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = reorderApps;
|
39
controllers/apps/updateApp.js
Normal file
39
controllers/apps/updateApp.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
|
||||
// @desc Update app
|
||||
// @route PUT /api/apps/:id
|
||||
// @access Public
|
||||
const updateApp = asyncWrapper(async (req, res, next) => {
|
||||
let app = await App.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`App with the id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let body = { ...req.body };
|
||||
|
||||
if (body.icon) {
|
||||
body.icon = body.icon.trim();
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
app = await app.update(body);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateApp;
|
Loading…
Add table
Add a link
Reference in a new issue