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 { asyncExecShell, getEngine } from '$lib/common';
|
||||||
import { decrypt, encrypt } from '$lib/crypto';
|
import { decrypt } from '$lib/crypto';
|
||||||
import { dockerInstance } from '$lib/docker';
|
import { dockerInstance } from '$lib/docker';
|
||||||
import { startCoolifyProxy } from '$lib/haproxy';
|
import { startCoolifyProxy } from '$lib/haproxy';
|
||||||
import { getDatabaseImage } from '.';
|
import { getDatabaseImage } from '.';
|
||||||
import { prisma } from './common';
|
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 } } } });
|
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({
|
return await prisma.service.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: { destinationDocker: { connect: { id: destinationId } } }
|
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({
|
return await prisma.application.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: { destinationDocker: { connect: { id: destinationId } } }
|
data: { destinationDocker: { connect: { id: destinationId } } }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
export async function configureDestinationForDatabase({ id, destinationId }) {
|
export async function configureDestinationForDatabase({
|
||||||
|
id,
|
||||||
|
destinationId
|
||||||
|
}: DestinationConfigurationObject): Promise<void> {
|
||||||
await prisma.database.update({
|
await prisma.database.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: { destinationDocker: { connect: { id: destinationId } } }
|
data: { destinationDocker: { connect: { id: destinationId } } }
|
||||||
@ -38,13 +59,18 @@ export async function configureDestinationForDatabase({ id, destinationId }) {
|
|||||||
const host = getEngine(engine);
|
const host = getEngine(engine);
|
||||||
if (type && version) {
|
if (type && version) {
|
||||||
const baseImage = getDatabaseImage(type);
|
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}" -`
|
`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 } });
|
return await prisma.destinationDocker.update({ where: { id }, data: { name, engine, network } });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,13 +80,8 @@ export async function newRemoteDestination({
|
|||||||
engine,
|
engine,
|
||||||
network,
|
network,
|
||||||
isCoolifyProxyUsed,
|
isCoolifyProxyUsed,
|
||||||
remoteEngine,
|
remoteEngine
|
||||||
ipAddress,
|
}: CreateDockerDestination): Promise<string> {
|
||||||
user,
|
|
||||||
port,
|
|
||||||
sshPrivateKey
|
|
||||||
}) {
|
|
||||||
const encryptedPrivateKey = encrypt(sshPrivateKey);
|
|
||||||
const destination = await prisma.destinationDocker.create({
|
const destination = await prisma.destinationDocker.create({
|
||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
@ -68,16 +89,18 @@ export async function newRemoteDestination({
|
|||||||
engine,
|
engine,
|
||||||
network,
|
network,
|
||||||
isCoolifyProxyUsed,
|
isCoolifyProxyUsed,
|
||||||
remoteEngine,
|
remoteEngine
|
||||||
ipAddress,
|
|
||||||
user,
|
|
||||||
port,
|
|
||||||
sshPrivateKey: encryptedPrivateKey
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return destination.id;
|
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 host = getEngine(engine);
|
||||||
const docker = dockerInstance({ destinationDocker: { engine, network } });
|
const docker = dockerInstance({ destinationDocker: { engine, network } });
|
||||||
const found = await docker.engine.listNetworks({ filters: { name: [`^${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
|
(destination) => destination.network !== network && destination.isCoolifyProxyUsed === true
|
||||||
);
|
);
|
||||||
if (proxyConfigured) {
|
if (proxyConfigured) {
|
||||||
if (proxyConfigured.isCoolifyProxyUsed) {
|
isCoolifyProxyUsed = !!proxyConfigured.isCoolifyProxyUsed;
|
||||||
isCoolifyProxyUsed = true;
|
|
||||||
} else {
|
|
||||||
isCoolifyProxyUsed = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
await prisma.destinationDocker.updateMany({ where: { engine }, data: { isCoolifyProxyUsed } });
|
await prisma.destinationDocker.updateMany({ where: { engine }, data: { isCoolifyProxyUsed } });
|
||||||
}
|
}
|
||||||
if (isCoolifyProxyUsed) await startCoolifyProxy(engine);
|
if (isCoolifyProxyUsed) await startCoolifyProxy(engine);
|
||||||
return destination.id;
|
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 } });
|
const destination = await prisma.destinationDocker.delete({ where: { id } });
|
||||||
if (destination.isCoolifyProxyUsed) {
|
if (destination.isCoolifyProxyUsed) {
|
||||||
const host = getEngine(destination.engine);
|
const host = getEngine(destination.engine);
|
||||||
@ -123,22 +142,34 @@ export async function removeDestination({ id }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getDestination({ id, teamId }) {
|
export async function getDestination({
|
||||||
let destination = await prisma.destinationDocker.findFirst({
|
id,
|
||||||
|
teamId
|
||||||
|
}: FindDestinationFromTeam): Promise<DestinationDocker & { sshPrivateKey?: string }> {
|
||||||
|
const destination = (await prisma.destinationDocker.findFirst({
|
||||||
where: { id, teams: { some: { id: teamId } } }
|
where: { id, teams: { some: { id: teamId } } }
|
||||||
});
|
})) as DestinationDocker & { sshPrivateKey?: string };
|
||||||
if (destination.remoteEngine) {
|
if (destination.remoteEngine) {
|
||||||
destination.sshPrivateKey = decrypt(destination.sshPrivateKey);
|
destination.sshPrivateKey = decrypt(destination.sshPrivateKey);
|
||||||
}
|
}
|
||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
export async function getDestinationByApplicationId({ id, teamId }) {
|
export async function getDestinationByApplicationId({
|
||||||
|
id,
|
||||||
|
teamId
|
||||||
|
}: FindDestinationFromTeam): Promise<DestinationDocker> {
|
||||||
return await prisma.destinationDocker.findFirst({
|
return await prisma.destinationDocker.findFirst({
|
||||||
where: { application: { some: { id } }, teams: { some: { id: teamId } } }
|
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({
|
return await prisma.destinationDocker.updateMany({
|
||||||
where: { engine },
|
where: { engine },
|
||||||
data: { isCoolifyProxyUsed }
|
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 * as db from '$lib/database';
|
||||||
import { ErrorHandler } from '$lib/database';
|
import { ErrorHandler } from '$lib/database';
|
||||||
import { dockerInstance } from '$lib/docker';
|
|
||||||
import type { RequestHandler } from '@sveltejs/kit';
|
import type { RequestHandler } from '@sveltejs/kit';
|
||||||
|
import type { CreateDockerDestination } from '$lib/types/destinations';
|
||||||
|
|
||||||
export const post: RequestHandler = async (event) => {
|
export const post: RequestHandler = async (event) => {
|
||||||
const { teamId, status, body } = await getUserDetails(event);
|
const { teamId, status, body } = await getUserDetails(event);
|
||||||
if (status === 401) return { status, body };
|
if (status === 401) return { status, body };
|
||||||
|
|
||||||
const {
|
const dockerDestinationProps = {
|
||||||
name,
|
...((await event.request.json()) as Omit<CreateDockerDestination, 'teamId'>),
|
||||||
engine,
|
teamId
|
||||||
network,
|
};
|
||||||
isCoolifyProxyUsed,
|
|
||||||
remoteEngine,
|
|
||||||
ipAddress,
|
|
||||||
user,
|
|
||||||
port,
|
|
||||||
sshPrivateKey
|
|
||||||
} = await event.request.json();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let id = null;
|
const id = dockerDestinationProps.remoteEngine
|
||||||
if (remoteEngine) {
|
? await db.newRemoteDestination(dockerDestinationProps)
|
||||||
id = await db.newRemoteDestination({
|
: await db.newLocalDestination(dockerDestinationProps);
|
||||||
name,
|
|
||||||
teamId,
|
|
||||||
engine,
|
|
||||||
network,
|
|
||||||
isCoolifyProxyUsed,
|
|
||||||
remoteEngine,
|
|
||||||
ipAddress,
|
|
||||||
user,
|
|
||||||
port,
|
|
||||||
sshPrivateKey
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
id = await db.newLocalDestination({ name, teamId, engine, network, isCoolifyProxyUsed });
|
|
||||||
}
|
|
||||||
return { status: 200, body: { id } };
|
return { status: 200, body: { id } };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return ErrorHandler(error);
|
return ErrorHandler(error);
|
||||||
|
Loading…
Reference in New Issue
Block a user