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 endpoints to retrieve service logs by UUID for each container

Yanluis Ferminbc9bfaefc78c
diff --git a/app/Http/Controllers/Api/ServicesController.php b/app/Http/Controllers/Api/ServicesController.php
index 542be83de4..61ff80a602 100644
--- a/app/Http/Controllers/Api/ServicesController.php
+++ b/app/Http/Controllers/Api/ServicesController.php
@@ -448,6 +448,121 @@ class ServicesController extends Controller
return response()->json($this->removeSensitiveData($service));
}
+ #[OA\Get(
+ summary: 'Get service logs.',
+ description: 'Get service logs by UUID.',
+ path: '/services/{uuid}/containers/{container_id}/logs',
+ operationId: 'get-service-logs-by-uuid',
+ security: [
+ ['bearerAuth' => []],
+ ],
+ tags: ['Services'],
+ parameters: [
+ new OA\Parameter(
+ name: 'uuid',
+ in: 'path',
+ description: 'UUID of the service.',
+ required: true,
+ schema: new OA\Schema(
+ type: 'string',
+ format: 'uuid',
+ )
+ ),
+ new OA\Parameter(
+ name: 'container_id',
+ in: 'path',
+ description: 'Container ID.',
+ required: true,
+ schema: new OA\Schema(type: 'string'),
+ ),
+ 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 service 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);
+ }
+ $service = Service::whereRelation('environment.project.team', 'id', $teamId)->whereUuid($request->uuid)->first();
+ if (! $service) {
+ return response()->json(['message' => 'Service not found.'], 404);
+ }
+
+ $containers = getCurrentServiceContainerStatus($service->destination->server, $service->id);
+
+ if ($containers->count() == 0) {
+ return response()->json([
+ 'message' => 'Service is not running.',
+ ], 400);
+ }
+
+ $container = $containers->first(function ($container) use ($request) {
+ return $container['ID'] === $request->container_id;
+ });
+
+ if (! $container) {
+ return response()->json(['message' => 'Container not found.'], 404);
+ }
+
+ $status = getContainerStatus($service->destination->server, $container['Names']);
+ if ($status !== 'running') {
+ return response()->json([
+ 'message' => 'Container is not running.',
+ ], 400);
+ }
+
+ $lines = $request->query->get('lines', 100) ?: 100;
+ $logs = getContainerLogs($service->destination->server, $container['ID'], $lines);
+
+ return response()->json([
+ 'logs' => $logs,
+ ]);
+ }
+
#[OA\Delete(
summary: 'Delete',
description: 'Delete service by UUID.',
diff --git a/routes/api.php b/routes/api.php
index 958c88fda9..c790627b8a 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -130,6 +130,7 @@ Route::group([
Route::patch('/services/{uuid}/envs/bulk', [ServicesController::class, 'create_bulk_envs'])->middleware(['api.ability:write']);
Route::patch('/services/{uuid}/envs', [ServicesController::class, 'update_env_by_uuid'])->middleware(['api.ability:write']);
Route::delete('/services/{uuid}/envs/{env_uuid}', [ServicesController::class, 'delete_env_by_uuid'])->middleware(['api.ability:write']);
+ Route::get('/services/{uuid}/containers/{container_id}/logs', [ServicesController::class, 'logs_by_uuid'])->middleware(['api.ability:read']);
Route::match(['get', 'post'], '/services/{uuid}/start', [ServicesController::class, 'action_deploy'])->middleware(['api.ability:write']);
Route::match(['get', 'post'], '/services/{uuid}/restart', [ServicesController::class, 'action_restart'])->middleware(['api.ability:write']);