From cd8509411383e0a6b96a353e6e734d5e63a4987f Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 21 Jun 2024 16:46:13 +0200 Subject: [PATCH] feat: more api endpoints --- app/Http/Controllers/Api/Applications.php | 136 ++++++++++++++++++ app/Http/Controllers/Api/Deploy.php | 20 ++- bootstrap/helpers/api.php | 26 ++++ bootstrap/helpers/applications.php | 3 +- ...4_06_21_143358_add_api_deployment_type.php | 28 ++++ .../application/deployment/index.blade.php | 6 +- routes/api.php | 19 ++- 7 files changed, 230 insertions(+), 8 deletions(-) create mode 100644 app/Http/Controllers/Api/Applications.php create mode 100644 database/migrations/2024_06_21_143358_add_api_deployment_type.php diff --git a/app/Http/Controllers/Api/Applications.php b/app/Http/Controllers/Api/Applications.php new file mode 100644 index 000000000..b4831cb57 --- /dev/null +++ b/app/Http/Controllers/Api/Applications.php @@ -0,0 +1,136 @@ +get(); + $applications = collect(); + $applications->push($projects->pluck('applications')->flatten()); + $applications = $applications->flatten(); + + return response()->json($applications); + } + + public function application_by_uuid(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $uuid = $request->route('uuid'); + if (! $uuid) { + return response()->json(['error' => 'UUID is required.'], 400); + } + $application = Application::where('uuid', $uuid)->first(); + if (! $application) { + return response()->json(['error' => 'Application not found.'], 404); + } + + return response()->json($application); + } + + public function action_deploy(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $force = $request->query->get('force') ?? false; + $instant_deploy = $request->query->get('instant_deploy') ?? false; + $uuid = $request->route('uuid'); + if (! $uuid) { + return response()->json(['error' => 'UUID is required.'], 400); + } + $application = Application::where('uuid', $uuid)->first(); + if (! $application) { + return response()->json(['error' => 'Application not found.'], 404); + } + + $deployment_uuid = new Cuid2(7); + + queue_application_deployment( + application: $application, + deployment_uuid: $deployment_uuid, + force_rebuild: $force, + is_api: true, + no_questions_asked: $instant_deploy + ); + + return response()->json( + [ + 'message' => 'Deployment request queued.', + 'deployment_uuid' => $deployment_uuid->toString(), + 'deployment_api_url' => base_url().'/api/v1/deployment/'.$deployment_uuid->toString(), + ], + 200 + ); + } + + public function action_stop(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $uuid = $request->route('uuid'); + if (! $uuid) { + return response()->json(['error' => 'UUID is required.'], 400); + } + $application = Application::where('uuid', $uuid)->first(); + if (! $application) { + return response()->json(['error' => 'Application not found.'], 404); + } + StopApplication::dispatch($application); + + return response()->json(['message' => 'Stopping request queued.'], 200); + } + + public function action_restart(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $uuid = $request->route('uuid'); + if (! $uuid) { + return response()->json(['error' => 'UUID is required.'], 400); + } + $application = Application::where('uuid', $uuid)->first(); + if (! $application) { + return response()->json(['error' => 'Application not found.'], 404); + } + + $deployment_uuid = new Cuid2(7); + + queue_application_deployment( + application: $application, + deployment_uuid: $deployment_uuid, + restart_only: true, + is_api: true, + ); + + return response()->json( + [ + 'message' => 'Restart request queued.', + 'deployment_uuid' => $deployment_uuid->toString(), + 'deployment_api_url' => base_url().'/api/v1/deployment/'.$deployment_uuid->toString(), + ], + 200 + ); + + } +} diff --git a/app/Http/Controllers/Api/Deploy.php b/app/Http/Controllers/Api/Deploy.php index d2abe2e31..f7a34facc 100644 --- a/app/Http/Controllers/Api/Deploy.php +++ b/app/Http/Controllers/Api/Deploy.php @@ -38,7 +38,25 @@ public function deployments(Request $request) 'status', ])->sortBy('id')->toArray(); - return response()->json($deployments_per_server, 200); + return response()->json(serialize_api_response($deployments_per_server), 200); + } + + public function deployment_by_uuid(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $uuid = $request->route('uuid'); + if (! $uuid) { + return response()->json(['error' => 'UUID is required.'], 400); + } + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', $uuid)->first()->makeHidden('logs'); + if (! $deployment) { + return response()->json(['error' => 'Deployment not found.'], 404); + } + + return response()->json(serialize_api_response($deployment), 200); } public function deploy(Request $request) diff --git a/bootstrap/helpers/api.php b/bootstrap/helpers/api.php index 999de45c2..c278a5045 100644 --- a/bootstrap/helpers/api.php +++ b/bootstrap/helpers/api.php @@ -1,5 +1,7 @@ user()->currentAccessToken(); @@ -10,3 +12,27 @@ function invalid_token() { return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api-reference/authorization'], 400); } + +function serialize_api_response($data) +{ + if (! $data instanceof Collection) { + $data = collect($data); + } + $data = $data->sortKeys(); + $created_at = data_get($data, 'created_at'); + $updated_at = data_get($data, 'updated_at'); + if ($created_at) { + unset($data['created_at']); + $data['created_at'] = $created_at; + + } + if ($updated_at) { + unset($data['updated_at']); + $data['updated_at'] = $updated_at; + } + if (data_get($data, 'id')) { + $data = $data->prepend($data['id'], 'id'); + } + + return $data; +} diff --git a/bootstrap/helpers/applications.php b/bootstrap/helpers/applications.php index 816a13853..df891b824 100644 --- a/bootstrap/helpers/applications.php +++ b/bootstrap/helpers/applications.php @@ -8,7 +8,7 @@ use App\Models\StandaloneDocker; use Spatie\Url\Url; -function queue_application_deployment(Application $application, string $deployment_uuid, ?int $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false, bool $restart_only = false, ?string $git_type = null, bool $no_questions_asked = false, ?Server $server = null, ?StandaloneDocker $destination = null, bool $only_this_server = false, bool $rollback = false) +function queue_application_deployment(Application $application, string $deployment_uuid, ?int $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false, bool $is_api = false, bool $restart_only = false, ?string $git_type = null, bool $no_questions_asked = false, ?Server $server = null, ?StandaloneDocker $destination = null, bool $only_this_server = false, bool $rollback = false) { $application_id = $application->id; $deployment_link = Url::fromString($application->link()."/deployment/{$deployment_uuid}"); @@ -35,6 +35,7 @@ function queue_application_deployment(Application $application, string $deployme 'pull_request_id' => $pull_request_id, 'force_rebuild' => $force_rebuild, 'is_webhook' => $is_webhook, + 'is_api' => $is_api, 'restart_only' => $restart_only, 'commit' => $commit, 'rollback' => $rollback, diff --git a/database/migrations/2024_06_21_143358_add_api_deployment_type.php b/database/migrations/2024_06_21_143358_add_api_deployment_type.php new file mode 100644 index 000000000..03f4d4796 --- /dev/null +++ b/database/migrations/2024_06_21_143358_add_api_deployment_type.php @@ -0,0 +1,28 @@ +boolean('is_api')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('application_deployment_queues', function (Blueprint $table) { + $table->dropColumn('is_api'); + }); + } +}; diff --git a/resources/views/livewire/project/application/deployment/index.blade.php b/resources/views/livewire/project/application/deployment/index.blade.php index 241530fe0..001499b71 100644 --- a/resources/views/livewire/project/application/deployment/index.blade.php +++ b/resources/views/livewire/project/application/deployment/index.blade.php @@ -75,7 +75,11 @@ class="w-6 h-6" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> @if (data_get($deployment, 'rollback') === true) Rollback @else - Manual + @if (data_get($deployment, 'is_api')) + API + @else + Manual + @endif @endif @if (data_get($deployment, 'commit'))
json(['error' => 'Not found.'], 404); })->where('any', '.*');