refactor(api): update service logs endpoint to use sub service name
This commit is contained in:
parent
bc9bfaefc7
commit
28e20473da
3 changed files with 24 additions and 9 deletions
|
|
@ -451,7 +451,7 @@ public function service_by_uuid(Request $request)
|
|||
#[OA\Get(
|
||||
summary: 'Get service logs.',
|
||||
description: 'Get service logs by UUID.',
|
||||
path: '/services/{uuid}/containers/{container_id}/logs',
|
||||
path: '/services/{uuid}/logs',
|
||||
operationId: 'get-service-logs-by-uuid',
|
||||
security: [
|
||||
['bearerAuth' => []],
|
||||
|
|
@ -469,9 +469,9 @@ public function service_by_uuid(Request $request)
|
|||
)
|
||||
),
|
||||
new OA\Parameter(
|
||||
name: 'container_id',
|
||||
in: 'path',
|
||||
description: 'Container ID.',
|
||||
name: 'sub_service_name',
|
||||
in: 'query',
|
||||
description: 'Sub service name.',
|
||||
required: true,
|
||||
schema: new OA\Schema(type: 'string'),
|
||||
),
|
||||
|
|
@ -527,12 +527,16 @@ public function logs_by_uuid(Request $request)
|
|||
if (! $uuid) {
|
||||
return response()->json(['message' => 'UUID is required.'], 400);
|
||||
}
|
||||
$subServiceName = $request->query->get('sub_service_name');
|
||||
if (! $subServiceName) {
|
||||
return response()->json(['message' => 'Sub service name 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);
|
||||
$containers = getCurrentServiceSubContainerStatus($service->destination->server, $service->id, $subServiceName);
|
||||
|
||||
if ($containers->count() == 0) {
|
||||
return response()->json([
|
||||
|
|
@ -540,9 +544,7 @@ public function logs_by_uuid(Request $request)
|
|||
], 400);
|
||||
}
|
||||
|
||||
$container = $containers->first(function ($container) use ($request) {
|
||||
return $container['ID'] === $request->container_id;
|
||||
});
|
||||
$container = $containers->first();
|
||||
|
||||
if (! $container) {
|
||||
return response()->json(['message' => 'Container not found.'], 404);
|
||||
|
|
|
|||
|
|
@ -66,6 +66,19 @@ function getCurrentDatabaseContainerStatus(Server $server, int $id): Collection
|
|||
return $containers;
|
||||
}
|
||||
|
||||
function getCurrentServiceSubContainerStatus(Server $server, int $id, string $subName): Collection
|
||||
{
|
||||
$containers = collect([]);
|
||||
if (! $server->isSwarm()) {
|
||||
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.serviceId={$id}' --filter='label=coolify.service.subName={$subName}' --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);
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@
|
|||
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::get('/services/{uuid}/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']);
|
||||
|
|
|
|||
Loading…
Reference in a new issue