feat: cleanup unconfigured applications

This commit is contained in:
Andras Bacsai 2022-09-28 11:45:02 +02:00
parent 60a033f93a
commit 28377a156d
7 changed files with 88 additions and 21 deletions

View File

@ -21,7 +21,7 @@ import { scheduler } from './scheduler';
import { supportedServiceTypesAndVersions } from './services/supportedVersions';
import { includeServices } from './services/common';
export const version = '3.10.11';
export const version = '3.10.12';
export const isDev = process.env.NODE_ENV === 'development';
const algorithm = 'aes-256-ctr';

View File

@ -69,6 +69,43 @@ export async function getImages(request: FastifyRequest<GetImages>) {
return errorHandler({ status, message })
}
}
export async function cleanupUnconfigured(request: FastifyRequest<any>) {
try {
let teamId = request.user.teamId
let applications = await prisma.application.findMany({
where: { teams: { some: { id: teamId === "0" ? undefined : teamId } } },
include: { settings: true, destinationDocker: true, teams: true },
});
for (const application of applications) {
if (!application.buildPack || !application.destinationDockerId || !application.branch || (!application.settings?.isBot && !application?.fqdn)) {
if (application?.destinationDockerId && application.destinationDocker?.network) {
const { stdout: containers } = await executeDockerCmd({
dockerId: application.destinationDocker.id,
command: `docker ps -a --filter network=${application.destinationDocker.network} --filter name=${application.id} --format '{{json .}}'`
})
if (containers) {
const containersArray = containers.trim().split('\n');
for (const container of containersArray) {
const containerObj = JSON.parse(container);
const id = containerObj.ID;
await removeContainer({ id, dockerId: application.destinationDocker.id });
}
}
}
await prisma.applicationSettings.deleteMany({ where: { applicationId: application.id } });
await prisma.buildLog.deleteMany({ where: { applicationId: application.id } });
await prisma.build.deleteMany({ where: { applicationId: application.id } });
await prisma.secret.deleteMany({ where: { applicationId: application.id } });
await prisma.applicationPersistentStorage.deleteMany({ where: { applicationId: application.id } });
await prisma.applicationConnectedDatabase.deleteMany({ where: { applicationId: application.id } });
await prisma.application.deleteMany({ where: { id: application.id } });
}
}
return {}
} catch ({ status, message }) {
return errorHandler({ status, message })
}
}
export async function getApplicationStatus(request: FastifyRequest<OnlyId>) {
try {
const { id } = request.params

View File

@ -1,6 +1,6 @@
import { FastifyPluginAsync } from 'fastify';
import { OnlyId } from '../../../../types';
import { cancelDeployment, checkDNS, checkDomain, checkRepository, deleteApplication, deleteSecret, deleteStorage, deployApplication, getApplication, getApplicationLogs, getApplicationStatus, getBuildIdLogs, getBuildPack, getBuilds, getGitHubToken, getGitLabSSHKey, getImages, getPreviews, getPreviewStatus, getSecrets, getStorages, getUsage, listApplications, loadPreviews, newApplication, restartApplication, restartPreview, saveApplication, saveApplicationSettings, saveApplicationSource, saveBuildPack, saveConnectedDatabase, saveDeployKey, saveDestination, saveGitLabSSHKey, saveRepository, saveSecret, saveStorage, stopApplication, stopPreviewApplication, updatePreviewSecret, updateSecret } from './handlers';
import { cancelDeployment, checkDNS, checkDomain, checkRepository, cleanupUnconfigured, deleteApplication, deleteSecret, deleteStorage, deployApplication, getApplication, getApplicationLogs, getApplicationStatus, getBuildIdLogs, getBuildPack, getBuilds, getGitHubToken, getGitLabSSHKey, getImages, getPreviews, getPreviewStatus, getSecrets, getStorages, getUsage, listApplications, loadPreviews, newApplication, restartApplication, restartPreview, saveApplication, saveApplicationSettings, saveApplicationSource, saveBuildPack, saveConnectedDatabase, saveDeployKey, saveDestination, saveGitLabSSHKey, saveRepository, saveSecret, saveStorage, stopApplication, stopPreviewApplication, updatePreviewSecret, updateSecret } from './handlers';
import type { CancelDeployment, CheckDNS, CheckDomain, CheckRepository, DeleteApplication, DeleteSecret, DeleteStorage, DeployApplication, GetApplicationLogs, GetBuildIdLogs, GetBuilds, GetImages, RestartPreviewApplication, SaveApplication, SaveApplicationSettings, SaveApplicationSource, SaveDeployKey, SaveDestination, SaveSecret, SaveStorage, StopPreviewApplication } from './types';
@ -11,6 +11,8 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.get('/', async (request) => await listApplications(request));
fastify.post<GetImages>('/images', async (request) => await getImages(request));
fastify.post<any>('/cleanup/unconfigured', async (request) => await cleanupUnconfigured(request));
fastify.post('/new', async (request, reply) => await newApplication(request, reply));
fastify.get<OnlyId>('/:id', async (request) => await getApplication(request));

View File

@ -122,7 +122,7 @@ export async function showDashboard(request: FastifyRequest) {
try {
const userId = request.user.userId;
const teamId = request.user.teamId;
const applications = await prisma.application.findMany({
let applications = await prisma.application.findMany({
where: { teams: { some: { id: teamId === "0" ? undefined : teamId } } },
include: { settings: true, destinationDocker: true, teams: true },
});
@ -143,7 +143,15 @@ export async function showDashboard(request: FastifyRequest) {
include: { teams: true },
});
const settings = await listSettings();
let foundUnconfiguredApplication = false;
for (const application of applications) {
if (!application.buildPack || !application.destinationDockerId || !application.branch || (!application.settings?.isBot && !application?.fqdn)) {
foundUnconfiguredApplication = true
}
}
return {
foundUnconfiguredApplication,
applications,
databases,
services,

View File

@ -19,7 +19,7 @@
<div class="dropdown dropdown-bottom">
<slot>
<label for="new" tabindex="0" class="btn btn-sm text-sm bg-coollabs hover:bg-coollabs-100">
Create New Resource <svg
<svg
class="h-6 w-6"
xmlns="http://www.w3.org/2000/svg"
fill="none"
@ -31,8 +31,8 @@
stroke-width="2"
d="M12 6v6m0 0v6m0-6h6m-6 0H6"
/></svg
></label
>
> Create New Resource
</label>
</slot>
<ul id="new" tabindex="0" class="dropdown-content menu p-2 shadow bg-coolgray-300 rounded w-52">

View File

@ -21,6 +21,7 @@
<script lang="ts">
export let applications: any;
export let foundUnconfiguredApplication: boolean;
export let databases: any;
export let services: any;
export let settings: any;
@ -28,9 +29,9 @@
export let destinations: any;
let filtered: any = setInitials();
import { get } from '$lib/api';
import { get, post } from '$lib/api';
import { t } from '$lib/translations';
import { asyncSleep, getRndInteger } from '$lib/common';
import { asyncSleep, errorNotification, getRndInteger } from '$lib/common';
import { appSession, search } from '$lib/store';
import ApplicationsIcons from '$lib/components/svg/applications/ApplicationIcons.svelte';
@ -325,6 +326,17 @@
filtered = setInitials();
}
}
async function cleanupApplications() {
try {
const sure = confirm('Are you sure? This will delete all UNCONFIGURED applications and their data.');
if (sure) {
await post(`/applications/cleanup/unconfigured`, {});
return window.location.reload();
}
} catch (error) {
return errorNotification(error);
}
}
</script>
<nav class="header">
@ -334,7 +346,7 @@
{/if}
</nav>
<div class="container lg:mx-auto lg:p-0 px-8 pt-5">
<div class="space-x-2 lg:flex lg:justify-center text-center mb-4 ">
<div class="space-x-2 lg:flex lg:justify-center text-center mb-4 ">
<button
class="btn btn-sm btn-ghost"
class:bg-applications={$search === '!app'}
@ -521,7 +533,7 @@
</div>
{/if}
{#if (filtered.applications.length > 0 && applications.length > 0) || filtered.otherApplications.length > 0}
<div class="flex items-center mt-10">
<div class="flex items-center mt-10 space-x-2">
<h1 class="title lg:text-3xl pr-4">Applications</h1>
<button
class="btn btn-sm btn-primary"
@ -530,6 +542,14 @@
on:click={refreshStatusApplications}
>{noInitialStatus.applications ? 'Load Status' : 'Refresh Status'}</button
>
{#if foundUnconfiguredApplication}
<button
class="btn btn-sm"
class:loading={loading.applications}
disabled={loading.applications}
on:click={cleanupApplications}>Cleanup Unconfigured Resources</button
>
{/if}
</div>
{/if}
{#if filtered.applications.length > 0 && applications.length > 0}
@ -559,7 +579,7 @@
<div class="w-full flex flex-row">
<ApplicationsIcons {application} isAbsolute={true} />
<div class="w-full flex flex-col">
<h1 class="font-bold text-lg lg:text-xl truncate">
<h1 class="font-bold text-base truncate">
{application.name}
{#if application.settings?.isBot}
<span class="text-xs badge bg-coolblack border-none text-applications"
@ -666,7 +686,7 @@
<div class="w-full flex flex-row">
<ApplicationsIcons {application} isAbsolute={true} />
<div class="w-full flex flex-col">
<h1 class="font-bold text-lg lg:text-xl truncate">
<h1 class="font-bold text-base truncate">
{application.name}
{#if application.settings?.isBot}
<span class="text-xs badge bg-coolblack border-none text-applications">BOT</span
@ -778,7 +798,7 @@
<div class="w-full flex flex-row">
<ServiceIcons type={service.type} isAbsolute={true} />
<div class="w-full flex flex-col">
<h1 class="font-bold text-lg lg:text-xl truncate">{service.name}</h1>
<h1 class="font-bold text-base truncate">{service.name}</h1>
<div class="h-10 text-xs">
{#if service?.fqdn}
<h2>{service?.fqdn.replace('https://', '').replace('http://', '')}</h2>
@ -851,7 +871,7 @@
<div class="w-full flex flex-row">
<ServiceIcons type={service.type} isAbsolute={true} />
<div class="w-full flex flex-col">
<h1 class="font-bold text-lg lg:text-xl truncate">{service.name}</h1>
<h1 class="font-bold text-base truncate">{service.name}</h1>
<div class="h-10 text-xs">
{#if service?.fqdn}
<h2>{service?.fqdn.replace('https://', '').replace('http://', '')}</h2>
@ -933,7 +953,7 @@
<DatabaseIcons type={database.type} isAbsolute={true} />
<div class="w-full flex flex-col">
<div class="h-10">
<h1 class="font-bold text-lg lg:text-xl truncate">{database.name}</h1>
<h1 class="font-bold text-base truncate">{database.name}</h1>
<div class="h-10 text-xs">
{#if database?.version}
<h2 class="">{database?.version}</h2>
@ -1010,7 +1030,7 @@
<DatabaseIcons type={database.type} isAbsolute={true} />
<div class="w-full flex flex-col">
<div class="h-10">
<h1 class="font-bold text-lg lg:text-xl truncate">{database.name}</h1>
<h1 class="font-bold text-base truncate">{database.name}</h1>
<div class="h-10 text-xs">
{#if database?.version}
<h2 class="">{database?.version}</h2>
@ -1129,7 +1149,7 @@
</div>
<div class="w-full flex flex-col">
<div class="h-10">
<h1 class="font-bold text-lg lg:text-xl truncate">{source.name}</h1>
<h1 class="font-bold text-base truncate">{source.name}</h1>
{#if source.teams.length > 0 && source.teams[0]?.name}
<div class="truncate text-xs">{source.teams[0]?.name}</div>
{/if}
@ -1218,7 +1238,7 @@
</div>
<div class="w-full flex flex-col">
<div class="h-10">
<h1 class="font-bold text-lg lg:text-xl truncate">{source.name}</h1>
<h1 class="font-bold text-base truncate">{source.name}</h1>
{#if source.teams.length > 0 && source.teams[0]?.name}
<div class="truncate text-xs">{source.teams[0]?.name}</div>
{/if}
@ -1292,7 +1312,7 @@
{/if}
</div>
<div class="w-full flex flex-col">
<h1 class="font-bold text-lg lg:text-xl truncate">{destination.name}</h1>
<h1 class="font-bold text-base truncate">{destination.name}</h1>
<div class="h-10 text-xs">
{#if $appSession.teamId === '0' && destination.remoteVerified === false && destination.remoteEngine}
<h2 class="text-red-500">Not verified yet</h2>
@ -1373,7 +1393,7 @@
{/if}
</div>
<div class="w-full flex flex-col">
<h1 class="font-bold text-lg lg:text-xl truncate">{destination.name}</h1>
<h1 class="font-bold text-base truncate">{destination.name}</h1>
<div class="h-10 text-xs">
{#if $appSession.teamId === '0' && destination.remoteVerified === false && destination.remoteEngine}
<h2 class="text-red-500">Not verified yet</h2>

View File

@ -1,7 +1,7 @@
{
"name": "coolify",
"description": "An open-source & self-hostable Heroku / Netlify alternative.",
"version": "3.10.11",
"version": "3.10.12",
"license": "Apache-2.0",
"repository": "github:coollabsio/coolify",
"scripts": {