fixes and remote tempaltes

This commit is contained in:
Andras Bacsai 2022-10-24 14:23:34 +02:00
parent 8bbe771f5b
commit 88f1c36929
4 changed files with 46 additions and 1207 deletions

View File

@ -1,3 +1,5 @@
// Convert caprover format to coolify format
import fs from 'fs/promises';
import yaml from 'js-yaml';
const templateYml = await fs.readFile('./caprover.yml', 'utf8')

View File

@ -106,32 +106,44 @@ const host = '0.0.0.0';
});
fastify.register(cookie)
fastify.register(cors);
fastify.addHook('onRequest', async (request, reply) => {
let allowedList = ['coolify:3000'];
const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({})
// To detect allowed origins
// fastify.addHook('onRequest', async (request, reply) => {
// let allowedList = ['coolify:3000'];
// const { ipv4, ipv6, fqdn } = await prisma.setting.findFirst({})
// ipv4 && allowedList.push(`${ipv4}:3000`);
// ipv6 && allowedList.push(ipv6);
// fqdn && allowedList.push(getDomain(fqdn));
// isDev && allowedList.push('localhost:3000') && allowedList.push('localhost:3001') && allowedList.push('host.docker.internal:3001');
// const remotes = await prisma.destinationDocker.findMany({ where: { remoteEngine: true, remoteVerified: true } })
// if (remotes.length > 0) {
// remotes.forEach(remote => {
// allowedList.push(`${remote.remoteIpAddress}:3000`);
// })
// }
// if (!allowedList.includes(request.headers.host)) {
// // console.log('not allowed', request.headers.host)
// }
// })
ipv4 && allowedList.push(`${ipv4}:3000`);
ipv6 && allowedList.push(ipv6);
fqdn && allowedList.push(getDomain(fqdn));
isDev && allowedList.push('localhost:3000') && allowedList.push('localhost:3001') && allowedList.push('host.docker.internal:3001');
const remotes = await prisma.destinationDocker.findMany({ where: { remoteEngine: true, remoteVerified: true } })
if (remotes.length > 0) {
remotes.forEach(remote => {
allowedList.push(`${remote.remoteIpAddress}:3000`);
})
}
if (!allowedList.includes(request.headers.host)) {
// console.log('not allowed', request.headers.host)
}
})
try {
const templateJson = await getTemplates()
if (isDev) {
await fs.writeFile('./template.json', JSON.stringify(templateJson, null, 2))
} else {
await fs.writeFile('/app/template.json', JSON.stringify(templateJson, null, 2))
const { default: got } = await import('got')
let templates = {}
try {
const response = await got.get('https://get.coollabs.io/coolify/service-templates.yaml').text()
templates = yaml.load(response)
} catch (error) {
console.log("Couldn't get latest templates.")
console.log(error)
}
if (isDev) {
await fs.writeFile('./template.json', JSON.stringify(templates, null, 2))
} else {
await fs.writeFile('/app/template.json', JSON.stringify(templates, null, 2))
}
const templateJson = await getTemplates()
await migrateServicesToNewTemplate(templateJson)
await fastify.listen({ port, host })

File diff suppressed because it is too large Load Diff

View File

@ -484,7 +484,7 @@ export async function saveService(request: FastifyRequest<SaveService>, reply: F
let { name, fqdn, exposePort, type, serviceSetting } = request.body;
if (fqdn) fqdn = fqdn.toLowerCase();
if (exposePort) exposePort = Number(exposePort);
console.log({ serviceSetting })
type = fixType(type)
// const update = saveUpdateableFields(type, request.body[type])
const data = {
@ -496,12 +496,12 @@ export async function saveService(request: FastifyRequest<SaveService>, reply: F
// data[type] = { update: update }
// }
for (const setting of serviceSetting) {
const { id: settingId, name, value, changed = false, isNew = false } = setting
const { id: settingId, name, value, changed = false, isNew = false, variableName } = setting
if (changed) {
await prisma.serviceSetting.update({ where: { id: settingId }, data: { value } })
}
if (isNew) {
await prisma.serviceSetting.create({ data: { name, value, service: { connect: { id } } } })
await prisma.serviceSetting.create({ data: { name, value, variableName, service: { connect: { id } } } })
}
}
await prisma.service.update({
@ -510,6 +510,7 @@ export async function saveService(request: FastifyRequest<SaveService>, reply: F
});
return reply.code(201).send()
} catch ({ status, message }) {
console.log({ status, message })
return errorHandler({ status, message })
}
}