From 8e61e9fecbdfaad47898b86bb09bc72edf53da5b Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 20 Sep 2022 14:36:06 +0200 Subject: [PATCH] feat: Add migration button to appwrite --- apps/api/src/lib/services/handlers.ts | 21 ++++++++ apps/api/src/routes/api/v1/services/index.ts | 4 +- apps/ui/src/lib/components/DocLink.svelte | 21 ++------ .../services/[id]/_Services/_Services.svelte | 52 +++++++++++++++---- 4 files changed, 70 insertions(+), 28 deletions(-) diff --git a/apps/api/src/lib/services/handlers.ts b/apps/api/src/lib/services/handlers.ts index f6aaa6be5..6f811ba83 100644 --- a/apps/api/src/lib/services/handlers.ts +++ b/apps/api/src/lib/services/handlers.ts @@ -5,6 +5,7 @@ import bcrypt from 'bcryptjs'; import { ServiceStartStop } from '../../routes/api/v1/services/types'; import { asyncSleep, ComposeFile, createDirectories, defaultComposeConfiguration, errorHandler, executeDockerCmd, getDomain, getFreePublicPort, getServiceFromDB, getServiceImage, getServiceMainPort, isARM, isDev, makeLabelForServices, persistentVolumes, prisma } from '../common'; import { defaultServiceConfigurations } from '../services'; +import { OnlyId } from '../../types'; export async function startService(request: FastifyRequest) { try { @@ -2640,3 +2641,23 @@ async function startTaigaService(request: FastifyRequest) { } } +export async function migrateAppwriteDB(request: FastifyRequest, reply: FastifyReply) { + try { + const { id } = request.params + const teamId = request.user.teamId; + const { + destinationDockerId, + destinationDocker, + } = await getServiceFromDB({ id, teamId }); + if (destinationDockerId) { + await executeDockerCmd({ + dockerId: destinationDocker.id, + command: `docker exec ${id} migrate` + }) + return await reply.code(201).send() + } + throw { status: 500, message: 'Could cleanup logs.' } + } catch ({ status, message }) { + return errorHandler({ status, message }) + } +} diff --git a/apps/api/src/routes/api/v1/services/index.ts b/apps/api/src/routes/api/v1/services/index.ts index 454f4c06e..17ac60fdd 100644 --- a/apps/api/src/routes/api/v1/services/index.ts +++ b/apps/api/src/routes/api/v1/services/index.ts @@ -30,7 +30,7 @@ import { import type { OnlyId } from '../../../../types'; import type { ActivateWordpressFtp, CheckService, CheckServiceDomain, DeleteServiceSecret, DeleteServiceStorage, GetServiceLogs, SaveService, SaveServiceDestination, SaveServiceSecret, SaveServiceSettings, SaveServiceStorage, SaveServiceType, SaveServiceVersion, ServiceStartStop, SetGlitchTipSettings, SetWordpressSettings } from './types'; -import { startService, stopService } from '../../../../lib/services/handlers'; +import { migrateAppwriteDB, startService, stopService } from '../../../../lib/services/handlers'; const root: FastifyPluginAsync = async (fastify): Promise => { fastify.addHook('onRequest', async (request) => { @@ -76,6 +76,8 @@ const root: FastifyPluginAsync = async (fastify): Promise => { fastify.post('/:id/plausibleanalytics/activate', async (request, reply) => await activatePlausibleUsers(request, reply)); fastify.post('/:id/plausibleanalytics/cleanup', async (request, reply) => await cleanupPlausibleLogs(request, reply)); fastify.post('/:id/wordpress/ftp', async (request, reply) => await activateWordpressFtp(request, reply)); + + fastify.post('/:id/appwrite/migrate', async (request, reply) => await migrateAppwriteDB(request, reply)); }; export default root; diff --git a/apps/ui/src/lib/components/DocLink.svelte b/apps/ui/src/lib/components/DocLink.svelte index 39c8302c8..d561566f0 100644 --- a/apps/ui/src/lib/components/DocLink.svelte +++ b/apps/ui/src/lib/components/DocLink.svelte @@ -11,22 +11,9 @@ - - - - - - + + + + See details in the documentation diff --git a/apps/ui/src/routes/services/[id]/_Services/_Services.svelte b/apps/ui/src/routes/services/[id]/_Services/_Services.svelte index 60f080e7e..3a1dabcb7 100644 --- a/apps/ui/src/routes/services/[id]/_Services/_Services.svelte +++ b/apps/ui/src/routes/services/[id]/_Services/_Services.svelte @@ -39,6 +39,7 @@ import Weblate from './_Weblate.svelte'; import Explainer from '$lib/components/Explainer.svelte'; import Taiga from './_Taiga.svelte'; + import DocLink from '$lib/components/DocLink.svelte'; const { id } = $page.params; $: isDisabled = @@ -126,6 +127,20 @@ loading.verification = false; } } + async function migrateAppwriteDB() { + loading.verification = true; + try { + await post(`/services/${id}/${service.type}/migrate`, { id: service.id }); + return addToast({ + message: "Appwrite's database has been migrated.", + type: 'success' + }); + } catch (error) { + return errorNotification(error); + } finally { + loading.verification = false; + } + } async function changeSettings(name: any) { try { if (name === 'dualCerts') { @@ -154,6 +169,9 @@ loading.cleanup = false; } } + function doNothing() { + return; + } onMount(async () => { if (browser && window.location.hostname === 'demo.coolify.io' && !service.fqdn) { service.fqdn = `http://${cuid()}.demo.coolify.io`; @@ -182,7 +200,7 @@

{$t('general')}

-
+
{#if $appSession.isAdmin} + +
+ {/if} + {#if service.type === 'appwrite' && $status.service.isRunning} - + {/if}