diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 9ec70de70..34d8de6b1 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -147,8 +147,12 @@ model DestinationDocker { id String @id @default(cuid()) network String @unique name String - engine String + engine String? remoteEngine Boolean @default(false) + ipAddress String? + sshPrivateKey String? + user String? @default("root") + port Int? @default(22) isCoolifyProxyUsed Boolean? @default(false) teams Team[] application Application[] diff --git a/src/lib/common.ts b/src/lib/common.ts index 3ec0c923c..de33fb267 100644 --- a/src/lib/common.ts +++ b/src/lib/common.ts @@ -103,7 +103,7 @@ export const getUserDetails = async (event, isAdminRequired = true) => { }; export function getEngine(engine) { - return engine === '/var/run/docker.sock' ? 'unix:///var/run/docker.sock' : `tcp://${engine}:2375`; + return engine === '/var/run/docker.sock' ? 'unix:///var/run/docker.sock' : engine; } export async function removeContainer(id, engine) { diff --git a/src/lib/components/common.ts b/src/lib/components/common.ts index fd887526a..687819c6d 100644 --- a/src/lib/components/common.ts +++ b/src/lib/components/common.ts @@ -15,3 +15,6 @@ export const notNodeDeployments = ['php', 'docker', 'rust']; export function getDomain(domain) { return domain?.replace('https://', '').replace('http://', ''); } +export function generateRemoteEngine(destination) { + return `ssh://${destination.user}@${destination.ipAddress}:${destination.port}`; +} diff --git a/src/lib/database/destinations.ts b/src/lib/database/destinations.ts index b566becb4..3f3aadec5 100644 --- a/src/lib/database/destinations.ts +++ b/src/lib/database/destinations.ts @@ -1,4 +1,5 @@ import { asyncExecShell, getEngine } from '$lib/common'; +import { decrypt, encrypt } from '$lib/crypto'; import { dockerInstance } from '$lib/docker'; import { startCoolifyProxy } from '$lib/haproxy'; import { getDatabaseImage } from '.'; @@ -47,7 +48,36 @@ export async function updateDestination({ id, name, engine, network }) { return await prisma.destinationDocker.update({ where: { id }, data: { name, engine, network } }); } -export async function newDestination({ name, teamId, engine, network, isCoolifyProxyUsed }) { +export async function newRemoteDestination({ + name, + teamId, + engine, + network, + isCoolifyProxyUsed, + remoteEngine, + ipAddress, + user, + port, + sshPrivateKey +}) { + const encryptedPrivateKey = encrypt(sshPrivateKey); + const destination = await prisma.destinationDocker.create({ + data: { + name, + teams: { connect: { id: teamId } }, + engine, + network, + isCoolifyProxyUsed, + remoteEngine, + ipAddress, + user, + port, + sshPrivateKey: encryptedPrivateKey + } + }); + return destination.id; +} +export async function newLocalDestination({ name, teamId, engine, network, isCoolifyProxyUsed }) { const host = getEngine(engine); const docker = dockerInstance({ destinationDocker: { engine, network } }); const found = await docker.engine.listNetworks({ filters: { name: [`^${network}$`] } }); @@ -94,9 +124,13 @@ export async function removeDestination({ id }) { } export async function getDestination({ id, teamId }) { - return await prisma.destinationDocker.findFirst({ + let destination = await prisma.destinationDocker.findFirst({ where: { id, teams: { some: { id: teamId } } } }); + if (destination.remoteEngine) { + destination.sshPrivateKey = decrypt(destination.sshPrivateKey); + } + return destination; } export async function getDestinationByApplicationId({ id, teamId }) { return await prisma.destinationDocker.findFirst({ diff --git a/src/routes/destinations/[id]/_LocalDocker.svelte b/src/routes/destinations/[id]/_LocalDocker.svelte index 76a36787a..287f544ef 100644 --- a/src/routes/destinations/[id]/_LocalDocker.svelte +++ b/src/routes/destinations/[id]/_LocalDocker.svelte @@ -4,7 +4,7 @@ export let state; import { toast } from '@zerodevx/svelte-toast'; - import { page } from '$app/stores'; + import { page, session } from '$app/stores'; import Setting from '$lib/components/Setting.svelte'; import { errorNotification } from '$lib/form'; import { post } from '$lib/api'; @@ -125,27 +125,35 @@