2021-03-24 22:11:14 +01:00
|
|
|
const { docker } = require('../../../libs/docker')
|
|
|
|
const Deployment = require('../../../models/Deployment')
|
|
|
|
const ServerLog = require('../../../models/Logs/Server')
|
2021-04-19 09:46:05 +02:00
|
|
|
const { saveServerLog } = require('../../../libs/logging')
|
2021-03-24 22:11:14 +01:00
|
|
|
|
|
|
|
module.exports = async function (fastify) {
|
|
|
|
fastify.get('/', async (request, reply) => {
|
2021-04-02 15:05:23 +02:00
|
|
|
try {
|
|
|
|
const latestDeployments = await Deployment.aggregate([
|
2021-03-24 22:11:14 +01:00
|
|
|
{
|
2021-04-02 15:05:23 +02:00
|
|
|
$sort: { createdAt: -1 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$group:
|
|
|
|
{
|
|
|
|
_id: {
|
|
|
|
repoId: '$repoId',
|
|
|
|
branch: '$branch'
|
|
|
|
},
|
|
|
|
createdAt: { $last: '$createdAt' },
|
|
|
|
progress: { $first: '$progress' }
|
|
|
|
}
|
2021-03-24 22:11:14 +01:00
|
|
|
}
|
2021-04-02 15:05:23 +02:00
|
|
|
])
|
|
|
|
const serverLogs = await ServerLog.find()
|
2021-04-22 23:48:29 +02:00
|
|
|
const dockerServices = await docker.engine.listServices()
|
|
|
|
let applications = dockerServices.filter(r => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application' && r.Spec.Labels.configuration)
|
|
|
|
let databases = dockerServices.filter(r => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'database' && r.Spec.Labels.configuration)
|
|
|
|
let services = dockerServices.filter(r => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'service' && r.Spec.Labels.configuration)
|
2021-04-02 15:05:23 +02:00
|
|
|
applications = applications.map(r => {
|
|
|
|
if (JSON.parse(r.Spec.Labels.configuration)) {
|
|
|
|
const configuration = JSON.parse(r.Spec.Labels.configuration)
|
|
|
|
const status = latestDeployments.find(l => configuration.repository.id === l._id.repoId && configuration.repository.branch === l._id.branch)
|
|
|
|
if (status && status.progress) r.progress = status.progress
|
|
|
|
r.Spec.Labels.configuration = configuration
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return {}
|
|
|
|
})
|
|
|
|
databases = databases.map(r => {
|
|
|
|
const configuration = r.Spec.Labels.configuration ? JSON.parse(r.Spec.Labels.configuration) : null
|
2021-03-24 22:11:14 +01:00
|
|
|
r.Spec.Labels.configuration = configuration
|
|
|
|
return r
|
2021-04-02 15:05:23 +02:00
|
|
|
})
|
2021-04-22 23:48:29 +02:00
|
|
|
services = services.map(r => {
|
|
|
|
const configuration = r.Spec.Labels.configuration ? JSON.parse(r.Spec.Labels.configuration) : null
|
|
|
|
r.Spec.Labels.configuration = configuration
|
|
|
|
return r
|
|
|
|
})
|
2021-04-15 22:40:44 +02:00
|
|
|
applications = [...new Map(applications.map(item => [item.Spec.Labels.configuration.publish.domain + item.Spec.Labels.configuration.publish.path, item])).values()]
|
2021-04-02 15:05:23 +02:00
|
|
|
return {
|
|
|
|
serverLogs,
|
|
|
|
applications: {
|
|
|
|
deployed: applications
|
|
|
|
},
|
|
|
|
databases: {
|
|
|
|
deployed: databases
|
2021-04-22 23:48:29 +02:00
|
|
|
},
|
|
|
|
services: {
|
|
|
|
deployed: services
|
2021-04-02 15:05:23 +02:00
|
|
|
}
|
2021-03-24 22:11:14 +01:00
|
|
|
}
|
2021-04-02 15:05:23 +02:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT' && error.errno === -2) {
|
|
|
|
throw new Error(`Docker service unavailable at ${error.address}.`)
|
2021-04-15 22:40:44 +02:00
|
|
|
} else {
|
2021-04-19 09:46:05 +02:00
|
|
|
await saveServerLog(error)
|
|
|
|
throw new Error(error)
|
2021-03-24 22:11:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|