2021-05-10 15:47:26 +00:00
|
|
|
const asyncWrapper = require('../middleware/asyncWrapper');
|
|
|
|
const ErrorResponse = require('../utils/ErrorResponse');
|
|
|
|
const App = require('../models/App');
|
2021-06-09 08:58:45 +00:00
|
|
|
const Config = require('../models/Config');
|
2021-06-15 10:36:23 +00:00
|
|
|
const { Sequelize } = require('sequelize');
|
2021-08-04 20:19:35 +00:00
|
|
|
const axios = require('axios');
|
2021-08-05 06:56:02 +00:00
|
|
|
const Logger = require('../utils/Logger');
|
|
|
|
const logger = new Logger();
|
2021-08-17 08:32:15 +00:00
|
|
|
const k8s = require('@kubernetes/client-node');
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
// @desc Create new app
|
|
|
|
// @route POST /api/apps
|
|
|
|
// @access Public
|
|
|
|
exports.createApp = asyncWrapper(async (req, res, next) => {
|
2021-06-09 08:58:45 +00:00
|
|
|
// Get config from database
|
|
|
|
const pinApps = await Config.findOne({
|
|
|
|
where: { key: 'pinAppsByDefault' }
|
|
|
|
});
|
|
|
|
|
|
|
|
let app;
|
2021-06-23 12:15:14 +00:00
|
|
|
let _body = { ...req.body };
|
|
|
|
|
|
|
|
if (req.file) {
|
|
|
|
_body.icon = req.file.filename;
|
|
|
|
}
|
|
|
|
|
2021-06-09 08:58:45 +00:00
|
|
|
if (pinApps) {
|
|
|
|
if (parseInt(pinApps.value)) {
|
|
|
|
app = await App.create({
|
2021-06-23 12:15:14 +00:00
|
|
|
..._body,
|
2021-06-09 08:58:45 +00:00
|
|
|
isPinned: true
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
2021-06-09 08:58:45 +00:00
|
|
|
} else {
|
|
|
|
app = await App.create(req.body);
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
success: true,
|
|
|
|
data: app
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
// @desc Get all apps
|
|
|
|
// @route GET /api/apps
|
|
|
|
// @access Public
|
|
|
|
exports.getApps = asyncWrapper(async (req, res, next) => {
|
2021-06-17 08:56:27 +00:00
|
|
|
// Get config from database
|
|
|
|
const useOrdering = await Config.findOne({
|
|
|
|
where: { key: 'useOrdering' }
|
2021-05-13 16:23:12 +00:00
|
|
|
});
|
2021-08-04 20:19:35 +00:00
|
|
|
const useDockerApi = await Config.findOne({
|
|
|
|
where: { key: 'dockerApps' }
|
|
|
|
});
|
2021-08-17 08:32:15 +00:00
|
|
|
const useKubernetesApi = await Config.findOne({
|
|
|
|
where: { key: 'kubernetesApps' }
|
|
|
|
});
|
2021-08-04 20:19:35 +00:00
|
|
|
const unpinStoppedApps = await Config.findOne({
|
|
|
|
where: { key: 'unpinStoppedApps' }
|
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
2021-06-17 08:56:27 +00:00
|
|
|
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
|
|
|
let apps;
|
|
|
|
|
2021-08-06 08:36:05 +00:00
|
|
|
if (useDockerApi && useDockerApi.value == 1) {
|
2021-08-05 06:56:02 +00:00
|
|
|
let containers = null;
|
|
|
|
|
|
|
|
try {
|
2021-08-06 08:36:05 +00:00
|
|
|
let { data } = await axios.get(
|
|
|
|
'http://localhost/containers/json?{"status":["running"]}',
|
|
|
|
{
|
|
|
|
socketPath: '/var/run/docker.sock'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
containers = data;
|
|
|
|
} catch {
|
|
|
|
logger.log("Can't connect to the docker socket", 'ERROR');
|
|
|
|
}
|
2021-08-04 20:19:35 +00:00
|
|
|
|
|
|
|
if (containers) {
|
|
|
|
apps = await App.findAll({
|
2021-08-06 08:36:05 +00:00
|
|
|
order: [[orderType, 'ASC']]
|
2021-08-04 20:19:35 +00:00
|
|
|
});
|
|
|
|
|
2021-08-06 08:36:05 +00:00
|
|
|
containers = containers.filter(e => Object.keys(e.Labels).length !== 0);
|
2021-08-04 20:19:35 +00:00
|
|
|
const dockerApps = [];
|
|
|
|
for (const container of containers) {
|
|
|
|
const labels = container.Labels;
|
|
|
|
|
2021-08-06 08:36:05 +00:00
|
|
|
if (
|
|
|
|
'flame.name' in labels &&
|
|
|
|
'flame.url' in labels &&
|
|
|
|
/^app/.test(labels['flame.type'])
|
|
|
|
) {
|
2021-08-04 20:19:35 +00:00
|
|
|
dockerApps.push({
|
|
|
|
name: labels['flame.name'],
|
|
|
|
url: labels['flame.url'],
|
|
|
|
icon: labels['flame.icon'] || 'docker'
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
2021-08-04 20:19:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 08:36:05 +00:00
|
|
|
if (unpinStoppedApps && unpinStoppedApps.value == 1) {
|
2021-08-04 20:19:35 +00:00
|
|
|
for (const app of apps) {
|
|
|
|
await app.update({ isPinned: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const item of dockerApps) {
|
|
|
|
if (apps.some(app => app.name === item.name)) {
|
|
|
|
const app = apps.filter(e => e.name === item.name)[0];
|
2021-08-06 08:36:05 +00:00
|
|
|
await app.update({ ...item, isPinned: true });
|
2021-08-04 20:19:35 +00:00
|
|
|
} else {
|
|
|
|
await App.create({
|
|
|
|
...item,
|
|
|
|
isPinned: true
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
2021-08-04 20:19:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 08:32:15 +00:00
|
|
|
if (useKubernetesApi && useKubernetesApi.value == 1) {
|
|
|
|
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'])
|
|
|
|
) {
|
|
|
|
kubernetesApps.push({
|
|
|
|
name: annotations['flame.pawelmalak/name'],
|
|
|
|
url: annotations['flame.pawelmalak/url'],
|
|
|
|
icon: annotations['flame.pawelmalak/icon'] || 'kubernetes'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unpinStoppedApps && unpinStoppedApps.value == 1) {
|
|
|
|
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.filter(e => e.name === item.name)[0];
|
|
|
|
await app.update({ ...item, isPinned: true });
|
|
|
|
} else {
|
|
|
|
await App.create({
|
|
|
|
...item,
|
|
|
|
isPinned: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 08:56:27 +00:00
|
|
|
if (orderType == 'name') {
|
|
|
|
apps = await App.findAll({
|
2021-08-06 08:36:05 +00:00
|
|
|
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
2021-06-17 08:56:27 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
apps = await App.findAll({
|
2021-08-06 08:36:05 +00:00
|
|
|
order: [[orderType, 'ASC']]
|
2021-06-17 08:56:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:15:54 +00:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
// Set header to fetch containers info every time
|
|
|
|
res.status(200).setHeader('Cache-Control', 'no-store').json({
|
|
|
|
success: true,
|
|
|
|
data: apps
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json({
|
2021-05-10 15:47:26 +00:00
|
|
|
success: true,
|
|
|
|
data: apps
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
// @desc Get single app
|
|
|
|
// @route GET /api/apps/:id
|
|
|
|
// @access Public
|
|
|
|
exports.getApp = asyncWrapper(async (req, res, next) => {
|
|
|
|
const app = await App.findOne({
|
|
|
|
where: { id: req.params.id }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!app) {
|
2021-08-06 08:36:05 +00:00
|
|
|
return next(
|
|
|
|
new ErrorResponse(`App with id of ${req.params.id} was not found`, 404)
|
|
|
|
);
|
2021-05-10 15:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: app
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
// @desc Update app
|
|
|
|
// @route PUT /api/apps/:id
|
|
|
|
// @access Public
|
|
|
|
exports.updateApp = asyncWrapper(async (req, res, next) => {
|
|
|
|
let app = await App.findOne({
|
|
|
|
where: { id: req.params.id }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!app) {
|
2021-08-06 08:36:05 +00:00
|
|
|
return next(
|
|
|
|
new ErrorResponse(`App with id of ${req.params.id} was not found`, 404)
|
|
|
|
);
|
2021-05-10 15:47:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 09:36:48 +00:00
|
|
|
let _body = { ...req.body };
|
|
|
|
|
|
|
|
if (req.file) {
|
|
|
|
_body.icon = req.file.filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
app = await app.update(_body);
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: app
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
// @desc Delete app
|
|
|
|
// @route DELETE /api/apps/:id
|
|
|
|
// @access Public
|
|
|
|
exports.deleteApp = asyncWrapper(async (req, res, next) => {
|
|
|
|
await App.destroy({
|
|
|
|
where: { id: req.params.id }
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
2021-06-15 14:02:57 +00:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: {}
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
|
|
|
});
|
2021-06-15 14:02:57 +00:00
|
|
|
|
|
|
|
// @desc Reorder apps
|
|
|
|
// @route PUT /api/apps/0/reorder
|
|
|
|
// @access Public
|
|
|
|
exports.reorderApps = asyncWrapper(async (req, res, next) => {
|
|
|
|
req.body.apps.forEach(async ({ id, orderId }) => {
|
2021-08-06 08:36:05 +00:00
|
|
|
await App.update(
|
|
|
|
{ orderId },
|
|
|
|
{
|
|
|
|
where: { id }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2021-06-15 14:02:57 +00:00
|
|
|
|
2021-05-10 15:47:26 +00:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: {}
|
2021-08-06 08:36:05 +00:00
|
|
|
});
|
|
|
|
});
|