This commit is contained in:
Andras Bacsai 2022-10-24 22:54:19 +02:00
parent f9dfbd5800
commit 811ea5b92a
5 changed files with 82 additions and 24 deletions

View File

@ -2,6 +2,8 @@ import axios from "axios";
import { compareVersions } from "compare-versions"; import { compareVersions } from "compare-versions";
import cuid from "cuid"; import cuid from "cuid";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import fs from 'fs/promises';
import yaml from 'js-yaml';
import { import {
asyncExecShell, asyncExecShell,
asyncSleep, asyncSleep,
@ -36,6 +38,30 @@ export async function cleanupManually(request: FastifyRequest) {
return errorHandler({ status, message }); return errorHandler({ status, message });
} }
} }
export async function refreshTemplates(request: FastifyRequest) {
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)
} catch (error) {
throw {
status: 500,
message: 'Could not fetch templates from get.coollabs.io'
};
}
if (isDev) {
await fs.writeFile('./template.json', JSON.stringify(templates, null, 2))
} else {
await fs.writeFile('/app/template.json', JSON.stringify(templates, null, 2))
}
return {};
} catch ({ status, message }) {
return errorHandler({ status, message });
}
}
export async function checkUpdate(request: FastifyRequest) { export async function checkUpdate(request: FastifyRequest) {
try { try {
const isStaging = const isStaging =

View File

@ -1,5 +1,5 @@
import { FastifyPluginAsync } from 'fastify'; import { FastifyPluginAsync } from 'fastify';
import { checkUpdate, login, showDashboard, update, resetQueue, getCurrentUser, cleanupManually, restartCoolify } from './handlers'; import { checkUpdate, login, showDashboard, update, resetQueue, getCurrentUser, cleanupManually, restartCoolify, refreshTemplates } from './handlers';
import { GetCurrentUser } from './types'; import { GetCurrentUser } from './types';
import pump from 'pump' import pump from 'pump'
import fs from 'fs' import fs from 'fs'
@ -55,6 +55,10 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.post('/internal/cleanup', { fastify.post('/internal/cleanup', {
onRequest: [fastify.authenticate] onRequest: [fastify.authenticate]
}, async (request) => await cleanupManually(request)); }, async (request) => await cleanupManually(request));
fastify.post('/internal/refreshTemplates', {
onRequest: [fastify.authenticate]
}, async (request) => await refreshTemplates(request));
}; };
export default root; export default root;

View File

@ -153,10 +153,10 @@ export async function parseAndFindServiceTemplates(service: any, workdir?: strin
if (proxyValue.domain) { if (proxyValue.domain) {
const variable = foundTemplate.variables.find(v => v.id === proxyValue.domain) const variable = foundTemplate.variables.find(v => v.id === proxyValue.domain)
if (variable) { if (variable) {
const { name, label, description, defaultValue, extras } = variable const { id, name, label, description, defaultValue, extras } = variable
const found = await prisma.serviceSetting.findFirst({ where: { variableName: proxyValue.domain } }) const found = await prisma.serviceSetting.findFirst({ where: { variableName: proxyValue.domain } })
parsedTemplate[realKey].fqdns.push( parsedTemplate[realKey].fqdns.push(
{ name, value: found.value || '', label, description, defaultValue, extras } { id, name, value: found?.value || '', label, description, defaultValue, extras }
) )
} }
@ -485,27 +485,29 @@ export async function saveService(request: FastifyRequest<SaveService>, reply: F
if (fqdn) fqdn = fqdn.toLowerCase(); if (fqdn) fqdn = fqdn.toLowerCase();
if (exposePort) exposePort = Number(exposePort); if (exposePort) exposePort = Number(exposePort);
type = fixType(type) type = fixType(type)
// const update = saveUpdateableFields(type, request.body[type])
const data = { const data = {
fqdn, fqdn,
name, name,
exposePort, exposePort,
} }
// if (Object.keys(update).length > 0) { const templates = await getTemplates()
// data[type] = { update: update } const service = await prisma.service.findUnique({ where: { id } })
// } const foundTemplate = templates.find(t => t.name.toLowerCase() === service.type.toLowerCase())
for (const setting of serviceSetting) { for (const setting of serviceSetting) {
const { id: settingId, name, value, changed = false, isNew = false, variableName } = setting let { id: settingId, name, value, changed = false, isNew = false, variableName } = setting
if (changed) { if (changed) {
await prisma.serviceSetting.update({ where: { id: settingId }, data: { value } }) await prisma.serviceSetting.update({ where: { id: settingId }, data: { value } })
} }
if (isNew) { if (isNew) {
if (!variableName) {
variableName = foundTemplate.variables.find(v => v.name === name).id
}
await prisma.serviceSetting.create({ data: { name, value, variableName, service: { connect: { id } } } }) await prisma.serviceSetting.create({ data: { name, value, variableName, service: { connect: { id } } } })
} }
} }
await prisma.service.update({ await prisma.service.update({
where: { id }, data where: { id }, data
}); });
return reply.code(201).send() return reply.code(201).send()
} catch ({ status, message }) { } catch ({ status, message }) {

View File

@ -65,7 +65,8 @@
status, status,
location, location,
setLocation, setLocation,
checkIfDeploymentEnabledServices checkIfDeploymentEnabledServices,
addToast
} from '$lib/store'; } from '$lib/store';
import { onDestroy, onMount } from 'svelte'; import { onDestroy, onMount } from 'svelte';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
@ -76,6 +77,9 @@
$isDeploymentEnabled = checkIfDeploymentEnabledServices($appSession.isAdmin, service); $isDeploymentEnabled = checkIfDeploymentEnabledServices($appSession.isAdmin, service);
let statusInterval: any; let statusInterval: any;
let loading = {
refreshTemplates: false
};
async function deleteService() { async function deleteService() {
const sure = confirm($t('application.confirm_to_delete', { name: service.name })); const sure = confirm($t('application.confirm_to_delete', { name: service.name }));
@ -97,6 +101,20 @@
await stopService(); await stopService();
await startService(); await startService();
} }
async function refreshTemplate() {
try {
loading.refreshTemplates = true;
await post(`/internal/refreshTemplates`, {});
addToast({
message: 'Services refreshed.',
type: 'success'
});
} catch (error) {
return errorNotification(error);
} finally {
loading.refreshTemplates = false;
}
}
async function stopService() { async function stopService() {
const sure = confirm($t('database.confirm_stop', { name: service.name })); const sure = confirm($t('database.confirm_stop', { name: service.name }));
if (sure) { if (sure) {
@ -187,7 +205,7 @@
</script> </script>
<div class="mx-auto max-w-screen-2xl px-6 grid grid-cols-1 lg:grid-cols-2"> <div class="mx-auto max-w-screen-2xl px-6 grid grid-cols-1 lg:grid-cols-2">
<nav class="header flex flex-row order-2 lg:order-1 px-0 lg:px-4 items-start"> <nav class="header flex flex-col lg:flex-row order-2 lg:order-1 px-0 lg:px-4 items-start">
<div class="title lg:pb-10"> <div class="title lg:pb-10">
<div class="flex justify-center items-center space-x-2"> <div class="flex justify-center items-center space-x-2">
<div> <div>
@ -217,8 +235,8 @@
</div> </div>
</div> </div>
</div> </div>
{#if $page.url.pathname.startsWith(`/services/${id}/configuration/`)} <div class="flex flex-row space-x-2 lg:px-2">
<div class="px-2"> {#if $page.url.pathname.startsWith(`/services/${id}/configuration/`)}
<button <button
on:click={() => deleteService()} on:click={() => deleteService()}
disabled={!$appSession.isAdmin} disabled={!$appSession.isAdmin}
@ -228,8 +246,16 @@
> >
Delete Service Delete Service
</button> </button>
</div> {/if}
{/if} {#if $page.url.pathname === `/services/${id}/configuration/type`}
<button
disabled={loading.refreshTemplates}
class:loading={loading.refreshTemplates}
class="btn btn-sm btn-primary text-sm"
on:click={refreshTemplate}>Refresh Services List</button
>
{/if}
</div>
</nav> </nav>
<div <div
class="pt-4 flex flex-row items-start justify-center lg:justify-end space-x-2 order-1 lg:order-2" class="pt-4 flex flex-row items-start justify-center lg:justify-end space-x-2 order-1 lg:order-2"
@ -267,7 +293,7 @@
class="w-6 h-6" class="w-6 h-6"
viewBox="0 0 24 24" viewBox="0 0 24 24"
stroke-width="1.5" stroke-width="1.5"
stroke="currentColor" stroke="currentColor"
fill="none" fill="none"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" stroke-linejoin="round"

View File

@ -53,14 +53,14 @@ export async function saveForm(formData: any, service: any) {
} }
return setting; return setting;
}); });
// if (!settings.includes(key) && !baseCoolifySetting.includes(key)) { if (!settings.includes(key) && !baseCoolifySetting.includes(key)) {
// service.serviceSetting.push({ service.serviceSetting.push({
// id: service.id, id: service.id,
// name: key, name: key,
// value: value, value: value,
// isNew: true isNew: true
// }); });
// } }
} }
await post(`/services/${service.id}`, { ...service }); await post(`/services/${service.id}`, { ...service });
const { service: reloadedService } = await get(`/services/${service.id}`); const { service: reloadedService } = await get(`/services/${service.id}`);