plausible migration done

This commit is contained in:
Andras Bacsai 2022-10-18 12:02:09 +02:00
parent a6f457749b
commit 877577efdb
5 changed files with 50 additions and 13 deletions

View File

@ -6,7 +6,7 @@ import cookie from '@fastify/cookie';
import multipart from '@fastify/multipart';
import path, { join } from 'path';
import autoLoad from '@fastify/autoload';
import { asyncExecShell, cleanupDockerStorage, createRemoteEngineConfiguration, decrypt, executeDockerCmd, executeSSHCmd, generateDatabaseConfiguration, getDomain, isDev, listSettings, prisma, startTraefikProxy, startTraefikTCPProxy, version } from './lib/common';
import { asyncExecShell, cleanupDockerStorage, createRemoteEngineConfiguration, decrypt, encrypt, executeDockerCmd, executeSSHCmd, generateDatabaseConfiguration, getDomain, isDev, listSettings, prisma, startTraefikProxy, startTraefikTCPProxy, version } from './lib/common';
import { scheduler } from './lib/scheduler';
import { compareVersions } from 'compare-versions';
import Graceful from '@ladjs/graceful'
@ -14,6 +14,7 @@ import axios from 'axios';
import fs from 'fs/promises';
import { verifyRemoteDockerEngineFn } from './routes/api/v1/destinations/handlers';
import { checkContainer } from './lib/docker';
import { includeServices } from './lib/services/common';
declare module 'fastify' {
interface FastifyInstance {
config: {
@ -122,6 +123,7 @@ const host = '0.0.0.0';
}
})
try {
await migrateServicesToNewTemplate()
await fastify.listen({ port, host })
console.log(`Coolify's API is listening on ${host}:${port}`);
await initServer();
@ -159,6 +161,7 @@ const host = '0.0.0.0';
getArch(),
getIPAddress(),
configureRemoteDockers(),
])
} catch (error) {
console.error(error);
@ -166,7 +169,44 @@ const host = '0.0.0.0';
}
})();
async function migrateServicesToNewTemplate() {
// This function migrates old hardcoded services to the new template based services
try {
const services = await prisma.service.findMany({ include: includeServices })
for (const service of services) {
if (service.type === 'plausibleanalytics') {
const { email = 'admin@example.com', username = 'admin', password, postgresqlUser, postgresqlPassword, postgresqlDatabase, secretKeyBase, scriptName } = service.plausibleAnalytics;
// Migrate Secrets
await prisma.serviceSecret.create({ data: { name: 'ADMIN_USER_PWD', value: password, service: { connect: { id: service.id } } } })
await prisma.serviceSecret.create({ data: { name: 'SECRET_KEY_BASE', value: secretKeyBase, service: { connect: { id: service.id } } } })
await prisma.serviceSecret.create({ data: { name: 'POSTGRESQL_PASSWORD', value: postgresqlPassword, service: { connect: { id: service.id } } } })
await prisma.serviceSecret.create({ data: { name: 'DATABASE_URL', value: encrypt(`postgresql://${postgresqlUser}:${decrypt(postgresqlPassword)}@${service.id}-postgresql:5432/${postgresqlDatabase}`), service: { connect: { id: service.id } } } })
await prisma.serviceSecret.create({ data: { name: 'CLICKHOUSE_DATABASE_URL', value: encrypt(`http://${service.id}-clickhouse:8123/plausible`), service: { connect: { id: service.id } } } })
// Migrate Configs
await prisma.serviceSetting.create({ data: { name: 'BASE_URL', value: '$$generate_fqdn', service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'ADMIN_USER_EMAIL', value: email, service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'ADMIN_USER_NAME', value: username, service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'DISABLE_AUTH', value: 'false', service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'DISABLE_REGISTRATION', value: 'true', service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'POSTGRESQL_USERNAME', value: postgresqlUser, service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'POSTGRESQL_DATABASE', value: postgresqlDatabase, service: { connect: { id: service.id } } } })
await prisma.serviceSetting.create({ data: { name: 'SCRIPT_NAME', value: scriptName, service: { connect: { id: service.id } } } })
// Create predefined persistent volumes
await prisma.servicePersistentStorage.create({ data: { path: '/bitnami/postgresql', containerId: `${service.id}-postgresql`, volumeName: `${service.id}-postgresql-data`, predefined: true, service: { connect: { id: service.id } } } })
await prisma.servicePersistentStorage.create({ data: { path: '/var/lib/clickhouse', containerId: `${service.id}-clickhouse`, volumeName: `${service.id}-clickhouse-data`, predefined: true, service: { connect: { id: service.id } } } })
// Remove old service data
await prisma.service.update({ where: { id: service.id }, data: { plausibleAnalytics: { delete: true } } })
}
}
} catch (error) {
console.log(error)
}
}
async function getIPAddress() {
const { publicIpv4, publicIpv6 } = await import('public-ip')
try {

View File

@ -69,7 +69,7 @@ export default [
"documentation": "Taken from https://plausible.io/",
"image": "bitnami/postgresql:13.2.0",
"volumes": [
'$$id-postgresql-data:/bitnami/postgresql/',
'$$id-postgresql-data:/bitnami/postgresql',
],
"environment": [
"POSTGRESQL_PASSWORD=$$secret_postgresql_password",

View File

@ -68,10 +68,6 @@ export async function getServiceStatus(request: FastifyRequest<OnlyId>) {
try {
const teamId = request.user.teamId;
const { id } = request.params;
let isRunning = false;
let isExited = false
let isRestarting = false;
const service = await getServiceFromDB({ id, teamId });
const { destinationDockerId, settings } = service;
let payload = {}

View File

@ -323,11 +323,11 @@
on:click={stopApplication}
type="submit"
disabled={!$isDeploymentEnabled}
class="btn btn-sm btn-error gap-2"
class="btn btn-sm gap-2"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-6 h-6 "
class="w-6 h-6 text-error"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
@ -393,11 +393,11 @@
on:click={stopApplication}
type="submit"
disabled={!$isDeploymentEnabled}
class="btn btn-sm btn-error gap-2"
class="btn btn-sm gap-2"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-6 h-6 "
class="w-6 h-6 text-error"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"

View File

@ -280,11 +280,11 @@
on:click={stopService}
type="submit"
disabled={!$isDeploymentEnabled}
class="btn btn-sm btn-error gap-2"
class="btn btn-sm gap-2"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-6 h-6 "
class="w-6 h-6 text-error "
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
@ -293,7 +293,8 @@
stroke-linejoin="round"
>
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<rect x="5" y="5" width="14" height="14" rx="2" />
<rect x="6" y="5" width="4" height="14" rx="1" />
<rect x="14" y="5" width="4" height="14" rx="1" />
</svg>
Stop
</button>