Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(api): add endpoint to retrieve database logs by UUID

Yanluis Fermin5848b07fc244
diff --git a/app/Http/Controllers/Api/DatabasesController.php b/app/Http/Controllers/Api/DatabasesController.php
index 504665f6a8..fc2b7b6d04 100644
--- a/app/Http/Controllers/Api/DatabasesController.php
+++ b/app/Http/Controllers/Api/DatabasesController.php
@@ -1535,6 +1535,107 @@ class DatabasesController extends Controller
return response()->json(['message' => 'Invalid database type requested.'], 400);
}
+ #[OA\Get(
+ summary: 'Get database logs.',
+ description: 'Get database logs by UUID.',
+ path: '/databases/{uuid}/logs',
+ operationId: 'get-database-logs-by-uuid',
+ security: [
+ ['bearerAuth' => []],
+ ],
+ tags: ['Databases'],
+ parameters: [
+ new OA\Parameter(
+ name: 'uuid',
+ in: 'path',
+ description: 'UUID of the database.',
+ required: true,
+ schema: new OA\Schema(
+ type: 'string',
+ format: 'uuid',
+ )
+ ),
+ new OA\Parameter(
+ name: 'lines',
+ in: 'query',
+ description: 'Number of lines to show from the end of the logs.',
+ required: false,
+ schema: new OA\Schema(
+ type: 'integer',
+ format: 'int32',
+ default: 100,
+ )
+ ),
+ ],
+ responses: [
+ new OA\Response(
+ response: 200,
+ description: 'Get database logs by UUID.',
+ content: [
+ new OA\MediaType(
+ mediaType: 'application/json',
+ schema: new OA\Schema(
+ type: 'object',
+ properties: [
+ 'logs' => ['type' => 'string'],
+ ]
+ )
+ ),
+ ]
+ ),
+ new OA\Response(
+ response: 401,
+ ref: '#/components/responses/401',
+ ),
+ new OA\Response(
+ response: 400,
+ ref: '#/components/responses/400',
+ ),
+ new OA\Response(
+ response: 404,
+ ref: '#/components/responses/404',
+ ),
+ ]
+ )]
+ public function logs_by_uuid(Request $request)
+ {
+ $teamId = getTeamIdFromToken();
+ if (is_null($teamId)) {
+ return invalidTokenResponse();
+ }
+ $uuid = $request->route('uuid');
+ if (! $uuid) {
+ return response()->json(['message' => 'UUID is required.'], 400);
+ }
+ $database = queryDatabaseByUuidWithinTeam($uuid, $teamId);
+ if (! $database) {
+ return response()->json(['message' => 'Database not found.'], 404);
+ }
+
+ $containers = getCurrentDatabaseContainerStatus($database->destination->server, $database->id);
+
+ if ($containers->count() == 0) {
+ return response()->json([
+ 'message' => 'Database is not running.',
+ ], 400);
+ }
+
+ $container = $containers->first();
+
+ $status = getContainerStatus($database->destination->server, $container['Names']);
+ if ($status !== 'running') {
+ return response()->json([
+ 'message' => 'Database is not running.',
+ ], 400);
+ }
+
+ $lines = $request->query->get('lines', 100) ?: 100;
+ $logs = getContainerLogs($database->destination->server, $container['ID'], $lines);
+
+ return response()->json([
+ 'logs' => $logs,
+ ]);
+ }
#[OA\Delete(
summary: 'Delete',
diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php
index 944c51e3cc..cac8ffb6c1 100644
--- a/bootstrap/helpers/docker.php
+++ b/bootstrap/helpers/docker.php
@@ -53,6 +53,19 @@ function getCurrentServiceContainerStatus(Server $server, int $id): Collection
return $containers;
}
+function getCurrentDatabaseContainerStatus(Server $server, int $id): Collection
+{
+ $containers = collect([]);
+ if (! $server->isSwarm()) {
+ $containers = instant_remote_process(["docker ps -a --filter='label=coolify.databaseId={$id}' --format '{{json .}}' "], $server);
+ $containers = format_docker_command_output_to_json($containers);
+
+ return $containers->filter();
+ }
+
+ return $containers;
+}
+
function format_docker_command_output_to_json($rawOutput): Collection
{
$outputLines = explode(PHP_EOL, $rawOutput);
diff --git a/routes/api.php b/routes/api.php
index d63e3ee0ef..958c88fda9 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -112,6 +112,7 @@ Route::group([
Route::get('/databases/{uuid}', [DatabasesController::class, 'database_by_uuid'])->middleware(['api.ability:read']);
Route::patch('/databases/{uuid}', [DatabasesController::class, 'update_by_uuid'])->middleware(['api.ability:write']);
Route::delete('/databases/{uuid}', [DatabasesController::class, 'delete_by_uuid'])->middleware(['api.ability:write']);
+ Route::get('/databases/{uuid}/logs', [DatabasesController::class, 'logs_by_uuid'])->middleware(['api.ability:read']);
Route::match(['get', 'post'], '/databases/{uuid}/start', [DatabasesController::class, 'action_deploy'])->middleware(['api.ability:write']);
Route::match(['get', 'post'], '/databases/{uuid}/restart', [DatabasesController::class, 'action_restart'])->middleware(['api.ability:write']);