2024-02-02 11:50:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2024-07-02 13:39:44 +02:00
|
|
|
use App\Actions\Database\StartDatabase;
|
2024-02-02 11:50:28 +01:00
|
|
|
use App\Actions\Service\StartService;
|
|
|
|
use App\Http\Controllers\Controller;
|
2024-03-07 12:27:23 +01:00
|
|
|
use App\Models\ApplicationDeploymentQueue;
|
|
|
|
use App\Models\Server;
|
2024-02-02 11:50:28 +01:00
|
|
|
use App\Models\Tag;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
|
2024-07-01 16:26:50 +02:00
|
|
|
class DeployController extends Controller
|
2024-02-02 11:50:28 +01:00
|
|
|
{
|
2024-07-02 12:15:58 +02:00
|
|
|
private function removeSensitiveData($deployment)
|
|
|
|
{
|
|
|
|
$token = auth()->user()->currentAccessToken();
|
|
|
|
if ($token->can('view:sensitive')) {
|
|
|
|
return serializeApiResponse($deployment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$deployment->makeHidden([
|
|
|
|
'logs',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return serializeApiResponse($deployment);
|
|
|
|
}
|
|
|
|
|
2024-03-11 09:42:02 +01:00
|
|
|
public function deployments(Request $request)
|
|
|
|
{
|
2024-07-01 16:26:50 +02:00
|
|
|
$teamId = getTeamIdFromToken();
|
2024-03-07 12:27:23 +01:00
|
|
|
if (is_null($teamId)) {
|
2024-07-01 16:26:50 +02:00
|
|
|
return invalidTokenResponse();
|
2024-03-07 12:27:23 +01:00
|
|
|
}
|
|
|
|
$servers = Server::whereTeamId($teamId)->get();
|
2024-06-10 20:43:34 +00:00
|
|
|
$deployments_per_server = ApplicationDeploymentQueue::whereIn('status', ['in_progress', 'queued'])->whereIn('server_id', $servers->pluck('id'))->get([
|
2024-07-04 13:45:06 +02:00
|
|
|
'deployment_uuid',
|
|
|
|
'commit',
|
|
|
|
'commit_message',
|
2024-06-10 20:43:34 +00:00
|
|
|
'application_id',
|
|
|
|
'application_name',
|
|
|
|
'deployment_url',
|
|
|
|
'pull_request_id',
|
|
|
|
'server_name',
|
|
|
|
'server_id',
|
|
|
|
'status',
|
2024-07-04 13:45:06 +02:00
|
|
|
'is_api',
|
|
|
|
'is_webhook',
|
|
|
|
'restart_only',
|
|
|
|
'force_rebuild',
|
|
|
|
'rollback',
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
])->sortBy('id');
|
|
|
|
$deployments_per_server = $deployments_per_server->map(function ($deployment) {
|
|
|
|
return $this->removeSensitiveData($deployment);
|
|
|
|
});
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-04 13:45:06 +02:00
|
|
|
return response()->json($deployments_per_server);
|
2024-06-21 16:46:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deployment_by_uuid(Request $request)
|
|
|
|
{
|
2024-07-01 16:26:50 +02:00
|
|
|
$teamId = getTeamIdFromToken();
|
2024-06-21 16:46:13 +02:00
|
|
|
if (is_null($teamId)) {
|
2024-07-01 16:26:50 +02:00
|
|
|
return invalidTokenResponse();
|
2024-06-21 16:46:13 +02:00
|
|
|
}
|
|
|
|
$uuid = $request->route('uuid');
|
|
|
|
if (! $uuid) {
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'UUID is required.'], 400);
|
2024-06-21 16:46:13 +02:00
|
|
|
}
|
2024-07-04 13:45:06 +02:00
|
|
|
$deployment = ApplicationDeploymentQueue::where('deployment_uuid', $uuid)->first([
|
|
|
|
'deployment_uuid',
|
|
|
|
'commit',
|
|
|
|
'commit_message',
|
|
|
|
'application_id',
|
|
|
|
'application_name',
|
|
|
|
'deployment_url',
|
|
|
|
'pull_request_id',
|
|
|
|
'server_name',
|
|
|
|
'server_id',
|
|
|
|
'logs',
|
|
|
|
'status',
|
|
|
|
'is_api',
|
|
|
|
'is_webhook',
|
|
|
|
'restart_only',
|
|
|
|
'force_rebuild',
|
|
|
|
'rollback',
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
]);
|
2024-06-21 16:46:13 +02:00
|
|
|
if (! $deployment) {
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'Deployment not found.'], 404);
|
2024-06-21 16:46:13 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json($this->removeSensitiveData($deployment));
|
2024-03-07 12:27:23 +01:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-02 11:50:28 +01:00
|
|
|
public function deploy(Request $request)
|
|
|
|
{
|
2024-07-01 16:26:50 +02:00
|
|
|
$teamId = getTeamIdFromToken();
|
2024-02-02 11:50:28 +01:00
|
|
|
$uuids = $request->query->get('uuid');
|
|
|
|
$tags = $request->query->get('tag');
|
|
|
|
$force = $request->query->get('force') ?? false;
|
|
|
|
|
|
|
|
if ($uuids && $tags) {
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'You can only use uuid or tag, not both.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
|
|
|
if (is_null($teamId)) {
|
2024-07-01 16:26:50 +02:00
|
|
|
return invalidTokenResponse();
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
|
|
|
if ($tags) {
|
|
|
|
return $this->by_tags($tags, $teamId, $force);
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($uuids) {
|
2024-02-02 11:50:28 +01:00
|
|
|
return $this->by_uuids($uuids, $teamId, $force);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'You must provide uuid or tag.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-02 11:50:28 +01:00
|
|
|
private function by_uuids(string $uuid, int $teamId, bool $force = false)
|
|
|
|
{
|
|
|
|
$uuids = explode(',', $uuid);
|
|
|
|
$uuids = collect(array_filter($uuids));
|
|
|
|
|
|
|
|
if (count($uuids) === 0) {
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'No UUIDs provided.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
2024-03-07 12:22:18 +01:00
|
|
|
$deployments = collect();
|
|
|
|
$payload = collect();
|
2024-02-02 11:50:28 +01:00
|
|
|
foreach ($uuids as $uuid) {
|
|
|
|
$resource = getResourceByUuid($uuid, $teamId);
|
|
|
|
if ($resource) {
|
2024-03-07 12:22:18 +01:00
|
|
|
['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $this->deploy_resource($resource, $force);
|
|
|
|
if ($deployment_uuid) {
|
2024-07-03 13:13:38 +02:00
|
|
|
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid, 'deployment_uuid' => $deployment_uuid->toString()]);
|
2024-03-11 09:42:02 +01:00
|
|
|
} else {
|
2024-07-03 13:13:38 +02:00
|
|
|
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid]);
|
2024-03-07 12:22:18 +01:00
|
|
|
}
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-11 09:42:02 +01:00
|
|
|
if ($deployments->count() > 0) {
|
|
|
|
$payload->put('deployments', $deployments->toArray());
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(serializeApiResponse($payload->toArray()));
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'No resources found.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 404);
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-02 11:50:28 +01:00
|
|
|
public function by_tags(string $tags, int $team_id, bool $force = false)
|
|
|
|
{
|
|
|
|
$tags = explode(',', $tags);
|
|
|
|
$tags = collect(array_filter($tags));
|
|
|
|
|
|
|
|
if (count($tags) === 0) {
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'No TAGs provided.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 400);
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
|
|
|
$message = collect([]);
|
2024-03-07 12:22:18 +01:00
|
|
|
$deployments = collect();
|
|
|
|
$payload = collect();
|
2024-02-02 11:50:28 +01:00
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$found_tag = Tag::where(['name' => $tag, 'team_id' => $team_id])->first();
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $found_tag) {
|
2024-03-07 12:35:38 +01:00
|
|
|
// $message->push("Tag {$tag} not found.");
|
2024-02-02 11:50:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-06 07:21:06 +01:00
|
|
|
$applications = $found_tag->applications()->get();
|
|
|
|
$services = $found_tag->services()->get();
|
2024-02-03 12:39:07 +01:00
|
|
|
if ($applications->count() === 0 && $services->count() === 0) {
|
2024-02-02 11:50:28 +01:00
|
|
|
$message->push("No resources found for tag {$tag}.");
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-02 11:50:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-03 12:39:07 +01:00
|
|
|
foreach ($applications as $resource) {
|
2024-03-07 12:22:18 +01:00
|
|
|
['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $this->deploy_resource($resource, $force);
|
|
|
|
if ($deployment_uuid) {
|
|
|
|
$deployments->push(['resource_uuid' => $resource->uuid, 'deployment_uuid' => $deployment_uuid->toString()]);
|
|
|
|
}
|
2024-02-03 12:39:07 +01:00
|
|
|
$message = $message->merge($return_message);
|
|
|
|
}
|
|
|
|
foreach ($services as $resource) {
|
2024-03-07 12:22:18 +01:00
|
|
|
['message' => $return_message] = $this->deploy_resource($resource, $force);
|
2024-02-02 11:50:28 +01:00
|
|
|
$message = $message->merge($return_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($message->count() > 0) {
|
2024-03-07 12:22:18 +01:00
|
|
|
$payload->put('message', $message->toArray());
|
|
|
|
if ($deployments->count() > 0) {
|
|
|
|
$payload->put('details', $deployments->toArray());
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(serializeApiResponse($payload->toArray()));
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return response()->json(['message' => 'No resources found with this tag.', 'docs' => 'https://coolify.io/docs/api-reference/deploy-webhook'], 404);
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-07 12:22:18 +01:00
|
|
|
public function deploy_resource($resource, bool $force = false): array
|
2024-02-02 11:50:28 +01:00
|
|
|
{
|
2024-03-11 09:42:02 +01:00
|
|
|
$message = null;
|
|
|
|
$deployment_uuid = null;
|
2024-02-06 07:19:11 +01:00
|
|
|
if (gettype($resource) !== 'object') {
|
2024-07-03 13:13:38 +02:00
|
|
|
return ['message' => "Resource ($resource) not found.", 'deployment_uuid' => $deployment_uuid];
|
2024-02-06 07:19:11 +01:00
|
|
|
}
|
2024-07-02 13:39:44 +02:00
|
|
|
switch ($resource?->getMorphClass()) {
|
|
|
|
case 'App\Models\Application':
|
|
|
|
$deployment_uuid = new Cuid2(7);
|
|
|
|
queue_application_deployment(
|
|
|
|
application: $resource,
|
|
|
|
deployment_uuid: $deployment_uuid,
|
|
|
|
force_rebuild: $force,
|
|
|
|
);
|
|
|
|
$message = "Application {$resource->name} deployment queued.";
|
|
|
|
break;
|
|
|
|
case 'App\Models\Service':
|
|
|
|
StartService::run($resource);
|
|
|
|
$message = "Service {$resource->name} started. It could take a while, be patient.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Database resource
|
|
|
|
StartDatabase::dispatch($resource);
|
|
|
|
$resource->update([
|
|
|
|
'started_at' => now(),
|
|
|
|
]);
|
|
|
|
$message = "Database {$resource->name} started.";
|
|
|
|
break;
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-03 13:13:38 +02:00
|
|
|
return ['message' => $message, 'deployment_uuid' => $deployment_uuid];
|
2024-02-02 11:50:28 +01:00
|
|
|
}
|
|
|
|
}
|