Files
coolify/app/Actions/Service/DeleteService.php
T
Andras BacsaiandClaude Opus 5.5 9270413b5f fix(services): remove service containers on servers with a non-root user
The container removal started with a variable assignment
(container_ids=$(docker ps ...)). For non-root SSH users the sudo
parser turned it into "sudo container_ids=...", which sudo rejects,
so deleting a service or a service sub-resource failed. The script now
runs as one sh -c line, which the parser runs with sudo as a whole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 23:49:21 +02:00

90 lines
3.3 KiB
PHP

<?php
namespace App\Actions\Service;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
use RuntimeException;
class DeleteService
{
public function cleanupRemote(Service $service, bool $deleteVolumes, bool $deleteConnectedNetworks, bool $deleteConfigurations): void
{
$server = data_get($service, 'server');
if (! $server?->isFunctional()) {
throw new RuntimeException('Server is not functional.');
}
$this->removeContainers($service);
if ($deleteVolumes) {
$commands = [];
foreach ($service->applications()->get() as $application) {
foreach ($application->persistentStorages()->get() as $storage) {
$commands[] = 'docker volume rm -f '.escapeshellarg($storage->name);
}
}
foreach ($service->databases()->get() as $database) {
foreach ($database->persistentStorages()->get() as $storage) {
$commands[] = 'docker volume rm -f '.escapeshellarg($storage->name);
}
}
foreach ($commands as $command) {
instant_remote_process([$command], $server);
}
}
if ($deleteConnectedNetworks) {
$service->deleteConnectedNetworks();
}
if ($deleteConfigurations) {
$service->deleteConfigurations();
}
}
public function removeSubresourceContainer(ServiceApplication|ServiceDatabase $resource): void
{
$service = $resource->service;
$server = $service?->server;
if (! $server?->isFunctional()) {
throw new RuntimeException('Server is not functional.');
}
$this->removeContainers($service, $resource);
}
private function removeContainers(Service $service, ServiceApplication|ServiceDatabase|null $subresource = null): void
{
$serviceId = (int) $service->id;
$filters = "--filter label=coolify.serviceId={$serviceId}";
if ($subresource !== null) {
// Applications and databases are separate tables, so an id alone can match the other type.
$subType = $subresource instanceof ServiceDatabase ? 'database' : 'application';
$subId = (int) $subresource->id;
$filters .= " --filter label=coolify.service.subId={$subId} --filter label=coolify.service.subType={$subType}";
}
// One sh -c line, so non-root servers run the whole script with sudo. A leading variable
// assignment would become "sudo container_ids=...", which sudo rejects.
$script = "container_ids=\$(docker ps -aq {$filters}); [ -z \"\$container_ids\" ] || docker rm -f \$container_ids";
instant_remote_process(['sh -c '.escapeshellarg($script)], $service->server);
}
public function deleteLocal(Service $service): void
{
foreach ($service->applications()->get() as $application) {
$application->forceDelete();
}
foreach ($service->databases()->get() as $database) {
$database->forceDelete();
}
foreach ($service->scheduled_tasks as $task) {
$task->delete();
}
$service->environment_variables()->delete();
$service->tags()->detach();
$service->forceDelete();
}
}