fixes, dev templates, etc
This commit is contained in:
@@ -17,6 +17,7 @@ import { verifyRemoteDockerEngineFn } from './routes/api/v1/destinations/handler
|
||||
import { checkContainer } from './lib/docker';
|
||||
import { migrateServicesToNewTemplate } from './lib';
|
||||
import { getTemplates } from './lib/services';
|
||||
import { refreshTemplates } from './routes/api/v1/handlers';
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
config: {
|
||||
@@ -129,22 +130,21 @@ const host = '0.0.0.0';
|
||||
|
||||
try {
|
||||
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)
|
||||
if (isDev) {
|
||||
const response = await fs.readFile('./devTemplates.yaml', 'utf8')
|
||||
await fs.writeFile('./template.json', JSON.stringify(yaml.load(response), null, 2))
|
||||
} else {
|
||||
const response = await got.get('https://get.coollabs.io/coolify/service-templates.yaml').text()
|
||||
await fs.writeFile('/app/template.json', JSON.stringify(yaml.load(response), null, 2))
|
||||
}
|
||||
|
||||
} 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 migrateServicesToNewTemplate()
|
||||
|
||||
await fastify.listen({ port, host })
|
||||
console.log(`Coolify's API is listening on ${host}:${port}`);
|
||||
@@ -169,10 +169,11 @@ const host = '0.0.0.0';
|
||||
await cleanupStorage()
|
||||
}, 60000 * 10)
|
||||
|
||||
// checkProxies and checkFluentBit
|
||||
// checkProxies, checkFluentBit & refresh templates
|
||||
setInterval(async () => {
|
||||
await checkProxies();
|
||||
await checkFluentBit();
|
||||
await refreshTemplates()
|
||||
}, 10000)
|
||||
|
||||
setInterval(async () => {
|
||||
|
@@ -1,10 +1,12 @@
|
||||
import cuid from "cuid";
|
||||
import { decrypt, encrypt, generatePassword, getDomain, prisma } from "./lib/common";
|
||||
import { getTemplates } from "./lib/services";
|
||||
import { includeServices } from "./lib/services/common";
|
||||
|
||||
export async function migrateServicesToNewTemplate(templates: any) {
|
||||
export async function migrateServicesToNewTemplate() {
|
||||
// This function migrates old hardcoded services to the new template based services
|
||||
try {
|
||||
let templates = await getTemplates()
|
||||
const services: any = await prisma.service.findMany({ include: includeServices })
|
||||
for (const service of services) {
|
||||
const { id } = service
|
||||
|
@@ -244,7 +244,11 @@ export async function isDNSValid(hostname: any, domain: string): Promise<any> {
|
||||
}
|
||||
|
||||
export function getDomain(domain: string): string {
|
||||
return domain?.replace('https://', '').replace('http://', '');
|
||||
if (domain) {
|
||||
return domain?.replace('https://', '').replace('http://', '');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export async function isDomainConfigured({
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -38,7 +38,7 @@ export async function cleanupManually(request: FastifyRequest) {
|
||||
return errorHandler({ status, message });
|
||||
}
|
||||
}
|
||||
export async function refreshTemplates(request: FastifyRequest) {
|
||||
export async function refreshTemplates() {
|
||||
try {
|
||||
const { default: got } = await import('got')
|
||||
let templates = {}
|
||||
|
@@ -58,7 +58,7 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
|
||||
fastify.post('/internal/refreshTemplates', {
|
||||
onRequest: [fastify.authenticate]
|
||||
}, async (request) => await refreshTemplates(request));
|
||||
}, async () => await refreshTemplates());
|
||||
};
|
||||
|
||||
export default root;
|
||||
|
@@ -136,13 +136,12 @@ export async function parseAndFindServiceTemplates(service: any, workdir?: strin
|
||||
const description = variable?.description
|
||||
const defaultValue = variable?.defaultValue
|
||||
const main = variable?.main || '$$id'
|
||||
const extras = variable?.extras
|
||||
if (envValue.startsWith('$$config') || extras?.isVisibleOnUI) {
|
||||
if (envValue.startsWith('$$config') || variable?.showOnUI) {
|
||||
if (envValue.startsWith('$$config_coolify')) {
|
||||
continue
|
||||
}
|
||||
parsedTemplate[realKey].environment.push(
|
||||
{ name: envKey, value: envValue, main, label, description, defaultValue, extras }
|
||||
{ name: envKey, value: envValue, main, label, description, defaultValue }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -153,10 +152,10 @@ export async function parseAndFindServiceTemplates(service: any, workdir?: strin
|
||||
if (proxyValue.domain) {
|
||||
const variable = foundTemplate.variables.find(v => v.id === proxyValue.domain)
|
||||
if (variable) {
|
||||
const { id, name, label, description, defaultValue, extras } = variable
|
||||
const { id, name, label, description, defaultValue, required = false } = variable
|
||||
const found = await prisma.serviceSetting.findFirst({ where: { variableName: proxyValue.domain } })
|
||||
parsedTemplate[realKey].fqdns.push(
|
||||
{ id, name, value: found?.value || '', label, description, defaultValue, extras }
|
||||
{ id, name, value: found?.value || '', label, description, defaultValue, required }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -186,9 +185,9 @@ export async function parseAndFindServiceTemplates(service: any, workdir?: strin
|
||||
if (variableName.startsWith('$$config_coolify')) {
|
||||
continue;
|
||||
}
|
||||
if (service.fqdn && value === '$$generate_fqdn') {
|
||||
strParsedTemplate = strParsedTemplate.replaceAll(variableName, service.fqdn)
|
||||
} else if (service.fqdn && value === '$$generate_domain') {
|
||||
if (value === '$$generate_fqdn') {
|
||||
strParsedTemplate = strParsedTemplate.replaceAll(variableName, service.fqdn || '')
|
||||
} else if (value === '$$generate_domain') {
|
||||
strParsedTemplate = strParsedTemplate.replaceAll(variableName, getDomain(service.fqdn))
|
||||
} else if (service.destinationDocker?.network && value === '$$generate_network') {
|
||||
strParsedTemplate = strParsedTemplate.replaceAll(variableName, service.destinationDocker.network)
|
||||
@@ -260,7 +259,9 @@ export async function saveServiceType(request: FastifyRequest<SaveServiceType>,
|
||||
const regex = /^\$\$.*\((\d+)\)$/g;
|
||||
const length = Number(regex.exec(defaultValue)?.[1]) || undefined
|
||||
if (variable.defaultValue.startsWith('$$generate_password')) {
|
||||
console.log(variable)
|
||||
variable.value = generatePassword({ length });
|
||||
console.log(variable.value)
|
||||
} else if (variable.defaultValue.startsWith('$$generate_hex')) {
|
||||
variable.value = generatePassword({ length, isHex: true });
|
||||
} else if (variable.defaultValue.startsWith('$$generate_username')) {
|
||||
|
@@ -374,6 +374,7 @@ export async function traefikConfiguration(request, reply) {
|
||||
type,
|
||||
destinationDockerId,
|
||||
dualCerts,
|
||||
serviceSetting
|
||||
} = service;
|
||||
if (destinationDockerId) {
|
||||
const templates = await getTemplates();
|
||||
@@ -387,13 +388,17 @@ export async function traefikConfiguration(request, reply) {
|
||||
const { proxy } = found.services[oneService];
|
||||
for (const configuration of proxy) {
|
||||
const publicPort = service[type]?.publicPort;
|
||||
if (configuration.domain) {
|
||||
const setting = serviceSetting.find((a) => a.variableName === configuration.domain);
|
||||
configuration.domain = configuration.domain.replace(configuration.domain, setting.value);
|
||||
}
|
||||
if (fqdn) {
|
||||
data.services.push({
|
||||
id: oneService,
|
||||
publicPort,
|
||||
fqdn,
|
||||
dualCerts,
|
||||
configuration
|
||||
configuration,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -404,17 +409,20 @@ export async function traefikConfiguration(request, reply) {
|
||||
}
|
||||
}
|
||||
for (const service of data.services) {
|
||||
const { id, fqdn, dualCerts, configuration: { port, pathPrefix = '/' }, isCustomSSL = false } = service
|
||||
let { id, fqdn, dualCerts, configuration: { port, pathPrefix = '/', domain: customDomain }, isCustomSSL = false } = service
|
||||
if (customDomain) {
|
||||
fqdn = customDomain
|
||||
}
|
||||
const domain = getDomain(fqdn);
|
||||
const nakedDomain = domain.replace(/^www\./, '');
|
||||
const isHttps = fqdn.startsWith('https://');
|
||||
const isWWW = fqdn.includes('www.');
|
||||
if (isHttps) {
|
||||
traefik.http.routers[id] = generateHttpRouter(id, nakedDomain, pathPrefix)
|
||||
traefik.http.routers[`${id}-secure`] = generateProtocolRedirectRouter(id, nakedDomain, pathPrefix, 'http-to-https')
|
||||
traefik.http.services[id] = generateLoadBalancerService(id, port)
|
||||
traefik.http.routers[`${id}-${port || 'default'}`] = generateHttpRouter(`${id}-${port || 'default'}`, nakedDomain, pathPrefix)
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`] = generateProtocolRedirectRouter(`${id}-${port || 'default'}-secure`, nakedDomain, pathPrefix, 'http-to-https')
|
||||
traefik.http.services[`${id}-${port || 'default'}`] = generateLoadBalancerService(id, port)
|
||||
if (dualCerts) {
|
||||
traefik.http.routers[`${id}-secure`] = {
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`] = {
|
||||
entrypoints: ['websecure'],
|
||||
rule: `(Host(\`${nakedDomain}\`) || Host(\`www.${nakedDomain}\`)) && PathPrefix(\`${pathPrefix}\`)`,
|
||||
service: `${id}`,
|
||||
@@ -425,7 +433,7 @@ export async function traefikConfiguration(request, reply) {
|
||||
};
|
||||
} else {
|
||||
if (isWWW) {
|
||||
traefik.http.routers[`${id}-secure-www`] = {
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure-www`] = {
|
||||
entrypoints: ['websecure'],
|
||||
rule: `Host(\`www.${nakedDomain}\`) && PathPrefix(\`${pathPrefix}\`)`,
|
||||
service: `${id}`,
|
||||
@@ -434,7 +442,7 @@ export async function traefikConfiguration(request, reply) {
|
||||
},
|
||||
middlewares: []
|
||||
};
|
||||
traefik.http.routers[`${id}-secure`] = {
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`] = {
|
||||
entrypoints: ['websecure'],
|
||||
rule: `Host(\`${nakedDomain}\`) && PathPrefix(\`${pathPrefix}\`)`,
|
||||
service: `${id}`,
|
||||
@@ -445,9 +453,9 @@ export async function traefikConfiguration(request, reply) {
|
||||
},
|
||||
middlewares: ['redirect-to-www']
|
||||
};
|
||||
traefik.http.routers[`${id}`].middlewares.push('redirect-to-www');
|
||||
traefik.http.routers[`${id}-${port || 'default'}`].middlewares.push('redirect-to-www');
|
||||
} else {
|
||||
traefik.http.routers[`${id}-secure-www`] = {
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure-www`] = {
|
||||
entrypoints: ['websecure'],
|
||||
rule: `Host(\`www.${nakedDomain}\`) && PathPrefix(\`${pathPrefix}\`)`,
|
||||
service: `${id}`,
|
||||
@@ -458,7 +466,7 @@ export async function traefikConfiguration(request, reply) {
|
||||
},
|
||||
middlewares: ['redirect-to-non-www']
|
||||
};
|
||||
traefik.http.routers[`${id}-secure`] = {
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`] = {
|
||||
entrypoints: ['websecure'],
|
||||
rule: `Host(\`${domain}\`) && PathPrefix(\`${pathPrefix}\`)`,
|
||||
service: `${id}`,
|
||||
@@ -467,21 +475,21 @@ export async function traefikConfiguration(request, reply) {
|
||||
},
|
||||
middlewares: []
|
||||
};
|
||||
traefik.http.routers[`${id}`].middlewares.push('redirect-to-non-www');
|
||||
traefik.http.routers[`${id}-${port || 'default'}`].middlewares.push('redirect-to-non-www');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
traefik.http.routers[id] = generateHttpRouter(id, nakedDomain, pathPrefix)
|
||||
traefik.http.routers[`${id}-secure`] = generateProtocolRedirectRouter(id, nakedDomain, pathPrefix, 'https-to-http')
|
||||
traefik.http.services[id] = generateLoadBalancerService(id, port)
|
||||
traefik.http.routers[`${id}-${port || 'default'}`] = generateHttpRouter(`${id}-${port || 'default'}`, nakedDomain, pathPrefix)
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`] = generateProtocolRedirectRouter(`${id}-${port || 'default'}-secure`, nakedDomain, pathPrefix, 'https-to-http')
|
||||
traefik.http.services[`${id}-${port || 'default'}`] = generateLoadBalancerService(id, port)
|
||||
|
||||
if (!dualCerts) {
|
||||
if (isWWW) {
|
||||
traefik.http.routers[`${id}`].middlewares.push('redirect-to-www');
|
||||
traefik.http.routers[`${id}-secure`].middlewares.push('redirect-to-www');
|
||||
traefik.http.routers[`${id}-${port || 'default'}`].middlewares.push('redirect-to-www');
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`].middlewares.push('redirect-to-www');
|
||||
} else {
|
||||
traefik.http.routers[`${id}`].middlewares.push('redirect-to-non-www');
|
||||
traefik.http.routers[`${id}-secure`].middlewares.push('redirect-to-non-www');
|
||||
traefik.http.routers[`${id}-${port || 'default'}`].middlewares.push('redirect-to-non-www');
|
||||
traefik.http.routers[`${id}-${port || 'default'}-secure`].middlewares.push('redirect-to-non-www');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user