From c5bcff0e1056bf9a8a14cb6a8c62f32106142d2e Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 9 Sep 2022 15:14:58 +0200 Subject: [PATCH] fix: show restarting application & logs --- apps/api/src/lib/common.ts | 8 ++--- apps/api/src/lib/docker.ts | 25 ++++++++++++---- apps/api/src/lib/importers/gitlab.ts | 24 +++++++++++---- apps/api/src/lib/services/handlers.ts | 1 - .../routes/api/v1/applications/handlers.ts | 17 +++++++---- .../routes/api/v1/destinations/handlers.ts | 2 +- .../src/routes/api/v1/services/handlers.ts | 12 +++++--- .../src/routes/webhooks/github/handlers.ts | 2 +- .../src/routes/webhooks/gitlab/handlers.ts | 2 +- apps/ui/src/lib/store.ts | 1 + .../routes/applications/[id]/__layout.svelte | 30 +++++++++++++++++-- .../applications/[id]/logs/index.svelte | 11 +++++-- 12 files changed, 102 insertions(+), 33 deletions(-) diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index f4cf94ba1..55bfb08ab 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -537,7 +537,7 @@ export async function executeDockerCmd({ debug, buildId, applicationId, dockerId } export async function startTraefikProxy(id: string): Promise { const { engine, network, remoteEngine, remoteIpAddress } = await prisma.destinationDocker.findUnique({ where: { id } }) - const found = await checkContainer({ dockerId: id, container: 'coolify-proxy', remove: true }); + const { found } = await checkContainer({ dockerId: id, container: 'coolify-proxy', remove: true }); const { id: settingsId, ipv4, ipv6 } = await listSettings(); if (!found) { @@ -621,7 +621,7 @@ export async function configureNetworkTraefikProxy(destination: any): Promise { - const found = await checkContainer({ dockerId: id, container: 'coolify-proxy' }); + const { found } = await checkContainer({ dockerId: id, container: 'coolify-proxy' }); await prisma.destinationDocker.update({ where: { id }, data: { isCoolifyProxyUsed: false } @@ -1062,7 +1062,7 @@ export async function stopTcpHttpProxy( const { id: dockerId } = destinationDocker; let container = `${id}-${publicPort}`; if (forceName) container = forceName; - const found = await checkContainer({ dockerId, container }); + const { found } = await checkContainer({ dockerId, container }); try { if (found) { return await executeDockerCmd({ @@ -1284,7 +1284,7 @@ export async function startTraefikTCPProxy( ): Promise<{ stdout: string; stderr: string } | Error> { const { network, id: dockerId, remoteEngine } = destinationDocker; const container = `${id}-${publicPort}`; - const found = await checkContainer({ dockerId, container, remove: true }); + const { found } = await checkContainer({ dockerId, container, remove: true }); const { ipv4, ipv6 } = await listSettings(); let dependentId = id; diff --git a/apps/api/src/lib/docker.ts b/apps/api/src/lib/docker.ts index 8cf71422c..dff59b7db 100644 --- a/apps/api/src/lib/docker.ts +++ b/apps/api/src/lib/docker.ts @@ -13,7 +13,7 @@ export function formatLabelsOnDocker(data) { return container }) } -export async function checkContainer({ dockerId, container, remove = false }: { dockerId: string, container: string, remove?: boolean }): Promise { +export async function checkContainer({ dockerId, container, remove = false }: { dockerId: string, container: string, remove?: boolean }): Promise<{ found: boolean, status?: { isExited: boolean, isRunning: boolean, isRestarting: boolean } }> { let containerFound = false; try { const { stdout } = await executeDockerCmd({ @@ -21,9 +21,12 @@ export async function checkContainer({ dockerId, container, remove = false }: { command: `docker inspect --format '{{json .State}}' ${container}` }); + containerFound = true const parsedStdout = JSON.parse(stdout); const status = parsedStdout.Status; - const isRunning = status === 'running' || status === 'restarting'; + const isRunning = status === 'running'; + const isRestarting = status === 'restarting' + const isExited = status === 'exited' if (status === 'created') { await executeDockerCmd({ dockerId, @@ -38,13 +41,23 @@ export async function checkContainer({ dockerId, container, remove = false }: { `docker rm ${container}` }); } - if (isRunning) { - containerFound = true; - } + + return { + found: containerFound, + status: { + isRunning, + isRestarting, + isExited + + } + }; } catch (err) { // Container not found } - return containerFound; + return { + found: false + }; + } export async function isContainerExited(dockerId: string, containerName: string): Promise { diff --git a/apps/api/src/lib/importers/gitlab.ts b/apps/api/src/lib/importers/gitlab.ts index b1784b6b9..69f204155 100644 --- a/apps/api/src/lib/importers/gitlab.ts +++ b/apps/api/src/lib/importers/gitlab.ts @@ -10,7 +10,8 @@ export default async function ({ branch, buildId, privateSshKey, - customPort + customPort, + forPublic }: { applicationId: string; workdir: string; @@ -21,11 +22,15 @@ export default async function ({ repodir: string; privateSshKey: string; customPort: number; + forPublic: boolean; }): Promise { const url = htmlUrl.replace('https://', '').replace('http://', '').replace(/\/$/, ''); await saveBuildLog({ line: 'GitLab importer started.', buildId, applicationId }); - await asyncExecShell(`echo '${privateSshKey}' > ${repodir}/id.rsa`); - await asyncExecShell(`chmod 600 ${repodir}/id.rsa`); + + if (!forPublic) { + await asyncExecShell(`echo '${privateSshKey}' > ${repodir}/id.rsa`); + await asyncExecShell(`chmod 600 ${repodir}/id.rsa`); + } await saveBuildLog({ line: `Cloning ${repository}:${branch} branch.`, @@ -33,9 +38,16 @@ export default async function ({ applicationId }); - await asyncExecShell( - `git clone -q -b ${branch} git@${url}:${repository}.git --config core.sshCommand="ssh -p ${customPort} -q -i ${repodir}id.rsa -o StrictHostKeyChecking=no" ${workdir}/ && cd ${workdir}/ && git submodule update --init --recursive && git lfs pull && cd .. ` - ); + if (forPublic) { + await asyncExecShell( + `git clone -q -b ${branch} git@${url}:${repository}.git --config core.sshCommand="ssh -p ${customPort} -q -o StrictHostKeyChecking=no" ${workdir}/ && cd ${workdir}/ && git submodule update --init --recursive && git lfs pull && cd .. ` + ); + } else { + await asyncExecShell( + `git clone -q -b ${branch} git@${url}:${repository}.git --config core.sshCommand="ssh -p ${customPort} -q -i ${repodir}id.rsa -o StrictHostKeyChecking=no" ${workdir}/ && cd ${workdir}/ && git submodule update --init --recursive && git lfs pull && cd .. ` + ); + } + const { stdout: commit } = await asyncExecShell(`cd ${workdir}/ && git rev-parse HEAD`); return commit.replace('\n', ''); } diff --git a/apps/api/src/lib/services/handlers.ts b/apps/api/src/lib/services/handlers.ts index 7f13f7219..f2f0aafa9 100644 --- a/apps/api/src/lib/services/handlers.ts +++ b/apps/api/src/lib/services/handlers.ts @@ -1116,7 +1116,6 @@ async function startUmamiService(request: FastifyRequest) { }, volumes: volumeMounts }; - console.log(composeFile) const composeFileDestination = `${workdir}/docker-compose.yaml`; await fs.writeFile(composeFileDestination, yaml.dump(composeFile)); await startServiceContainers(destinationDocker.id, composeFileDestination) diff --git a/apps/api/src/routes/api/v1/applications/handlers.ts b/apps/api/src/routes/api/v1/applications/handlers.ts index 8a390a3f5..65f2eac8e 100644 --- a/apps/api/src/routes/api/v1/applications/handlers.ts +++ b/apps/api/src/routes/api/v1/applications/handlers.ts @@ -74,14 +74,21 @@ export async function getApplicationStatus(request: FastifyRequest) { const { teamId } = request.user let isRunning = false; let isExited = false; - + let isRestarting = false; const application: any = await getApplicationFromDB(id, teamId); if (application?.destinationDockerId) { - isRunning = await checkContainer({ dockerId: application.destinationDocker.id, container: id }); - isExited = await isContainerExited(application.destinationDocker.id, id); + const status = await checkContainer({ dockerId: application.destinationDocker.id, container: id }); + if (status?.found) { + isRunning = status.status.isRunning; + isExited = status.status.isExited; + isRestarting = status.status.isRestarting + } + + // isExited = await isContainerExited(application.destinationDocker.id, id); } return { isRunning, + isRestarting, isExited, }; } catch ({ status, message }) { @@ -339,7 +346,7 @@ export async function stopPreviewApplication(request: FastifyRequest, reply: Fa const application: any = await getApplicationFromDB(id, teamId); if (application?.destinationDockerId) { const { id: dockerId } = application.destinationDocker; - const found = await checkContainer({ dockerId, container: id }); + const { found } = await checkContainer({ dockerId, container: id }); if (found) { await removeContainer({ id, dockerId: application.destinationDocker.id }); } diff --git a/apps/api/src/routes/api/v1/destinations/handlers.ts b/apps/api/src/routes/api/v1/destinations/handlers.ts index bb96981cf..e4f038881 100644 --- a/apps/api/src/routes/api/v1/destinations/handlers.ts +++ b/apps/api/src/routes/api/v1/destinations/handlers.ts @@ -229,7 +229,7 @@ export async function getDestinationStatus(request: FastifyRequest) { try { const { id } = request.params const destination = await prisma.destinationDocker.findUnique({ where: { id } }) - const isRunning = await checkContainer({ dockerId: destination.id, container: 'coolify-proxy', remove: true }) + const { found: isRunning } = await checkContainer({ dockerId: destination.id, container: 'coolify-proxy', remove: true }) return { isRunning } diff --git a/apps/api/src/routes/api/v1/services/handlers.ts b/apps/api/src/routes/api/v1/services/handlers.ts index c6f10cbb4..48e58ca78 100644 --- a/apps/api/src/routes/api/v1/services/handlers.ts +++ b/apps/api/src/routes/api/v1/services/handlers.ts @@ -43,13 +43,17 @@ export async function getServiceStatus(request: FastifyRequest) { let isRunning = false; let isExited = false - + let isRestarting = false; const service = await getServiceFromDB({ id, teamId }); const { destinationDockerId, settings } = service; if (destinationDockerId) { - isRunning = await checkContainer({ dockerId: service.destinationDocker.id, container: id }); - isExited = await isContainerExited(service.destinationDocker.id, id); + const status = await checkContainer({ dockerId: service.destinationDocker.id, container: id }); + if (status?.found) { + isRunning = status.status.isRunning; + isExited = status.status.isExited; + isRestarting = status.status.isRestarting + } } return { isRunning, @@ -554,7 +558,7 @@ export async function activateWordpressFtp(request: FastifyRequest): Promi if (application.settings.previews) { if (application.destinationDockerId) { - const isRunning = await checkContainer( + const { found: isRunning } = await checkContainer( { dockerId: application.destinationDocker.id, container: application.id diff --git a/apps/api/src/routes/webhooks/gitlab/handlers.ts b/apps/api/src/routes/webhooks/gitlab/handlers.ts index 58d01182a..264344c4a 100644 --- a/apps/api/src/routes/webhooks/gitlab/handlers.ts +++ b/apps/api/src/routes/webhooks/gitlab/handlers.ts @@ -107,7 +107,7 @@ export async function gitLabEvents(request: FastifyRequest) { const buildId = cuid(); if (application.settings.previews) { if (application.destinationDockerId) { - const isRunning = await checkContainer( + const { found: isRunning } = await checkContainer( { dockerId: application.destinationDocker.id, container: application.id diff --git a/apps/ui/src/lib/store.ts b/apps/ui/src/lib/store.ts index ec841a2e5..b0719e591 100644 --- a/apps/ui/src/lib/store.ts +++ b/apps/ui/src/lib/store.ts @@ -73,6 +73,7 @@ export const status: Writable = writable({ application: { isRunning: false, isExited: false, + isRestarting: false, loading: false, initialLoading: true }, diff --git a/apps/ui/src/routes/applications/[id]/__layout.svelte b/apps/ui/src/routes/applications/[id]/__layout.svelte index f2854f092..89127fce6 100644 --- a/apps/ui/src/routes/applications/[id]/__layout.svelte +++ b/apps/ui/src/routes/applications/[id]/__layout.svelte @@ -154,6 +154,7 @@ const data = await get(`/applications/${id}/status`); $status.application.isRunning = data.isRunning; $status.application.isExited = data.isExited; + $status.application.isRestarting = data.isRestarting; $status.application.loading = false; $status.application.initialLoading = false; } @@ -162,6 +163,7 @@ $status.application.initialLoading = true; $status.application.isRunning = false; $status.application.isExited = false; + $status.application.isRestarting = false; $status.application.loading = false; $location = null; $isDeploymentEnabled = false; @@ -171,6 +173,7 @@ setLocation(application, settings); $status.application.isRunning = false; $status.application.isExited = false; + $status.application.isRestarting = false; $status.application.loading = false; if ( application.gitSourceId && @@ -215,7 +218,7 @@
{/if} - {#if $status.application.isExited} + {#if $status.application.isExited || $status.application.isRestarting} - Application exited with an error! + Application exited or restarting! + + Stop {/if} {#if $status.application.initialLoading}