Skip to content

Commit 4d36bc4

Browse files
authored
Merge pull request coollabsio#3535 from Luca-Sordetti/main
feat(api): add an endpoint to execute a command
2 parents 6639379 + 4e167dc commit 4d36bc4

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

app/Http/Controllers/Api/ApplicationsController.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,108 @@ public function action_restart(Request $request)
25292529

25302530
}
25312531

2532+
#[OA\Post(
2533+
summary: 'Execute Command',
2534+
description: "Execute a command on the application's current container.",
2535+
path: '/applications/{uuid}/execute',
2536+
operationId: 'execute-command-application',
2537+
security: [
2538+
['bearerAuth' => []],
2539+
],
2540+
tags: ['Applications'],
2541+
parameters: [
2542+
new OA\Parameter(
2543+
name: 'uuid',
2544+
in: 'path',
2545+
description: 'UUID of the application.',
2546+
required: true,
2547+
schema: new OA\Schema(
2548+
type: 'string',
2549+
format: 'uuid',
2550+
)
2551+
),
2552+
],
2553+
requestBody: new OA\RequestBody(
2554+
required: true,
2555+
description: 'Command to execute.',
2556+
content: new OA\MediaType(
2557+
mediaType: 'application/json',
2558+
schema: new OA\Schema(
2559+
type: 'object',
2560+
properties: [
2561+
'command' => ['type' => 'string', 'description' => 'Command to execute.'],
2562+
],
2563+
),
2564+
),
2565+
),
2566+
responses: [
2567+
new OA\Response(
2568+
response: 200,
2569+
description: "Execute a command on the application's current container.",
2570+
content: [
2571+
new OA\MediaType(
2572+
mediaType: 'application/json',
2573+
schema: new OA\Schema(
2574+
type: 'object',
2575+
properties: [
2576+
'message' => ['type' => 'string', 'example' => 'Command executed.'],
2577+
'response' => ['type' => 'string'],
2578+
]
2579+
)
2580+
),
2581+
]
2582+
),
2583+
new OA\Response(
2584+
response: 401,
2585+
ref: '#/components/responses/401',
2586+
),
2587+
new OA\Response(
2588+
response: 400,
2589+
ref: '#/components/responses/400',
2590+
),
2591+
new OA\Response(
2592+
response: 404,
2593+
ref: '#/components/responses/404',
2594+
),
2595+
]
2596+
)]
2597+
public function execute_command_by_uuid(Request $request)
2598+
{
2599+
$data = $request->validate([
2600+
'command' => 'required|string|max:255',
2601+
]);
2602+
$teamId = getTeamIdFromToken();
2603+
if (is_null($teamId)) {
2604+
return invalidTokenResponse();
2605+
}
2606+
$uuid = $request->route('uuid');
2607+
if (!$uuid) {
2608+
return response()->json(['message' => 'UUID is required.'], 400);
2609+
}
2610+
$application = Application::ownedByCurrentTeamAPI($teamId)->where('uuid', $request->uuid)->first();
2611+
if (!$application) {
2612+
return response()->json(['message' => 'Application not found.'], 404);
2613+
}
2614+
2615+
$container = getCurrentApplicationContainerStatus($application->destination->server, $application->id)->firstOrFail();
2616+
$status = getContainerStatus($application->destination->server, $container['Names']);
2617+
2618+
if ('running' !== $status) {
2619+
return;
2620+
}
2621+
2622+
$commands = collect([
2623+
executeInDocker($container['Names'], $data['command']),
2624+
]);
2625+
2626+
$res = instant_remote_process($commands, $application->destination->server);
2627+
2628+
return response()->json([
2629+
'message' => 'Command executed.',
2630+
'response' => $res,
2631+
]);
2632+
}
2633+
25322634
private function validateDataApplications(Request $request, Server $server)
25332635
{
25342636
$teamId = getTeamIdFromToken();

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
Route::patch('/applications/{uuid}/envs/bulk', [ApplicationsController::class, 'create_bulk_envs'])->middleware([IgnoreReadOnlyApiToken::class]);
8787
Route::patch('/applications/{uuid}/envs', [ApplicationsController::class, 'update_env_by_uuid'])->middleware([IgnoreReadOnlyApiToken::class]);
8888
Route::delete('/applications/{uuid}/envs/{env_uuid}', [ApplicationsController::class, 'delete_env_by_uuid'])->middleware([IgnoreReadOnlyApiToken::class]);
89+
Route::post('/applications/{uuid}/execute', [ApplicationsController::class, 'execute_command_by_uuid'])->middleware([IgnoreReadOnlyApiToken::class]);
8990

9091
Route::match(['get', 'post'], '/applications/{uuid}/start', [ApplicationsController::class, 'action_deploy'])->middleware([IgnoreReadOnlyApiToken::class]);
9192
Route::match(['get', 'post'], '/applications/{uuid}/restart', [ApplicationsController::class, 'action_restart'])->middleware([IgnoreReadOnlyApiToken::class]);

0 commit comments

Comments
 (0)