Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions php/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
$app->post('/api/docker/backup-test', AIO\Controller\DockerController::class . ':StartBackupContainerTest');
$app->post('/api/docker/restore', AIO\Controller\DockerController::class . ':StartBackupContainerRestore');
$app->post('/api/docker/stop', AIO\Controller\DockerController::class . ':StopContainer');
$app->post('/api/docker/prune', AIO\Controller\DockerController::class . ':SystemPrune');
$app->get('/api/docker/logs', AIO\Controller\DockerController::class . ':GetLogs');
$app->post('/api/auth/login', AIO\Controller\LoginController::class . ':TryLogin');
$app->get('/api/auth/getlogin', AIO\Controller\LoginController::class . ':GetTryLogin');
Expand Down
9 changes: 9 additions & 0 deletions php/src/Controller/DockerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ public function StopContainer(Request $request, Response $response, array $args)
return $response->withStatus(201)->withHeader('Location', '.');
}

public function SystemPrune(Request $request, Response $response, array $args) : Response {
$results = $this->dockerActionManager->SystemPrune();
$body = $response->getBody();
$body->write(json_encode($results));
return $response
->withStatus(200)
->withHeader('Content-Type', 'application/json; charset=utf-8');
}

public function stopTopContainer() : void {
$id = self::TOP_CONTAINER;
$this->PerformRecursiveContainerStop($id);
Expand Down
45 changes: 44 additions & 1 deletion php/src/Docker/DockerActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,4 +983,47 @@ public function GetLatestDigestOfTag(string $imageName, string $tag): ?string {
return $this->dockerHubManager->GetLatestDigestOfTag($imageName, $tag);
}
}
}

public function SystemPrune(): array {
$endpoints = [
// Remove stopped containers
'containers/prune',
// Remove unused images
'images/prune',
// Remove unused volumes
'volumes/prune',
// Remove unused networks
'networks/prune',
// Prune build cache
'build/prune',
];

$results = [];
foreach ($endpoints as $endpoint) {
// Special-case images prune to include the dangling filter as requested
if ($endpoint === 'images/prune') {
$filters = json_encode(['dangling' => ['false']]);
$url = $this->BuildApiUrl($endpoint . '?filters=' . urlencode($filters));
} else {
$url = $this->BuildApiUrl($endpoint);
}

try {
$resp = $this->guzzleClient->post($url);
$body = (string)$resp->getBody();
$json = null;
try {
$json = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable $e) {
// Non-JSON body, keep raw
$json = $body;
}
$results[$endpoint] = $json;
} catch (RequestException $e) {
error_log(sprintf('Docker prune (%s) failed: %s', $endpoint, $e->getMessage()));
$results[$endpoint] = ['error' => $e->getMessage()];
// continue with next prune step
}
}
return $results;
}
5 changes: 5 additions & 0 deletions php/templates/containers.twig
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input type="submit" value="Stop containers" />
</form>
<form method="POST" action="api/docker/prune" class="xhr">
<input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}">
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input type="submit" value="Run docker system prune" onclick="return confirm('Run docker system prune? This will remove unused images, containers and volumes. Continue?')" />
</form>
Comment on lines +325 to +329
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably move this somewhere else and use the loader to show the current process

{% endif %}
{% else %}
{% if isBackupOrRestoreRunning == true %}
Expand Down
Loading