fix: deletion + cleanupStuckedContainers

This commit is contained in:
Andras Bacsai 2023-01-20 10:10:36 +01:00
parent 27021538d8
commit 6b2a453b8f
12 changed files with 99 additions and 57 deletions

View File

@ -171,6 +171,11 @@ const host = '0.0.0.0';
await cleanupStorage(); await cleanupStorage();
}, 60000 * 15); }, 60000 * 15);
// Cleanup stucked containers (not defined in Coolify, but still running and managed by Coolify)
setInterval(async () => {
await cleanupStuckedContainers();
}, 60000 * 5);
// checkProxies, checkFluentBit & refresh templates // checkProxies, checkFluentBit & refresh templates
setInterval(async () => { setInterval(async () => {
await checkProxies(); await checkProxies();

View File

@ -732,14 +732,15 @@ export async function deleteApplication(
) { ) {
try { try {
const { id } = request.params; const { id } = request.params;
const { force } = request.body;
const { teamId } = request.user; const { teamId } = request.user;
const application = await prisma.application.findUnique({ const application = await prisma.application.findUnique({
where: { id }, where: { id },
include: { destinationDocker: true } include: { destinationDocker: true, teams: true }
}); });
if (!force && application?.destinationDockerId && application.destinationDocker?.network) { if (!application.teams.find((team) => team.id === teamId) || teamId !== '0') {
throw { status: 403, message: 'You are not allowed to delete this application.' };
}
if (application?.destinationDocker?.id && application.destinationDocker?.network) {
const { stdout: containers } = await executeCommand({ const { stdout: containers } = await executeCommand({
dockerId: application.destinationDocker.id, dockerId: application.destinationDocker.id,
command: `docker ps -a --filter network=${application.destinationDocker.network} --filter name=${id} --format '{{json .}}'` command: `docker ps -a --filter network=${application.destinationDocker.network} --filter name=${id} --format '{{json .}}'`

View File

@ -427,19 +427,15 @@ export async function deleteDatabase(request: FastifyRequest<DeleteDatabase>) {
try { try {
const teamId = request.user.teamId; const teamId = request.user.teamId;
const { id } = request.params; const { id } = request.params;
const { force } = request.body;
const database = await prisma.database.findFirst({ const database = await prisma.database.findFirst({
where: { id, teams: { some: { id: teamId === '0' ? undefined : teamId } } }, where: { id, teams: { some: { id: teamId === '0' ? undefined : teamId } } },
include: { destinationDocker: true, settings: true } include: { destinationDocker: true, settings: true }
}); });
if (!force) { if (database.dbUserPassword) database.dbUserPassword = decrypt(database.dbUserPassword);
if (database.dbUserPassword) database.dbUserPassword = decrypt(database.dbUserPassword); if (database.rootUserPassword) database.rootUserPassword = decrypt(database.rootUserPassword);
if (database.rootUserPassword) database.rootUserPassword = decrypt(database.rootUserPassword); if (database.destinationDockerId) {
if (database.destinationDockerId) { const everStarted = await stopDatabaseContainer(database);
const everStarted = await stopDatabaseContainer(database); if (everStarted) await stopTcpHttpProxy(id, database.destinationDocker, database.publicPort);
if (everStarted)
await stopTcpHttpProxy(id, database.destinationDocker, database.publicPort);
}
} }
await prisma.databaseSettings.deleteMany({ where: { databaseId: id } }); await prisma.databaseSettings.deleteMany({ where: { databaseId: id } });
await prisma.databaseSecret.deleteMany({ where: { databaseId: id } }); await prisma.databaseSecret.deleteMany({ where: { databaseId: id } });

View File

@ -4,7 +4,7 @@ export interface SaveDatabaseType extends OnlyId {
Body: { type: string } Body: { type: string }
} }
export interface DeleteDatabase extends OnlyId { export interface DeleteDatabase extends OnlyId {
Body: { force: string } Body: { }
} }
export interface SaveVersion extends OnlyId { export interface SaveVersion extends OnlyId {
Body: { Body: {

View File

@ -617,6 +617,29 @@ export async function getServiceLogs(request: FastifyRequest<GetServiceLogs>) {
export async function deleteService(request: FastifyRequest<OnlyId>) { export async function deleteService(request: FastifyRequest<OnlyId>) {
try { try {
const { id } = request.params; const { id } = request.params;
const teamId = request.user.teamId;
const { destinationDockerId } = await getServiceFromDB({ id, teamId });
if (destinationDockerId) {
const { stdout: containers } = await executeCommand({
dockerId: destinationDockerId,
command: `docker ps -a --filter 'label=com.docker.compose.project=${id}' --format {{.ID}}`
});
if (containers) {
const containerArray = containers.split('\n');
if (containerArray.length > 0) {
for (const container of containerArray) {
await executeCommand({
dockerId: destinationDockerId,
command: `docker stop -t 0 ${container}`
});
await executeCommand({
dockerId: destinationDockerId,
command: `docker rm --force ${container}`
});
}
}
}
}
await removeService({ id }); await removeService({ id });
return {}; return {};
} catch ({ status, message }) { } catch ({ status, message }) {

View File

@ -827,6 +827,29 @@ export const servicesRouter = router({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
// todo: check if user is allowed to delete service // todo: check if user is allowed to delete service
const { id } = input; const { id } = input;
const teamId = ctx.user?.teamId;
const { destinationDockerId } = await getServiceFromDB({ id, teamId });
if (destinationDockerId) {
const { stdout: containers } = await executeCommand({
dockerId: destinationDockerId,
command: `docker ps -a --filter 'label=com.docker.compose.project=${id}' --format {{.ID}}`
});
if (containers) {
const containerArray = containers.split('\n');
if (containerArray.length > 0) {
for (const container of containerArray) {
await executeCommand({
dockerId: destinationDockerId,
command: `docker stop -t 0 ${container}`
});
await executeCommand({
dockerId: destinationDockerId,
command: `docker rm --force ${container}`
});
}
}
}
}
await prisma.serviceSecret.deleteMany({ where: { serviceId: id } }); await prisma.serviceSecret.deleteMany({ where: { serviceId: id } });
await prisma.serviceSetting.deleteMany({ where: { serviceId: id } }); await prisma.serviceSetting.deleteMany({ where: { serviceId: id } });
await prisma.servicePersistentStorage.deleteMany({ where: { serviceId: id } }); await prisma.servicePersistentStorage.deleteMany({ where: { serviceId: id } });

View File

@ -89,7 +89,7 @@
const sure = confirm($t('application.confirm_to_delete', { name })); const sure = confirm($t('application.confirm_to_delete', { name }));
if (sure) { if (sure) {
try { try {
await del(`/applications/${id}`, { id, force }); await del(`/applications/${id}`, {});
return await goto('/'); return await goto('/');
} catch (error) { } catch (error) {
if (error.message.startsWith(`Command failed: SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid`)) { if (error.message.startsWith(`Command failed: SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid`)) {

View File

@ -34,7 +34,7 @@
if (sure) { if (sure) {
$status.application.initialLoading = true; $status.application.initialLoading = true;
try { try {
await del(`/applications/${id}`, { id, force }); await del(`/applications/${id}`,{});
return await goto('/') return await goto('/')
} catch (error) { } catch (error) {
if (error.message.startsWith(`Command failed: SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid`)) { if (error.message.startsWith(`Command failed: SSH_AUTH_SOCK=/tmp/coolify-ssh-agent.pid`)) {

View File

@ -75,7 +75,7 @@
if (sure) { if (sure) {
$status.database.initialLoading = true; $status.database.initialLoading = true;
try { try {
await del(`/databases/${database.id}`, { id: database.id, force }); await del(`/databases/${database.id}`, {});
return await window.location.assign('/'); return await window.location.assign('/');
} catch (error) { } catch (error) {
return errorNotification(error); return errorNotification(error);

View File

@ -437,7 +437,7 @@
try { try {
const sure = confirm('Are you sure? This will delete this application!'); const sure = confirm('Are you sure? This will delete this application!');
if (sure) { if (sure) {
await del(`/applications/${id}`, { force: true }); await del(`/applications/${id}`, {});
return window.location.reload(); return window.location.reload();
} }
} catch (error) { } catch (error) {
@ -459,7 +459,7 @@
try { try {
const sure = confirm('Are you sure? This will delete this database!'); const sure = confirm('Are you sure? This will delete this database!');
if (sure) { if (sure) {
await del(`/databases/${id}`, { force: true }); await del(`/databases/${id}`, { });
return window.location.reload(); return window.location.reload();
} }
} catch (error) { } catch (error) {
@ -784,11 +784,11 @@
</a> </a>
{/if} {/if}
{#if $appSession.isAdmin} {#if $appSession.isAdmin}
<button <button
class="icons hover:bg-green-500" class="icons hover:bg-green-500"
on:click|stopPropagation|preventDefault={() => on:click|stopPropagation|preventDefault={() =>
deleteApplication(application.id)}><DeleteIcon /></button deleteApplication(application.id)}><DeleteIcon /></button
> >
{/if} {/if}
</div> </div>
</div> </div>
@ -899,11 +899,11 @@
</a> </a>
{/if} {/if}
{#if $appSession.isAdmin} {#if $appSession.isAdmin}
<button <button
class="icons hover:bg-green-500" class="icons hover:bg-green-500"
on:click|stopPropagation|preventDefault={() => on:click|stopPropagation|preventDefault={() =>
deleteApplication(application.id)}><DeleteIcon /></button deleteApplication(application.id)}><DeleteIcon /></button
> >
{/if} {/if}
</div> </div>
</div> </div>
@ -996,11 +996,11 @@
</a> </a>
{/if} {/if}
{#if $appSession.isAdmin} {#if $appSession.isAdmin}
<button <button
class="icons hover:bg-pink-500" class="icons hover:bg-pink-500"
on:click|stopPropagation|preventDefault={() => deleteService(service.id)} on:click|stopPropagation|preventDefault={() => deleteService(service.id)}
><DeleteIcon /></button ><DeleteIcon /></button
> >
{/if} {/if}
</div> </div>
</div> </div>
@ -1084,11 +1084,11 @@
</a> </a>
{/if} {/if}
{#if $appSession.isAdmin} {#if $appSession.isAdmin}
<button <button
class="icons hover:bg-pink-500" class="icons hover:bg-pink-500"
on:click|stopPropagation|preventDefault={() => deleteService(service.id)} on:click|stopPropagation|preventDefault={() => deleteService(service.id)}
><DeleteIcon /></button ><DeleteIcon /></button
> >
{/if} {/if}
</div> </div>
</div> </div>
@ -1182,11 +1182,11 @@
</div> </div>
{/if} {/if}
{#if $appSession.isAdmin} {#if $appSession.isAdmin}
<button <button
class="icons hover:bg-databases-100" class="icons hover:bg-databases-100"
on:click|stopPropagation|preventDefault={() => deleteDatabase(database.id)} on:click|stopPropagation|preventDefault={() =>
><DeleteIcon /></button deleteDatabase(database.id)}><DeleteIcon /></button
> >
{/if} {/if}
</div> </div>
</div> </div>
@ -1270,11 +1270,11 @@
</div> </div>
{/if} {/if}
{#if $appSession.isAdmin} {#if $appSession.isAdmin}
<button <button
class="icons hover:bg-databases" class="icons hover:bg-databases"
on:click|stopPropagation|preventDefault={() => deleteDatabase(database.id)} on:click|stopPropagation|preventDefault={() => deleteDatabase(database.id)}
><DeleteIcon /></button ><DeleteIcon /></button
> >
{/if} {/if}
</div> </div>
</div> </div>

View File

@ -85,9 +85,7 @@
if (sure) { if (sure) {
$status.service.initialLoading = true; $status.service.initialLoading = true;
try { try {
if (service.type && $status.service.isRunning) await del(`/services/${service.id}`, {});
await post(`/services/${service.id}/stop`, {});
await del(`/services/${service.id}`, { id: service.id });
return await goto('/'); return await goto('/');
} catch (error) { } catch (error) {
return errorNotification(error); return errorNotification(error);

View File

@ -28,16 +28,12 @@
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
const { id } = $page.params; const { id } = $page.params;
let forceDelete = false;
async function deleteService() { async function deleteService() {
const sure = confirm($t('application.confirm_to_delete', { name: service.name })); const sure = confirm($t('application.confirm_to_delete', { name: service.name }));
if (sure) { if (sure) {
$status.service.initialLoading = true; $status.service.initialLoading = true;
try { try {
if (service.type && $status.service.overallStatus !== 'stopped') { await del(`/services/${service.id}`, {});
await post(`/services/${service.id}/stop`, {});
}
await del(`/services/${service.id}`, { id: service.id });
return await goto('/'); return await goto('/');
} catch (error) { } catch (error) {
return errorNotification(error); return errorNotification(error);