Added typings for database/destinations
This commit is contained in:
parent
82f7633c3a
commit
faeae8fd6c
@ -1,27 +1,48 @@
|
||||
import { asyncExecShell, getEngine } from '$lib/common';
|
||||
import { decrypt, encrypt } from '$lib/crypto';
|
||||
import { decrypt } from '$lib/crypto';
|
||||
import { dockerInstance } from '$lib/docker';
|
||||
import { startCoolifyProxy } from '$lib/haproxy';
|
||||
import { getDatabaseImage } from '.';
|
||||
import { prisma } from './common';
|
||||
import type { DestinationDocker, Service, Application, Prisma } from '@prisma/client';
|
||||
import type { CreateDockerDestination } from '$lib/types/destinations';
|
||||
|
||||
export async function listDestinations(teamId) {
|
||||
type DestinationConfigurationObject = {
|
||||
id: string;
|
||||
destinationId: string;
|
||||
};
|
||||
|
||||
type FindDestinationFromTeam = {
|
||||
id: string;
|
||||
teamId: string;
|
||||
};
|
||||
|
||||
export async function listDestinations(teamId: string): Promise<DestinationDocker[]> {
|
||||
return await prisma.destinationDocker.findMany({ where: { teams: { some: { id: teamId } } } });
|
||||
}
|
||||
|
||||
export async function configureDestinationForService({ id, destinationId }) {
|
||||
export async function configureDestinationForService({
|
||||
id,
|
||||
destinationId
|
||||
}: DestinationConfigurationObject): Promise<Service> {
|
||||
return await prisma.service.update({
|
||||
where: { id },
|
||||
data: { destinationDocker: { connect: { id: destinationId } } }
|
||||
});
|
||||
}
|
||||
export async function configureDestinationForApplication({ id, destinationId }) {
|
||||
export async function configureDestinationForApplication({
|
||||
id,
|
||||
destinationId
|
||||
}: DestinationConfigurationObject): Promise<Application> {
|
||||
return await prisma.application.update({
|
||||
where: { id },
|
||||
data: { destinationDocker: { connect: { id: destinationId } } }
|
||||
});
|
||||
}
|
||||
export async function configureDestinationForDatabase({ id, destinationId }) {
|
||||
export async function configureDestinationForDatabase({
|
||||
id,
|
||||
destinationId
|
||||
}: DestinationConfigurationObject): Promise<void> {
|
||||
await prisma.database.update({
|
||||
where: { id },
|
||||
data: { destinationDocker: { connect: { id: destinationId } } }
|
||||
@ -38,13 +59,18 @@ export async function configureDestinationForDatabase({ id, destinationId }) {
|
||||
const host = getEngine(engine);
|
||||
if (type && version) {
|
||||
const baseImage = getDatabaseImage(type);
|
||||
asyncExecShell(
|
||||
await asyncExecShell(
|
||||
`DOCKER_HOST=${host} docker pull ${baseImage}:${version} && echo "FROM ${baseImage}:${version}" | docker build --label coolify.image="true" -t "${baseImage}:${version}" -`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
export async function updateDestination({ id, name, engine, network }) {
|
||||
export async function updateDestination({
|
||||
id,
|
||||
name,
|
||||
engine,
|
||||
network
|
||||
}: Pick<DestinationDocker, 'id' | 'name' | 'engine' | 'network'>): Promise<DestinationDocker> {
|
||||
return await prisma.destinationDocker.update({ where: { id }, data: { name, engine, network } });
|
||||
}
|
||||
|
||||
@ -54,13 +80,8 @@ export async function newRemoteDestination({
|
||||
engine,
|
||||
network,
|
||||
isCoolifyProxyUsed,
|
||||
remoteEngine,
|
||||
ipAddress,
|
||||
user,
|
||||
port,
|
||||
sshPrivateKey
|
||||
}) {
|
||||
const encryptedPrivateKey = encrypt(sshPrivateKey);
|
||||
remoteEngine
|
||||
}: CreateDockerDestination): Promise<string> {
|
||||
const destination = await prisma.destinationDocker.create({
|
||||
data: {
|
||||
name,
|
||||
@ -68,16 +89,18 @@ export async function newRemoteDestination({
|
||||
engine,
|
||||
network,
|
||||
isCoolifyProxyUsed,
|
||||
remoteEngine,
|
||||
ipAddress,
|
||||
user,
|
||||
port,
|
||||
sshPrivateKey: encryptedPrivateKey
|
||||
remoteEngine
|
||||
}
|
||||
});
|
||||
return destination.id;
|
||||
}
|
||||
export async function newLocalDestination({ name, teamId, engine, network, isCoolifyProxyUsed }) {
|
||||
export async function newLocalDestination({
|
||||
name,
|
||||
teamId,
|
||||
engine,
|
||||
network,
|
||||
isCoolifyProxyUsed
|
||||
}: CreateDockerDestination): Promise<string> {
|
||||
const host = getEngine(engine);
|
||||
const docker = dockerInstance({ destinationDocker: { engine, network } });
|
||||
const found = await docker.engine.listNetworks({ filters: { name: [`^${network}$`] } });
|
||||
@ -95,18 +118,14 @@ export async function newLocalDestination({ name, teamId, engine, network, isCoo
|
||||
(destination) => destination.network !== network && destination.isCoolifyProxyUsed === true
|
||||
);
|
||||
if (proxyConfigured) {
|
||||
if (proxyConfigured.isCoolifyProxyUsed) {
|
||||
isCoolifyProxyUsed = true;
|
||||
} else {
|
||||
isCoolifyProxyUsed = false;
|
||||
}
|
||||
isCoolifyProxyUsed = !!proxyConfigured.isCoolifyProxyUsed;
|
||||
}
|
||||
await prisma.destinationDocker.updateMany({ where: { engine }, data: { isCoolifyProxyUsed } });
|
||||
}
|
||||
if (isCoolifyProxyUsed) await startCoolifyProxy(engine);
|
||||
return destination.id;
|
||||
}
|
||||
export async function removeDestination({ id }) {
|
||||
export async function removeDestination({ id }: Pick<DestinationDocker, 'id'>): Promise<void> {
|
||||
const destination = await prisma.destinationDocker.delete({ where: { id } });
|
||||
if (destination.isCoolifyProxyUsed) {
|
||||
const host = getEngine(destination.engine);
|
||||
@ -123,22 +142,34 @@ export async function removeDestination({ id }) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDestination({ id, teamId }) {
|
||||
let destination = await prisma.destinationDocker.findFirst({
|
||||
export async function getDestination({
|
||||
id,
|
||||
teamId
|
||||
}: FindDestinationFromTeam): Promise<DestinationDocker & { sshPrivateKey?: string }> {
|
||||
const destination = (await prisma.destinationDocker.findFirst({
|
||||
where: { id, teams: { some: { id: teamId } } }
|
||||
});
|
||||
})) as DestinationDocker & { sshPrivateKey?: string };
|
||||
if (destination.remoteEngine) {
|
||||
destination.sshPrivateKey = decrypt(destination.sshPrivateKey);
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
export async function getDestinationByApplicationId({ id, teamId }) {
|
||||
export async function getDestinationByApplicationId({
|
||||
id,
|
||||
teamId
|
||||
}: FindDestinationFromTeam): Promise<DestinationDocker> {
|
||||
return await prisma.destinationDocker.findFirst({
|
||||
where: { application: { some: { id } }, teams: { some: { id: teamId } } }
|
||||
});
|
||||
}
|
||||
|
||||
export async function setDestinationSettings({ engine, isCoolifyProxyUsed }) {
|
||||
export async function setDestinationSettings({
|
||||
engine,
|
||||
isCoolifyProxyUsed
|
||||
}: {
|
||||
engine: string;
|
||||
isCoolifyProxyUsed: boolean;
|
||||
}): Promise<Prisma.BatchPayload> {
|
||||
return await prisma.destinationDocker.updateMany({
|
||||
where: { engine },
|
||||
data: { isCoolifyProxyUsed }
|
||||
|
8
src/lib/types/destinations.ts
Normal file
8
src/lib/types/destinations.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export type CreateDockerDestination = {
|
||||
name: string;
|
||||
engine: string;
|
||||
remoteEngine: boolean;
|
||||
network: string;
|
||||
isCoolifyProxyUsed: boolean;
|
||||
teamId: string;
|
||||
};
|
@ -1,43 +1,22 @@
|
||||
import { asyncExecShell, getUserDetails } from '$lib/common';
|
||||
import { getUserDetails } from '$lib/common';
|
||||
import * as db from '$lib/database';
|
||||
import { ErrorHandler } from '$lib/database';
|
||||
import { dockerInstance } from '$lib/docker';
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import type { CreateDockerDestination } from '$lib/types/destinations';
|
||||
|
||||
export const post: RequestHandler = async (event) => {
|
||||
const { teamId, status, body } = await getUserDetails(event);
|
||||
if (status === 401) return { status, body };
|
||||
|
||||
const {
|
||||
name,
|
||||
engine,
|
||||
network,
|
||||
isCoolifyProxyUsed,
|
||||
remoteEngine,
|
||||
ipAddress,
|
||||
user,
|
||||
port,
|
||||
sshPrivateKey
|
||||
} = await event.request.json();
|
||||
const dockerDestinationProps = {
|
||||
...((await event.request.json()) as Omit<CreateDockerDestination, 'teamId'>),
|
||||
teamId
|
||||
};
|
||||
|
||||
try {
|
||||
let id = null;
|
||||
if (remoteEngine) {
|
||||
id = await db.newRemoteDestination({
|
||||
name,
|
||||
teamId,
|
||||
engine,
|
||||
network,
|
||||
isCoolifyProxyUsed,
|
||||
remoteEngine,
|
||||
ipAddress,
|
||||
user,
|
||||
port,
|
||||
sshPrivateKey
|
||||
});
|
||||
} else {
|
||||
id = await db.newLocalDestination({ name, teamId, engine, network, isCoolifyProxyUsed });
|
||||
}
|
||||
const id = dockerDestinationProps.remoteEngine
|
||||
? await db.newRemoteDestination(dockerDestinationProps)
|
||||
: await db.newLocalDestination(dockerDestinationProps);
|
||||
return { status: 200, body: { id } };
|
||||
} catch (error) {
|
||||
return ErrorHandler(error);
|
||||
|
Loading…
Reference in New Issue
Block a user